| 1 |
#! /usr/bin/env stap |
| 2 |
|
| 3 |
global drop_count |
| 4 |
|
| 5 |
probe begin { |
| 6 |
|
| 7 |
// Make sure the protocol is either UDP or TCP |
| 8 |
// and the number of packets is positive. |
| 9 |
if ((@1 != "TCP" && @1 != "UDP" && @1 != "ALL") || ($2 <0)) |
| 10 |
{ |
| 11 |
printf("Please enter \"TCP\", \"UDP\" or \"ALL\" on the command line, followed by the number of packets to drop.\n") |
| 12 |
exit() |
| 13 |
} |
| 14 |
else |
| 15 |
printf("Dropping packets! Ctrl-C to exit.\n") |
| 16 |
} |
| 17 |
|
| 18 |
probe netfilter.ipv4.local_in { |
| 19 |
|
| 20 |
// If the protocol matches that specified (or ALL), |
| 21 |
// make sure we have not exceeded the number |
| 22 |
// provided, then drop the packet. |
| 23 |
if(convert_protocol(protocol) == @1 || @1 == "ALL") { |
| 24 |
if(@count(drop_count[@1]) >= $2 && $2 != 0) |
| 25 |
exit() |
| 26 |
else { |
| 27 |
$verdict = nf_drop |
| 28 |
drop_count[@1]<<<length |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
probe end { |
| 34 |
|
| 35 |
foreach (proto in drop_count) |
| 36 |
printf("%d %s packets dropped, %d total bytes\n", |
| 37 |
@count(drop_count[proto]), proto, @sum(drop_count[proto])) |
| 38 |
} |
| 39 |
|
| 40 |
// Convert numerical protocol to string equivalent |
| 41 |
function convert_protocol (proto_n){ |
| 42 |
|
| 43 |
proto_s = "Other" |
| 44 |
if(proto_n == 6) |
| 45 |
proto_s = "TCP" |
| 46 |
else if (proto_n == 17) |
| 47 |
proto_s = "UDP" |
| 48 |
|
| 49 |
return proto_s |
| 50 |
} |