xref: /openbsd/usr.sbin/relayd/relayd.conf.5 (revision 17df1aa7)
1.\"	$OpenBSD: relayd.conf.5,v 1.112 2009/09/01 13:43:36 reyk Exp $
2.\"
3.\" Copyright (c) 2006, 2007 Reyk Floeter <reyk@openbsd.org>
4.\" Copyright (c) 2006, 2007 Pierre-Yves Ritschard <pyr@openbsd.org>
5.\"
6.\" Permission to use, copy, modify, and distribute this software for any
7.\" purpose with or without fee is hereby granted, provided that the above
8.\" copyright notice and this permission notice appear in all copies.
9.\"
10.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17.\"
18.Dd $Mdocdate: September 1 2009 $
19.Dt RELAYD.CONF 5
20.Os
21.Sh NAME
22.Nm relayd.conf
23.Nd relay daemon configuration file
24.Sh DESCRIPTION
25.Nm
26is the configuration file for the relay daemon,
27.Xr relayd 8 .
28.Sh SECTIONS
29.Nm
30is divided into six main sections:
31.Bl -tag -width xxxx
32.It Sy Macros
33User-defined variables may be defined and used later, simplifying the
34configuration file.
35.It Sy Global Configuration
36Global settings for
37.Xr relayd 8 .
38Do note that the config file allows global settings to be added after
39defining tables in the config file, but those tables will use the
40built-in defaults instead of the global settings below them.
41.It Sy Tables
42Table definitions describe a list of hosts,
43in a similar fashion to
44.Xr pf 4
45tables.
46They are used for relay, redirection, and router target selection with
47the described options and health checking on the host they contain.
48.It Sy Redirections
49Redirections are translated to
50.Xr pf 4
51rdr-to rules for stateful forwarding to a target host from a
52health-checked table on layer 3.
53.It Sy Relays
54Relays allow application layer load balancing, SSL acceleration, and
55general purpose TCP proxying on layer 7.
56.It Sy Protocols
57Protocols are predefined protocol handlers and settings for relays.
58.It Sy Routers
59Routers are used to insert routes with health-checked gateways for
60(WAN) link balancing.
61.El
62.Pp
63Within the sections,
64a host
65.Ar address
66can be specified by IPv4 address, IPv6 address, interface name,
67or DNS hostname.
68If the address is an interface name,
69.Xr relayd 8
70will look up the first IPv4 address and any other IPv4 and IPv6
71addresses of the specified network interface.
72A
73.Ar port
74can be specified by number or name.
75The port name to number mappings are found in the file
76.Pa /etc/services ;
77see
78.Xr services 5
79for details.
80.Pp
81Comments can be put anywhere in the file using a hash mark
82.Pq Sq # ,
83and extend to the end of the current line.
84.Pp
85Additional configuration files can be included with the
86.Ic include
87keyword, for example:
88.Bd -literal -offset indent
89include "/etc/relayd.conf.local"
90.Ed
91.Sh MACROS
92Macros can be defined that will later be expanded in context.
93Macro names must start with a letter, and may contain letters, digits,
94and underscores.
95Macro names may not be reserved words (for example,
96.Ic table ,
97.Ic relay ,
98or
99.Ic timeout ) .
100Macros are not expanded inside quotes.
101.Pp
102For example:
103.Bd -literal -offset indent
104www1="10.0.0.1"
105www2="10.0.0.2"
106table \*(Ltwebhosts\*(Gt {
107	$www1
108	$www2
109}
110.Ed
111.Sh GLOBAL CONFIGURATION
112Here are the settings that can be set globally:
113.Bl -tag -width Ds
114.It Ic demote Ar group
115Enable the global
116.Xr carp 4
117demotion option, resetting the carp demotion counter for the
118specified interface group to zero on startup and to 128 on shutdown of
119the daemon.
120For more information on interface groups,
121see the
122.Ic group
123keyword in
124.Xr ifconfig 8 .
125.It Ic interval Ar number
126Set the interval in seconds at which the hosts will be checked.
127The default interval is 10 seconds.
128.It Xo
129.Ic log
130.Pq Ic updates Ns \&| Ns Ic all
131.Xc
132Log state notifications after completed host checks.
133Either only log the
134.Ic updates
135to new states or log
136.Ic all
137state notifications, even if the state didn't change.
138The host state can be
139.Ar up
140(the health check completed successfully),
141.Ar down
142(the host is down or didn't match the check criteria),
143or
144.Ar unknown
145(the host is disabled or has not been checked yet).
146.It Ic prefork Ar number
147When using relays, run the specified number of processes to handle
148relayed connections.
149This increases the performance and prevents delays when connecting
150to a relay.
151.Xr relayd 8
152runs 5 relay processes by default and every process will handle
153all configured relays.
154.It Ic send trap
155Send an SNMP trap when the state of a host changes.
156.Xr relayd 8
157will try to connect to
158.Xr snmpd 8
159and request it send a trap to the registered trap receivers;
160see
161.Xr snmpd.conf 5
162for more information about the configuration.
163.It Ic timeout Ar number
164Set the global timeout in milliseconds for checks.
165This can be overridden by the timeout value in the table definitions.
166The default interval is 200 milliseconds and it must not exceed the
167global interval.
168Please note that the default value is optimized for checks within the
169same collision domain \(en use a higher timeout, such as 1000 milliseconds,
170for checks of hosts in other subnets.
171If this option is to be set, it should be placed before overrides in tables.
172.El
173.Sh TABLES
174Tables are used to group a set of hosts as the target for redirections
175or relays; they will be mapped to a
176.Xr pf 4
177table for redirections.
178Tables may be defined with the following attribute:
179.Bl -tag -width disable
180.It Ic disable
181Start the table disabled \(en no hosts will be checked in this table.
182The table can be later enabled through
183.Xr relayctl 8 .
184.Pp
185.El
186Each table must contain at least one host;
187multiple hosts are separated by newline, comma, or whitespace.
188Host entries may be defined with the following attributes:
189.Bl -tag -width retry
190.It Ic ip ttl Ar number
191Change the default time-to-live value in the IP headers for host checks.
192.It Ic parent Ar number
193The optional parent option inherits the state from a parent
194host with the specified identifier.
195The check will be skipped for this host and copied from the parent host.
196This can be used to prevent multiple checks on hosts with multiple IP
197addresses for the same service.
198The host identifiers are sequentially assigned to the configured hosts
199starting with 1; it can be shown with the
200.Xr relayctl 8
201.Ic show summary
202commands.
203.It Ic retry Ar number
204The optional retry option adds a tolerance for failed host checks;
205the check will be retried for
206.Ar number
207more times before setting the host state to down.
208If this table is used by a relay, it will also specify the number of
209retries for outgoing connection attempts.
210.El
211.Pp
212For example:
213.Bd -literal -offset indent
214table \*(Ltservice\*(Gt { 192.168.1.1, 192.168.1.2, 192.168.2.3 }
215table \*(Ltfallback\*(Gt disable { 10.1.5.1 retry 2 }
216
217redirect "www" {
218	listen on www.example.com port 80
219	forward to \*(Ltservice\*(Gt check http "/" code 200
220	forward to \*(Ltfallback\*(Gt check http "/" code 200
221}
222.Ed
223.Pp
224Tables are used by
225.Ic forward to
226directives in redirections or relays with a set of general options,
227health-checking rules, and timings;
228see the
229.Sx REDIRECTIONS
230and
231.Sx RELAYS
232sections for more information about the forward context.
233Table specific configuration directives are described below.
234Multiple options can be appended to
235.Ic forward to
236directives, separated by whitespaces.
237.Pp
238The following options will configure the health-checking method for
239the table, and is mandatory for redirections:
240.Bl -tag -width Ds
241.It Xo
242.Ic check http Ar path
243.Op Ic host Ar hostname
244.Ic code Ar number
245.Xc
246For each host in the table, verify that retrieving the URL
247.Ar path
248gives the HTTP return code
249.Ar number .
250If
251.Ar hostname
252is specified, it is used as the
253.Dq Host:
254header to query a specific hostname at the target host.
255To validate the HTTP return code, use this shell command:
256.Bd -literal -offset indent
257$ echo -n "HEAD <path> HTTP/1.0\er\en\er\en" | \e
258	nc <host> <port> | head -n1
259.Ed
260.Pp
261This prints the status header including the actual return code:
262.Bd -literal -offset indent
263HTTP/1.1 200 OK
264.Ed
265.It Xo
266.Ic check https Ar path
267.Op Ic host Ar hostname
268.Ic code Ar number
269.Xc
270This has the same effect as above but wraps the HTTP request in SSL.
271.It Xo
272.Ic check http Ar path
273.Op Ic host Ar hostname
274.Ic digest Ar string
275.Xc
276For each host in the table, verify that retrieving the URL
277.Ar path
278produces non-binary content whose message digest matches the defined string.
279The algorithm used is determined by the string length of the
280.Ar digest
281argument, either SHA1 (40 characters) or MD5 (32 characters).
282If
283.Ar hostname
284is specified, it is used as the
285.Dq Host:
286header to query a specific hostname at the target host.
287The digest does not take the HTTP headers into account.
288Do not specify a binary object (such as a graphic) as the target of the
289request, as
290.Nm
291expects the data returned to be a string.
292To compute the digest, use this simple command:
293.Bd -literal -offset indent
294$ ftp -o - http://host[:port]/path | sha1
295.Ed
296.Pp
297This gives a digest that can be used as-is in a digest statement:
298.Bd -literal -offset indent
299a9993e36476816aba3e25717850c26c9cd0d89d
300.Ed
301.It Xo
302.Ic check https Ar path
303.Op Ic host Ar hostname
304.Ic digest Ar string
305.Xc
306This has the same effect as above but wraps the HTTP request in SSL.
307.It Ic check icmp
308Ping hosts in this table to determine whether they are up or not.
309This method will automatically use ICMP or ICMPV6 depending on the
310address family of each host.
311.It Ic check script Ar path
312Execute an external program to check the host state.
313The program will be executed for each host by specifying the hostname
314on the command line:
315.Bd -literal -offset indent
316/usr/local/bin/checkload.pl front-www1.private.example.com
317.Ed
318.Pp
319.Xr relayd 8
320expects a positive return value on success and zero on failure.
321Note that the script will be executed with the privileges of the
322.Qq _relayd
323user and terminated after
324.Ar timeout
325milliseconds.
326.It Xo
327.Ic check send
328.Ar data
329.Ic expect
330.Ar pattern
331.Op Ic ssl
332.Xc
333For each host in the table, a TCP connection is established on the
334port specified, then
335.Ar data
336is sent.
337Incoming data is then read and is expected to match against
338.Ar pattern
339using shell globbing rules.
340If
341.Ar data
342is an empty string or
343.Ic nothing
344then nothing is sent on the connection and data is immediately
345read.
346This can be useful with protocols that output a banner like
347SMTP, NNTP, and FTP.
348If the
349.Ic ssl
350keyword is present,
351the transaction will occur in an SSL tunnel.
352.It Ic check ssl
353Perform a complete SSL handshake with each host to check their availability.
354.It Ic check tcp
355Use a simple TCP connect to check that hosts are up.
356.El
357.Pp
358The following general table options are available:
359.Bl -tag -width Ds
360.It Ic demote Ar group
361Enable the per-table
362.Xr carp 4
363demotion option.
364This will increment the carp demotion counter for the
365specified interface group if all hosts in the table are down.
366For more information on interface groups,
367see the
368.Ic group
369keyword in
370.Xr ifconfig 8 .
371.It Ic interval Ar number
372Override the global interval and specify one for this table.
373It must be a multiple of the global interval.
374.It Ic timeout Ar number
375Set the timeout in milliseconds for each host that is checked using
376TCP as the transport.
377This will override the global timeout, which is 200 milliseconds by default.
378.El
379.Pp
380The following options will set the scheduling algorithm to select a
381host from the specified table:
382.Bl -tag -width Ds
383.It Ic mode hash
384Balances the outgoing connections across the active hosts based on the
385hashed name of the table.
386Additional input can be fed into the hash by looking at HTTP
387headers and GET variables; see the
388.Sx PROTOCOLS
389section below.
390This mode is only supported by relays.
391.It Ic mode loadbalance
392Balances the outgoing connections across the active hosts based on the
393hashed name of the table, the source and destination addresses,
394and the corresponding ports.
395This mode is only supported by relays.
396.It Ic mode roundrobin
397Distributes the outgoing connections using a round-robin scheduler
398through all active hosts.
399This is the default mode and will be used if no option has been specified.
400This mode is supported by redirections and relays.
401.El
402.Sh REDIRECTIONS
403Redirections represent a
404.Xr pf 4
405rdr-to rule.
406They are used for stateful redirections to the hosts in the specified
407tables.
408.Xr pf 4
409rewrites the target IP addresses and ports of the incoming
410connections, operating on layer 3.
411The configuration directives that are valid in the
412.Ic redirect
413context are described below:
414.Bl -tag -width Ds
415.It Ic disable
416The redirection is initially disabled.
417It can be later enabled through
418.Xr relayctl 8 .
419.It Xo
420.Ic forward to
421.Aq Ar table
422.Op Ic port Ar number
423.Ar options ...
424.Xc
425Specify the tables of target hosts to be used; see the
426.Sx TABLES
427section above for information about table options.
428If the
429.Ic port
430option is not specified, the first port from the
431.Ic listen on
432directive will be used.
433This directive can be specified twice \(en the second entry will be used
434as the backup table if all hosts in the main table are down.
435At least one entry for the main table is mandatory.
436.It Xo
437.Ic listen on Ar address
438.Op ip-proto
439.Ic port Ar port
440.Op Ic interface Ar name
441.Xc
442Specify an
443.Ar address
444and a
445.Ar port
446to listen on.
447.Xr pf 4
448will redirect incoming connections for the specified target to the
449hosts in the main or backup table.
450The
451.Ar port
452argument can optionally specify a port range instead of a single port;
453the format is
454.Ar min-port : Ns Ar max-port .
455The optional argument
456.Ar ip-proto
457can be used to specify an IP protocol like
458.Ar tcp
459or
460.Ar udp ;
461it defaults to
462.Ar tcp .
463The rule can be optionally restricted to a given interface name.
464.It Xo
465.Ic route to
466.Aq Ar table
467.Op Ic port Ar number
468.Ar options ...
469.Xc
470Like the
471.Ic forward to
472directive, but directly routes the packets to the target host without
473modifying the target address using a
474.Xr pf 4
475route-to rule.
476This can be used for
477.Dq direct server return
478to force the target host to respond via a different gateway.
479Note that hosts have to accept sessions for the same address as
480the gateway, which is typically done by configuring a loopback
481interface on the host with this address.
482.It Ic session timeout Ar seconds
483Specify the inactivity timeout in seconds for established redirections.
484The default timeout is 600 seconds (10 minutes).
485.It Ic sticky-address
486This has the same effect as specifying sticky-address
487for an rdr-to rule in
488.Xr pf.conf 5 .
489It will ensure that multiple connections from the same source are
490mapped to the same redirection address.
491.It Ic tag Ar name
492Automatically tag packets passing through the
493.Xr pf 4
494rdr-to rule with the name supplied.
495This allows simpler filter rules.
496.El
497.Sh RELAYS
498Relays will forward traffic between a client and a target server.
499In contrast to redirections and IP forwarding in the network stack, a
500relay will accept incoming connections from remote clients as a
501server, open an outgoing connection to a target host, and forward
502any traffic between the target host and the remote client,
503operating on layer 7.
504A relay is also called an application layer gateway or layer 7 proxy.
505.Pp
506The main purpose of a relay is to provide advanced load balancing
507functionality based on specified protocol characteristics, such as
508HTTP headers, to provide SSL acceleration and to allow
509basic handling of the underlying application protocol.
510.Pp
511The
512.Ic relay
513configuration directives are described below:
514.Bl -tag -width Ds
515.It Ic disable
516Start the relay but immediately close any accepted connections.
517.It Xo
518.Op Ic transparent
519.Ic forward
520.Op Ic with ssl
521.Ic to
522.Ar address
523.Op Ic port Ar port
524.Ar options ...
525.Xc
526Specify the address and port of the target host to connect to.
527If the
528.Ic port
529option is not specified, the port from the
530.Ic listen on
531directive will be used.
532Use the
533.Ic transparent
534keyword to enable fully-transparent mode; the source address of the
535client will be retained in this case.
536.Pp
537The
538.Ic with ssl
539directive enables client-side SSL mode to connect to the remote host.
540Verification of server certificates can be enabled by setting the
541.Ic ca file
542option in the protocol section.
543.Pp
544The following options may be specified for forward directives:
545.Pp
546.Bl -tag -width Ds
547.It Ic retry Ar number
548The optional host
549.Ic retry
550option will be used as a tolerance for failed
551host connections; the connection will be retried for
552.Ar number
553more times.
554.It Ic inet
555If the requested destination is an IPv6 address,
556.Xr relayd 8
557will forward the connection to an IPv4 address which is determined by
558the last 4 octets of the original IPv6 destination.
559For example, if the original IPv6 destination address is
5602001:db8:7395:ffff::a01:101, the session is relayed to the IPv4
561address 10.1.1.1 (a01:101).
562.It Ic inet6 Ar address-prefix
563If the requested destination is an IPv4 address,
564.Xr relayd 8
565will forward the connection to an IPv6 address which is determined by
566setting the last 4 octets of the specified IPv6
567.Ar address-prefix
568to the 4 octets of the original IPv4 destination.
569For example, if the original IPv4 destination address is 10.1.1.1 and
570the specified address prefix is 2001:db8:7395:ffff::, the session is
571relayed to the IPv6 address 2001:db8:7395:ffff::a01:101.
572.El
573.It Xo
574.Ic forward to
575.Aq Ar table
576.Op Ic port Ar port
577.Ar options ...
578.Xc
579Like the previous directive, but connect to a host from the specified
580table; see the
581.Sx TABLES
582section above for information about table options.
583.It Xo
584.Ic forward to
585.Ic nat lookup
586.Ar options ...
587.Xc
588When redirecting connections with an rdr-to rule in
589.Xr pf.conf 5
590to a relay listening on localhost, this directive will
591look up the real destination address of the intended target host,
592allowing the relay to be run as a transparent proxy.
593If an additional
594.Ic forward to
595directive to a specified address or table is present,
596it will be used as a backup if the NAT lookup failed.
597.It Xo
598.Ic listen on Ar address
599.Op Ic port Ar port
600.Op Ic ssl
601.Xc
602Specify the address and port for the relay to listen on.
603The relay will accept incoming connections to the specified address.
604If the
605.Ic port
606option is not specified, the port from the
607.Ic listen on
608directive will be used.
609.Pp
610If the
611.Ic ssl
612keyword is present, the relay will accept connections using the
613encrypted SSL protocol.
614The relay will look up a private key in
615.Pa /etc/ssl/private/address.key
616and a public certificate in
617.Pa /etc/ssl/address.crt ,
618where
619.Ar address
620is the specified IP address of the relay to listen on.
621See
622.Xr ssl 8
623for details about SSL server certificates.
624.It Ic protocol Ar name
625Use the specified protocol definition for the relay.
626The generic TCP protocol options will be used by default;
627see the
628.Sx PROTOCOLS
629section below.
630.It Ic session timeout Ar seconds
631Specify the inactivity timeout in seconds for accepted sessions.
632The default timeout is 600 seconds (10 minutes).
633.El
634.Sh PROTOCOLS
635Protocols are templates defining actions and settings for relays.
636They allow setting generic TCP options, SSL settings, and actions
637specific to the selected application layer protocol.
638.Pp
639The protocol directive is available for a number of different
640application layer protocols.
641There is no generic handler for UDP-based protocols because it is a
642stateless datagram-based protocol which has to look into the
643application layer protocol to find any possible state information.
644.Bl -tag -width Ds
645.It Ic dns protocol
646(UDP)
647Domain Name System (DNS) protocol.
648The requested IDs in the DNS header will be used to match the state.
649.Xr relayd 8
650replaces these IDs with random values to compensate for
651predictable values generated by some hosts.
652.It Ic http protocol
653Handle the Hypertext Transfer Protocol
654(HTTP, or "HTTPS" if encapsulated in an SSL tunnel).
655.It Xo
656.Op Ic tcp
657.Ic protocol
658.Xc
659Generic handler for TCP-based protocols.
660This is the default.
661.El
662.Pp
663The available configuration directives are described below:
664.Bl -tag -width Ds
665.It Xo
666.Op Ar direction
667.Op Ar type
668.Ar action
669.Op Ic marked Ar id
670.Op Ic log
671.Xc
672Define an action for the selected entity.
673The optional
674.Ic log
675keyword will log the entity name and the value and
676the optional
677.Ic marked
678keyword requires that the session has been marked with a given
679identifier in order to execute the action.
680The actions are dependent on the underlying application
681.Ic protocol .
682.El
683.Pp
684.Bq Ar direction
685may be one of:
686.Bl -tag -width Ds
687.It Ic request
688Handle the data stream from the client to the relay, like HTTP
689requests.
690This is the default if the
691.Ar direction
692directive is omitted.
693.It Ic response
694Handle the data stream from the target host to the relay, like
695HTTP server replies.
696.El
697.Pp
698.Bq Ar type
699may be one of:
700.Bl -tag -width Ds
701.It Ic cookie
702Look up the entity as a value in the Cookie header when using the
703.Ic http
704protocol.
705This type is only available with the direction
706.Ic request .
707.It Ic header
708Look up the entity in the application protocol headers, like HTTP
709headers in
710.Ic http
711mode.
712.It Ic path
713Look up the entity as a value in the URL path when using the
714.Ic http
715protocol.
716This type is only available with the direction
717.Ic request .
718The
719.Ar key
720will match the path of the requested URL without the hostname
721and query and the value will match the complete query,
722for example:
723.Bd -literal -offset indent
724request path filter "/index.html"
725request path filter "foo=bar*" from "/cgi-bin/t.cgi"
726.Ed
727.It Ic query
728Look up the entity as a query variable in the URL when using the
729.Ic http
730protocol.
731This type is only available with the direction
732.Ic request ,
733for example:
734.Bd -literal -offset indent
735# Will match /cgi-bin/example.pl?foo=bar&ok=yes
736request query expect "bar" from "foo"
737.Ed
738.It Ic url
739Look up the entity as a URL suffix/prefix expression consisting of a
740canonicalized hostname without port or suffix and a path name or
741prefix when using the
742.Ic http
743protocol.
744This type is only available with the direction
745.Ic request ,
746for example:
747.Bd -literal -offset indent
748request url filter "example.com/index.html"
749request url filter "example.com/test.cgi?val=1"
750.Ed
751.Pp
752.Xr relayd 8
753will match the full URL and different possible suffix/prefix
754combinations by stripping subdomains and path components (up to 5
755levels), and the query string.
756For example, the following
757lookups will be done for
758.Ar http://www.example.com:81/1/2/3/4/5.html?query=yes :
759.Bd -literal -offset indent
760www.example.com/1/2/3/4/5.html?query=yes
761www.example.com/1/2/3/4/5.html
762www.example.com/
763www.example.com/1/
764www.example.com/1/2/
765www.example.com/1/2/3/
766example.com/1/2/3/4/5.html?query=yes
767example.com/1/2/3/4/5.html
768example.com/
769example.com/1/
770example.com/1/2/
771example.com/1/2/3/
772.Ed
773.El
774.Pp
775.Bq Ar action
776may be one of:
777.Bl -tag -width Ds
778.It Ic append Ar value Ic to Ar key
779Append the specified value to a protocol entity with the selected name.
780When using the
781.Ic http
782protocol,
783.Ic key
784will indicate a specified HTTP header.
785If
786.Ar key
787does not exist in the request, it will be created with the value
788set to
789.Ar value .
790.Pp
791The
792.Ar value
793string
794may contain predefined macros that will be expanded at runtime:
795.Pp
796.Bl -tag -width $SERVER_ADDR -offset indent -compact
797.It Ic $REMOTE_ADDR
798The IP address of the connected client.
799.It Ic $REMOTE_PORT
800The TCP source port of the connected client.
801.It Ic $SERVER_ADDR
802The configured IP address of the relay.
803.It Ic $SERVER_PORT
804The configured TCP server port of the relay.
805.It Ic $SERVER_NAME
806The server software name of
807.Xr relayd 8 .
808.It Ic $TIMEOUT
809The configured session timeout of the relay.
810.El
811.It Ic change Ar key Ic to Ar value
812Like the
813.Ic append
814directive above, but change the contents of the specified entity.
815If
816.Ar key
817does not exist in the request, it will be created with the value
818set to
819.Ar value .
820.Pp
821The
822.Ar value
823string
824may contain predefined macros that will be expanded at runtime,
825as detailed for the
826.Ic append
827directive above.
828.It Ic expect Ar value Ic from Ar key
829Expect an entity
830.Ar key
831and match against
832.Ar value
833using shell globbing rules.
834If the entity is not present or the value doesn't match, the connection
835will be dropped.
836.It Xo
837.Ic expect
838.Op Ic digest
839.Ar key
840.Xc
841Expect an entity
842.Ar key
843with any possible value.
844This is the short form of
845.Ic expect Ar * Ic from Ar key .
846.Pp
847If the
848.Ic digest
849keyword is specified,
850compare the message digest of the entity against the defined string.
851The algorithm used is determined by the string length of the
852.Ar key
853argument, either SHA1 (40 characters) or MD5 (32 characters).
854To compute the digest, use this simple command:
855.Bd -literal -offset indent
856$ echo -n "example.com/path/?args" | sha1
857.Ed
858.It Ic expect file Ar path
859Like the directive above, but load the non-digest keys from an
860external file with the specified
861.Ar path
862containing one key per line.
863Lines will be stripped at the first whitespace or newline character.
864Any empty lines or lines beginning with a hash mark
865.Pq Sq #
866will be ignored.
867.It Ic filter Ar value Ic from Ar key
868Like the
869.Ic expect Ar .. Ic from
870directive above, but drop any connections with the specified entity
871.Ar key
872and a matching
873.Ar value .
874.It Xo
875.Ic filter
876.Op Ic digest
877.Ar key
878.Xc
879Like the
880.Ic expect
881directive above, but drop any connections with the specified entity
882.Ar key
883and any possible value.
884This is the short form of
885.Ic filter Ar * Ic from Ar key .
886.It Ic filter file Ar path
887Like the directive above, but load the non-digest keys from
888.Ar path .
889See
890.Ic expect file Ar path
891for more information.
892.It Ic hash Ar key
893Feed the value of the selected entity into the load balancing hash to
894select the target host.
895See the
896.Ic table
897keyword in the
898.Sx RELAYS
899section above.
900.It Ic log Ar key
901Log the name and the value of the entity.
902.It Ic log file Ar path
903Like the directive above, but load the keys from
904.Ar path .
905See
906.Ic expect file Ar path
907for more information.
908.It Xo
909.Ic mark
910.Op Ar value Ic from
911.Ar key Ic with Ar id
912.Xc
913Mark the session with the specified identifier (a positive number
914between 1 and 65535) if the specified condition matches.
915Note that the
916.Ic mark
917action does not accept the
918.Ic marked
919option (see above).
920.It Ic label Ar string
921Add a label to subsequently added actions.
922The label will be printed as part of the error message if the
923.Ic return error
924option is set and may contain HTML tags, for example:
925.Bd -literal -offset indent
926label "\*(Lta href='http://example.com/advisory.pl?id=7359'\*(Gt\e
927	Advisory provided by example.com\*(Lt/a\*(Gt"
928url filter digest 5c1e03f58f8ce0b457474ffb371fd1ef
929url filter digest 80c1a7b8337462093ef8359c57b4d56a
930no label
931.Ed
932.It Ic no label
933Do not set a label for subsequently added actions; this is the default.
934.It Ic remove Ar key
935Remove the entity with the selected name.
936.It Ic remove file Ar path
937Like the directive above, but load the keys from
938.Ar path .
939See
940.Ic expect file Ar path
941for more information.
942.It Ic return error Op Ar option
943Return an error response to the client if an internal operation or the
944forward connection to the client failed.
945By default, the connection will be silently dropped.
946The effect of this option depends on the protocol: HTTP will send an
947error header and page to the client before closing the connection.
948Additional valid options are:
949.Bl -tag -width Ds
950.It Ic style Ar string
951Specify a Cascading Style Sheet (CSS) to be used for the returned
952HTTP error pages, for example:
953.Bd -literal -offset indent
954body { background: #a00000; color: white; }
955.Ed
956.El
957.It Ic ssl Ar option
958Set the SSL options and session settings.
959This is only used if SSL is enabled in the relay.
960Valid options are:
961.Bl -tag -width Ds
962.It Ic ca file Ar path
963This option enables CA verification in SSL client mode.
964The daemon will load the CA (Certificate Authority) certificates from
965the specified path to verify the server certificates.
966.Ox
967provides a default CA bundle in
968.Pa /etc/ssl/cert.pem .
969.It Ic ciphers Ar string
970Set the string defining the SSL cipher suite.
971If not specified, the default value
972.Ar HIGH:!ADH
973will be used (strong crypto cipher suites without anonymous DH).
974See the
975.Sx CIPHERS
976section of
977.Xr openssl 1
978for information about SSL cipher suites and preference lists.
979.It Ic session cache Ar value
980Set the maximum size of the SSL session cache.
981If the
982.Ar value
983is zero, the default size defined by the SSL library will be used.
984A positive number will set the maximum size in bytes and the keyword
985.Ic disable
986will disable the SSL session cache.
987.It Xo
988.Op Ic no
989.Ic sslv2
990.Xc
991Enable the SSLv2 protocol;
992disabled by default.
993.It Xo
994.Op Ic no
995.Ic sslv3
996.Xc
997Disable the SSLv3 protocol;
998enabled by default.
999.It Xo
1000.Op Ic no
1001.Ic tlsv1
1002.Xc
1003Disable the TLSv1/SSLv3.1 protocol;
1004enabled by default.
1005.El
1006.It Ic tcp Ar option
1007Enable or disable the specified TCP/IP options; see
1008.Xr tcp 4
1009and
1010.Xr ip 4
1011for more information about the options.
1012Valid options are:
1013.Bl -tag -width Ds
1014.It Ic backlog Ar number
1015Set the maximum length the queue of pending connections may grow to.
1016The backlog option is 10 by default and is limited by the
1017.Ic kern.somaxconn
1018.Xr sysctl 8
1019variable.
1020.It Ic ip minttl Ar number
1021This option for the underlying IP connection may be used to discard packets
1022with a TTL lower than the specified value.
1023This can be used to implement the
1024.Ar Generalized TTL Security Mechanism (GTSM)
1025according to RFC 3682.
1026.It Ic ip ttl Ar number
1027Change the default time-to-live value in the IP headers.
1028.It Xo
1029.Op Ic no
1030.Ic nodelay
1031.Xc
1032Enable the TCP NODELAY option for this connection.
1033This is recommended to avoid delays in the relayed data stream,
1034e.g. for SSH connections.
1035.It Xo
1036.Op Ic no
1037.Ic sack
1038.Xc
1039Use selective acknowledgements for this connection.
1040.It Ic socket buffer Ar number
1041Set the socket-level buffer size for input and output for this
1042connection.
1043This will affect the TCP window size.
1044.El
1045.El
1046.Sh ROUTERS
1047Routers represent routing table entries in the kernel forwarding
1048database, see
1049.Xr route 4 ,
1050and a table of associated gateways.
1051They are used to dynamically insert or remove routes with gateways
1052based on their availability and health-check results.
1053A router can include multiple network statements and a single forward
1054statement with a table of one or more gateways.
1055All entries in a single router directive must match the same address
1056family, either IPv4 or IPv6.
1057.Pp
1058The kernel supports multipath routing when multiple gateways exist to
1059the same destination address.
1060The multipath routing behaviour can be changed globally using the
1061.Xr sysctl 8
1062variables
1063.Va net.inet.ip.multipath
1064and
1065.Va net.inet6.ip6.multipath .
1066With the default setting of 0,
1067the first route selected will be used for subsequent packets to that
1068destination regardless of source.
1069Setting it to 1 will enable load balancing based on the packet source
1070address across gateways; multiple routes with the same priority are
1071used equally.
1072The kernel will also check the link state of the related network
1073interface and try a different route if it is not active.
1074.Pp
1075The configuration directives that are valid in the
1076.Ic routers
1077context are described below:
1078.Bl -tag -width Ds
1079.It Xo
1080.Ic forward to
1081.Aq Ar table
1082.Ic port Ar number
1083.Ar options ...
1084.Xc
1085Specify the table of target gateways to be used; see the
1086.Sx TABLES
1087section above for information about table options.
1088This entry is mandatory and must be specified once.
1089.It Xo
1090.Ic route
1091.Ar address Ns Li / Ns Ar prefix
1092.Xc
1093Specify the network address and prefix length of a route destination
1094that is reachable via the active gateways.
1095This entry must be specified at least once in a router directive.
1096.It Ic rtable Ar id
1097Add the routes to the kernel routing table with the specified
1098.Ar id .
1099.It Ic rtlabel Ar label
1100Add the routes with the specified
1101.Ar label
1102to the kernel routing table.
1103.El
1104.Sh FILES
1105.Bl -tag -width "/etc/ssl/private/address.keyXX" -compact
1106.It Pa /etc/relayd.conf
1107.Xr relayd 8
1108configuration file.
1109.Pp
1110.It Pa /etc/services
1111Service name database.
1112.Pp
1113.It Pa /etc/ssl/address.crt
1114.It Pa /etc/ssl/private/address.key
1115Location of the relay SSL server certificates, where
1116.Ar address
1117is the configured IP address of the relay.
1118.It Pa /etc/ssl/cert.pem
1119Default location of the CA bundle that can be used with
1120.Xr relayd 8 .
1121.El
1122.Sh EXAMPLES
1123This configuration file would create a redirection service
1124.Dq www
1125which load balances four hosts
1126and falls back to one host containing a
1127.Dq sorry page :
1128.Bd -literal -offset indent
1129www1=front-www1.private.example.com
1130www2=front-www2.private.example.com
1131www3=front-www3.private.example.com
1132www4=front-www4.private.example.com
1133
1134interval 5
1135
1136table \*(Ltphphosts\*(Gt { $www1, $www2, $www3, $www4 }
1137table \*(Ltsorryhost\*(Gt disable { sorryhost.private.example.com }
1138
1139redirect "www" {
1140	listen on www.example.com port 8080 interface trunk0
1141	listen on www6.example.com port 80 interface trunk0
1142
1143	tag REDIRECTED
1144
1145	forward to \*(Ltphphosts\*(Gt port 8080 timeout 300 \e
1146		check http "/" digest "630aa3c2f..."
1147	forward to \*(Ltsorryhost\*(Gt port 8080 timeout 300 check icmp
1148}
1149.Ed
1150.Pp
1151It is possible to specify multiple listen directives with different IP
1152protocols in a single redirection configuration:
1153.Bd -literal -offset indent
1154redirect "dns" {
1155	listen on dns.example.com tcp port 53
1156	listen on dns.example.com udp port 53
1157
1158	forward to \*(Ltdnshosts\*(Gt port 53 check tcp
1159}
1160.Ed
1161.Pp
1162The following configuration would add a relay to forward
1163secure HTTPS connections to a pool of HTTP webservers
1164using the
1165.Ic loadbalance
1166mode (SSL acceleration and layer 7 load balancing).
1167The HTTP protocol definition will add two HTTP headers containing
1168address information of the client and the server, set the
1169.Dq Keep-Alive
1170header value to the configured session timeout,
1171and include the
1172.Dq sessid
1173variable in the hash to calculate the target host:
1174.Bd -literal -offset indent
1175http protocol "http_ssl" {
1176	header append "$REMOTE_ADDR" to "X-Forwarded-For"
1177	header append "$SERVER_ADDR:$SERVER_PORT" to "X-Forwarded-By"
1178	header change "Keep-Alive" to "$TIMEOUT"
1179	query hash "sessid"
1180	cookie hash "sessid"
1181	path filter "*command=*" from "/cgi-bin/index.cgi"
1182
1183	ssl { sslv2, ciphers "MEDIUM:HIGH" }
1184}
1185
1186relay "sslaccel" {
1187	listen on www.example.com port 443 ssl
1188	protocol "http_ssl"
1189	forward to \*(Ltphphosts\*(Gt port 8080 mode loadbalance check tcp
1190}
1191.Ed
1192.Pp
1193The second relay example will accept incoming connections to port
11942222 and forward them to a remote SSH server.
1195The TCP
1196.Ic nodelay
1197option will allow a
1198.Dq smooth
1199SSH session without delays between keystrokes or displayed output on
1200the terminal:
1201.Bd -literal -offset indent
1202protocol "myssh" {
1203        tcp { nodelay, socket buffer 65536 }
1204}
1205
1206relay "sshforward" {
1207        listen on www.example.com port 2222
1208	protocol "myssh"
1209	forward to shell.example.com port 22
1210}
1211.Ed
1212.Pp
1213The next simple router configuration example can be used to run
1214redundant, health-checked WAN links:
1215.Bd -literal -offset indent
1216table \*(Ltgateways\*(Gt { $gw1 ip ttl 1, $gw2 ip ttl 1 }
1217router "uplinks" {
1218	route 0.0.0.0/0
1219	forward to \*(Ltgateways\*(Gt check icmp
1220}
1221.Ed
1222.Sh SEE ALSO
1223.Xr relayctl 8 ,
1224.Xr relayd 8 ,
1225.Xr snmpd 8 ,
1226.Xr ssl 8
1227.Sh HISTORY
1228The
1229.Nm
1230file format, formerly known as
1231.Ic hoststated.conf ,
1232first appeared in
1233.Ox 4.1 .
1234It was renamed to
1235.Nm
1236in
1237.Ox 4.3 .
1238.Sh AUTHORS
1239.An -nosplit
1240The
1241.Xr relayd 8
1242program was written by
1243.An Pierre-Yves Ritschard Aq pyr@openbsd.org
1244and
1245.An Reyk Floeter Aq reyk@openbsd.org .
1246.Sh CAVEATS
1247.Xr relayd 8
1248Verification of SSL server certificates is based on a static CA bundle
1249and
1250.Xr relayd 8
1251currently does not support CRLs (Certificate Revocation Lists).
1252