Miroslav Lichvar 967c8f
commit a36602f1e65cd6bace6ed9405b0ce359de4a27d1
Miroslav Lichvar 967c8f
Author: Miroslav Lichvar <mlichvar@redhat.com>
Miroslav Lichvar 967c8f
Date:   Thu Jan 3 15:23:54 2019 +0100
Miroslav Lichvar 967c8f
Miroslav Lichvar 967c8f
    unicast: limit message rate and grant duration
Miroslav Lichvar 967c8f
    
Miroslav Lichvar 967c8f
    Deny service requests with logInterMessagePeriod smaller than -7 (128
Miroslav Lichvar 967c8f
    packets per second) or larger than 16. This limits the network and CPU
Miroslav Lichvar 967c8f
    consumption per address and prevents undefined shifts in the calculation
Miroslav Lichvar 967c8f
    of the interval.
Miroslav Lichvar 967c8f
    
Miroslav Lichvar 967c8f
    Also, limit the maximum grant duration to one hour.
Miroslav Lichvar 967c8f
    
Miroslav Lichvar 967c8f
    Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
Miroslav Lichvar 967c8f
Miroslav Lichvar 967c8f
diff --git a/unicast_service.c b/unicast_service.c
Miroslav Lichvar 967c8f
index 9c9b95b..c6c17c6 100644
Miroslav Lichvar 967c8f
--- a/unicast_service.c
Miroslav Lichvar 967c8f
+++ b/unicast_service.c
Miroslav Lichvar 967c8f
@@ -31,6 +31,9 @@
Miroslav Lichvar 967c8f
 #include "unicast_service.h"
Miroslav Lichvar 967c8f
 #include "util.h"
Miroslav Lichvar 967c8f
 
Miroslav Lichvar 967c8f
+#define MIN_LOG_INTER_MESSAGE_PERIOD -7
Miroslav Lichvar 967c8f
+#define MAX_LOG_INTER_MESSAGE_PERIOD 16
Miroslav Lichvar 967c8f
+#define MAX_DURATION 3600
Miroslav Lichvar 967c8f
 #define QUEUE_LEN 16
Miroslav Lichvar 967c8f
 
Miroslav Lichvar 967c8f
 struct unicast_client_address {
Miroslav Lichvar 967c8f
@@ -289,6 +292,15 @@ int unicast_service_add(struct port *p, struct ptp_message *m,
Miroslav Lichvar 967c8f
 		return SERVICE_DENIED;
Miroslav Lichvar 967c8f
 	}
Miroslav Lichvar 967c8f
 
Miroslav Lichvar 967c8f
+	if (req->logInterMessagePeriod < MIN_LOG_INTER_MESSAGE_PERIOD ||
Miroslav Lichvar 967c8f
+	    req->logInterMessagePeriod > MAX_LOG_INTER_MESSAGE_PERIOD) {
Miroslav Lichvar 967c8f
+		return SERVICE_DENIED;
Miroslav Lichvar 967c8f
+	}
Miroslav Lichvar 967c8f
+
Miroslav Lichvar 967c8f
+	if (req->durationField > MAX_DURATION) {
Miroslav Lichvar 967c8f
+		req->durationField = MAX_DURATION;
Miroslav Lichvar 967c8f
+	}
Miroslav Lichvar 967c8f
+
Miroslav Lichvar 967c8f
 	LIST_FOREACH(itmp, &p->unicast_service->intervals, list) {
Miroslav Lichvar 967c8f
 		/*
Miroslav Lichvar 967c8f
 		 * Remember the interval of interest.