<!-- Module User's Guide -->

<chapter>

	<title>&adminguide;</title>

	<section id="overview" xreflabel="Overview">
	<title>Overview</title>
	<para>
	The WSS (Secure WebSocket) module provides the ability to communicate with
	a WebSocket (<ulink url='http://tools.ietf.org/html/rfc6455'>RFC
		6455</ulink>) client or server over a secure (TLS encrypted) channel.
	As part of the <ulink url='https://webrtc.org/'>WebRTC</ulink>
	specifications, this protocol can be used to provide secure VoIP calls to
	HTTPS enabled browsers.
	</para>
	<para>
	This module behaves as any other transport protocol module: in order to
	use it, you must define one or more listeners that will handle the secure
	WebSocket traffic, <emphasis>after</emphasis> the <varname>mpath</varname>
	parameter:
	<para>
		<programlisting format="linespecific">
...
mpath=/path/to/modules
...
socket=wss:10.0.0.1			# change with the listening IP
socket=wss:10.0.0.1:5060	# change with the listening IP and port
...
</programlisting>
	</para>
	Besides that, you need to define the TLS parameters for securing the connection. This is done through the <emphasis>tls_mgm</emphasis> module interface, similar to the <emphasis>proto_tls</emphasis> module:
	<para>
		<programlisting format="linespecific">
modparam("tls_mgm", "certificate", "/certs/biloxy.com/cert.pem")
modparam("tls_mgm", "private_key", "/certs/biloxy.com/privkey.pem")
modparam("tls_mgm", "ca_list", "/certs/wellknownCAs")
modparam("tls_mgm", "tls_method", "tlsv1")
modparam("tls_mgm", "verify_cert", "1")
modparam("tls_mgm", "require_cert", "1")
</programlisting>
	</para>
	Check the <emphasis>tls_mgm</emphasis> module documentation for more info.
	</para>
	</section>

	<section id="dependencies" xreflabel="Dependencies">
	<title>Dependencies</title>
	<section>
		<title>&osips; Modules</title>
		<para>
		The following modules must be loaded before this module:
         <itemizedlist>
			<listitem>
			<para>
				<emphasis>tls_openssl</emphasis> or <emphasis>tls_wolfssl</emphasis>,
				depending on the desired TLS library
			</para>
			</listitem>
			<listitem>
			<para>
				<emphasis>tls_mgm</emphasis>.
			</para>
			</listitem>
        </itemizedlist>
		</para>
	</section>
	<section>
		<title>Dependencies of external libraries</title>
		<para>
		The following libraries or applications must be installed before
		running &osips; with this module loaded:
			<itemizedlist>
			<listitem>
			<para>
				<emphasis>None</emphasis>.
			</para>
			</listitem>
			</itemizedlist>
		</para>
	</section>
	</section>

	<section id="exported_parameters" xreflabel="Exported Parameters">
	<title>Exported Parameters</title>
		<para>
		All these parameters can be used from the opensips.cfg file,
		to configure the behavior of &osips;-WSS.
		</para>

		<section id="param_listen" xreflabel="listen">
			<title><varname>listen</varname>=interface</title>
			<para>
			This is a global parameter that specifies what interface/IP and
			port should handle WSS traffic.
			</para>
			<example>
				<title>Set <varname>listen</varname> variable</title>
				<programlisting format="linespecific">
...
socket= wss:1.2.3.4:44344
...
				</programlisting>
			</example>
		</section>

		<section id="param_wss_port" xreflabel="wss_port">
			<title><varname>wss_port</varname> (integer)</title>
			<para>
			The default port to be used for all WSS related operation. Be 
			careful as the default port impacts both the SIP listening part 
			(if no port is defined in the WSS listeners) and the SIP sending 
			part (if the destination WSS URI has no explicit port).
			</para>
			<para>
			If you want to change only the listening port for WSS, use the port
			option in the SIP listener defintion.
			</para>
			<para><emphasis>
				Default value is 443.
			</emphasis></para>
			<example>
				<title>Set <varname>wss_port</varname> variable</title>
				<programlisting format="linespecific">
...
modparam("proto_wss", "wss_port", 44344)
...
				</programlisting>
			</example>
		</section>

	<section id="param_wss_max_msg_chunks" xreflabel="wss_max_msg_chunks">
		<title><varname>wss_max_msg_chunks</varname> (integer)</title>
		<para>
			The maximum number of chunks in which a SIP message is expected to
			arrive via WSS. If a received packet is more fragmented than this,
			the connection is dropped (either the connection is very
			overloaded and this leads to high fragmentation - or we are the
			victim of an ongoing attack where the attacker is sending very
			fragmented traffic in order to decrease server performance).
		</para>
		<para>
		<emphasis>
			Default value is 4.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>wss_max_msg_chunks</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_wss", "wss_max_msg_chunks", 8)
...
</programlisting>
		</example>
	</section>

	<section id="param_wss_resource" xreflabel="wss_resource">
		<title><varname>wss_resource</varname> (string)</title>
		<para>
			The resource queried for when a WebSocket handshake is initiated.
		</para>
		<para>
		<emphasis>
			Default value is <quote>/</quote>.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>wss_resource</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_wss", "wss_resource", "/wss")
...
</programlisting>
		</example>
	</section>
	<section id="param_wss_handshake_timeout" xreflabel="wss_handshake_timeout">
		<title><varname>wss_handshake_timeout</varname> (integer)</title>
		<para>
			This parameter specifies the time in milliseconds the proto_wss module
			waits for a WebSocket handshake reply from a WebSocket server.
		</para>
		<para>
		<emphasis>
			Default value is 100.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>wss_handshake_timeout</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_wss", "wss_handshake_timeout", 300)
...
</programlisting>
		</example>
	</section>

	<section id="param_tls_cert_check_on_conn_reusage" xreflabel="cert_check_on_conn_reusage">
		<title><varname>cert_check_on_conn_reusage</varname> (integer)</title>
		<para>
		This parameter turns on or off the extra checking/matching of the
		TLS domain (SSL certificate) when comes to reusing an existing TLS
		connection. Without this extra check, only IP and port of the
		connections will be check (in order to re-use an existing connection).
		With this extra check, the connection to be reused must have the same
		SSL certificate as the one set for the current signaling operation.
		</para>
		<para>
		This checking is done only when comes to send SIP traffic via TLS and
		it is applied only against connections that were created / initiated 
		by OpenSIPS (as TLS client). Any accepte connection (as TLS server) will
		automatically match (the extra test will be skipped).
		</para>
		<para>
		<emphasis>
			Default value is 0 (disabled).
		</emphasis>
		</para>
		<example>
		<title>Set <varname>cert_check_on_conn_reusage</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_wss", "cert_check_on_conn_reusage", 1)
...
</programlisting>
		</example>
	</section>

	<section id="param_trace_destination" xreflabel="trace_destination">
		<title><varname>trace_destination</varname> (string)</title>
		<para>
			Trace destination as defined in the tracing module. Currently
		the only tracing module is <emphasis role="bold">proto_hep</emphasis>.
		Network events such as connect, accept and connection closed events
		shall be traced along with errors that could appear in the process.
		For each connection that is created an event containing information
		about the client and server certificate, master key, http request and
		reply belonging to  web socket protocol handshake and network layer
		information shall be sent.
		</para>
		<para>
			<emphasis role="bold">WARNING: </emphasis>A tracing module must be
			loaded in order for this parameter to work. (for example
			<emphasis role="bold">proto_hep</emphasis>).
		</para>
		<para>
		<emphasis>
			Default value is none(not defined).
		</emphasis>
		</para>
		<example>
		<title>Set <varname>trace_destination</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_hep", "hep_id", "[hep_dest]10.0.0.2;transport=tcp;version=3")

modparam("proto_wss", "trace_destination", "hep_dest")
...
</programlisting>
		</example>
	</section>

	<section id="param_trace_on" xreflabel="trace_on">
		<title><varname>trace_on</varname> (int)</title>
		<para>
			This controls whether tracing for wss is on or not. You still need to define
			<xref linkend="param_trace_destination"/>in order to work, but this value will be
			controlled using mi function <xref linkend="mi_wss_trace"/>.
		</para>
		<emphasis>
			Default value is 0(tracing inactive).
		</emphasis>
		<example>
		<title>Set <varname>trace_on</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_wss", "trace_on", 1)
...
</programlisting>
		</example>
	</section>

	<section id="param_trace_filter_route" xreflabel="trace_filter_route">
		<title><varname>trace_filter_route</varname> (string)</title>
		<para>
			Define the name of a route in which you can filter which connections will
			be trace and which connections won't be. In this route you will have
			information regarding source and destination ips and ports for the current
			connection. To disable tracing for a specific connection the last call in
			this route must be <emphasis role="bold">drop</emphasis>, any other exit
			mode resulting in tracing the current connection ( of course you still
			have to define a <xref linkend="param_trace_destination"/> and trace must be
			on at the time this connection is opened.
		</para>
		<para>
			<emphasis role="bold">IMPORTANT</emphasis>
			Filtering on ip addresses and ports can be made using <emphasis role="bold">
			$si</emphasis> and <emphasis role="bold">$sp</emphasis> for matching
			either the entity that is connecting to &osips; or the entity to which
			&osips; is connecting. The name might be misleading (<emphasis role="bold">
				$si</emphasis> meaning the source ip if you read the docs) but in reality
			it is simply the socket other than the &osips; socket. In order to match
			&osips; interface (either the one that accepted the connection or the one
			that initiated a connection) <emphasis role="bold">$socket_in(ip)</emphasis> (ip) and
			<emphasis role="bold">$socket_in(port)</emphasis> (port) can be used.
		</para>

		<para>
			<emphasis role="bold">WARNING:</emphasis> IF <xref linkend="param_trace_on"/> is
			set to 0 or tracing is deactived via the mi command <xref linkend="mi_wss_trace"/>
			this route won't be called.
		</para>
		<emphasis>
			Default value is none(no route is set).
		</emphasis>
		<example>
		<title>Set <varname>trace_filter_route</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("proto_wss", "trace_filter_route", "wss_filter")
...
/* all wss connections will go through this route if tracing is activated
 * and a trace destination is defined */
route[wss_filter] {
	...
	/* all connections opened from/by ip 1.1.1.1:8000 will be traced
	   on interface 1.1.1.10:5060(opensips listener)
	   all the other connections won't be */
	 if ( $si == "1.1.1.1" &amp;&amp; $sp == 8000 &amp;&amp;
		$socket_in(ip) == "1.1.1.10"  &amp;&amp; $socket_in(port) == 5060)
		exit;
	else
		drop;
}
...
</programlisting>
		</example>
	</section>

	<section id="param_wss_tls_handshake_timeout" xreflabel="wss_tls_handshake_timeout">
		<title><varname>wss_tls_handshake_timeout</varname> (integer)</title>
		<para>
		Sets the timeout (in milliseconds) for the SSL handshake sequence to complete.
			It may be necessary to increase this value when using a CPU intensive cipher
			for the connection to allow time for keys to be generated and processed.
		</para>
		<para>
		The timeout is invoked during acceptance of a new connection (inbound) and
			during the wait period when a new session is being initiated (outbound).
		</para>
		<para><emphasis>
			Default value is 100.
		</emphasis></para>
		<example>
			<title>Set <varname>wss_tls_handshake_timeout</varname> variable</title>
			<programlisting format="linespecific">

param("proto_wss", "wss_tls_handshake_timeout", 200) # number of milliseconds

			</programlisting>
		</example>
	</section>

	<section id="param_wss_send_timeout" xreflabel="wss_send_timeout">
		<title><varname>wss_send_timeout</varname> (integer)</title>
		<para>
			Sets the timeout (in milliseconds) for the send operations to complete
		</para>
		<para>
			The send timeout is invoked for all TLS write operations, excluding
			the handshake process (see: wss_tls_handshake_timeout)
		</para>
		<para><emphasis>
			Default value is 100.
		</emphasis></para>
		<example>
			<title>Set <varname>wss_send_timeout</varname> variable</title>
			<programlisting format="linespecific">

modparam("proto_wss", "wss_send_timeout", 200) # number of milliseconds

			</programlisting>
		</example>
	</section>

	<section id="param_require_origin" xreflabel="require_origin">
		<title><varname>require_origin</varname> (int)</title>
		<para>
			Controls whether the module should require the Origin header or not.
		</para>
		<emphasis>
			Default value is 1(require Origin header).
		</emphasis>
		<example>
		<title>Set <varname>require_origin</varname> parameter</title>
		<programlisting format="linespecific">

modparam("proto_wss", "require_origin", no)

		</programlisting>
		</example>
	</section>
</section>


	<section id="exported_mi_functions" xreflabel="Exported MI Functions">
	<title>Exported MI Functions</title>

		<section id="mi_wss_trace" xreflabel="wss_trace">
			<title>
			<function moreinfo="none">wss_trace</function>
			</title>

			<para>
			</para>

			<para>
			Name: <emphasis>wss_trace</emphasis>
			</para>

			<para>Parameters: </para>
			<itemizedlist>
				<listitem>
					<para>trace_mode(optional): set wss tracing on and off. This parameter
						can be missing and the command will show the current tracing
						status for this module( on or off );
						Possible values:
						<itemizedlist>
							<listitem><para> on </para></listitem>
							<listitem><para> off </para></listitem>
						</itemizedlist>
					</para>
				</listitem>
			</itemizedlist>

			<para>
			MI FIFO Command Format:
			</para>
			<programlisting  format="linespecific">
			opensips-cli -x mi wss_trace on
			</programlisting>
		</section>
	</section>

</chapter>
