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; they're unused for this
37 * purpose while conducting a local SOCKS exchange, which means
38 * we can also use them as a buffer and pointer for reading
39 * data from the SOCKS client.
44 * When doing dynamic port forwarding, we can receive
45 * connection data before we are actually able to send it; so
46 * we may have to temporarily hold some in a dynamically
47 * allocated buffer here.
53 static void pfd_log(Plug plug
, int type
, SockAddr addr
, int port
,
54 const char *error_msg
, int error_code
)
56 /* we have to dump these since we have no interface to logging.c */
59 static int pfd_closing(Plug plug
, const char *error_msg
, int error_code
,
62 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) plug
;
65 * We have no way to communicate down the forwarded connection,
66 * so if an error occurred on the socket, we just ignore it
67 * and treat it like a proper close.
75 static int pfd_receive(Plug plug
, int urgent
, char *data
, int len
)
77 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) plug
;
81 * Throughout SOCKS negotiation, "hostname" is re-used as a
82 * random protocol buffer with "port" storing the length.
84 if (pr
->port
>= lenof(pr
->hostname
)) {
85 /* Request too long. */
86 if ((pr
->dynamic
>> 12) == 4) {
87 /* Send back a SOCKS 4 error before closing. */
89 memset(data
, 0, sizeof(data
));
90 data
[1] = 91; /* generic `request rejected' */
91 sk_write(pr
->s
, data
, 8);
96 pr
->hostname
[pr
->port
++] = *data
++;
99 * Now check what's in the buffer to see if it's a
100 * valid and complete message in the SOCKS exchange.
102 if ((pr
->dynamic
== 1 || (pr
->dynamic
>> 12) == 4) &&
103 pr
->hostname
[0] == 4) {
107 if (pr
->dynamic
== 1)
108 pr
->dynamic
= 0x4000;
109 if (pr
->port
< 2) continue;/* don't have command code yet */
110 if (pr
->hostname
[1] != 1) {
112 /* Send back a SOCKS 4 error before closing. */
114 memset(data
, 0, sizeof(data
));
115 data
[1] = 91; /* generic `request rejected' */
116 sk_write(pr
->s
, data
, 8);
120 if (pr
->port
<= 8) continue; /* haven't started user/hostname */
121 if (pr
->hostname
[pr
->port
-1] != 0)
122 continue; /* haven't _finished_ user/hostname */
124 * Now we have a full SOCKS 4 request. Check it to
125 * see if it's a SOCKS 4A request.
127 if (pr
->hostname
[4] == 0 && pr
->hostname
[5] == 0 &&
128 pr
->hostname
[6] == 0 && pr
->hostname
[7] != 0) {
130 * It's SOCKS 4A. So if we haven't yet
131 * collected the host name, we should continue
132 * waiting for data in order to do so; if we
133 * have, we can go ahead.
136 if (pr
->dynamic
== 0x4000) {
137 pr
->dynamic
= 0x4001;
138 pr
->port
= 8; /* reset buffer to overwrite name */
141 pr
->hostname
[0] = 0; /* reply version code */
142 pr
->hostname
[1] = 90; /* request granted */
143 sk_write(pr
->s
, pr
->hostname
, 8);
145 pr
->port
= GET_16BIT_MSB_FIRST(pr
->hostname
+2);
146 memmove(pr
->hostname
, pr
->hostname
+ 8, len
);
150 * It's SOCKS 4, which means we should format
151 * the IP address into the hostname string and
154 pr
->hostname
[0] = 0; /* reply version code */
155 pr
->hostname
[1] = 90; /* request granted */
156 sk_write(pr
->s
, pr
->hostname
, 8);
157 pr
->port
= GET_16BIT_MSB_FIRST(pr
->hostname
+2);
158 sprintf(pr
->hostname
, "%d.%d.%d.%d",
159 (unsigned char)pr
->hostname
[4],
160 (unsigned char)pr
->hostname
[5],
161 (unsigned char)pr
->hostname
[6],
162 (unsigned char)pr
->hostname
[7]);
167 if ((pr
->dynamic
== 1 || (pr
->dynamic
>> 12) == 5) &&
168 pr
->hostname
[0] == 5) {
172 if (pr
->dynamic
== 1)
173 pr
->dynamic
= 0x5000;
175 if (pr
->dynamic
== 0x5000) {
179 * We're receiving a set of method identifiers.
181 if (pr
->port
< 2) continue;/* no method count yet */
182 if (pr
->port
< 2 + (unsigned char)pr
->hostname
[1])
183 continue; /* no methods yet */
184 method
= 0xFF; /* invalid */
185 for (i
= 0; i
< (unsigned char)pr
->hostname
[1]; i
++)
186 if (pr
->hostname
[2+i
] == 0) {
187 method
= 0;/* no auth */
192 sk_write(pr
->s
, data
, 2);
193 pr
->dynamic
= 0x5001;
194 pr
->port
= 0; /* re-empty the buffer */
198 if (pr
->dynamic
== 0x5001) {
200 * We're receiving a SOCKS request.
202 unsigned char reply
[10]; /* SOCKS5 atyp=1 reply */
206 * Pre-fill reply packet.
207 * In all cases, we set BND.{HOST,ADDR} to 0.0.0.0:0
208 * (atyp=1) in the reply; if we succeed, we don't know
209 * the right answers, and if we fail, they should be
212 memset(reply
, 0, lenof(reply
));
213 reply
[0] = 5; /* VER */
214 reply
[3] = 1; /* ATYP = 1 (IPv4, 0.0.0.0:0) */
216 if (pr
->port
< 6) continue;
217 atype
= (unsigned char)pr
->hostname
[3];
218 if (atype
== 1) /* IPv4 address */
220 if (atype
== 4) /* IPv6 address */
222 if (atype
== 3) /* domain name has leading length */
223 alen
= 1 + (unsigned char)pr
->hostname
[4];
224 if (pr
->port
< 6 + alen
) continue;
225 if (pr
->hostname
[1] != 1 || pr
->hostname
[2] != 0) {
226 /* Not CONNECT or reserved field nonzero - error */
227 reply
[1] = 1; /* generic failure */
228 sk_write(pr
->s
, (char *) reply
, lenof(reply
));
233 * Now we have a viable connect request. Switch
236 pr
->port
= GET_16BIT_MSB_FIRST(pr
->hostname
+4+alen
);
238 /* REP=0 (success) already */
239 sk_write(pr
->s
, (char *) reply
, lenof(reply
));
240 sprintf(pr
->hostname
, "%d.%d.%d.%d",
241 (unsigned char)pr
->hostname
[4],
242 (unsigned char)pr
->hostname
[5],
243 (unsigned char)pr
->hostname
[6],
244 (unsigned char)pr
->hostname
[7]);
246 } else if (atype
== 3) {
247 /* REP=0 (success) already */
248 sk_write(pr
->s
, (char *) reply
, lenof(reply
));
249 memmove(pr
->hostname
, pr
->hostname
+ 5, alen
-1);
250 pr
->hostname
[alen
-1] = '\0';
254 * Unknown address type. (FIXME: support IPv6!)
256 reply
[1] = 8; /* atype not supported */
257 sk_write(pr
->s
, (char *) reply
, lenof(reply
));
265 * If we get here without either having done `continue'
266 * or `goto connect', it must be because there is no
267 * sensible interpretation of what's in our buffer. So
268 * close the connection rudely.
276 * We come here when we're ready to make an actual
282 * Freeze the socket until the SSH server confirms the
285 sk_set_frozen(pr
->s
, 1);
287 pr
->c
= new_sock_channel(pr
->backhandle
, pr
->s
);
292 /* asks to forward to the specified host/port for this */
293 ssh_send_port_open(pr
->c
, pr
->hostname
, pr
->port
, "forwarding");
298 * If there's any data remaining in our current buffer,
299 * save it to be sent on pfd_confirm().
302 pr
->buffer
= snewn(len
, char);
303 memcpy(pr
->buffer
, data
, len
);
308 if (sshfwd_write(pr
->c
, data
, len
) > 0) {
310 sk_set_frozen(pr
->s
, 1);
316 static void pfd_sent(Plug plug
, int bufsize
)
318 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) plug
;
321 sshfwd_unthrottle(pr
->c
, bufsize
);
325 * Called when receiving a PORT OPEN from the server
327 const char *pfd_newconnect(Socket
*s
, char *hostname
, int port
,
328 void *c
, const Config
*cfg
, int addressfamily
)
330 static const struct plug_function_table fn_table
= {
340 char *dummy_realhost
;
341 struct PFwdPrivate
*pr
;
346 addr
= name_lookup(hostname
, port
, &dummy_realhost
, cfg
, addressfamily
);
347 if ((err
= sk_addr_error(addr
)) != NULL
) {
355 pr
= snew(struct PFwdPrivate
);
358 pr
->throttled
= pr
->throttle_override
= 0;
361 pr
->backhandle
= NULL
; /* we shouldn't need this */
364 pr
->s
= *s
= new_connection(addr
, dummy_realhost
, port
,
365 0, 1, 0, 0, (Plug
) pr
, cfg
);
366 if ((err
= sk_socket_error(*s
)) != NULL
) {
371 sk_set_private_ptr(*s
, pr
);
376 called when someone connects to the local port
379 static int pfd_accepting(Plug p
, OSSocket sock
)
381 static const struct plug_function_table fn_table
= {
388 struct PFwdPrivate
*pr
, *org
;
392 org
= (struct PFwdPrivate
*)p
;
393 pr
= snew(struct PFwdPrivate
);
398 pr
->backhandle
= org
->backhandle
;
400 pr
->s
= s
= sk_register(sock
, (Plug
) pr
);
401 if ((err
= sk_socket_error(s
)) != NULL
) {
406 sk_set_private_ptr(s
, pr
);
408 pr
->throttled
= pr
->throttle_override
= 0;
413 pr
->port
= 0; /* "hostname" buffer is so far empty */
414 sk_set_frozen(s
, 0); /* we want to receive SOCKS _now_! */
417 strcpy(pr
->hostname
, org
->hostname
);
418 pr
->port
= org
->port
;
419 pr
->c
= new_sock_channel(org
->backhandle
, s
);
425 /* asks to forward to the specified host/port for this */
426 ssh_send_port_open(pr
->c
, pr
->hostname
, pr
->port
, "forwarding");
434 /* Add a new forwarding from port -> desthost:destport
435 sets up a listener on the local machine on (srcaddr:)port
437 const char *pfd_addforward(char *desthost
, int destport
, char *srcaddr
,
438 int port
, void *backhandle
, const Config
*cfg
,
439 void **sockdata
, int address_family
)
441 static const struct plug_function_table fn_table
= {
444 pfd_receive
, /* should not happen... */
445 pfd_sent
, /* also should not happen */
450 struct PFwdPrivate
*pr
;
456 pr
= snew(struct PFwdPrivate
);
461 strcpy(pr
->hostname
, desthost
);
466 pr
->throttled
= pr
->throttle_override
= 0;
468 pr
->backhandle
= backhandle
;
470 pr
->s
= s
= new_listener(srcaddr
, port
, (Plug
) pr
,
471 !cfg
->lport_acceptall
, cfg
, address_family
);
472 if ((err
= sk_socket_error(s
)) != NULL
) {
477 sk_set_private_ptr(s
, pr
);
479 *sockdata
= (void *)s
;
484 void pfd_close(Socket s
)
486 struct PFwdPrivate
*pr
;
491 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
500 * Terminate a listener.
502 void pfd_terminate(void *sv
)
504 pfd_close((Socket
)sv
);
507 void pfd_unthrottle(Socket s
)
509 struct PFwdPrivate
*pr
;
512 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
515 sk_set_frozen(s
, pr
->throttled
|| pr
->throttle_override
);
518 void pfd_override_throttle(Socket s
, int enable
)
520 struct PFwdPrivate
*pr
;
523 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
525 pr
->throttle_override
= enable
;
526 sk_set_frozen(s
, pr
->throttled
|| pr
->throttle_override
);
530 * Called to send data down the raw connection.
532 int pfd_send(Socket s
, char *data
, int len
)
536 return sk_write(s
, data
, len
);
540 void pfd_confirm(Socket s
)
542 struct PFwdPrivate
*pr
;
547 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
550 sk_write(s
, NULL
, 0);
552 sshfwd_write(pr
->c
, pr
->buffer
, pr
->buflen
);