]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
SQLite DHCP query updates
authorNick Porter <nick@portercomputing.co.uk>
Thu, 10 Sep 2020 14:12:58 +0000 (15:12 +0100)
committerAlan DeKok <aland@freeradius.org>
Wed, 16 Sep 2020 12:47:35 +0000 (08:47 -0400)
raddb/mods-config/sql/ippool-dhcp/sqlite/queries.conf

index eeaa7a8ad08eb26132d7d285b0e46fcb253e944c..7d486ee92b6feab89be5c9bccd2079f0607127d3 100644 (file)
@@ -4,6 +4,11 @@
 #
 #  $Id$
 
+
+#  *****************
+#  * DHCP DISCOVER *
+#  *****************
+
 #
 #  SQLite does not implement SELECT FOR UPDATE which is normally used to place
 #  an exclusive lock over rows to prevent the same address from being
@@ -25,41 +30,44 @@ alloc_commit = "COMMIT"
 #  This series of queries allocates an IP address - used in response to
 #  DHCP Discover packets
 #
-#  Either pull the most recent allocated IP for this client or the
-#  oldest expired one.  The first sub query returns the most recent
-#  lease for the client (if there is one), the second returns the
-#  oldest expired one.
-#  Sorting the result by expiry_time DESC will return the client specific
-#  IP if it exists, otherwise an expired one.
+
 #
-alloc_find = "\
-       SELECT framedipaddress, expiry_time \
-       FROM ( \
-               SELECT framedipaddress, expiry_time \
-               FROM ${ippool_table} \
-               JOIN dhcpstatus \
-               ON ${ippool_table}.status_id = dhcpstatus.status_id \
-               WHERE pool_name = '%{control:${pool_name}}' \
-               AND pool_key = '${pool_key}' \
-               AND status IN ('dynamic', 'static') \
-               ORDER BY expiry_time DESC \
-               LIMIT 1 \
-       ) UNION \
-       SELECT framedipaddress, expiry_time \
-       FROM ( \
-               SELECT framedipaddress, expiry_time \
-               FROM ${ippool_table} \
-               JOIN dhcpstatus \
-               ON ${ippool_table}.status_id = dhcpstatus.status_id \
-               WHERE pool_name = '%{control:${pool_name}}' \
-               AND expiry_time < datetime('now') \
-               AND status = 'dynamic' \
-               ORDER BY expiry_time LIMIT 1 \
-       ) \
-       ORDER BY \
-               expiry_time DESC \
+#  Is there an exsiting address for this client
+#
+alloc_existing = "SELECT framedipaddress \
+       FROM ${ippool_table} \
+       JOIN dhcpstatus \
+       ON ${ippool_table}.status_id = dhcpstatus.status_id \
+       WHERE pool_name = '%{control:${pool_name}}' \
+       AND pool_key = '${pool_key}' \
+       AND status IN ('dynamic', 'static') \
+       ORDER BY expiry_time DESC \
        LIMIT 1"
 
+#
+#  If the client has requested an address, is it available
+#
+alloc_requested = "SELECT framedipaddress \
+       FROM ${ippool_table} \
+       JOIN dhcpstatus \
+       ON ${ippool_table}.status_id = dhcpstatus.status_id \
+       WHERE pool_name = '%{control:${pool_name}}' \
+       AND framedipaddress = '%{%{${req_attribute_name}}:-0.0.0.0}' \
+       AND status = 'dynamic'"
+
+#
+#  Finally find a free address
+#
+alloc_find = "\
+       SELECT framedipaddress \
+       FROM ${ippool_table} \
+       JOIN dhcpstatus \
+       ON ${ippool_table}.status_id = dhcpstatus.status_id \
+       WHERE pool_name = '%{control:${pool_name}}' \
+       AND expiry_time < datetime('now') \
+       AND status = 'dynamic' \
+       ORDER BY expiry_time LIMIT 1"
+
 #
 # If you prefer to allocate a random IP address every time, use this query instead
 #
@@ -67,12 +75,12 @@ alloc_find = "\
 #      SELECT framedipaddress FROM ${ippool_table} \
 #      JOIN dhcpstatus \
 #      ON ${ippool_table}.status_id = dhcpstatus.status_id \
-#      WHERE pool_name = '%{control:Pool-Name}' \
+#      WHERE pool_name = '%{control:${pool-name}}' \
 #      AND expiry_time IS NULL \
 #      AND status = 'dynamic' \
 #      ORDER BY RAND() \
-#      LIMIT 1 \
-#      FOR UPDATE"
+#      LIMIT 1"
+
 
 #
 #  If an IP could not be allocated, check to see if the pool exists or not
@@ -94,10 +102,31 @@ alloc_update = "\
        SET \
                gateway = '%{DHCP-Gateway-IP-Address}', \
                pool_key = '${pool_key}', \
-               expiry_time = datetime(strftime('%%s', 'now') + ${lease_duration}, 'unixepoch') \
+               expiry_time = datetime(strftime('%%s', 'now') + ${offer_duration}, 'unixepoch') \
        WHERE framedipaddress = '%I' \
        AND expiry_time IS NULL"
 
+
+#  ****************
+#  * DHCP REQUEST *
+#  ****************
+
+#
+#  Query used to clear any other addresses that have been offered to the client
+#
+update_free = "\
+       UPDATE ${ippool_table} \
+       SET \
+               gateway = '', \
+               pool_key = '', \
+               expiry_time = datetime('now') \
+       WHERE pool_name = '%{control:${pool_name}}' \
+       AND pool_key = '${pool_key}' \
+       AND framedipaddress <> '%{DHCP-Requested-IP-Address}' \
+       AND expiry_time > datetime('now') \
+       AND ${ippool_table}.status_id IN \
+       (SELECT status_id FROM dhcpstatus WHERE status = 'dynamic')"
+
 #
 #  Queries to update a lease - used in response to DHCP-Request packets
 #
@@ -108,6 +137,11 @@ update_update = "\
        AND pool_key = '${pool_key}' \
        AND framedipaddress = '%{%{DHCP-Requested-IP-Address}:-%{DHCP-Client-IP-Address}}'"
 
+
+#  ****************
+#  * DHCP RELEASE *
+#  ****************
+
 #
 #  Queries to release a lease - used in response to DHCP-Release packets
 #
@@ -118,7 +152,14 @@ release_clear = "\
                expiry_time = datetime('now') \
        WHERE pool_name = '%{control:${pool_name}}' \
        AND pool_key = '${pool_key}' \
-       AND framedipaddress = '%{DHCP-Client-IP-Address}'"
+       AND framedipaddress = '%{DHCP-Client-IP-Address}' \
+       AND ${ippool_table}.status_id IN \
+       (SELECT status_id FROM dhcpstatus WHERE status = 'dynamic')"
+
+
+#  ****************
+#  * DHCP DECLINE *
+#  ****************
 
 #
 #  Queries to mark leases as "bad" - used in response to DHCP-Decline
@@ -129,4 +170,3 @@ mark_update = "\
        WHERE pool_name = '%{control:${pool_name}}' \
        AND framedipaddress = '%{DHCP-Requested-IP-Address}' \
        AND pool_key = '${pool_key}'"
-