@@ -346,25 +346,11 @@ func (r *router) AddRouteFiltering(
346346 }
347347
348348 chain := r .chains [chainNameRoutingFw ]
349- var exprs []expr.Any
350-
351- var source firewall.Network
352- switch {
353- case len (sources ) == 1 && sources [0 ].Bits () == 0 :
354- // If it's 0.0.0.0/0, we don't need to add any source matching
355- case len (sources ) == 1 :
356- // If there's only one source, we can use it directly
357- source .Prefix = sources [0 ]
358- default :
359- // If there are multiple sources, use a set
360- source .Set = firewall .NewPrefixSet (sources )
361- }
362349
363- sourceExp , err := r .applyNetwork ( source , sources , true )
350+ exprs , err := r .applySources ( sources )
364351 if err != nil {
365- return nil , fmt . Errorf ( "apply source: %w" , err )
352+ return nil , err
366353 }
367- exprs = append (exprs , sourceExp ... )
368354
369355 destExp , err := r .applyNetwork (destination , nil , false )
370356 if err != nil {
@@ -425,6 +411,42 @@ func (r *router) AddRouteFiltering(
425411 return ruleKey , nil
426412}
427413
414+ func (r * router ) applySources (sources []netip.Prefix ) ([]expr.Any , error ) {
415+ var exprs []expr.Any
416+
417+ var source firewall.Network
418+ if len (sources ) == 1 {
419+ if sources [0 ].Bits () == 0 {
420+ // If it's 0.0.0.0/0, we don't need to add any source matching
421+ } else { // If there's only one source, we can use it directly
422+ source .Prefix = sources [0 ]
423+ }
424+ sourceExp , err := r .applyNetwork (source , sources , true )
425+ if err != nil {
426+ return nil , fmt .Errorf ("apply source: %w" , err )
427+ }
428+ exprs = append (exprs , sourceExp ... )
429+ } else { // If there are multiple sources, use a set
430+ var subEnd int
431+ maxSize := 200
432+ for subStart := 0 ; subStart < len (sources ); subStart += maxSize {
433+ subEnd += maxSize
434+ if subEnd > len (sources ) {
435+ subEnd = len (sources )
436+ }
437+ subSources := sources [subStart :subEnd ]
438+ source .Set = firewall .NewPrefixSet (subSources )
439+
440+ sourceExp , err := r .applyNetwork (source , subSources , true )
441+ if err != nil {
442+ return nil , fmt .Errorf ("apply source: %w" , err )
443+ }
444+ exprs = append (exprs , sourceExp ... )
445+ }
446+ }
447+ return exprs , nil
448+ }
449+
428450func (r * router ) getIpSet (set firewall.Set , prefixes []netip.Prefix , isSource bool ) ([]expr.Any , error ) {
429451 ref , err := r .ipsetCounter .Increment (set .HashedName (), setInput {
430452 set : set ,
0 commit comments