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