Define a packet filter rule with iptables to count all tcp packets.
Count all incoming packets with tcp protocol:
# iptables -I INPUT -p tcp
Count all outgoing packets with tcp protocol:
# iptables -I OUTPUT -p tcp
Show iptables rules including packet counts for incoming packets:
# iptables -nvL INPUT
Show iptables rules including packet counts for outgoing packets:
# iptables -nvL OUTPUT
iptables -nvL
shows the packet count and the byte count in the first two columns.
The two defined rules will be the first ones on the top of the list.
If you have a lot of iptables rules, an extra chain could be helpful:
# iptables -N count_in # create custom chain named 'count_in'
# iptables -A count_in -j RETURN # append RETURN action to chain 'count_in'
# iptables -I INPUT -j count_in # insert chain at the top of chain INPUT
# iptables -I count_in 1 -p tcp # insert rule that matches all tcp packets
# and has no action (does nothing)
# iptables -nvL count_in # list chain 'count_in' rules
Do the same for the outgoing packets with a custom chain count_out
.