src/modules/pike/README
31ccf6a2
 pike Module
 
 Bogdan-Andrei Iancu
 
d9bb387b
    Voice Sistem SRL
31ccf6a2
 
 Edited by
 
 Bogdan-Andrei Iancu
 
9cb2a163
    Copyright © 2003 FhG FOKUS
ea4ff090
      __________________________________________________________________
31ccf6a2
 
    Table of Contents
d77df08a
 
9fc784c6
    1. Admin Guide
31ccf6a2
 
ea4ff090
         1. Overview
         2. Dependencies
31ccf6a2
 
ea4ff090
               2.1. Kamailio Modules
               2.2. External Libraries or Applications
31ccf6a2
 
ba7961bb
         3. Parameters
31ccf6a2
 
ea4ff090
               3.1. sampling_time_unit (integer)
               3.2. reqs_density_per_unit (integer)
               3.3. remove_latency (integer)
               3.4. pike_log_level (integer)
31ccf6a2
 
ba7961bb
         4. Functions
31ccf6a2
 
ea4ff090
               4.1. pike_check_req()
31ccf6a2
 
07f2b283
         5. RPC Commands
1061d103
 
07f2b283
               5.1. pike.top
1061d103
 
3ad60fe1
    2. RPC calls
 
         1. pike.top
f22d1eba
         2. pike.list
3ad60fe1
 
    3. Developer Guide
31ccf6a2
 
    List of Examples
d77df08a
 
    1.1. Set sampling_time_unit parameter
    1.2. Set reqs_density_per_unit parameter
    1.3. Set remove_latency parameter
9cbd0164
    1.4. Set pike_log_level parameter
    1.5. pike_check_req usage
f22d1eba
    2.1. Using pike.top
3ad60fe1
    3.1. Tree of IP addresses
31ccf6a2
 
9fc784c6
 Chapter 1. Admin Guide
31ccf6a2
 
ea4ff090
    Table of Contents
 
    1. Overview
    2. Dependencies
 
         2.1. Kamailio Modules
         2.2. External Libraries or Applications
 
ba7961bb
    3. Parameters
ea4ff090
 
         3.1. sampling_time_unit (integer)
         3.2. reqs_density_per_unit (integer)
         3.3. remove_latency (integer)
         3.4. pike_log_level (integer)
 
ba7961bb
    4. Functions
ea4ff090
 
         4.1. pike_check_req()
31ccf6a2
 
07f2b283
    5. RPC Commands
31ccf6a2
 
07f2b283
         5.1. pike.top
31ccf6a2
 
ea4ff090
 1. Overview
31ccf6a2
 
77462923
    The pike module keeps trace of all (or selected ones) incoming
    request's IP source and blocks the ones that exceed the limit. It works
    simultaneously for IPv4 and IPv6 addresses.
ea4ff090
 
    The module does not implement any actions on blocking - it just simply
77462923
    reports that there is high traffic from an IP; what to do, is the
8868e624
    administrator decision (via scripting).
ea4ff090
 
 2. Dependencies
 
    2.1. Kamailio Modules
    2.2. External Libraries or Applications
 
 2.1. Kamailio Modules
31ccf6a2
 
    The following modules must be loaded before this module:
37e4f522
      * No dependencies on other Kamailio modules.
31ccf6a2
 
ea4ff090
 2.2. External Libraries or Applications
31ccf6a2
 
ea4ff090
    The following libraries or applications must be installed before
    running Kamailio with this module loaded:
31ccf6a2
      * None.
 
ba7961bb
 3. Parameters
31ccf6a2
 
ea4ff090
    3.1. sampling_time_unit (integer)
    3.2. reqs_density_per_unit (integer)
    3.3. remove_latency (integer)
    3.4. pike_log_level (integer)
31ccf6a2
 
ea4ff090
 3.1. sampling_time_unit (integer)
31ccf6a2
 
98b3adf2
    Time period in seconds used for sampling (or the sampling accuracy).
    The smaller the better, but slower. If you want to detect peeks, use a
ea4ff090
    small one. To limit the access (like total number of requests on a long
77462923
    period of time) to a proxy resource (a gateway for example), use a
    bigger value of this parameter.
ea4ff090
 
77462923
    IMPORTANT: a too small value may lead to performance penalties due to
ea4ff090
    timer process overloading.
31ccf6a2
 
9cb2a163
    Default value is “2”.
31ccf6a2
 
d77df08a
    Example 1.1. Set sampling_time_unit parameter
31ccf6a2
 ...
 modparam("pike", "sampling_time_unit", 10)
 ...
 
ea4ff090
 3.2. reqs_density_per_unit (integer)
31ccf6a2
 
ea4ff090
    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
77462923
    3*x for IPv4 addresses and between x and 8*x for IPv6 addresses.
31ccf6a2
 
989206bd
    Default value is 30.
31ccf6a2
 
d77df08a
    Example 1.2. Set reqs_density_per_unit parameter
31ccf6a2
 ...
 modparam("pike", "reqs_density_per_unit", 30)
 ...
 
ea4ff090
 3.3. remove_latency (integer)
31ccf6a2
 
155160c7
    Specifies for how long the IP address will be kept in memory after the
77462923
    last request from that IP address. It's a sort of timeout value, in
    seconds. Note that it is not the duration to keep the IP in state
8868e624
    'blocked'. An IP is unblocked next occurrence of 'sampling_time_unit'
77462923
    that does not exceed 'reqs_density_per_unit'. Keeping an IP in memory
    results in faster reaching of blocked state -- see the notes about the
    limits of getting to state 'blocked'.
31ccf6a2
 
989206bd
    Default value is 120.
31ccf6a2
 
d77df08a
    Example 1.3. Set remove_latency parameter
31ccf6a2
 ...
 modparam("pike", "remove_latency", 130)
 ...
 
ea4ff090
 3.4. pike_log_level (integer)
9cbd0164
 
77462923
    Syslog log level to be used by module to auto report the blocking (only
    first time) and unblocking of IPs detected as source of floods.
9cbd0164
 
989206bd
    Default value is 1 (L_WARN).
9cbd0164
 
    Example 1.4. Set pike_log_level parameter
 ...
 modparam("pike", "pike_log_level", -1)
 ...
 
ba7961bb
 4. Functions
ea4ff090
 
    4.1. pike_check_req()
31ccf6a2
 
9cb2a163
 4.1.  pike_check_req()
31ccf6a2
 
77462923
    Process the source IP of the current request and return false if the IP
    was exceeding the blocking limit.
31ccf6a2
 
9cbd0164
    Return codes:
de468aae
      * 1 (true) - IP is not to be blocked or internal error occurred.
9cbd0164
 
 Warning
ea4ff090
        IMPORTANT: in case of internal error, the function returns true to
        avoid reporting the current processed IP as blocked.
77462923
      * -1 (false) - IP is source of flooding, previously detected
ea4ff090
      * -2 (false) - IP is detected as a new source of flooding - first
        time detection
31ccf6a2
 
bfcbd22f
    This function can be used from REQUEST_ROUTE.
 
9cbd0164
    Example 1.5. pike_check_req usage
31ccf6a2
 ...
1aab08d8
 if (!pike_check_req()) { exit; };
31ccf6a2
 ...
1061d103
 
07f2b283
 5. RPC Commands
ea4ff090
 
07f2b283
    5.1. pike.top
1061d103
 
07f2b283
 5.1.  pike.top
1061d103
 
07f2b283
    Lists nodes in the pike tree.
1061d103
 
07f2b283
    Name: pike.top
1061d103
 
07f2b283
    Parameters: filter (optional) - it can be "ALL", "HOT" or "WARM". If
    missing, the "HOT" nodes are listed.
1061d103
 
07f2b283
    RPC Command Example:
 ...
 kamcmd pike.top
 ...
31ccf6a2
 
3ad60fe1
 Chapter 2. RPC calls
 
    Table of Contents
 
    1. pike.top
f22d1eba
    2. pike.list
3ad60fe1
 
9cb2a163
 1.  pike.top
3ad60fe1
 
    Pike.top behaves like a 'top' command and shows source IP addresses of
    incoming requestes to pike_check_req() function.
 
    The IP list is sorted by sum of leaf hits (prev and curr) descending
    and in second level by hits.
 
    Some IPs could be marked as HOT depending on theirs request rates.
 
f22d1eba
    pike.top command can take one string parameter which specifies what
    kind of nodes you are interested in. Possible values are HOT or ALL. If
    no argument is given, it behaves as HOT was used.
3ad60fe1
 
    Marking nodes HOT is done on server side, client only presents given
    data and make some postprocessing like sorting.
 
    Output of this command is a simple dump of ip_tree nodes marked as
    ip-leafs.
 
f22d1eba
    Example 2.1. Using pike.top
 ...
 kamcli rpc pike.top ALL
 ...
 
 2.  pike.list
 
    Alias for "pike.top" command.
 
3ad60fe1
 Chapter 3. Developer Guide
31ccf6a2
 
ea4ff090
    One single tree (for both IPv4 and IPv6) is used. Each node contains a
    byte, the IP addresses stretching from root to the leafs.
31ccf6a2
 
3ad60fe1
    Example 3.1. Tree of IP addresses
31ccf6a2
            / 193 - 175 - 132 - 164
55e721ee
 tree root /                \ - 142
31ccf6a2
           \ 195 - 37 - 78 - 163
55e721ee
                    \ - 79 - 134
31ccf6a2
 
ea4ff090
    To detect the whole address, step by step, from the root to the leafs,
    the nodes corresponding to each byte of the ip address are expanded. In
    order to be expended a node has to be hit for a given number of times
    (possible by different addresses; in the previous example, the node
9cb2a163
    “37” was expended by the 195.37.78.163 and 195.37.79.134 hits).
31ccf6a2
 
    For 193.175.132.164 with x= reqs_density_per_unit:
9cb2a163
      * After first req hits -> the “193” node is built.
      * After x more hits, the “175” node is build; the hits of “193” node
ea4ff090
        are split between itself and its child--both of them gone have x/2.
9cb2a163
      * And so on for node “132” and “164”.
      * Once “164” build the entire address can be found in the tree. “164”
ea4ff090
        becomes a leaf. After it will be hit as a leaf for x times, it will
9cb2a163
        become “RED” (further request from this address will be blocked).
ea4ff090
 
    So, to build and block this address were needed 3*x hits. Now, if reqs
    start coming from 193.175.132.142, the first 3 bytes are already in the
    tree (they are shared with the previous address), so I will need only x
9cb2a163
    hits (to build node “142” and to make it “RED”) to make this address
ea4ff090
    also to be blocked. This is the reason for the variable number of hits
    necessary to block an IP.
31ccf6a2
 
989206bd
    The maximum number of hits to turn an address red are (n is the
    address's number of bytes):
31ccf6a2
 
ea4ff090
    1 (first byte) + x (second byte) + (x / 2) * (n - 2) (for the rest of
    the bytes) + (n - 1) (to turn the node to red).
31ccf6a2
 
ea4ff090
    So, for IPv4 (n = 4) will be 3x and for IPv6 (n = 16) will be 9x. The
    minimum number of hits to turn an address red is x.