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 void wrap_send_port_open(void *channel, const char *hostname, int port,
163 char *peerinfo, *description;
164 peerinfo = sk_peer_info(s);
166 description = dupprintf("forwarding from %s", peerinfo);
169 description = dupstr("forwarding");
171 ssh_send_port_open(channel, hostname, port, description);
175 static int pfd_receive(Plug plug, int urgent, char *data, int len)
177 struct PortForwarding *pf = (struct PortForwarding *) plug;
180 if (pf->sockslen >= pf->sockssize) {
181 pf->sockssize = pf->sockslen * 5 / 4 + 256;
182 pf->socksbuf = sresize(pf->socksbuf, pf->sockssize, char);
184 pf->socksbuf[pf->sockslen++] = *data++;
187 * Now check what's in the buffer to see if it's a
188 * valid and complete message in the SOCKS exchange.
190 if ((pf->dynamic == 1 || (pf->dynamic >> 12) == 4) &&
191 pf->socksbuf[0] == 4) {
195 if (pf->dynamic == 1)
196 pf->dynamic = 0x4000;
197 if (pf->sockslen < 2)
198 continue; /* don't have command code yet */
199 if (pf->socksbuf[1] != 1) {
201 /* Send back a SOCKS 4 error before closing. */
203 memset(data, 0, sizeof(data));
204 data[1] = 91; /* generic `request rejected' */
205 sk_write(pf->s, data, 8);
209 if (pf->sockslen <= 8)
210 continue; /* haven't started user/hostname */
211 if (pf->socksbuf[pf->sockslen-1] != 0)
212 continue; /* haven't _finished_ user/hostname */
214 * Now we have a full SOCKS 4 request. Check it to
215 * see if it's a SOCKS 4A request.
217 if (pf->socksbuf[4] == 0 && pf->socksbuf[5] == 0 &&
218 pf->socksbuf[6] == 0 && pf->socksbuf[7] != 0) {
220 * It's SOCKS 4A. So if we haven't yet
221 * collected the host name, we should continue
222 * waiting for data in order to do so; if we
223 * have, we can go ahead.
226 if (pf->dynamic == 0x4000) {
227 pf->dynamic = 0x4001;
228 pf->sockslen = 8; /* reset buffer to overwrite name */
231 pf->socksbuf[0] = 0; /* reply version code */
232 pf->socksbuf[1] = 90; /* request granted */
233 sk_write(pf->s, pf->socksbuf, 8);
234 len = pf->sockslen - 8;
235 pf->port = GET_16BIT_MSB_FIRST(pf->socksbuf+2);
236 pf->hostname = snewn(len+1, char);
237 pf->hostname[len] = '\0';
238 memcpy(pf->hostname, pf->socksbuf + 8, len);
242 * It's SOCKS 4, which means we should format
243 * the IP address into the hostname string and
246 pf->socksbuf[0] = 0; /* reply version code */
247 pf->socksbuf[1] = 90; /* request granted */
248 sk_write(pf->s, pf->socksbuf, 8);
249 pf->port = GET_16BIT_MSB_FIRST(pf->socksbuf+2);
250 pf->hostname = dupprintf("%d.%d.%d.%d",
251 (unsigned char)pf->socksbuf[4],
252 (unsigned char)pf->socksbuf[5],
253 (unsigned char)pf->socksbuf[6],
254 (unsigned char)pf->socksbuf[7]);
259 if ((pf->dynamic == 1 || (pf->dynamic >> 12) == 5) &&
260 pf->socksbuf[0] == 5) {
264 if (pf->dynamic == 1)
265 pf->dynamic = 0x5000;
267 if (pf->dynamic == 0x5000) {
271 * We're receiving a set of method identifiers.
273 if (pf->sockslen < 2)
274 continue; /* no method count yet */
275 if (pf->sockslen < 2 + (unsigned char)pf->socksbuf[1])
276 continue; /* no methods yet */
277 method = 0xFF; /* invalid */
278 for (i = 0; i < (unsigned char)pf->socksbuf[1]; i++)
279 if (pf->socksbuf[2+i] == 0) {
280 method = 0;/* no auth */
285 sk_write(pf->s, data, 2);
286 pf->dynamic = 0x5001;
287 pf->sockslen = 0; /* re-empty the buffer */
291 if (pf->dynamic == 0x5001) {
293 * We're receiving a SOCKS request.
295 unsigned char reply[10]; /* SOCKS5 atyp=1 reply */
299 * Pre-fill reply packet.
300 * In all cases, we set BND.{HOST,ADDR} to 0.0.0.0:0
301 * (atyp=1) in the reply; if we succeed, we don't know
302 * the right answers, and if we fail, they should be
305 memset(reply, 0, lenof(reply));
306 reply[0] = 5; /* VER */
307 reply[3] = 1; /* ATYP = 1 (IPv4, 0.0.0.0:0) */
309 if (pf->sockslen < 6) continue;
310 atype = (unsigned char)pf->socksbuf[3];
311 if (atype == 1) /* IPv4 address */
313 if (atype == 4) /* IPv6 address */
315 if (atype == 3) /* domain name has leading length */
316 alen = 1 + (unsigned char)pf->socksbuf[4];
317 if (pf->sockslen < 6 + alen) continue;
318 if (pf->socksbuf[1] != 1 || pf->socksbuf[2] != 0) {
319 /* Not CONNECT or reserved field nonzero - error */
320 reply[1] = 1; /* generic failure */
321 sk_write(pf->s, (char *) reply, lenof(reply));
326 * Now we have a viable connect request. Switch
329 pf->port = GET_16BIT_MSB_FIRST(pf->socksbuf+4+alen);
331 /* REP=0 (success) already */
332 sk_write(pf->s, (char *) reply, lenof(reply));
333 pf->hostname = dupprintf("%d.%d.%d.%d",
334 (unsigned char)pf->socksbuf[4],
335 (unsigned char)pf->socksbuf[5],
336 (unsigned char)pf->socksbuf[6],
337 (unsigned char)pf->socksbuf[7]);
339 } else if (atype == 3) {
340 /* REP=0 (success) already */
341 sk_write(pf->s, (char *) reply, lenof(reply));
342 pf->hostname = snewn(alen, char);
343 pf->hostname[alen-1] = '\0';
344 memcpy(pf->hostname, pf->socksbuf + 5, alen-1);
348 * Unknown address type. (FIXME: support IPv6!)
350 reply[1] = 8; /* atype not supported */
351 sk_write(pf->s, (char *) reply, lenof(reply));
359 * If we get here without either having done `continue'
360 * or `goto connect', it must be because there is no
361 * sensible interpretation of what's in our buffer. So
362 * close the connection rudely.
370 * We come here when we're ready to make an actual
378 * Freeze the socket until the SSH server confirms the
381 sk_set_frozen(pf->s, 1);
383 pf->c = new_sock_channel(pf->backhandle, pf);
388 /* asks to forward to the specified host/port for this */
389 wrap_send_port_open(pf->c, pf->hostname, pf->port, pf->s);
394 * If there's any data remaining in our current buffer,
395 * save it to be sent on pfd_confirm().
398 pf->buffer = snewn(len, char);
399 memcpy(pf->buffer, data, len);
404 if (sshfwd_write(pf->c, data, len) > 0) {
406 sk_set_frozen(pf->s, 1);
412 static void pfd_sent(Plug plug, int bufsize)
414 struct PortForwarding *pf = (struct PortForwarding *) plug;
417 sshfwd_unthrottle(pf->c, bufsize);
421 * Called when receiving a PORT OPEN from the server to make a
422 * connection to a destination host.
424 * On success, returns NULL and fills in *pf_ret. On error, returns a
425 * dynamically allocated error message string.
427 char *pfd_connect(struct PortForwarding **pf_ret, char *hostname,int port,
428 void *c, Conf *conf, int addressfamily)
430 static const struct plug_function_table fn_table = {
440 char *dummy_realhost;
441 struct PortForwarding *pf;
446 addr = name_lookup(hostname, port, &dummy_realhost, conf, addressfamily);
447 if ((err = sk_addr_error(addr)) != NULL) {
448 char *err_ret = dupstr(err);
450 sfree(dummy_realhost);
457 pf = *pf_ret = new_portfwd_state();
459 pf->throttled = pf->throttle_override = 0;
462 pf->backhandle = NULL; /* we shouldn't need this */
465 pf->s = new_connection(addr, dummy_realhost, port,
466 0, 1, 0, 0, (Plug) pf, conf);
467 sfree(dummy_realhost);
468 if ((err = sk_socket_error(pf->s)) != NULL) {
469 char *err_ret = dupstr(err);
471 free_portfwd_state(pf);
480 called when someone connects to the local port
483 static int pfl_accepting(Plug p, accept_fn_t constructor, accept_ctx_t ctx)
485 static const struct plug_function_table fn_table = {
492 struct PortForwarding *pf;
493 struct PortListener *pl;
497 pl = (struct PortListener *)p;
498 pf = new_portfwd_state();
502 pf->backhandle = pl->backhandle;
504 pf->s = s = constructor(ctx, (Plug) pf);
505 if ((err = sk_socket_error(s)) != NULL) {
506 free_portfwd_state(pf);
510 pf->throttled = pf->throttle_override = 0;
515 pf->port = 0; /* "hostname" buffer is so far empty */
516 sk_set_frozen(s, 0); /* we want to receive SOCKS _now_! */
519 pf->hostname = dupstr(pl->hostname);
521 pf->c = new_sock_channel(pl->backhandle, pf);
524 free_portfwd_state(pf);
527 /* asks to forward to the specified host/port for this */
528 wrap_send_port_open(pf->c, pf->hostname, pf->port, s);
537 * Add a new port-forwarding listener from srcaddr:port -> desthost:destport.
539 * On success, returns NULL and fills in *pl_ret. On error, returns a
540 * dynamically allocated error message string.
542 char *pfl_listen(char *desthost, int destport, char *srcaddr,
543 int port, void *backhandle, Conf *conf,
544 struct PortListener **pl_ret, int address_family)
546 static const struct plug_function_table fn_table = {
555 struct PortListener *pl;
560 pl = *pl_ret = new_portlistener_state();
563 pl->hostname = dupstr(desthost);
568 pl->backhandle = backhandle;
570 pl->s = new_listener(srcaddr, port, (Plug) pl,
571 !conf_get_int(conf, CONF_lport_acceptall),
572 conf, address_family);
573 if ((err = sk_socket_error(pl->s)) != NULL) {
574 char *err_ret = dupstr(err);
576 free_portlistener_state(pl);
584 void pfd_close(struct PortForwarding *pf)
590 free_portfwd_state(pf);
594 * Terminate a listener.
596 void pfl_terminate(struct PortListener *pl)
602 free_portlistener_state(pl);
605 void pfd_unthrottle(struct PortForwarding *pf)
611 sk_set_frozen(pf->s, pf->throttled || pf->throttle_override);
614 void pfd_override_throttle(struct PortForwarding *pf, int enable)
619 pf->throttle_override = enable;
620 sk_set_frozen(pf->s, pf->throttled || pf->throttle_override);
624 * Called to send data down the raw connection.
626 int pfd_send(struct PortForwarding *pf, char *data, int len)
630 return sk_write(pf->s, data, len);
633 void pfd_send_eof(struct PortForwarding *pf)
638 void pfd_confirm(struct PortForwarding *pf)
644 sk_set_frozen(pf->s, 0);
645 sk_write(pf->s, NULL, 0);
647 sshfwd_write(pf->c, pf->buffer, pf->buflen);