Forum Discussion

Joko_Yuliantor3's avatar
Joko_Yuliantor3
Historic F5 Account
Dec 31, 2012

IP Address Pool Management with iRule

IP address pool management is a management of allocating and de-allocating an IP address out of a range of IP addresses or pool. There are several use cases of IP address pool management such as DHCP server and Remote Access Server (RAS). The manager allocates an IP address from the pool when a new connection is established. The manager de-allocates the IP address back to the pool when the connection is torn down.

 

The following iRule is an example of the IP address pool management with the help of data group and session table. The data group serves as the list of IP address ranges. The session table serves as the place to record the allocation of the IP addresses. The allocation is triggered on RADIUS Accounting Start message. The de-allocation is triggered on RADIUS Accounting Stop message.

 

Hope this helps...

 

Cheers,

 

-joko

 

 

=== iRule Start ===

 

when CLIENT_DATA {

 

if {[RADIUS::code]==4} {

 

if {[RADIUS::avp 40]==1} {

 

set ippool {}

 

set n [class size CLASS_IP_POOL]

 

for {set i 0} {$i < $n} {incr i} {

 

set subnetmask [split [class element -name $i CLASS_IP_POOL] "/"]

 

set masklen [lindex $subnetmask 1]

 

if {$masklen < 32} {

 

set mask 0

 

for {set i 0} {$i<32} {incr i} {

 

set mask [expr {$mask << 1}]

 

if {$masklen > 0} {

 

incr mask

 

}

 

incr masklen -1

 

}

 

set a [split [lindex $subnetmask 0] "."]

 

set subnet [expr {([lindex $a 0]<<24)+([lindex $a 1]<<16)+([lindex $a 2]<<8)+[lindex $a 3]}]

 

set notfoundunused 1

 

while {$notfoundunused} {

 

incr subnet

 

if {[table lookup $subnet.alloc] eq ""} {

 

set maskxor [expr {$mask^0xffffffff}]

 

if {[expr {$maskxor&$subnet}] != $maskxor} {

 

set ippool [expr {($subnet>>24)&0xff}].[expr {($subnet>>16)&0xff}].[expr {($subnet>>8)&0xff}].[expr {$subnet&0xff}]

 

table set $subnet.alloc 1 3600 3600

 

set notfoundunused 0

 

break

 

}

 

}

 

}

 

} else {

 

if {[table lookup [lindex $subnetmask 0].snat] eq ""} {

 

set ippool [lindex $subnetmask 0]

 

break

 

}

 

}

 

}

 

if {$ippool ne ""} {

 

table set [RADIUS::avp 8].snat $ippool 900 indef

 

log local0. "[RADIUS::avp 8] is SNAT-ed to $ippool"

 

RADIUS::avp replace 8 $ippool ip4

 

}

 

else {

 

log local0. "Pool exhausted"

 

}

 

}

 

elseif {[RADIUS::avp 40]==2} {

 

set ipori [RADIUS::avp 8].snat

 

set ippool [table lookup $ipori]

 

if {$ippool ne ""} {

 

RADIUS::avp replace 8 $ippool ip4

 

table delete $ipori

 

set a [split [lindex $ippool 0] "."]

 

set subnet [expr {([lindex $a 0]<<24)+([lindex $a 1]<<16)+([lindex $a 2]<<8)+[lindex $a 3]}]

 

table delete $subnet.alloc

 

}

 

}

 

}

 

}

 

 

=== iRule End ===

 

 

 

No RepliesBe the first to reply