IP Filtering System Design

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2024-03-23

Problem Statement

Design the Low-Level Design (LLD) for an IP Filtering System that decides whether to allow or block incoming network requests based on configurable IP filtering rules.


The system should efficiently manage and apply whitelists, blacklists, and CIDR-based range filters, and support dynamic rule updates without downtime.


🎯 Core Requirements


1. IP Rule Management


•The system should maintain a set of rules defining which IPs are allowed or blocked.
•Rules can be:
•Single IPs (e.g., 192.168.1.5)
•IP Ranges / CIDR blocks (e.g., 192.168.0.0/16)
•Wildcards or regex patterns (optional)


2. Filtering Logic


•For each incoming request with a given IP address:
•Check if the IP is explicitly blacklisted → block.
•Else if whitelisted → allow.
•Else → use default action (allow or block, configurable).
•Matching should be fast, even for large rule sets.


3. Dynamic Updates


•The system should support adding, removing, or updating rules at runtime.
•Updates must not interrupt ongoing filtering operations.


4. Persistence & Caching


•Optionally persist the rules in a datastore (file, DB, or cache).
•Cache frequently checked IPs and their decisions to reduce lookup time.


5. Audit Logging


•Every allow/deny decision should be logged with:
•IP address
•Matched rule
•Timestamp
•Decision (ALLOW / DENY)


⚙️ Functional APIs


APIDescription


addRule(ruleId, ruleType, value)


Adds a new whitelist or blacklist rule.


removeRule(ruleId)


Deletes an existing rule.


checkAccess(ipAddress)


Returns ALLOW or DENY for a given IP.


loadRules()


Loads rules from a persistent store on startup.


listRules()


Returns all active filtering rules.


🧱 Class Design (Suggested)


ClassResponsibility


IPRule


Represents a single filtering rule (id, type, IP/CIDR pattern).


IPFilterManager


Maintains collections of whitelist and blacklist rules.


IPMatcher


Performs IP or CIDR matching logic.


CacheManager


Stores cached results for previously checked IPs.


AuditLogger


Logs filtering decisions.


IPFilterService


Main entry point for checking access and managing rules.


🧩 Example Scenario


Rules configured:

1 2Whitelist: 3 - 10.0.0.5 4 - 192.168.0.0/16 5Blacklist: 6 - 203.0.113.45 7 - 172.16.0.0/12


Requests:


IP AddressDecisionReason

10.0.0.5


ALLOW


Whitelisted exact IP

192.168.2.10


ALLOW


Matches CIDR range

203.0.113.45


DENY


Explicitly blacklisted

8.8.8.8


DENY


Default deny (not matched)


🧠 Design Considerations


Performance:

•Use a Trie or Radix Tree for fast IP range lookup.
•Use caching (LRU) to speed up repeated IP checks.

Thread Safety:

•Ensure updates to rules don’t cause race conditions (use ReadWriteLocks).

Extensibility:

•Allow future rule types (e.g., country-based, subnet-based).
•Plug-in architecture for advanced filters (GeoIP, ASN, device type).

Scalability:

•Handle thousands of rules efficiently.
•Allow distributed rule propagation if deployed across multiple servers.


🔄 Sample Flow


System Initialization

•Loads all stored whitelist/blacklist rules into memory.

Incoming Request

1 2checkAccess("192.168.2.10") 3→ IPMatcher checks whitelist and blacklist 4→ Decision: ALLOW 5→ AuditLogger logs event

Dynamic Update

1 2addRule("R102", "BLACKLIST", "10.0.0.0/8") 3→ Rule added in memory 4→ Future requests from 10.x.x.x will be blocked


🧱 Sample Class Diagram

1 2+-------------------+ 3| IPRule | 4+-------------------+ 5| ruleId | 6| type (ALLOW/DENY) | 7| cidrRange/IP | 8+-------------------+ 9 10+-------------------+ 11| IPMatcher | 12+-------------------+ 13| +matches(ip, rule)| 14+-------------------+ 15 16+-------------------+ 17| IPFilterManager | 18+-------------------+ 19| whitelist: List<IPRule> | 20| blacklist: List<IPRule> | 21| +addRule() | 22| +removeRule() | 23| +getDecision(ip) | 24+-------------------+ 25 26+-------------------+ 27| CacheManager | 28+-------------------+ 29| cache: Map<ip, result> | 30| +getResult(ip) | 31| +putResult(ip, result) | 32+-------------------+ 33 34+-------------------+ 35| IPFilterService | 36+-------------------+ 37| +checkAccess(ip) | 38| +addRule() | 39| +removeRule() | 40+-------------------+ 41 42+-------------------+ 43| AuditLogger | 44+-------------------+ 45| +log(ip, rule, result) | 46+-------------------+


🚀 Possible Extensions


•Rate-limiting based on IP (integrate Token Bucket).
•Group-based rules (per organization or user).
•Time-based rules (block IPs for 1 hour).
•UI or REST APIs for rule management.
•Integration with firewalls or load balancers.


✅ Goal:


Design an extensible, efficient IP Filtering System that can manage dynamic allow/deny rules, support CIDR-based IP ranges, provide fast lookups, and maintain audit logs for all access decisions.

Solution

Comments