Skip to content

Commit b00a8d6

Browse files
authored
DHCP DoS Logger for DHCP DoS Mitigation Feature (#18947)
Why I did it Added code for new daemon process responsible for detecting and logging DHCP DoS attack attempts (violation of DHCP rate limit) How I did it Added service and handler files for new systemd process dhcp_dos_logger How to verify it tc show command is used to identify dropped packets due to rate limiting
1 parent 75e9383 commit b00a8d6

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

files/build_templates/sonic_debian_extension.j2

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,11 @@ sudo cp $IMAGE_CONFIGS/copp/copp-config.sh $FILESYSTEM_ROOT/usr/bin/
464464
sudo cp $IMAGE_CONFIGS/copp/copp_cfg.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/
465465
echo "copp-config.service" | sudo tee -a $GENERATED_SERVICE_FILE
466466

467+
# Copy DHCP DoS logger configuration files
468+
sudo cp $IMAGE_CONFIGS/dhcp_dos_logger/dhcp_dos_logger.service $FILESYSTEM_ROOT_USR_LIB_SYSTEMD_SYSTEM
469+
sudo cp $IMAGE_CONFIGS/dhcp_dos_logger/dhcp_dos_logger.py $FILESYSTEM_ROOT/usr/bin/
470+
echo "dhcp_dos_logger.service" | sudo tee -a $GENERATED_SERVICE_FILE
471+
467472
# Copy dhcp client configuration template and create an initial configuration
468473
sudo cp files/dhcp/dhclient.conf.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/
469474
j2 files/dhcp/dhclient.conf.j2 | sudo tee $FILESYSTEM_ROOT/etc/dhcp/dhclient.conf
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env python3
2+
3+
import re
4+
import os
5+
import subprocess
6+
import time
7+
from sonic_py_common.logger import Logger
8+
from swsscommon.swsscommon import ConfigDBConnector
9+
10+
SYSLOG_IDENTIFIER = os.path.basename(__file__)
11+
12+
# Global logger instance
13+
logger = Logger(SYSLOG_IDENTIFIER)
14+
logger.log_info("Starting DHCP DoS logger...")
15+
16+
# Connect to config db
17+
config_db = ConfigDBConnector()
18+
config_db.connect()
19+
20+
# Initialize
21+
drop_pkts = {}
22+
23+
# Get list of ports
24+
ports = config_db.get_table('PORT').keys()
25+
26+
# Initialize the ports with zero initial packet drops
27+
drop_pkts = {port: 0 for port in ports}
28+
29+
# Main handler function
30+
def handler():
31+
"""
32+
Continuously monitors ports for dropped DHCP packets and logs them.
33+
"""
34+
while True:
35+
for port in drop_pkts.keys():
36+
try:
37+
output = subprocess.run(["tc", "-s", "qdisc", "show", "dev", str(port), "handle", "ffff:"], capture_output=True)
38+
if output.returncode == 0: # Check for successful execution
39+
match = re.search(r'dropped (\d+)', output.stdout)
40+
if match:
41+
dropped_count = int(match.group(1))
42+
if dropped_count > drop_pkts[port]:
43+
logger.log_warning(f"Port {port}: Current DHCP drop counter is {dropped_count}")
44+
drop_pkts[port] = dropped_count
45+
else:
46+
pass
47+
else:
48+
logger.log_warning(f"Failed to get dropped packet information for port {port}")
49+
except subprocess.CalledProcessError as e:
50+
logger.log_error(f"Error executing 'tc' command: {e}")
51+
52+
time.sleep(10)
53+
54+
55+
# Entry point function
56+
def main():
57+
"""
58+
Entry point for the daemon.
59+
"""
60+
handler()
61+
62+
63+
if __name__ == "__main__":
64+
main()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[Unit]
2+
Description=Log DHCP rate limit violations
3+
Requires=config-setup.service
4+
After=config-setup.service
5+
BindsTo=sonic.target
6+
After=sonic.target
7+
After=network.target
8+
9+
[Service]
10+
Type=simple
11+
ExecStart= /usr/bin/dhcp_dos_logger.py
12+
[Install]
13+
WantedBy=sonic.target
14+

0 commit comments

Comments
 (0)