@@ -385,16 +385,16 @@ func (c *OVNNbClient) CreateNodeACL(pgName, nodeIPStr, joinIPStr string) error {
385385 return nil
386386}
387387
388- func (c * OVNNbClient ) CreateSgDenyAllACL (sgName string ) error {
388+ func (c * OVNNbClient ) CreateSgDenyAllACL (sgName string , tier int ) error {
389389 pgName := GetSgPortGroupName (sgName )
390390
391- ingressACL , err := c .newACL (pgName , ovnnb .ACLDirectionToLport , util .SecurityGroupDropPriority , fmt .Sprintf ("outport == @%s && ip" , pgName ), ovnnb .ACLActionDrop , util . NetpolACLTier )
391+ ingressACL , err := c .newACL (pgName , ovnnb .ACLDirectionToLport , util .SecurityGroupDropPriority , fmt .Sprintf ("outport == @%s && ip" , pgName ), ovnnb .ACLActionDrop , tier )
392392 if err != nil {
393393 klog .Error (err )
394394 return fmt .Errorf ("new deny all ingress acl for security group %s: %w" , sgName , err )
395395 }
396396
397- egressACL , err := c .newACL (pgName , ovnnb .ACLDirectionFromLport , util .SecurityGroupDropPriority , fmt .Sprintf ("inport == @%s && ip" , pgName ), ovnnb .ACLActionDrop , util . NetpolACLTier )
397+ egressACL , err := c .newACL (pgName , ovnnb .ACLDirectionFromLport , util .SecurityGroupDropPriority , fmt .Sprintf ("inport == @%s && ip" , pgName ), ovnnb .ACLActionDrop , tier )
398398 if err != nil {
399399 klog .Error (err )
400400 return fmt .Errorf ("new deny all egress acl for security group %s: %w" , sgName , err )
@@ -410,7 +410,7 @@ func (c *OVNNbClient) CreateSgDenyAllACL(sgName string) error {
410410}
411411
412412// CreateSgACL create allow acl for security group
413- func (c * OVNNbClient ) CreateSgBaseACL (sgName , direction string ) error {
413+ func (c * OVNNbClient ) CreateSgBaseACL (sgName , direction string , tier int ) error {
414414 pgName := GetSgPortGroupName (sgName )
415415
416416 // ingress rule
@@ -434,7 +434,7 @@ func (c *OVNNbClient) CreateSgBaseACL(sgName, direction string) error {
434434 acls := make ([]* ovnnb.ACL , 0 )
435435
436436 newACL := func (match string ) {
437- acl , err := c .newACL (pgName , direction , util .SecurityGroupBasePriority , match , ovnnb .ACLActionAllowRelated , util . NetpolACLTier )
437+ acl , err := c .newACL (pgName , direction , util .SecurityGroupBasePriority , match , ovnnb .ACLActionAllowRelated , tier )
438438 if err != nil {
439439 klog .Error (err )
440440 klog .Errorf ("failed to create new base ingress acl for security group %s: %v" , sgName , err )
@@ -523,7 +523,7 @@ func (c *OVNNbClient) UpdateSgACL(sg *kubeovnv1.SecurityGroup, direction string)
523523 NewACLMatch (ipSuffix , "" , "" , "" ),
524524 NewACLMatch (ipSuffix + "." + srcOrDst , "==" , "$" + asName , "" ),
525525 )
526- acl , err := c .newACL (pgName , direction , util .SecurityGroupAllowPriority , match .String (), ovnnb .ACLActionAllowRelated , util . NetpolACLTier )
526+ acl , err := c .newACL (pgName , direction , util .SecurityGroupAllowPriority , match .String (), ovnnb .ACLActionAllowRelated , sg . Spec . SecurityGroupTier )
527527 if err != nil {
528528 klog .Error (err )
529529 return fmt .Errorf ("new allow acl for security group %s: %w" , sg .Name , err )
@@ -535,7 +535,7 @@ func (c *OVNNbClient) UpdateSgACL(sg *kubeovnv1.SecurityGroup, direction string)
535535
536536 /* create rule acl */
537537 for _ , rule := range sgRules {
538- acl , err := c .newSgRuleACL (sg .Name , direction , rule )
538+ acl , err := c .newSgRuleACL (sg .Name , direction , rule , sg . Spec . SecurityGroupTier )
539539 if err != nil {
540540 klog .Error (err )
541541 return fmt .Errorf ("new rule acl for security group %s: %w" , sg .Name , err )
@@ -1050,7 +1050,7 @@ func (c *OVNNbClient) newACLWithoutCheck(parent, direction, priority, match, act
10501050}
10511051
10521052// createSgRuleACL create security group rule acl
1053- func (c * OVNNbClient ) newSgRuleACL (sgName , direction string , rule kubeovnv1.SecurityGroupRule ) (* ovnnb.ACL , error ) {
1053+ func (c * OVNNbClient ) newSgRuleACL (sgName , direction string , rule kubeovnv1.SecurityGroupRule , tier int ) (* ovnnb.ACL , error ) {
10541054 ipSuffix := "ip4"
10551055 if rule .IPVersion == "ipv6" {
10561056 ipSuffix = "ip6"
@@ -1059,13 +1059,15 @@ func (c *OVNNbClient) newSgRuleACL(sgName, direction string, rule kubeovnv1.Secu
10591059 pgName := GetSgPortGroupName (sgName )
10601060
10611061 // ingress rule
1062- srcOrDst , portDirection := "src" , "outport"
1062+ localSrcOrDst , remoteSrcOrDst , portDirection := "dst" , "src" , "outport"
10631063 if direction == ovnnb .ACLDirectionFromLport { // egress rule
1064- srcOrDst = "dst"
1064+ remoteSrcOrDst = "dst"
1065+ localSrcOrDst = "src"
10651066 portDirection = "inport"
10661067 }
10671068
1068- ipKey := ipSuffix + "." + srcOrDst
1069+ remoteipKey := ipSuffix + "." + remoteSrcOrDst
1070+ localipKey := ipSuffix + "." + localSrcOrDst
10691071
10701072 /* match all traffic to or from pgName */
10711073 allIPMatch := NewAndACLMatch (
@@ -1075,9 +1077,10 @@ func (c *OVNNbClient) newSgRuleACL(sgName, direction string, rule kubeovnv1.Secu
10751077
10761078 /* allow allowed ip traffic */
10771079 // type address
1080+
10781081 allowedIPMatch := NewAndACLMatch (
10791082 allIPMatch ,
1080- NewACLMatch (ipKey , "==" , rule .RemoteAddress , "" ),
1083+ NewACLMatch (remoteipKey , "==" , rule .RemoteAddress , "" ),
10811084 )
10821085
10831086 // type securityGroup
@@ -1088,7 +1091,15 @@ func (c *OVNNbClient) newSgRuleACL(sgName, direction string, rule kubeovnv1.Secu
10881091 if rule .RemoteType == kubeovnv1 .SgRemoteTypeSg {
10891092 allowedIPMatch = NewAndACLMatch (
10901093 allIPMatch ,
1091- NewACLMatch (ipKey , "==" , "$" + remotePgName , "" ),
1094+ NewACLMatch (remoteipKey , "==" , "$" + remotePgName , "" ),
1095+ )
1096+ }
1097+
1098+ // Add a rule to match local address only if it is set
1099+ if rule .LocalAddress != "" {
1100+ allowedIPMatch = NewAndACLMatch (
1101+ allowedIPMatch ,
1102+ NewACLMatch (localipKey , "==" , rule .LocalAddress , "" ),
10921103 )
10931104 }
10941105
@@ -1113,16 +1124,27 @@ func (c *OVNNbClient) newSgRuleACL(sgName, direction string, rule kubeovnv1.Secu
11131124 allowedIPMatch ,
11141125 NewACLMatch (string (rule .Protocol )+ ".dst" , "<=" , strconv .Itoa (rule .PortRangeMin ), strconv .Itoa (rule .PortRangeMax )),
11151126 )
1127+
1128+ // Add a match on source port if a local address was provided.
1129+ if rule .LocalAddress != "" {
1130+ match = NewAndACLMatch (
1131+ match ,
1132+ NewACLMatch (string (rule .Protocol )+ ".src" , "<=" , strconv .Itoa (rule .LocalPortRangeMin ), strconv .Itoa (rule .LocalPortRangeMax )),
1133+ )
1134+ }
11161135 }
11171136
11181137 action := ovnnb .ACLActionDrop
11191138 if rule .Policy == kubeovnv1 .SgPolicyAllow {
11201139 action = ovnnb .ACLActionAllowRelated
11211140 }
1141+ if rule .Policy == kubeovnv1 .SgPolicyPass {
1142+ action = ovnnb .ACLActionPass
1143+ }
11221144
11231145 highestPriority , _ := strconv .Atoi (util .SecurityGroupHighestPriority )
11241146
1125- acl , err := c .newACL (pgName , direction , strconv .Itoa (highestPriority - rule .Priority ), match .String (), action , util . NetpolACLTier )
1147+ acl , err := c .newACL (pgName , direction , strconv .Itoa (highestPriority - rule .Priority ), match .String (), action , tier )
11261148 if err != nil {
11271149 klog .Error (err )
11281150 return nil , fmt .Errorf ("new security group acl for port group %s: %w" , pgName , err )
0 commit comments