From 27851e6e958fbad9768b546ed52676a777528b22 Mon Sep 17 00:00:00 2001
From: David Schmitz <David.Schmitz@lrz.de>
Date: Mon, 2 Jun 2025 13:35:48 +0000
Subject: [PATCH] fix/existingcheck_honor_fragtype: honor fragment types in
 duplicate rule checking

---
 flowspec/forms.py | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/flowspec/forms.py b/flowspec/forms.py
index e82303c5..b39cfc08 100644
--- a/flowspec/forms.py
+++ b/flowspec/forms.py
@@ -165,6 +165,7 @@ class RouteForm(forms.ModelForm):
         destination = self.cleaned_data.get('destination', None)
         destinationports = self.cleaned_data.get('destinationport', None)
         user = self.cleaned_data.get('applier', None)
+        fragmenttypes = self.cleaned_data.get('fragmenttype', None)
 
         if source:
             source = ip_network(source, strict=False).compressed
@@ -184,6 +185,15 @@ class RouteForm(forms.ModelForm):
         else:
             existing_routes = existing_routes.filter(protocol=None)
 
+        if fragmenttypes:
+            route_pk_list=get_fragementtypes_route_pks(fragmenttypes, existing_routes)
+            if route_pk_list:
+                existing_routes = existing_routes.filter(pk__in=route_pk_list)
+            else:
+                existing_routes = existing_routes.filter(fragmenttype=None) 
+        else:
+            existing_routes = existing_routes.filter(fragmenttype=None)
+
         if sourceports:
             route_pk_list=get_matchingport_route_pks(sourceports, existing_routes)
             if route_pk_list:
@@ -294,6 +304,14 @@ def get_matchingport_route_pks(portlist, routes):
             route_pk_list.append(route.pk)
     return route_pk_list
 
+def get_fragementtypes_route_pks(fragmenttype_list, routes):
+    route_pk_list = []
+    fragmenttype_value_list = value_list_to_list(fragmenttype_list.values_list('fragmenttype').order_by('fragmenttype'))
+    for route in routes:
+        rsp = value_list_to_list(route.fragmenttype.all().values_list('fragmenttype').order_by('fragmenttype'))
+        if rsp and rsp == fragmenttype_value_list:
+            route_pk_list.append(route.pk)
+    return route_pk_list
 
 def get_matchingprotocol_route_pks(protocolist, routes):
     route_pk_list = []
-- 
GitLab