1306196Sjkim/*
2238405Sjkim * Copyright (c) 2003 Proofpoint, Inc. and its suppliers.
3238405Sjkim *	All rights reserved.
4238405Sjkim *
5238405Sjkim * By using this file, you agree to the terms and conditions set
6238405Sjkim * forth in the LICENSE file which can be found at the top level of
7238405Sjkim * the sendmail distribution.
8238405Sjkim *
9238405Sjkim * Contributed by Jose Marcio Martins da Cruz - Ecole des Mines de Paris
10238405Sjkim *   Jose-Marcio.Martins@ensmp.fr
11238405Sjkim */
12238405Sjkim
13238405Sjkim/* a part of this code is based on inetd.c for which this copyright applies: */
14238405Sjkim/*
15238405Sjkim * Copyright (c) 1983, 1991, 1993, 1994
16238405Sjkim *      The Regents of the University of California.  All rights reserved.
17238405Sjkim *
18238405Sjkim * Redistribution and use in source and binary forms, with or without
19238405Sjkim * modification, are permitted provided that the following conditions
20238405Sjkim * are met:
21238405Sjkim * 1. Redistributions of source code must retain the above copyright
22238405Sjkim *    notice, this list of conditions and the following disclaimer.
23238405Sjkim * 2. Redistributions in binary form must reproduce the above copyright
24238405Sjkim *    notice, this list of conditions and the following disclaimer in the
25238405Sjkim *    documentation and/or other materials provided with the distribution.
26238405Sjkim * 3. All advertising materials mentioning features or use of this software
27238405Sjkim *    must display the following acknowledgement:
28238405Sjkim *      This product includes software developed by the University of
29238405Sjkim *      California, Berkeley and its contributors.
30238405Sjkim * 4. Neither the name of the University nor the names of its contributors
31238405Sjkim *    may be used to endorse or promote products derived from this software
32238405Sjkim *    without specific prior written permission.
33238405Sjkim *
34238405Sjkim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35238405Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36238405Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37238405Sjkim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38238405Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39238405Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40238405Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41276864Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42276864Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43238405Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44238405Sjkim * SUCH DAMAGE.
45238405Sjkim */
46238405Sjkim
47238405Sjkim#include <sendmail.h>
48238405SjkimSM_RCSID("@(#)$Id: ratectrl.c,v 8.14 2013-11-22 20:51:56 ca Exp $")
49238405Sjkim
50238405Sjkim/*
51238405Sjkim**  stuff included - given some warnings (inet_ntoa)
52238405Sjkim**	- surely not everything is needed
53276864Sjkim*/
54276864Sjkim
55276864Sjkim#if NETINET || NETINET6
56238405Sjkim# include <arpa/inet.h>
57276864Sjkim#endif	/* NETINET || NETINET6 */
58276864Sjkim
59276864Sjkim#include <sm/time.h>
60276864Sjkim
61276864Sjkim#ifndef HASH_ALG
62276864Sjkim# define HASH_ALG	2
63238405Sjkim#endif /* HASH_ALG */
64276864Sjkim
65276864Sjkim#ifndef RATECTL_DEBUG
66276864Sjkim# define RATECTL_DEBUG  0
67276864Sjkim#endif /* RATECTL_DEBUG */
68276864Sjkim
69238405Sjkim/* forward declarations */
70276864Sjkimstatic int client_rate __P((time_t, SOCKADDR *, bool));
71238405Sjkimstatic int total_rate __P((time_t, bool));
72238405Sjkim
73238405Sjkim/*
74238405Sjkim**  CONNECTION_RATE_CHECK - updates connection history data
75238405Sjkim**      and computes connection rate for the given host
76238405Sjkim**
77238405Sjkim**    Parameters:
78238405Sjkim**      hostaddr -- ip address of smtp client
79238405Sjkim**      e -- envelope
80238405Sjkim**
81238405Sjkim**    Returns:
82238405Sjkim**      true (always)
83238405Sjkim**
84238405Sjkim**    Side Effects:
85238405Sjkim**      updates connection history
86238405Sjkim**
87238405Sjkim**    Warnings:
88238405Sjkim**      For each connection, this call shall be
89238405Sjkim**      done only once with the value true for the
90238405Sjkim**      update parameter.
91238405Sjkim**      Typically, this call is done with the value
92238405Sjkim**      true by the father, and once again with
93238405Sjkim**      the value false by the children.
94238405Sjkim**
95238405Sjkim*/
96238405Sjkim
97238405Sjkimbool
98238405Sjkimconnection_rate_check(hostaddr, e)
99238405Sjkim	SOCKADDR *hostaddr;
100238405Sjkim	ENVELOPE *e;
101238405Sjkim{
102238405Sjkim	time_t now;
103238405Sjkim	int totalrate, clientrate;
104238405Sjkim	static int clientconn = 0;
105238405Sjkim
106238405Sjkim	now = time(NULL);
107238405Sjkim#if RATECTL_DEBUG
108238405Sjkim	sm_syslog(LOG_INFO, NOQID, "connection_rate_check entering...");
109238405Sjkim#endif /* RATECTL_DEBUG */
110238405Sjkim
111238405Sjkim	/* update server connection rate */
112238405Sjkim	totalrate = total_rate(now, e == NULL);
113238405Sjkim#if RATECTL_DEBUG
114238405Sjkim	sm_syslog(LOG_INFO, NOQID, "global connection rate: %d", totalrate);
115238405Sjkim#endif /* RATECTL_DEBUG */
116238405Sjkim
117238405Sjkim	/* update client connection rate */
118238405Sjkim	clientrate = client_rate(now, hostaddr, e == NULL);
119238405Sjkim
120238405Sjkim	if (e == NULL)
121238405Sjkim		clientconn = count_open_connections(hostaddr);
122238405Sjkim
123238405Sjkim	if (e != NULL)
124238405Sjkim	{
125238405Sjkim		char s[16];
126238405Sjkim
127238405Sjkim		sm_snprintf(s, sizeof(s), "%d", clientrate);
128238405Sjkim		macdefine(&e->e_macro, A_TEMP, macid("{client_rate}"), s);
129238405Sjkim		sm_snprintf(s, sizeof(s), "%d", totalrate);
130238405Sjkim		macdefine(&e->e_macro, A_TEMP, macid("{total_rate}"), s);
131238405Sjkim		sm_snprintf(s, sizeof(s), "%d", clientconn);
132238405Sjkim		macdefine(&e->e_macro, A_TEMP, macid("{client_connections}"),
133238405Sjkim				s);
134238405Sjkim	}
135238405Sjkim	return true;
136306196Sjkim}
137238405Sjkim
138238405Sjkim/*
139238405Sjkim**  Data declarations needed to evaluate connection rate
140238405Sjkim*/
141238405Sjkim
142238405Sjkimstatic int CollTime = 60;
143238405Sjkim
144238405Sjkim/* this should be a power of 2, otherwise CPMHMASK doesn't work well */
145238405Sjkim#ifndef CPMHSIZE
146238405Sjkim# define CPMHSIZE	1024
147238405Sjkim#endif /* CPMHSIZE */
148238405Sjkim
149238405Sjkim#define CPMHMASK	(CPMHSIZE-1)
150238405Sjkim
151238405Sjkim#ifndef MAX_CT_STEPS
152238405Sjkim# define MAX_CT_STEPS	10
153238405Sjkim#endif /* MAX_CT_STEPS */
154238405Sjkim
155238405Sjkim/*
156238405Sjkim**  time granularity: 10s (that's one "tick")
157238405Sjkim**  will be initialised to ConnectionRateWindowSize/CHTSIZE
158238405Sjkim**  before being used the first time
159238405Sjkim*/
160238405Sjkim
161238405Sjkimstatic int ChtGran = -1;
162238405Sjkim
163238405Sjkim#define CHTSIZE		6
164238405Sjkim
165238405Sjkim/* Number of connections for a certain "tick" */
166238405Sjkimtypedef struct CTime
167238405Sjkim{
168238405Sjkim	unsigned long	ct_Ticks;
169238405Sjkim	int		ct_Count;
170238405Sjkim}
171238405SjkimCTime_T;
172238405Sjkim
173238405Sjkimtypedef struct CHash
174276864Sjkim{
175238405Sjkim#if NETINET6 && NETINET
176238405Sjkim	union
177238405Sjkim	{
178238405Sjkim		struct in_addr	c4_Addr;
179238405Sjkim		struct in6_addr	c6_Addr;
180238405Sjkim	} cu_Addr;
181238405Sjkim# define ch_Addr4	cu_Addr.c4_Addr
182238405Sjkim# define ch_Addr6	cu_Addr.c6_Addr
183238405Sjkim#else /* NETINET6 && NETINET */
184238405Sjkim# if NETINET6
185238405Sjkim	struct in6_addr	ch_Addr;
186238405Sjkim#  define ch_Addr6	ch_Addr
187238405Sjkim# else /* NETINET6 */
188276864Sjkim	struct in_addr ch_Addr;
189238405Sjkim#  define ch_Addr4	ch_Addr
190238405Sjkim# endif /* NETINET6 */
191238405Sjkim#endif /* NETINET6 && NETINET */
192238405Sjkim
193238405Sjkim	int		ch_Family;
194238405Sjkim	time_t		ch_LTime;
195238405Sjkim	unsigned long	ch_colls;
196238405Sjkim
197238405Sjkim	/* 6 buckets for ticks: 60s */
198238405Sjkim	CTime_T		ch_Times[CHTSIZE];
199238405Sjkim}
200238405SjkimCHash_T;
201238405Sjkim
202238405Sjkimstatic CHash_T CHashAry[CPMHSIZE];
203238405Sjkimstatic bool CHashAryOK = false;
204
205/*
206**  CLIENT_RATE - Evaluate connection rate per smtp client
207**
208**	Parameters:
209**		now - current time in secs
210**		saddr - client address
211**		update - update data / check only
212**
213**	Returns:
214**		connection rate (connections / ConnectionRateWindowSize)
215**
216**	Side effects:
217**		update static global data
218**
219*/
220
221static int
222client_rate(now, saddr, update)
223	 time_t now;
224	 SOCKADDR *saddr;
225	 bool update;
226{
227	unsigned int hv;
228	int i;
229	int cnt;
230	bool coll;
231	CHash_T *chBest = NULL;
232	unsigned int ticks;
233
234	cnt = 0;
235	hv = 0xABC3D20F;
236	if (ChtGran < 0)
237		ChtGran = ConnectionRateWindowSize / CHTSIZE;
238	if (ChtGran <= 0)
239		ChtGran = 10;
240
241	ticks = now / ChtGran;
242
243	if (!CHashAryOK)
244	{
245		memset(CHashAry, 0, sizeof(CHashAry));
246		CHashAryOK = true;
247	}
248
249	{
250		char *p;
251		int addrlen;
252#if HASH_ALG != 1
253		int c, d;
254#endif /* HASH_ALG != 1 */
255
256		switch (saddr->sa.sa_family)
257		{
258#if NETINET
259		  case AF_INET:
260			p = (char *)&saddr->sin.sin_addr;
261			addrlen = sizeof(struct in_addr);
262			break;
263#endif /* NETINET */
264#if NETINET6
265		  case AF_INET6:
266			p = (char *)&saddr->sin6.sin6_addr;
267			addrlen = sizeof(struct in6_addr);
268			break;
269#endif /* NETINET6 */
270		  default:
271			/* should not happen */
272			return -1;
273		}
274
275		/* compute hash value */
276		for (i = 0; i < addrlen; ++i, ++p)
277#if HASH_ALG == 1
278			hv = (hv << 5) ^ (hv >> 23) ^ *p;
279		hv = (hv ^ (hv >> 16));
280#elif HASH_ALG == 2
281		{
282			d = *p;
283			c = d;
284			c ^= c<<6;
285			hv += (c<<11) ^ (c>>1);
286			hv ^= (d<<14) + (d<<7) + (d<<4) + d;
287		}
288#elif HASH_ALG == 3
289		{
290			hv = (hv << 4) + *p;
291			d = hv & 0xf0000000;
292			if (d != 0)
293			{
294				hv ^= (d >> 24);
295				hv ^= d;
296			}
297		}
298#else /* HASH_ALG == 1 */
299			hv = ((hv << 1) ^ (*p & 0377)) % cctx->cc_size;
300#endif /* HASH_ALG == 1 */
301	}
302
303	coll = true;
304	for (i = 0; i < MAX_CT_STEPS; ++i)
305	{
306		CHash_T *ch = &CHashAry[(hv + i) & CPMHMASK];
307
308#if NETINET
309		if (saddr->sa.sa_family == AF_INET &&
310		    ch->ch_Family == AF_INET &&
311		    (saddr->sin.sin_addr.s_addr == ch->ch_Addr4.s_addr ||
312		     ch->ch_Addr4.s_addr == 0))
313		{
314			chBest = ch;
315			coll = false;
316			break;
317		}
318#endif /* NETINET */
319#if NETINET6
320		if (saddr->sa.sa_family == AF_INET6 &&
321		    ch->ch_Family == AF_INET6 &&
322		    (IN6_ARE_ADDR_EQUAL(&saddr->sin6.sin6_addr,
323				       &ch->ch_Addr6) != 0 ||
324		     IN6_IS_ADDR_UNSPECIFIED(&ch->ch_Addr6)))
325		{
326			chBest = ch;
327			coll = false;
328			break;
329		}
330#endif /* NETINET6 */
331		if (chBest == NULL || ch->ch_LTime == 0 ||
332		    ch->ch_LTime < chBest->ch_LTime)
333			chBest = ch;
334	}
335
336	/* Let's update data... */
337	if (update)
338	{
339		if (coll && (now - chBest->ch_LTime < CollTime))
340		{
341			/*
342			**  increment the number of collisions last
343			**  CollTime for this client
344			*/
345
346			chBest->ch_colls++;
347
348			/*
349			**  Maybe shall log if collision rate is too high...
350			**  and take measures to resize tables
351			**  if this is the case
352			*/
353		}
354
355		/*
356		**  If it's not a match, then replace the data.
357		**  Note: this purges the history of a colliding entry,
358		**  which may cause "overruns", i.e., if two entries are
359		**  "cancelling" each other out, then they may exceed
360		**  the limits that are set. This might be mitigated a bit
361		**  by the above "best of 5" function however.
362		**
363		**  Alternative approach: just use the old data, which may
364		**  cause false positives however.
365		**  To activate this, change deactivate following memset call.
366		*/
367
368		if (coll)
369		{
370#if NETINET
371			if (saddr->sa.sa_family == AF_INET)
372			{
373				chBest->ch_Family = AF_INET;
374				chBest->ch_Addr4 = saddr->sin.sin_addr;
375			}
376#endif /* NETINET */
377#if NETINET6
378			if (saddr->sa.sa_family == AF_INET6)
379			{
380				chBest->ch_Family = AF_INET6;
381				chBest->ch_Addr6 = saddr->sin6.sin6_addr;
382			}
383#endif /* NETINET6 */
384#if 1
385			memset(chBest->ch_Times, '\0',
386			       sizeof(chBest->ch_Times));
387#endif /* 1 */
388		}
389
390		chBest->ch_LTime = now;
391		{
392			CTime_T *ct = &chBest->ch_Times[ticks % CHTSIZE];
393
394			if (ct->ct_Ticks != ticks)
395			{
396				ct->ct_Ticks = ticks;
397				ct->ct_Count = 0;
398			}
399			++ct->ct_Count;
400		}
401	}
402
403	/* Now let's count connections on the window */
404	for (i = 0; i < CHTSIZE; ++i)
405	{
406		CTime_T *ct = &chBest->ch_Times[i];
407
408		if (ct->ct_Ticks <= ticks && ct->ct_Ticks >= ticks - CHTSIZE)
409			cnt += ct->ct_Count;
410	}
411
412#if RATECTL_DEBUG
413	sm_syslog(LOG_WARNING, NOQID,
414		"cln: cnt=(%d), CHTSIZE=(%d), ChtGran=(%d)",
415		cnt, CHTSIZE, ChtGran);
416#endif /* RATECTL_DEBUG */
417	return cnt;
418}
419
420/*
421**  TOTAL_RATE - Evaluate global connection rate
422**
423**	Parameters:
424**		now - current time in secs
425**		update - update data / check only
426**
427**	Returns:
428**		connection rate (connections / ConnectionRateWindowSize)
429*/
430
431static CTime_T srv_Times[CHTSIZE];
432static bool srv_Times_OK = false;
433
434static int
435total_rate(now, update)
436	 time_t now;
437	 bool update;
438{
439	int i;
440	int cnt = 0;
441	CTime_T *ct;
442	unsigned int ticks;
443
444	if (ChtGran < 0)
445		ChtGran = ConnectionRateWindowSize / CHTSIZE;
446	if (ChtGran == 0)
447		ChtGran = 10;
448	ticks = now / ChtGran;
449	if (!srv_Times_OK)
450	{
451		memset(srv_Times, 0, sizeof(srv_Times));
452		srv_Times_OK = true;
453	}
454
455	/* Let's update data */
456	if (update)
457	{
458		ct = &srv_Times[ticks % CHTSIZE];
459
460		if (ct->ct_Ticks != ticks)
461		{
462			ct->ct_Ticks = ticks;
463			ct->ct_Count = 0;
464		}
465		++ct->ct_Count;
466	}
467
468	/* Let's count connections on the window */
469	for (i = 0; i < CHTSIZE; ++i)
470	{
471		ct = &srv_Times[i];
472
473		if (ct->ct_Ticks <= ticks && ct->ct_Ticks >= ticks - CHTSIZE)
474			cnt += ct->ct_Count;
475	}
476
477#if RATECTL_DEBUG
478	sm_syslog(LOG_WARNING, NOQID,
479		"srv: cnt=(%d), CHTSIZE=(%d), ChtGran=(%d)",
480		 cnt, CHTSIZE, ChtGran);
481#endif /* RATECTL_DEBUG */
482
483	return cnt;
484}
485