-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
qiluo-msft
merged 13 commits into
sonic-net:master
from
asraza07:dhcp_dos_mitigation_logger
May 29, 2025
+83
−0
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6ea2428
Add service and handler files for DHCP DoS logger process
asraza07 efe9e83
commited obtaining and matching than initail counter
muhammadalihussnain 893bd88
shell= False
muhammadalihussnain 0014f64
Entry Point Added
muhammadalihussnain 33fb23a
Entry Point + shell=False
muhammadalihussnain da21516
nothing seen
muhammadalihussnain 0219f08
changed path of dhcp_dos_logger.py file in service file
muhammadalihussnain d54ad45
Fix for log messages
asraza07 57c07c5
Add path for new service and handler files in debian template
asraza07 72de51b
Fix for semgrep
asraza07 eb7976a
Merge branch 'sonic-net:master' into dhcp_dos_mitigation_logger
muhammadalihussnain bd7d464
added test_for_logger
muhammadalihussnain d2e852c
removed the test-file
muhammadalihussnain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(): | ||
""" | ||
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
14
files/image_config/dhcp_dos_logger/dhcp_dos_logger.service
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.