Home | History | Annotate | Line # | Download | only in inetd
ratelimit.c revision 1.1
      1  1.1  christos /*	$NetBSD: ratelimit.c,v 1.1 2021/10/12 19:08:04 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2021 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by James Browning, Gabe Coffland, Alex Gavin, and Solomon Ritzow.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos #include <sys/cdefs.h>
     32  1.1  christos __RCSID("$NetBSD: ratelimit.c,v 1.1 2021/10/12 19:08:04 christos Exp $");
     33  1.1  christos 
     34  1.1  christos #include <sys/queue.h>
     35  1.1  christos 
     36  1.1  christos #include <arpa/inet.h>
     37  1.1  christos 
     38  1.1  christos #include <stdio.h>
     39  1.1  christos #include <stdlib.h>
     40  1.1  christos #include <syslog.h>
     41  1.1  christos #include <unistd.h>
     42  1.1  christos #include <string.h>
     43  1.1  christos #include <errno.h>
     44  1.1  christos #include <stddef.h>
     45  1.1  christos 
     46  1.1  christos #include "inetd.h"
     47  1.1  christos 
     48  1.1  christos union addr {
     49  1.1  christos 	struct in_addr	ipv4_addr;
     50  1.1  christos 	/* ensure aligned for comparison in rl_ipv6_eq (already is on 64-bit) */
     51  1.1  christos #ifdef INET6
     52  1.1  christos 	struct in6_addr	ipv6_addr __attribute__((aligned(16)));
     53  1.1  christos #endif
     54  1.1  christos 	char		other_addr[NI_MAXHOST];
     55  1.1  christos };
     56  1.1  christos 
     57  1.1  christos static void	rl_reset(struct servtab *, time_t);
     58  1.1  christos static time_t	rl_time(void);
     59  1.1  christos static void	rl_get_name(struct servtab *, int, union addr *);
     60  1.1  christos static void	rl_drop_connection(struct servtab *, int);
     61  1.1  christos static struct rl_ip_node	*rl_add(struct servtab *, union addr *);
     62  1.1  christos static struct rl_ip_node	*rl_try_get_ip(struct servtab *, union addr *);
     63  1.1  christos static bool	rl_ip_eq(struct servtab *, union addr *, struct rl_ip_node *);
     64  1.1  christos #ifdef INET6
     65  1.1  christos static bool	rl_ipv6_eq(struct in6_addr *, struct in6_addr *);
     66  1.1  christos #endif
     67  1.1  christos #ifdef DEBUG_ENABLE
     68  1.1  christos static void	rl_print_found_node(struct servtab *, struct rl_ip_node *);
     69  1.1  christos #endif
     70  1.1  christos static void	rl_log_address_exceed(struct servtab *, struct rl_ip_node *);
     71  1.1  christos static const char	*rl_node_tostring(struct servtab *, struct rl_ip_node *, char[NI_MAXHOST]);
     72  1.1  christos static bool	rl_process_service_max(struct servtab *, int, time_t *);
     73  1.1  christos static bool	rl_process_ip_max(struct servtab *, int, time_t *);
     74  1.1  christos 
     75  1.1  christos /* Return 0 on allow, -1 if connection should be blocked */
     76  1.1  christos int
     77  1.1  christos rl_process(struct servtab *sep, int ctrl)
     78  1.1  christos {
     79  1.1  christos 	time_t now = -1;
     80  1.1  christos 
     81  1.1  christos 	DPRINTF(SERV_FMT ": processing rate-limiting",
     82  1.1  christos 	    SERV_PARAMS(sep));
     83  1.1  christos 	DPRINTF(SERV_FMT ": se_service_max "
     84  1.1  christos 	    "%zu and se_count %zu", SERV_PARAMS(sep),
     85  1.1  christos 	    sep->se_service_max, sep->se_count);
     86  1.1  christos 
     87  1.1  christos 	if (sep->se_count == 0) {
     88  1.1  christos 		now = rl_time();
     89  1.1  christos 		sep->se_time = now;
     90  1.1  christos 	}
     91  1.1  christos 
     92  1.1  christos 	if(!rl_process_service_max(sep, ctrl, &now)
     93  1.1  christos 	    || !rl_process_ip_max(sep, ctrl, &now)) {
     94  1.1  christos 		return -1;
     95  1.1  christos 	}
     96  1.1  christos 
     97  1.1  christos 	DPRINTF(SERV_FMT ": running service ", SERV_PARAMS(sep));
     98  1.1  christos 
     99  1.1  christos 	/* se_count is only incremented if rl_process will return 0 */
    100  1.1  christos 	sep->se_count++;
    101  1.1  christos 	return 0;
    102  1.1  christos }
    103  1.1  christos 
    104  1.1  christos /*
    105  1.1  christos  * Get the identifier for the remote peer based on sep->se_socktype and
    106  1.1  christos  * sep->se_family
    107  1.1  christos  */
    108  1.1  christos static void
    109  1.1  christos rl_get_name(struct servtab *sep, int ctrl, union addr *out)
    110  1.1  christos {
    111  1.1  christos 	union {
    112  1.1  christos 		struct sockaddr_storage ss;
    113  1.1  christos 		struct sockaddr sa;
    114  1.1  christos 		struct sockaddr_in sin;
    115  1.1  christos 		struct sockaddr_in6 sin6;
    116  1.1  christos 	} addr;
    117  1.1  christos 
    118  1.1  christos 	/* Get the sockaddr of socket ctrl */
    119  1.1  christos 	switch (sep->se_socktype) {
    120  1.1  christos 	case SOCK_STREAM: {
    121  1.1  christos 		socklen_t len = sizeof(struct sockaddr_storage);
    122  1.1  christos 		if (getpeername(ctrl, &addr.sa, &len) == -1) {
    123  1.1  christos 			/* error, log it and skip ip rate limiting */
    124  1.1  christos 			syslog(LOG_ERR,
    125  1.1  christos 			    SERV_FMT " failed to get peer name of the "
    126  1.1  christos 			    "connection", SERV_PARAMS(sep));
    127  1.1  christos 			exit(EXIT_FAILURE);
    128  1.1  christos 		}
    129  1.1  christos 		break;
    130  1.1  christos 	}
    131  1.1  christos 	case SOCK_DGRAM: {
    132  1.1  christos 		struct msghdr header = {
    133  1.1  christos 			.msg_name = &addr.sa,
    134  1.1  christos 			.msg_namelen = sizeof(struct sockaddr_storage),
    135  1.1  christos 			/* scatter/gather and control info is null */
    136  1.1  christos 		};
    137  1.1  christos 		ssize_t count;
    138  1.1  christos 
    139  1.1  christos 		/* Peek so service can still get the packet */
    140  1.1  christos 		count = recvmsg(ctrl, &header, MSG_PEEK);
    141  1.1  christos 		if (count == -1) {
    142  1.1  christos 			syslog(LOG_ERR,
    143  1.1  christos 			    "failed to get dgram source address: %s; exiting",
    144  1.1  christos 			    strerror(errno));
    145  1.1  christos 			exit(EXIT_FAILURE);
    146  1.1  christos 		}
    147  1.1  christos 		break;
    148  1.1  christos 	}
    149  1.1  christos 	default:
    150  1.1  christos 		DPRINTF(SERV_FMT ": ip_max rate limiting not supported for "
    151  1.1  christos 		    "socktype", SERV_PARAMS(sep));
    152  1.1  christos 		syslog(LOG_ERR, SERV_FMT
    153  1.1  christos 		    ": ip_max rate limiting not supported for socktype",
    154  1.1  christos 		    SERV_PARAMS(sep));
    155  1.1  christos 		exit(EXIT_FAILURE);
    156  1.1  christos 	}
    157  1.1  christos 
    158  1.1  christos 	/* Convert addr to to rate limiting address */
    159  1.1  christos 	switch (sep->se_family) {
    160  1.1  christos 		case AF_INET:
    161  1.1  christos 			out->ipv4_addr = addr.sin.sin_addr;
    162  1.1  christos 			break;
    163  1.1  christos #ifdef INET6
    164  1.1  christos 		case AF_INET6:
    165  1.1  christos 			out->ipv6_addr = addr.sin6.sin6_addr;
    166  1.1  christos 			break;
    167  1.1  christos #endif
    168  1.1  christos 		default: {
    169  1.1  christos 			int res = getnameinfo(&addr.sa,
    170  1.1  christos 			    (socklen_t)addr.sa.sa_len,
    171  1.1  christos 			    out->other_addr, NI_MAXHOST,
    172  1.1  christos 			    NULL, 0,
    173  1.1  christos 			    NI_NUMERICHOST
    174  1.1  christos 			);
    175  1.1  christos 			if (res != 0) {
    176  1.1  christos 				syslog(LOG_ERR,
    177  1.1  christos 				    SERV_FMT ": failed to get name info of "
    178  1.1  christos 				    "the incoming connection: %s; exiting",
    179  1.1  christos 				    SERV_PARAMS(sep), gai_strerror(res));
    180  1.1  christos 				exit(EXIT_FAILURE);
    181  1.1  christos 			}
    182  1.1  christos 			break;
    183  1.1  christos 		}
    184  1.1  christos 	}
    185  1.1  christos }
    186  1.1  christos 
    187  1.1  christos static void
    188  1.1  christos rl_drop_connection(struct servtab *sep, int ctrl)
    189  1.1  christos {
    190  1.1  christos 
    191  1.1  christos 	if (sep->se_wait == 0 && sep->se_socktype == SOCK_STREAM) {
    192  1.1  christos 		/*
    193  1.1  christos 		 * If the fd isn't a listen socket,
    194  1.1  christos 		 * close the individual connection too.
    195  1.1  christos 		 */
    196  1.1  christos 		close(ctrl);
    197  1.1  christos 		return;
    198  1.1  christos 	}
    199  1.1  christos 	if (sep->se_socktype != SOCK_DGRAM) {
    200  1.1  christos 		return;
    201  1.1  christos 	}
    202  1.1  christos 	/*
    203  1.1  christos 	 * Drop the single datagram the service would have
    204  1.1  christos 	 * consumed if nowait. If this is a wait service, this
    205  1.1  christos 	 * will consume 1 datagram, and further received packets
    206  1.1  christos 	 * will be removed in the same way.
    207  1.1  christos 	 */
    208  1.1  christos 	struct msghdr header = {
    209  1.1  christos 		/* All fields null, just consume one message */
    210  1.1  christos 	};
    211  1.1  christos 	ssize_t count;
    212  1.1  christos 
    213  1.1  christos 	count = recvmsg(ctrl, &header, 0);
    214  1.1  christos 	if (count == -1) {
    215  1.1  christos 		syslog(LOG_ERR,
    216  1.1  christos 		    SERV_FMT ": failed to consume nowait dgram: %s",
    217  1.1  christos 		    SERV_PARAMS(sep), strerror(errno));
    218  1.1  christos 		exit(EXIT_FAILURE);
    219  1.1  christos 	}
    220  1.1  christos 	DPRINTF(SERV_FMT ": dropped dgram message",
    221  1.1  christos 	    SERV_PARAMS(sep));
    222  1.1  christos }
    223  1.1  christos 
    224  1.1  christos static time_t
    225  1.1  christos rl_time(void)
    226  1.1  christos {
    227  1.1  christos 	struct timespec ts;
    228  1.1  christos 	if(clock_gettime(CLOCK_MONOTONIC, &ts) == -1) {
    229  1.1  christos 		syslog(LOG_ERR, "clock_gettime for rate limiting failed: %s; "
    230  1.1  christos 		    "exiting", strerror(errno));
    231  1.1  christos 		/* Exit inetd if rate limiting fails */
    232  1.1  christos 		exit(EXIT_FAILURE);
    233  1.1  christos 	}
    234  1.1  christos 	return ts.tv_sec;
    235  1.1  christos }
    236  1.1  christos 
    237  1.1  christos /* Add addr to IP tracking or return NULL if malloc fails */
    238  1.1  christos static struct rl_ip_node *
    239  1.1  christos rl_add(struct servtab *sep, union addr *addr)
    240  1.1  christos {
    241  1.1  christos 
    242  1.1  christos 	struct rl_ip_node *node;
    243  1.1  christos 	size_t node_size, bufsize;
    244  1.1  christos #ifdef DEBUG_ENABLE
    245  1.1  christos 	char buffer[NI_MAXHOST];
    246  1.1  christos #endif
    247  1.1  christos 
    248  1.1  christos 	switch(sep->se_family) {
    249  1.1  christos 	case AF_INET:
    250  1.1  christos 		/* ip_node to end of IPv4 address */
    251  1.1  christos 		node_size = offsetof(struct rl_ip_node, ipv4_addr)
    252  1.1  christos 		    + sizeof(struct in_addr);
    253  1.1  christos 		break;
    254  1.1  christos 	case AF_INET6:
    255  1.1  christos 		/* ip_node to end of IPv6 address */
    256  1.1  christos 		node_size = offsetof(struct rl_ip_node, ipv6_addr)
    257  1.1  christos 		    + sizeof(struct in6_addr);
    258  1.1  christos 		break;
    259  1.1  christos 	default:
    260  1.1  christos 		/* ip_node to other_addr plus size of string + NULL */
    261  1.1  christos 		bufsize = strlen(addr->other_addr) + sizeof(char);
    262  1.1  christos 		node_size = offsetof(struct rl_ip_node, other_addr) + bufsize;
    263  1.1  christos 		break;
    264  1.1  christos 	}
    265  1.1  christos 
    266  1.1  christos 	node = malloc(node_size);
    267  1.1  christos 	if (node == NULL) {
    268  1.1  christos 		if(errno == ENOMEM) {
    269  1.1  christos 			return NULL;
    270  1.1  christos 		} else {
    271  1.1  christos 			syslog(LOG_ERR, "malloc failed unexpectedly: %s",
    272  1.1  christos 			    strerror(errno));
    273  1.1  christos 			exit(EXIT_FAILURE);
    274  1.1  christos 		}
    275  1.1  christos 	}
    276  1.1  christos 
    277  1.1  christos 	node->count = 0;
    278  1.1  christos 
    279  1.1  christos 	/* copy the data into the new allocation */
    280  1.1  christos 	switch(sep->se_family) {
    281  1.1  christos 	case AF_INET:
    282  1.1  christos 		node->ipv4_addr = addr->ipv4_addr;
    283  1.1  christos 		break;
    284  1.1  christos 	case AF_INET6:
    285  1.1  christos 		/* Hopefully this is inlined, means the same thing as memcpy */
    286  1.1  christos 		__builtin_memcpy(&node->ipv6_addr, &addr->ipv6_addr,
    287  1.1  christos 		    sizeof(struct in6_addr));
    288  1.1  christos 		break;
    289  1.1  christos 	default:
    290  1.1  christos 		strlcpy(node->other_addr, addr->other_addr, bufsize);
    291  1.1  christos 		break;
    292  1.1  christos 	}
    293  1.1  christos 
    294  1.1  christos 	/* initializes 'entries' member to NULL automatically */
    295  1.1  christos 	SLIST_INSERT_HEAD(&sep->se_rl_ip_list, node, entries);
    296  1.1  christos 
    297  1.1  christos 	DPRINTF(SERV_FMT ": add '%s' to rate limit tracking (%zu byte record)",
    298  1.1  christos  	    SERV_PARAMS(sep), rl_node_tostring(sep, node, buffer), node_size);
    299  1.1  christos 
    300  1.1  christos 	return node;
    301  1.1  christos }
    302  1.1  christos 
    303  1.1  christos static void
    304  1.1  christos rl_reset(struct servtab *sep, time_t now)
    305  1.1  christos {
    306  1.1  christos 	DPRINTF(SERV_FMT ": %ji seconds passed; resetting rate limiting ",
    307  1.1  christos 	    SERV_PARAMS(sep), (intmax_t)(now - sep->se_time));
    308  1.1  christos 
    309  1.1  christos 	sep->se_count = 0;
    310  1.1  christos 	sep->se_time = now;
    311  1.1  christos 	if (sep->se_ip_max != SERVTAB_UNSPEC_SIZE_T) {
    312  1.1  christos 		rl_clear_ip_list(sep);
    313  1.1  christos 	}
    314  1.1  christos }
    315  1.1  christos 
    316  1.1  christos void
    317  1.1  christos rl_clear_ip_list(struct servtab *sep)
    318  1.1  christos {
    319  1.1  christos 	while (!SLIST_EMPTY(&sep->se_rl_ip_list)) {
    320  1.1  christos 		struct rl_ip_node *node = SLIST_FIRST(&sep->se_rl_ip_list);
    321  1.1  christos 		SLIST_REMOVE_HEAD(&sep->se_rl_ip_list, entries);
    322  1.1  christos 		free(node);
    323  1.1  christos 	}
    324  1.1  christos }
    325  1.1  christos 
    326  1.1  christos /* Get the node associated with addr, or NULL */
    327  1.1  christos static struct rl_ip_node *
    328  1.1  christos rl_try_get_ip(struct servtab *sep, union addr *addr)
    329  1.1  christos {
    330  1.1  christos 
    331  1.1  christos 	struct rl_ip_node *cur;
    332  1.1  christos 	SLIST_FOREACH(cur, &sep->se_rl_ip_list, entries) {
    333  1.1  christos 		if (rl_ip_eq(sep, addr, cur)) {
    334  1.1  christos 			return cur;
    335  1.1  christos 		}
    336  1.1  christos 	}
    337  1.1  christos 
    338  1.1  christos 	return NULL;
    339  1.1  christos }
    340  1.1  christos 
    341  1.1  christos /* Return true if passed service rate limiting checks, false if blocked */
    342  1.1  christos static bool
    343  1.1  christos rl_process_service_max(struct servtab *sep, int ctrl, time_t *now)
    344  1.1  christos {
    345  1.1  christos 	if (sep->se_count >= sep->se_service_max) {
    346  1.1  christos 		if(*now == -1) {
    347  1.1  christos 			/* Only get the clock time if we didn't already */
    348  1.1  christos 			*now = rl_time();
    349  1.1  christos 		}
    350  1.1  christos 
    351  1.1  christos 		if (*now - sep->se_time > CNT_INTVL) {
    352  1.1  christos 			rl_reset(sep, *now);
    353  1.1  christos 		} else {
    354  1.1  christos 			syslog(LOG_ERR, SERV_FMT
    355  1.1  christos 			    ": max spawn rate (%zu in %ji seconds) "
    356  1.1  christos 			    "already met; closing for %ju seconds",
    357  1.1  christos 			    SERV_PARAMS(sep),
    358  1.1  christos 			    sep->se_service_max,
    359  1.1  christos 			    (intmax_t)CNT_INTVL,
    360  1.1  christos 			    (uintmax_t)RETRYTIME);
    361  1.1  christos 			DPRINTF(SERV_FMT
    362  1.1  christos 			    ": max spawn rate (%zu in %ji seconds) "
    363  1.1  christos 			    "already met; closing for %ju seconds",
    364  1.1  christos 			    SERV_PARAMS(sep),
    365  1.1  christos 			    sep->se_service_max,
    366  1.1  christos 			    (intmax_t)CNT_INTVL,
    367  1.1  christos 			    (uintmax_t)RETRYTIME);
    368  1.1  christos 
    369  1.1  christos 			rl_drop_connection(sep, ctrl);
    370  1.1  christos 
    371  1.1  christos 			/* Close the server for 10 minutes */
    372  1.1  christos 			close_sep(sep);
    373  1.1  christos 			if (!timingout) {
    374  1.1  christos 				timingout = true;
    375  1.1  christos 				alarm(RETRYTIME);
    376  1.1  christos 			}
    377  1.1  christos 
    378  1.1  christos 			return false;
    379  1.1  christos 		}
    380  1.1  christos 	}
    381  1.1  christos 	return true;
    382  1.1  christos }
    383  1.1  christos 
    384  1.1  christos /* Return true if passed IP rate limiting checks, false if blocked */
    385  1.1  christos static bool
    386  1.1  christos rl_process_ip_max(struct servtab *sep, int ctrl, time_t *now) {
    387  1.1  christos 	if (sep->se_ip_max != SERVTAB_UNSPEC_SIZE_T) {
    388  1.1  christos 		struct rl_ip_node *node;
    389  1.1  christos 		union addr addr;
    390  1.1  christos 
    391  1.1  christos 		rl_get_name(sep, ctrl, &addr);
    392  1.1  christos 		node = rl_try_get_ip(sep, &addr);
    393  1.1  christos 		if (node == NULL) {
    394  1.1  christos 			node = rl_add(sep, &addr);
    395  1.1  christos 			if (node == NULL) {
    396  1.1  christos 				/* If rl_add can't allocate, reject request */
    397  1.1  christos 				DPRINTF("Cannot allocate rl_ip_node");
    398  1.1  christos 				return false;
    399  1.1  christos 			}
    400  1.1  christos 		}
    401  1.1  christos #ifdef DEBUG_ENABLE
    402  1.1  christos 		else {
    403  1.1  christos 			/*
    404  1.1  christos 			 * in a separate function to prevent large stack
    405  1.1  christos 			 * frame
    406  1.1  christos 			 */
    407  1.1  christos 			rl_print_found_node(sep, node);
    408  1.1  christos 		}
    409  1.1  christos #endif
    410  1.1  christos 
    411  1.1  christos 		DPRINTF(
    412  1.1  christos 		    SERV_FMT ": se_ip_max %zu and ip_count %zu",
    413  1.1  christos 		    SERV_PARAMS(sep), sep->se_ip_max, node->count);
    414  1.1  christos 
    415  1.1  christos 		if (node->count >= sep->se_ip_max) {
    416  1.1  christos 			if (*now == -1) {
    417  1.1  christos 				*now = rl_time();
    418  1.1  christos 			}
    419  1.1  christos 
    420  1.1  christos 			if (*now - sep->se_time > CNT_INTVL) {
    421  1.1  christos 				rl_reset(sep, *now);
    422  1.1  christos 				node = rl_add(sep, &addr);
    423  1.1  christos 				if (node == NULL) {
    424  1.1  christos 					DPRINTF("Cannot allocate rl_ip_node");
    425  1.1  christos 					return false;
    426  1.1  christos 				}
    427  1.1  christos 			} else {
    428  1.1  christos 				if (debug && node->count == sep->se_ip_max) {
    429  1.1  christos 					/*
    430  1.1  christos 					 * Only log first failed request to
    431  1.1  christos 					 * prevent DoS attack writing to system
    432  1.1  christos 					 * log
    433  1.1  christos 					 */
    434  1.1  christos 					rl_log_address_exceed(sep, node);
    435  1.1  christos 				} else {
    436  1.1  christos 					DPRINTF(SERV_FMT
    437  1.1  christos 					    ": service not started",
    438  1.1  christos 					    SERV_PARAMS(sep));
    439  1.1  christos 				}
    440  1.1  christos 
    441  1.1  christos 				rl_drop_connection(sep, ctrl);
    442  1.1  christos 				/*
    443  1.1  christos 				 * Increment so debug-syslog message will
    444  1.1  christos 				 * trigger only once
    445  1.1  christos 				 */
    446  1.1  christos 				if (node->count < SIZE_MAX) {
    447  1.1  christos 					node->count++;
    448  1.1  christos 				}
    449  1.1  christos 				return false;
    450  1.1  christos 			}
    451  1.1  christos 		}
    452  1.1  christos 		node->count++;
    453  1.1  christos 	}
    454  1.1  christos 	return true;
    455  1.1  christos }
    456  1.1  christos 
    457  1.1  christos static bool
    458  1.1  christos rl_ip_eq(struct servtab *sep, union addr *addr, struct rl_ip_node *cur) {
    459  1.1  christos 	switch(sep->se_family) {
    460  1.1  christos 	case AF_INET:
    461  1.1  christos 		if (addr->ipv4_addr.s_addr == cur->ipv4_addr.s_addr) {
    462  1.1  christos 			return true;
    463  1.1  christos 		}
    464  1.1  christos 		break;
    465  1.1  christos #ifdef INET6
    466  1.1  christos 	case AF_INET6:
    467  1.1  christos 		if(rl_ipv6_eq(&addr->ipv6_addr, &cur->ipv6_addr)) {
    468  1.1  christos 			return true;
    469  1.1  christos 		}
    470  1.1  christos 		break;
    471  1.1  christos #endif
    472  1.1  christos 	default:
    473  1.1  christos 		if (strncmp(cur->other_addr, addr->other_addr, NI_MAXHOST)
    474  1.1  christos 		    == 0) {
    475  1.1  christos 			return true;
    476  1.1  christos 		}
    477  1.1  christos 		break;
    478  1.1  christos 	}
    479  1.1  christos 	return false;
    480  1.1  christos }
    481  1.1  christos 
    482  1.1  christos #ifdef INET6
    483  1.1  christos static bool
    484  1.1  christos rl_ipv6_eq(struct in6_addr *a, struct in6_addr *b)
    485  1.1  christos {
    486  1.1  christos #if UINTMAX_MAX >= UINT64_MAX
    487  1.1  christos 	{ /* requires 8 byte aligned structs */
    488  1.1  christos 		uint64_t *ap = (uint64_t *)a->s6_addr;
    489  1.1  christos 		uint64_t *bp = (uint64_t *)b->s6_addr;
    490  1.1  christos 		return (ap[0] == bp[0]) & (ap[1] == bp[1]);
    491  1.1  christos 	}
    492  1.1  christos #else
    493  1.1  christos 	{ /* requires 4 byte aligned structs */
    494  1.1  christos 		uint32_t *ap = (uint32_t *)a->s6_addr;
    495  1.1  christos 		uint32_t *bp = (uint32_t *)b->s6_addr;
    496  1.1  christos 		return ap[0] == bp[0] && ap[1] == bp[1] &&
    497  1.1  christos 			ap[2] == bp[2] && ap[3] == bp[3];
    498  1.1  christos 	}
    499  1.1  christos #endif
    500  1.1  christos }
    501  1.1  christos #endif
    502  1.1  christos 
    503  1.1  christos static const char *
    504  1.1  christos rl_node_tostring(struct servtab *sep, struct rl_ip_node *node,
    505  1.1  christos     char buffer[NI_MAXHOST])
    506  1.1  christos {
    507  1.1  christos 	switch (sep->se_family) {
    508  1.1  christos 	case AF_INET:
    509  1.1  christos #ifdef INET6
    510  1.1  christos 	case AF_INET6:
    511  1.1  christos #endif
    512  1.1  christos 		/* ipv4_addr/ipv6_addr share same address */
    513  1.1  christos 		return inet_ntop(sep->se_family, (void*)&node->ipv4_addr,
    514  1.1  christos 		    (char*)buffer, NI_MAXHOST);
    515  1.1  christos 	default:
    516  1.1  christos 		return (char *)&node->other_addr;
    517  1.1  christos 	}
    518  1.1  christos }
    519  1.1  christos 
    520  1.1  christos #ifdef DEBUG_ENABLE
    521  1.1  christos /* Separate function due to large buffer size */
    522  1.1  christos static void
    523  1.1  christos rl_print_found_node(struct servtab *sep, struct rl_ip_node *node)
    524  1.1  christos {
    525  1.1  christos 	char buffer[NI_MAXHOST];
    526  1.1  christos 	DPRINTF(SERV_FMT ": found record for address '%s'",
    527  1.1  christos 	    SERV_PARAMS(sep), rl_node_tostring(sep, node, buffer));
    528  1.1  christos }
    529  1.1  christos #endif
    530  1.1  christos 
    531  1.1  christos /* Separate function due to large buffer sie */
    532  1.1  christos static void
    533  1.1  christos rl_log_address_exceed(struct servtab *sep, struct rl_ip_node *node)
    534  1.1  christos {
    535  1.1  christos 	char buffer[NI_MAXHOST];
    536  1.1  christos 	const char * name = rl_node_tostring(sep, node, buffer);
    537  1.1  christos 	syslog(LOG_ERR, SERV_FMT
    538  1.1  christos 	    ": max ip spawn rate (%zu in "
    539  1.1  christos 	    "%ji seconds) for "
    540  1.1  christos 	    "'%." TOSTRING(NI_MAXHOST) "s' "
    541  1.1  christos 	    "already met; service not started",
    542  1.1  christos 	    SERV_PARAMS(sep),
    543  1.1  christos 	    sep->se_ip_max,
    544  1.1  christos 	    (intmax_t)CNT_INTVL,
    545  1.1  christos 	    name);
    546  1.1  christos 	DPRINTF(SERV_FMT
    547  1.1  christos 	    ": max ip spawn rate (%zu in "
    548  1.1  christos 	    "%ji seconds) for "
    549  1.1  christos 	    "'%." TOSTRING(NI_MAXHOST) "s' "
    550  1.1  christos 	    "already met; service not started",
    551  1.1  christos 	    SERV_PARAMS(sep),
    552  1.1  christos 	    sep->se_ip_max,
    553  1.1  christos 	    (intmax_t)CNT_INTVL,
    554  1.1  christos 	    name);
    555  1.1  christos }
    556