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.
69 * FIXME: except we could initiate a full close here instead of
70 * just an outgoing EOF? ssh.c currently has no API for that, but
74 sshfwd_write_eof(pr
->c
);
79 static int pfd_receive(Plug plug
, int urgent
, char *data
, int len
)
81 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) plug
;
85 * Throughout SOCKS negotiation, "hostname" is re-used as a
86 * random protocol buffer with "port" storing the length.
88 if (pr
->port
>= lenof(pr
->hostname
)) {
89 /* Request too long. */
90 if ((pr
->dynamic
>> 12) == 4) {
91 /* Send back a SOCKS 4 error before closing. */
93 memset(data
, 0, sizeof(data
));
94 data
[1] = 91; /* generic `request rejected' */
95 sk_write(pr
->s
, data
, 8);
100 pr
->hostname
[pr
->port
++] = *data
++;
103 * Now check what's in the buffer to see if it's a
104 * valid and complete message in the SOCKS exchange.
106 if ((pr
->dynamic
== 1 || (pr
->dynamic
>> 12) == 4) &&
107 pr
->hostname
[0] == 4) {
111 if (pr
->dynamic
== 1)
112 pr
->dynamic
= 0x4000;
113 if (pr
->port
< 2) continue;/* don't have command code yet */
114 if (pr
->hostname
[1] != 1) {
116 /* Send back a SOCKS 4 error before closing. */
118 memset(data
, 0, sizeof(data
));
119 data
[1] = 91; /* generic `request rejected' */
120 sk_write(pr
->s
, data
, 8);
124 if (pr
->port
<= 8) continue; /* haven't started user/hostname */
125 if (pr
->hostname
[pr
->port
-1] != 0)
126 continue; /* haven't _finished_ user/hostname */
128 * Now we have a full SOCKS 4 request. Check it to
129 * see if it's a SOCKS 4A request.
131 if (pr
->hostname
[4] == 0 && pr
->hostname
[5] == 0 &&
132 pr
->hostname
[6] == 0 && pr
->hostname
[7] != 0) {
134 * It's SOCKS 4A. So if we haven't yet
135 * collected the host name, we should continue
136 * waiting for data in order to do so; if we
137 * have, we can go ahead.
140 if (pr
->dynamic
== 0x4000) {
141 pr
->dynamic
= 0x4001;
142 pr
->port
= 8; /* reset buffer to overwrite name */
145 pr
->hostname
[0] = 0; /* reply version code */
146 pr
->hostname
[1] = 90; /* request granted */
147 sk_write(pr
->s
, pr
->hostname
, 8);
149 pr
->port
= GET_16BIT_MSB_FIRST(pr
->hostname
+2);
150 memmove(pr
->hostname
, pr
->hostname
+ 8, len
);
154 * It's SOCKS 4, which means we should format
155 * the IP address into the hostname string and
158 pr
->hostname
[0] = 0; /* reply version code */
159 pr
->hostname
[1] = 90; /* request granted */
160 sk_write(pr
->s
, pr
->hostname
, 8);
161 pr
->port
= GET_16BIT_MSB_FIRST(pr
->hostname
+2);
162 sprintf(pr
->hostname
, "%d.%d.%d.%d",
163 (unsigned char)pr
->hostname
[4],
164 (unsigned char)pr
->hostname
[5],
165 (unsigned char)pr
->hostname
[6],
166 (unsigned char)pr
->hostname
[7]);
171 if ((pr
->dynamic
== 1 || (pr
->dynamic
>> 12) == 5) &&
172 pr
->hostname
[0] == 5) {
176 if (pr
->dynamic
== 1)
177 pr
->dynamic
= 0x5000;
179 if (pr
->dynamic
== 0x5000) {
183 * We're receiving a set of method identifiers.
185 if (pr
->port
< 2) continue;/* no method count yet */
186 if (pr
->port
< 2 + (unsigned char)pr
->hostname
[1])
187 continue; /* no methods yet */
188 method
= 0xFF; /* invalid */
189 for (i
= 0; i
< (unsigned char)pr
->hostname
[1]; i
++)
190 if (pr
->hostname
[2+i
] == 0) {
191 method
= 0;/* no auth */
196 sk_write(pr
->s
, data
, 2);
197 pr
->dynamic
= 0x5001;
198 pr
->port
= 0; /* re-empty the buffer */
202 if (pr
->dynamic
== 0x5001) {
204 * We're receiving a SOCKS request.
206 unsigned char reply
[10]; /* SOCKS5 atyp=1 reply */
210 * Pre-fill reply packet.
211 * In all cases, we set BND.{HOST,ADDR} to 0.0.0.0:0
212 * (atyp=1) in the reply; if we succeed, we don't know
213 * the right answers, and if we fail, they should be
216 memset(reply
, 0, lenof(reply
));
217 reply
[0] = 5; /* VER */
218 reply
[3] = 1; /* ATYP = 1 (IPv4, 0.0.0.0:0) */
220 if (pr
->port
< 6) continue;
221 atype
= (unsigned char)pr
->hostname
[3];
222 if (atype
== 1) /* IPv4 address */
224 if (atype
== 4) /* IPv6 address */
226 if (atype
== 3) /* domain name has leading length */
227 alen
= 1 + (unsigned char)pr
->hostname
[4];
228 if (pr
->port
< 6 + alen
) continue;
229 if (pr
->hostname
[1] != 1 || pr
->hostname
[2] != 0) {
230 /* Not CONNECT or reserved field nonzero - error */
231 reply
[1] = 1; /* generic failure */
232 sk_write(pr
->s
, (char *) reply
, lenof(reply
));
237 * Now we have a viable connect request. Switch
240 pr
->port
= GET_16BIT_MSB_FIRST(pr
->hostname
+4+alen
);
242 /* REP=0 (success) already */
243 sk_write(pr
->s
, (char *) reply
, lenof(reply
));
244 sprintf(pr
->hostname
, "%d.%d.%d.%d",
245 (unsigned char)pr
->hostname
[4],
246 (unsigned char)pr
->hostname
[5],
247 (unsigned char)pr
->hostname
[6],
248 (unsigned char)pr
->hostname
[7]);
250 } else if (atype
== 3) {
251 /* REP=0 (success) already */
252 sk_write(pr
->s
, (char *) reply
, lenof(reply
));
253 memmove(pr
->hostname
, pr
->hostname
+ 5, alen
-1);
254 pr
->hostname
[alen
-1] = '\0';
258 * Unknown address type. (FIXME: support IPv6!)
260 reply
[1] = 8; /* atype not supported */
261 sk_write(pr
->s
, (char *) reply
, lenof(reply
));
269 * If we get here without either having done `continue'
270 * or `goto connect', it must be because there is no
271 * sensible interpretation of what's in our buffer. So
272 * close the connection rudely.
280 * We come here when we're ready to make an actual
286 * Freeze the socket until the SSH server confirms the
289 sk_set_frozen(pr
->s
, 1);
291 pr
->c
= new_sock_channel(pr
->backhandle
, pr
->s
);
296 /* asks to forward to the specified host/port for this */
297 ssh_send_port_open(pr
->c
, pr
->hostname
, pr
->port
, "forwarding");
302 * If there's any data remaining in our current buffer,
303 * save it to be sent on pfd_confirm().
306 pr
->buffer
= snewn(len
, char);
307 memcpy(pr
->buffer
, data
, len
);
312 if (sshfwd_write(pr
->c
, data
, len
) > 0) {
314 sk_set_frozen(pr
->s
, 1);
320 static void pfd_sent(Plug plug
, int bufsize
)
322 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) plug
;
325 sshfwd_unthrottle(pr
->c
, bufsize
);
329 * Called when receiving a PORT OPEN from the server
331 const char *pfd_newconnect(Socket
*s
, char *hostname
, int port
,
332 void *c
, Conf
*conf
, int addressfamily
)
334 static const struct plug_function_table fn_table
= {
344 char *dummy_realhost
;
345 struct PFwdPrivate
*pr
;
350 addr
= name_lookup(hostname
, port
, &dummy_realhost
, conf
, addressfamily
);
351 if ((err
= sk_addr_error(addr
)) != NULL
) {
359 pr
= snew(struct PFwdPrivate
);
362 pr
->throttled
= pr
->throttle_override
= 0;
365 pr
->backhandle
= NULL
; /* we shouldn't need this */
368 pr
->s
= *s
= new_connection(addr
, dummy_realhost
, port
,
369 0, 1, 0, 0, (Plug
) pr
, conf
);
370 if ((err
= sk_socket_error(*s
)) != NULL
) {
375 sk_set_private_ptr(*s
, pr
);
380 called when someone connects to the local port
383 static int pfd_accepting(Plug p
, OSSocket sock
)
385 static const struct plug_function_table fn_table
= {
392 struct PFwdPrivate
*pr
, *org
;
396 org
= (struct PFwdPrivate
*)p
;
397 pr
= snew(struct PFwdPrivate
);
402 pr
->backhandle
= org
->backhandle
;
404 pr
->s
= s
= sk_register(sock
, (Plug
) pr
);
405 if ((err
= sk_socket_error(s
)) != NULL
) {
410 sk_set_private_ptr(s
, pr
);
412 pr
->throttled
= pr
->throttle_override
= 0;
417 pr
->port
= 0; /* "hostname" buffer is so far empty */
418 sk_set_frozen(s
, 0); /* we want to receive SOCKS _now_! */
421 strcpy(pr
->hostname
, org
->hostname
);
422 pr
->port
= org
->port
;
423 pr
->c
= new_sock_channel(org
->backhandle
, s
);
429 /* asks to forward to the specified host/port for this */
430 ssh_send_port_open(pr
->c
, pr
->hostname
, pr
->port
, "forwarding");
438 /* Add a new forwarding from port -> desthost:destport
439 sets up a listener on the local machine on (srcaddr:)port
441 const char *pfd_addforward(char *desthost
, int destport
, char *srcaddr
,
442 int port
, void *backhandle
, Conf
*conf
,
443 void **sockdata
, int address_family
)
445 static const struct plug_function_table fn_table
= {
448 pfd_receive
, /* should not happen... */
449 pfd_sent
, /* also should not happen */
454 struct PFwdPrivate
*pr
;
460 pr
= snew(struct PFwdPrivate
);
465 strcpy(pr
->hostname
, desthost
);
470 pr
->throttled
= pr
->throttle_override
= 0;
472 pr
->backhandle
= backhandle
;
474 pr
->s
= s
= new_listener(srcaddr
, port
, (Plug
) pr
,
475 !conf_get_int(conf
, CONF_lport_acceptall
),
476 conf
, address_family
);
477 if ((err
= sk_socket_error(s
)) != NULL
) {
482 sk_set_private_ptr(s
, pr
);
484 *sockdata
= (void *)s
;
489 void pfd_close(Socket s
)
491 struct PFwdPrivate
*pr
;
496 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
505 * Terminate a listener.
507 void pfd_terminate(void *sv
)
509 pfd_close((Socket
)sv
);
512 void pfd_unthrottle(Socket s
)
514 struct PFwdPrivate
*pr
;
517 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
520 sk_set_frozen(s
, pr
->throttled
|| pr
->throttle_override
);
523 void pfd_override_throttle(Socket s
, int enable
)
525 struct PFwdPrivate
*pr
;
528 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
530 pr
->throttle_override
= enable
;
531 sk_set_frozen(s
, pr
->throttled
|| pr
->throttle_override
);
535 * Called to send data down the raw connection.
537 int pfd_send(Socket s
, char *data
, int len
)
541 return sk_write(s
, data
, len
);
544 void pfd_send_eof(Socket s
)
549 void pfd_confirm(Socket s
)
551 struct PFwdPrivate
*pr
;
556 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
559 sk_write(s
, NULL
, 0);
561 sshfwd_write(pr
->c
, pr
->buffer
, pr
->buflen
);