2626
2727#include <freeradius-devel/server/base.h>
2828#include <freeradius-devel/server/module.h>
29+ #include <freeradius-devel/server/log.h>
30+
2931#include <freeradius-devel/util/debug.h>
3032
3133#include <freeradius-devel/util/misc.h>
@@ -47,9 +49,19 @@ typedef struct {
4749 uint32_t num_connections ; //!< number of dynamic connections
4850 uint32_t num_pending_packets ; //!< number of pending packets
4951 uint64_t client_id ; //!< Unique client identifier.
50- fr_rate_limit_t unknown_client ;
51- fr_rate_limit_t repeat_nak ;
52- fr_rate_limit_t queue_full ;
52+
53+ struct {
54+ fr_rate_limit_t accept_failed ;
55+ fr_rate_limit_t alloc_failed ;
56+ fr_rate_limit_t bad_type ;
57+ fr_rate_limit_t conn_alloc_failed ;
58+ fr_rate_limit_t max_connections ;
59+ fr_rate_limit_t queue_full ;
60+ fr_rate_limit_t repeat_nak ;
61+ fr_rate_limit_t too_many_pending ;
62+ fr_rate_limit_t tracking_failed ;
63+ fr_rate_limit_t unknown_client ;
64+ } rate_limit ;
5365} fr_io_thread_t ;
5466
5567/** A saved packet
@@ -538,8 +550,9 @@ static fr_io_connection_t *fr_io_connection_alloc(fr_io_instance_t const *inst,
538550 (void ) fr_trie_walk (thread -> trie , & thread -> num_connections , count_connections );
539551
540552 if ((thread -> num_connections + 1 ) >= max_connections ) {
541- DEBUG ("proto_%s - Ignoring connection from client %s - 'max_connections' limit reached." ,
542- inst -> app -> common .name , client -> radclient -> shortname );
553+ RATE_LIMIT_LOCAL (& thread -> rate_limit .max_connections , INFO ,
554+ "proto_%s - Ignoring connection from client %s - 'max_connections' limit reached." ,
555+ inst -> app -> common .name , client -> radclient -> shortname );
543556 if (fd >= 0 ) close (fd );
544557 return NULL ;
545558 }
@@ -1305,6 +1318,10 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time
13051318 int value , accept_fd = -1 ;
13061319 uint32_t priority = PRIORITY_NORMAL ;
13071320
1321+ /** Log that we ignore clients in debug mode, or when it's enabled for a listener
1322+ */
1323+ #define LOG_IGNORED_CLIENTS (_inst ) ((_inst)->log_ignored_clients || fr_debug_lvl >= 1)
1324+
13081325 get_inst (li , & inst , & thread , & connection , & child );
13091326
13101327 track = NULL ;
@@ -1413,8 +1430,9 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time
14131430 * OK. So don't return -1.
14141431 */
14151432 if (accept_fd < 0 ) {
1416- INFO ("proto_%s - failed to accept new socket: %s" ,
1417- inst -> app -> common .name , fr_syserror (errno ));
1433+ RATE_LIMIT_LOCAL (& thread -> rate_limit .accept_failed ,
1434+ INFO , "proto_%s - failed to accept new socket: %s" ,
1435+ inst -> app -> common .name , fr_syserror (errno ));
14181436 return 0 ;
14191437 }
14201438
@@ -1494,8 +1512,11 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time
14941512 * the "name" of the socket, in the
14951513 * listener?
14961514 */
1497- DEBUG2 ("proto_%s - ignoring packet from IP %pV. It is not configured as 'type = ...'" ,
1498- inst -> app_io -> common .name , fr_box_ipaddr (address .socket .inet .src_ipaddr ));
1515+ if (LOG_IGNORED_CLIENTS (inst )) {
1516+ RATE_LIMIT_LOCAL (& thread -> rate_limit .bad_type , INFO ,
1517+ "proto_%s - ignoring packet from IP %pV. It is not configured as 'type = ...'" ,
1518+ inst -> app_io -> common .name , fr_box_ipaddr (address .socket .inet .src_ipaddr ));
1519+ }
14991520 return 0 ;
15001521 }
15011522 priority = value ;
@@ -1616,12 +1637,9 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time
16161637 close (accept_fd );
16171638 }
16181639
1619- if (DEBUG_ENABLED ) {
1620- DEBUG ("proto_%s - Ignoring %s from IP address %pV - %s" ,
1621- inst -> app_io -> common .name , msg , fr_box_ipaddr (address .socket .inet .src_ipaddr ),
1622- error );
1623- } else {
1624- RATE_LIMIT_LOCAL (& thread -> unknown_client , ERROR , "proto_%s - Ignoring %s from IP address %pV - %s" ,
1640+ if (LOG_IGNORED_CLIENTS (inst )) {
1641+ RATE_LIMIT_LOCAL (& thread -> rate_limit .unknown_client ,
1642+ ERROR , "proto_%s - Ignoring %s from IP address %pV - %s" ,
16251643 inst -> app_io -> common .name , msg , fr_box_ipaddr (address .socket .inet .src_ipaddr ),
16261644 error );
16271645 }
@@ -1642,7 +1660,8 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time
16421660 */
16431661 if (accept_fd >= 0 ) {
16441662 if (!fr_io_connection_alloc (inst , thread , client , accept_fd , & address , NULL )) {
1645- DEBUG ("Failed to allocate connection from client %s." , client -> radclient -> shortname );
1663+ RATE_LIMIT_LOCAL (& thread -> rate_limit .conn_alloc_failed ,
1664+ ERROR , "Failed to allocate connection from client %s" , client -> radclient -> shortname );
16461665 }
16471666
16481667 return 0 ;
@@ -1667,8 +1686,9 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time
16671686
16681687 track = fr_io_track_add (client , & address , buffer , packet_len , recv_time , & is_dup );
16691688 if (!track ) {
1670- DEBUG ("Failed tracking packet from client %s - discarding it" ,
1671- client -> radclient -> shortname );
1689+ RATE_LIMIT_LOCAL (& thread -> rate_limit .tracking_failed ,
1690+ ERROR , "Failed tracking packet from client %s - discarding it" ,
1691+ client -> radclient -> shortname );
16721692 return 0 ;
16731693 }
16741694
@@ -1736,8 +1756,9 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time
17361756 * to track pending packets.
17371757 */
17381758 if (!connection && inst -> max_pending_packets && (thread -> num_pending_packets >= inst -> max_pending_packets )) {
1739- DEBUG ("Too many pending packets from dynamic client %pV - discarding packet" ,
1740- fr_box_ipaddr (client -> src_ipaddr ));
1759+ RATE_LIMIT_LOCAL (& thread -> rate_limit .too_many_pending ,
1760+ ERROR , "Too many pending dynamic client packets for listener - discarding packet from %pV" ,
1761+ fr_box_ipaddr (client -> src_ipaddr ));
17411762
17421763 discard :
17431764 talloc_free (to_free );
@@ -1750,8 +1771,9 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time
17501771 pending = fr_io_pending_alloc (connection , client , buffer , packet_len ,
17511772 track , priority );
17521773 if (!pending ) {
1753- INFO ("proto_%s - Failed allocating space for dynamic client %pV - discarding packet" ,
1754- inst -> app_io -> common .name , fr_box_ipaddr (client -> src_ipaddr ));
1774+ RATE_LIMIT_LOCAL (& thread -> rate_limit .alloc_failed ,
1775+ ERROR , "proto_%s - Failed allocating space for dynamic client %pV - discarding packet" ,
1776+ inst -> app_io -> common .name , fr_box_ipaddr (client -> src_ipaddr ));
17551777 goto discard ;
17561778 }
17571779
@@ -1822,7 +1844,7 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time
18221844 * for it.
18231845 */
18241846 if (nak ) {
1825- RATE_LIMIT_LOCAL (& thread -> repeat_nak , ERROR , "proto_%s - Discarding repeated packet from NAK'd dynamic client %pV" ,
1847+ RATE_LIMIT_LOCAL (& thread -> rate_limit . repeat_nak , ERROR , "proto_%s - Discarding repeated packet from NAK'd dynamic client %pV" ,
18261848 inst -> app_io -> common .name , fr_box_ipaddr (address .socket .inet .src_ipaddr ));
18271849
18281850 DEBUG ("Discarding packet to NAKed connection %s" , connection -> name );
@@ -1836,7 +1858,8 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time
18361858 if (!connection ) {
18371859 connection = fr_io_connection_alloc (inst , thread , client , -1 , & address , NULL );
18381860 if (!connection ) {
1839- DEBUG ("Failed to allocate connection from client %s. Discarding packet." , client -> radclient -> shortname );
1861+ RATE_LIMIT_LOCAL (& thread -> rate_limit .conn_alloc_failed ,
1862+ ERROR , "Failed to allocate connection from client %s. Discarding packet." , client -> radclient -> shortname );
18401863 return 0 ;
18411864 }
18421865 }
@@ -1861,8 +1884,9 @@ static ssize_t mod_read(fr_listen_t *li, void **packet_ctx, fr_time_t *recv_time
18611884 */
18621885 if (fr_network_listen_inject (connection -> nr , connection -> listen ,
18631886 buffer , packet_len , recv_time ) < 0 ) {
1864- RATE_LIMIT_LOCAL (& thread -> queue_full , ERROR , "proto_%s - Discarding packet from dynamic client %pV - cannot push packet to connected socket due to %s" ,
1865- inst -> app_io -> common .name , fr_box_ipaddr (address .socket .inet .src_ipaddr ), fr_strerror ());
1887+ RATE_LIMIT_LOCAL (& thread -> rate_limit .queue_full , PERROR ,
1888+ "proto_%s - Discarding packet from dynamic client %pV - cannot push packet to connected socket" ,
1889+ inst -> app_io -> common .name , fr_box_ipaddr (address .socket .inet .src_ipaddr ));
18661890 /*
18671891 * Don't return an error, because that will cause the listener to close its socket.
18681892 */
0 commit comments