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

<chapter>

	<title>&adminguide;</title>

	<section id="overview" xreflabel="Overview">
	<title>Overview</title>
	<para>
		The module provides a simple mechanism for DOS protection - DOS based
		on floods at network level. The module keeps trace of all (or selected
		ones) IPs of incoming SIP traffic (as source IP) and blocks the ones
		that exceeded some limit.
		Works simultaneous for IPv4 and IPv6 addresses.
	</para>
	<para>
		The module does not implement any actions on blocking - it just simply
		reports that there is a high traffic from an IP; what to do, is
		the administator decision (via scripting).
	</para>
	</section>

	<section>
	<title>How to use</title>
		<para>
		There are 2 ways of using this module (as detecting flood attacks and
		as taking the right action to limit the impact on the system):
		<itemizedlist>
		<listitem>
			<para>
				<emphasis>manual</emphasis> - from routing script you can force
				the check of the source IP of an incoming requests, using
				"pike_check_req" function. Note that this checking works only
				for SIP requests and you can decide (based on scripting logic)
				what source IPs to be monitored and what action to be taken
				when a flood is detected.
			</para>
		</listitem>
		<listitem>
			<para>
				<emphasis>automatic</emphasis> - the module will install
				internal hooks to catch all incoming requests and replies (even
				if not well formed from SIP point of view) - more or less the
				module will monitor all incoming packages (from the network) on
				the SIP sockets. Each time the source IP of a package needs to
				be analyse (to see if trusted or not), the module will run a
				script route - see "check_route" module parameter -, where,
				based on custom logic, you can decide if that IP needs to be
				monitored for flooding or not. As action, when flood is
				detected, the module will automatically drop the packages.
			</para>
		</listitem>
		</itemizedlist>
		</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>No dependencies on other &osips; modules</emphasis>.
			</para>
			</listitem>
			</itemizedlist>
		</para>
	</section>
	<section>
		<title>External Libraries or Applications</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>
	<section id="param_sampling_time_unit" xreflabel="sampling_time_unit">
		<title><varname>sampling_time_unit</varname> (integer)</title>
		<para>
		Time period used for sampling (or the sampling accuracy ;-) ). The
		smaller the better, but slower. If you want to detect peaks, use a
		small one. To limit the access (like total number of requests on a
		long period of time) to a proxy resource (a gateway for ex), use
		a bigger value of this parameter.
		</para>
		<para>
		IMPORTANT: a too small value may lead to performance penalties due
		timer process overloading.
		</para>
		<para>
		<emphasis>
			Default value is 2.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>sampling_time_unit</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("pike", "sampling_time_unit", 10)
...
</programlisting>
		</example>
	</section>
	<section id="param_reqs_density_per_unit" xreflabel="reqs_density_per_unit">
		<title><varname>reqs_density_per_unit</varname> (integer)</title>
		<para>
		How many requests should be allowed per sampling_time_unit before
		blocking all the incoming request from that IP. Practically, the
		blocking limit is between ( let's have x=reqs_density_per_unit) x
		and 3*x for IPv4 addresses and between x and 8*x for ipv6 addresses.
		</para>
		<para>
		<emphasis>
			Default value is 30.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>reqs_density_per_unit</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("pike", "reqs_density_per_unit", 30)
...
</programlisting>
		</example>
	</section>
	<section id="param_remove_latency" xreflabel="remove_latency">
		<title><varname>remove_latency</varname> (integer)</title>
		<para>
		For how long the IP address will be kept in memory after the last
		request from that IP address. It's a sort of timeout value.
		</para>
		<para>
		<emphasis>Note:</emphasis> If the <emphasis>remove_latency</emphasis>
		value is lower than <emphasis>sampling_time_unit</emphasis> value,
		nodes might expire before being unblocked, therefore losing some
		UNBLOCK events. In order to prevent this, if the
		<emphasis>remove_latency</emphasis> is lower, &osips; internally
		forces its value to <emphasis>sampling_time_unit + 1</emphasis>.
		</para>
		<para>
		<emphasis>
			Default value is 120.
		</emphasis>
		</para>
		<example>
		<title>Set <varname>remove_latency</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("pike", "remove_latency", 130)
...
</programlisting>
		</example>
	</section>

	<section id="param_check_route" xreflabel="check_route">
		<title><varname>check_route</varname> (integer)</title>
		<para>
		The name of the script route to be triggers (in automatic way) when a
		package is received from the network. If you do a "drop" in this route,
		it will indicate to the module that the source IP of the package does
		not need to be monitored. Otherwise, the source IP will be
		automatically monitered.
		</para>
		<para>
		By defining this parameter, the automatic checking mode is enabled.
		</para>
		<para>
		<emphasis>
			Default value is NONE (no auto mode).
		</emphasis>
		</para>
		<example>
		<title>Set <varname>check_route</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("pike", "check_route", "pike")
...
route[pike]{
    if ($si==111.222.111.222)  /*trusted, do not check it*/
        drop;
    /* all other IPs are checked*/
}
....
</programlisting>
		</example>
	</section>

	<section id="param_pike_log_level" xreflabel="pike_log_level">
		<title><varname>pike_log_level</varname> (integer)</title>
		<para>
		Log level to be used by module to auto report the blocking (only first
		time) and unblocking of IPs detected as source of floods.
		</para>
		<para>
		<emphasis>
			Default value is 1 (L_WARN).
		</emphasis>
		</para>
		<example>
		<title>Set <varname>pike_log_level</varname> parameter</title>
		<programlisting format="linespecific">
...
modparam("pike", "pike_log_level", -1)
...
</programlisting>
		</example>
	</section>
	</section>


	<section id="exported_functions" xreflabel="exported_functions">
	<title>Exported Functions</title>
	<section id="func_pike_check_req" xreflabel="pike_check_req()">
		<title>
		<function moreinfo="none">pike_check_req()</function>
		</title>
		<para>
		Process the source IP of the current request and returns false if
		the IP was exceeding the blocking limit.
		</para>
		<para>
		Return codes:
		<itemizedlist>
			<listitem>
			<para>
				<emphasis>1 (true)</emphasis> - IP is not to be blocked or
				internal error occurred.
			</para>
			<warning>
			IMPORTANT: in case of internal error, the function returns true to
			avoid reporting the current processed IP as blocked.
			</warning>
			</listitem>
			<listitem>
			<para>
				<emphasis>-1 (false)</emphasis> - IP is source of
				flooding, being previously detected
			</para>
			</listitem>
			<listitem>
			<para>
				<emphasis>-2 (false)</emphasis> - IP is detected as a new
				source of flooding - first time detection
			</para>
			</listitem>
		</itemizedlist>
		</para>
		<para>
		This function can be used from REQUEST_ROUTE.
		</para>
		<example>
		<title><function>pike_check_req</function> usage</title>
		<programlisting format="linespecific">
...
if (!pike_check_req()) { exit; };
...
</programlisting>
		</example>
	</section>
	</section>

	<section id="exported_mi_functions" xreflabel="Exported MI Functions">
	<title>Exported MI Functions</title>
	<section id="mi_pike_list" xreflabel="pike_list">
		<title>
		<function moreinfo="none">pike_list</function>
		</title>
		<para>
		Lists the nodes in the pike tree.
		</para>
		<para>
		Name: <emphasis>pike_list</emphasis>
		</para>
		<para>Parameters: <emphasis>none</emphasis></para>
 		<para>
		MI FIFO Command Format:
		</para>
		<programlisting  format="linespecific">
		opensips-cli -x mi pike_list
		</programlisting>
	</section>
	<section id="mi_pike_rm" xreflabel="pike_rm">
		<title>
		<function moreinfo="none">pike_rm</function>
		</title>
		<para>
                Remove a node from the pike tree by IP address.
		</para>
		<para>
		Name: <emphasis>pike_rm</emphasis>
		</para>
		<para>Parameters: </para>
                <itemizedlist>
                        <listitem><para>
                                <emphasis>IP</emphasis> - IP address currently blocked.
                        </para></listitem>
                </itemizedlist>
 		<para>
		MI FIFO Command Format:
		</para>
		<programlisting  format="linespecific">
		opensips-cli -x mi pike_rm 10.0.0.106
		</programlisting>
	</section>
	</section>

	<section id="exported_events" xreflabel="Exported Events">
	<title>Exported Events</title>
	<section id="event_E_PIKE_BLOCKED" xreflabel="E_PIKE_BLOCKED">
		<title>
		<function moreinfo="none">E_PIKE_BLOCKED</function>
		</title>
		<para>
			This event is raised when the <emphasis>pike</emphasis> module
			decides that an IP should be blocked.
		</para>
		<para>Parameters:</para>
		<itemizedlist>
			<listitem><para>
				<emphasis>ip</emphasis> - the IP address that has been blocked.
			</para></listitem>
		</itemizedlist>
	</section>
	</section>

	<section id="sr_identifiers" xreflabel="Status/Report Identifiers">
	<title>Provided Status/Report Identifiers</title>

	<para>
	The module provides the "pike" Status/Report group, only with
	the "main"/default SR identifier.
	</para>
	<para>
	There is no usefull status published by the module.
	</para>
	<para>
	In terms of reports/logs, the following events will be reported:
	</para>
	<itemizedlist>
		<listitem><para>
		IP X.Y.Z.W detected as flooding
		</para></listitem>
	</itemizedlist>

	<para>
	For how to access and use the Status/Report information, please see
	<ulink url='>https://www.opensips.org/Documentation/Interface-StatusReport-3-3'>https://www.opensips.org/Documentation/Interface-StatusReport-3-3</ulink>.
	</para>

</section>


</chapter>

