Skip to content

DHCP DoS Logger for DHCP DoS Mitigation Feature #18947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions files/build_templates/sonic_debian_extension.j2
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,11 @@ sudo cp $IMAGE_CONFIGS/copp/copp-config.sh $FILESYSTEM_ROOT/usr/bin/
sudo cp $IMAGE_CONFIGS/copp/copp_cfg.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/
echo "copp-config.service" | sudo tee -a $GENERATED_SERVICE_FILE

# Copy DHCP DoS logger configuration files
sudo cp $IMAGE_CONFIGS/dhcp_dos_logger/dhcp_dos_logger.service $FILESYSTEM_ROOT_USR_LIB_SYSTEMD_SYSTEM
sudo cp $IMAGE_CONFIGS/dhcp_dos_logger/dhcp_dos_logger.py $FILESYSTEM_ROOT/usr/bin/
echo "dhcp_dos_logger.service" | sudo tee -a $GENERATED_SERVICE_FILE

# Copy dhcp client configuration template and create an initial configuration
sudo cp files/dhcp/dhclient.conf.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/
j2 files/dhcp/dhclient.conf.j2 | sudo tee $FILESYSTEM_ROOT/etc/dhcp/dhclient.conf
Expand Down
64 changes: 64 additions & 0 deletions files/image_config/dhcp_dos_logger/dhcp_dos_logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python3

import re
import os
import subprocess
import time
from sonic_py_common.logger import Logger
from swsscommon.swsscommon import ConfigDBConnector

SYSLOG_IDENTIFIER = os.path.basename(__file__)

# Global logger instance
logger = Logger(SYSLOG_IDENTIFIER)
logger.log_info("Starting DHCP DoS logger...")

# Connect to config db
config_db = ConfigDBConnector()
config_db.connect()

# Initialize
drop_pkts = {}

# Get list of ports
ports = config_db.get_table('PORT').keys()

# Initialize the ports with zero initial packet drops
drop_pkts = {port: 0 for port in ports}

# Main handler function
def handler():
"""
Copy link
Collaborator

@qiluo-msft qiluo-msft Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add test case and check test coverage? #WontFix

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test coverage required for handler? currently, as per practice we don't add test cases and coverage here. The same is followed by all other features in image_config.

Continuously monitors ports for dropped DHCP packets and logs them.
"""
while True:
for port in drop_pkts.keys():
try:
output = subprocess.run(["tc", "-s", "qdisc", "show", "dev", str(port), "handle", "ffff:"], capture_output=True)
if output.returncode == 0: # Check for successful execution
match = re.search(r'dropped (\d+)', output.stdout)
if match:
dropped_count = int(match.group(1))
if dropped_count > drop_pkts[port]:
logger.log_warning(f"Port {port}: Current DHCP drop counter is {dropped_count}")
drop_pkts[port] = dropped_count
else:
pass
else:
logger.log_warning(f"Failed to get dropped packet information for port {port}")
except subprocess.CalledProcessError as e:
logger.log_error(f"Error executing 'tc' command: {e}")

time.sleep(10)


# Entry point function
def main():
"""
Entry point for the daemon.
"""
handler()


if __name__ == "__main__":
main()
14 changes: 14 additions & 0 deletions files/image_config/dhcp_dos_logger/dhcp_dos_logger.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[Unit]
Description=Log DHCP rate limit violations
Requires=config-setup.service
After=config-setup.service
BindsTo=sonic.target
After=sonic.target
After=network.target

[Service]
Type=simple
ExecStart= /usr/bin/dhcp_dos_logger.py
[Install]
WantedBy=sonic.target

Loading