access control iRule

I cannot seem to get an access control iRule working. I am trying to limit access to a virtual server to only two external IP addresses. I have copied and modified an iRule found on F5 support site but when applied it blocks all traffic to this VS.

It would probably be easier to configure the source on the virtual server (Link to guide) or do this on a firewall.

When configuring a virtual sever, you can specify an IP address or network from which the virtual server will accept traffic. For this setting to function properly, you must specify a value other than 0.0.0.0/0 or ::/0 (that is, any/0, any6/0). To maximize utility of this setting, specify the most specific address prefixes spanning all customer addresses and no others.

Let’s say you wanted to allow 198.x.y.50 and 198.x.y.51. In the source field you would have 198.x.y.50/31. You might want to doublecheck the /31 with documentation, or one of the others here can confirm this.

Can you post the iRule you are working from Terry?

when CLIENT_ACCEPTED { while {1} { set dcfw_vdg [ class match -value [virtual name] equals /Common/rtp_allow ] if { ! [ class exists $dcfw_vdg ] } { break } if { ! [ class match [IP::remote_addr] equals $dcfw_vdg ] } { break } return } discard }

Try this one:

when CLIENT_ACCEPTED {
 if { [IP::addr [IP::client_addr] equals 10.10.10.10] or [IP::addr [IP::client_addr] equals 10.10.10.20]} {
     reject
     log local0. "Connection dropped from [IP::client_addr]"
  }
}

This did not work. I need to allow only two IP addresses to get through to the VS and reject all other IP address connections. The code above gave an error about the log local0 line

Apologies for the oversight. This should allow only the two addresses specified:

when CLIENT_ACCEPTED {
 if { ! [IP::addr [IP::client_addr] equals 10.10.10.10] or [IP::addr [IP::client_addr] equals 10.10.10.20]} {
     reject
     log local0. "Connection dropped from [IP::client_addr]"
  }
}

I loaded this in my 11.4.1 LTM without issue. What was the error you are seeing for the previous rule? Error when trying to load it, or error in the log?

I am on 11.2.1 and I got this when loading the iRule:

01070151:3: Rule [/external_part/rtp_ip_allow2] error: line 1: [parse error: missing close-brace] [{ if { ! [IP::addr [IP::client_addr] equals xx.xxx.xxx.x] or [IP::addr [IP::client_addr] equals xx.xxx.xx.x]} { reject log local0. “Connection dropped from [IP::client_addr]”] line 2: [parse error: missing close-brace] [{ reject log local0. “Connection dropped from [IP::client_addr]”] line 3: [command is not valid in the current scope] [reject] line 4: [command is not valid in the current scope] [log local0. “Connection dropped from [IP::client_addr]”] line 4: [command is not valid in the current scope] [IP::client_addr]

Try copying the rule in its entirety as I posted it above. Looks like maybe some braces/brackets aren’t lining up according to what already exists in your iRule.

Or post your full iRule here and we can help format it.

The above pastes in fine on my 11.2.1. It looks like you are missing a closing }

Also, won’t the above !(not) only apply to the first portion of the if statement? Isn’t it required after the or as well?

It just seems like the ‘reject’ statement would deny the IP addresses in the parameters. I have two external IP addresses the need to be the only source addresses allowed to get to this external facing virtual server.

My latest iRule includes a ! (not) before the addresses. So if it’s not one of the two listed, then it’ll be rejected.

Good catch mimlo. This one should work properly with parentheses:

when CLIENT_ACCEPTED {
 if { ! ([IP::addr [IP::client_addr] equals 10.10.10.10] or [IP::addr [IP::client_addr] equals 10.10.10.20])} {
     reject
     log local0. "Connection dropped from [IP::client_addr]"
  }
}

good enough. It let me load this latest iRule and we are currently testing. If this works in dev then we’ll apply to prod.

unfortunately this iRule blocked all traffic to this VS.

Stupid question but did you change the IP addresses in the rule to the two hosts that you want to permit?

Just another way of doing the same…

when CLIENT_ACCEPTED {
  switch [IP::client_addr] {
    10.10.10.10 -
    10.10.10.20 { return }
  }
  reject 
  log local0. "Connection rejected from [IP::client_addr]"
}

Valid question Cory, but yes, I am replacing the 10.x.x.x addresses with two external addresses with 67.x.x.x and 65.x.x.x. I’ll keep working on this.

Is the iRule logging the rejection messages to /var/log/ltm from the two IP addresses you have specified that should be permitted?

Thanks Cory, but I think we have solved this by using this code:

when CLIENT_ACCEPTED { if { not ( [class match [IP::client_addr] equals rtp_allow] ) } { reject } }

This has a Data Group (rtp_allow) with the two IP address in it that are to be allowed. In testing we were able to include my IP address in this Data Group and I was able to access the internal web server page and when my IP address gets removed from the Data Group the web page is not available.