Skip to contentSkip to conversation

Counting traffic - another question

DISCUSSION Marcin 2 participants 7 messages Jul 27, 2004

lore
Share & tools
lore

Marcin

author
#0 lore raw

Hi

I have set of rules that doing upload/download stats for my NATed network (10.0.0.0/8):

  # Marking
  $IPTABLES -t mangle -A FORWARD -p tcp -j CONNMARK --restore-mark
  $IPTABLES -t mangle -A FORWARD -p tcp -m mark ! --mark 0 -j ACCEPT

  $IPTABLES -t mangle -A FORWARD -p tcp -m ipp2p --ipp2p     -j MARK --set-mark $P2P
  $IPTABLES -t mangle -A FORWARD -p tcp --dport $FTP_PORT    -j MARK --set-mark $FTP
  $IPTABLES -t mangle -A FORWARD -p tcp --dport $SSH_PORT    -j MARK --set-mark $SSH
  $IPTABLES -t mangle -A FORWARD -p tcp --dport $TELNET_PORT -j MARK --set-mark $SSH
  $IPTABLES -t mangle -A FORWARD -p tcp --dport $SMTP_PORT   -j MARK --set-mark $SMTP
  $IPTABLES -t mangle -A FORWARD -p tcp --dport $HTTP_PORT   -j MARK --set-mark $HTTP
  $IPTABLES -t mangle -A FORWARD -p tcp --dport $HTTPS_PORT  -j MARK --set-mark $HTTPS
  $IPTABLES -t mangle -A FORWARD -p tcp --dport $POP3_PORT   -j MARK --set-mark $POP3
  $IPTABLES -t mangle -A FORWARD -p tcp --dport $IRC_PORT    -j MARK --set-mark $IRC

  $IPTABLES -t mangle -A FORWARD -p tcp -j CONNMARK --save-mark

Rules above (IMO) marks specified trafic traveling through FORWARD chain, and this is only traffic from localnet <-> internet, not from localnet <-> linuxbox.

Ok, now i make chains for counting uploads/downloads from/to my lan.

$IPTABLES -t mangle -N statsin
$IPTABLES -t mangle -N statsout

My internet interface is ppp0 ($INET_IF), and lan is connected to eth0 ($LOCAL_IF). So, with rules below I send packets leaving my box with ppp0 to statsout chain, and with eth0 to statsin.

$IPTABLES -t mangle -A POSTROUTING -o $INET_IF -j statsout $IPTABLES -t mangle -A POSTROUTING -o $LOCAL_IF -j statsin

And here is real counting. Note, that $OTHER is for not marked packets.

  $IPTABLES -t mangle -A statsin -m mark --mark $P2P
  $IPTABLES -t mangle -A statsin -m mark --mark $FTP
  $IPTABLES -t mangle -A statsin -m mark --mark $SSH
  $IPTABLES -t mangle -A statsin -m mark --mark $SMTP
  $IPTABLES -t mangle -A statsin -m mark --mark $HTTP
  $IPTABLES -t mangle -A statsin -m mark --mark $POP3
  $IPTABLES -t mangle -A statsin -m mark --mark $IRC
  $IPTABLES -t mangle -A statsin -m mark --mark $OTHER

  $IPTABLES -t mangle -A statsout -m mark --mark $P2P
  $IPTABLES -t mangle -A statsout -m mark --mark $FTP
  $IPTABLES -t mangle -A statsout -m mark --mark $SSH
  $IPTABLES -t mangle -A statsout -m mark --mark $SMTP
  $IPTABLES -t mangle -A statsout -m mark --mark $HTTP
  $IPTABLES -t mangle -A statsout -m mark --mark $POP3
  $IPTABLES -t mangle -A statsout -m mark --mark $IRC
  $IPTABLES -t mangle -A statsout -m mark --mark $OTHER

Ok, this works fine for me. But there is one thing, that wondering me. I want to count only traffic from lan <-> internet. But these rules:

$IPTABLES -t mangle -A POSTROUTING -o $INET_IF -j statsout $IPTABLES -t mangle -A POSTROUTING -o $LOCAL_IF -j statsin

should also match packets generated on linuxbox destined to lan or internet. Is that true? These packets are not marked, so my rules count them as $OTHER.

I dont want count these packets, so I wrote 2 new rules

$IPTABLES -t mangle -A FORWARD -s 10.0.0.0/8 -j statsout $IPTABLES -t mangle -A FORWARD -d 10.0.0.0/8 -j statsin

but this dont work :(

So how can I count trafic only from lan <-> internet?

PS. Yes, I know, my english is tragic :D

Signature · 2 lines
Pozdrawiam
 Marcin                         mailto:[email protected]
#1 lore raw
Quoted 11 lines · Marcin

Hi

I have set of rules that doing upload/download stats for my NATed network (10.0.0.0/8):

  # Marking
  $IPTABLES -t mangle -A FORWARD -p tcp -j CONNMARK --restore-mark
  $IPTABLES -t mangle -A FORWARD -p tcp -m mark ! --mark 0 -j ACCEPT

$IPTABLES -t mangle -A FORWARD -p tcp --dport $SSH_PORT -j MARK --set-mark
$SSH

<snip...>

Quoted 3 lines

Rules above (IMO) marks specified trafic traveling through FORWARD chain, and this is only traffic from localnet <-> internet, not from localnet <-> linuxbox.

Correct. FORWARD chain matches only packets which are routed *through* the
machine, not packets going to/from the machine itself.

Quoted 1 line

Ok, now i make chains for counting uploads/downloads from/to my lan.

Why? You seem to have one set of rules in the FORWARD mangle table for
MARKing packets, and then another set of rules in POSTROUTING looking for MARKs and counting the packets.

Why not simply count all the packets in FORWARD mangle?

For example:

iptables -A FORWARD -t mangle -i eth0 -o ppp0 -p tcp --dport ssh

Will count all packets going from eth0 to ppp0 destination ssh (TCP port 22).

No need to MARK the packets (unless you want the MARKs for something else?), and you can specify input & output interface in the FORWARD table to identify which way through your firewall the packets are going.

Regards,

Antony.

Signature · 4 lines
Most people have more than the average number of legs.

                                                     Please reply to the list;
                                                           please don't CC me.
#2 lore raw

Witam

Quoted 1 line · you

No need to MARK the packets (unless you want the MARKs for something else?),

Yes, I need MARKs for traffic shaping. Sorry, I forgot to mention this.

Quoted 2 lines

and you can specify input & output interface in the FORWARD table to identify which way through your firewall the packets are going.

$IPTABLES -t mangle -A FORWARD -i $LOCAL_IF -o $INET_IF -j statsout $IPTABLES -t mangle -A FORWARD -i $INET_IF -o $LOCAL_IF -j statsin

Signature · 2 lines
Pozdrawiam
 Marcin                            mailto:[email protected]
#3 lore raw
In reply to Antony Stone

Witam

Sorry for my previous mail ;) I press enter in wrong time :)

Quoted 1 line · you

No need to MARK the packets (unless you want the MARKs for something else?),

Yes, I need MARKs for traffic shaping. Sorry, I forgot to mention this.

Quoted 2 lines

and you can specify input & output interface in the FORWARD table to identify which way through your firewall the packets are going.

Ok, so I change the rules (i still use statsin and statsout for ... hmm ... order :)

$IPTABLES -t mangle -A FORWARD -i $LOCAL_IF -o $INET_IF -j statsout $IPTABLES -t mangle -A FORWARD -i $INET_IF -o $LOCAL_IF -j statsin

And this dont work :( I mean rules in statsout statsin incorrectly count trafic :(

What's wrong?

Signature · 2 lines
Pozdrawiam
 Marcin                            mailto:[email protected]
#4 lore raw
Quoted 5 lines · Marcin Sura
Quoted 2 lines · you

No need to MARK the packets (unless you want the MARKs for something else?),

Yes, I need MARKs for traffic shaping. Sorry, I forgot to mention this.

Okay, no problem, continue MARKing them then, but I still think you can count them in FORWARD mangle.

Quoted 8 lines
Quoted 2 lines

and you can specify input & output interface in the FORWARD table to identify which way through your firewall the packets are going.

Ok, so I change the rules (i still use statsin and statsout for ... hmm ... order :)

$IPTABLES -t mangle -A FORWARD -i $LOCAL_IF -o $INET_IF -j statsout $IPTABLES -t mangle -A FORWARD -i $INET_IF -o $LOCAL_IF -j statsin

No, don't put the "-j statsout" or "-j statsin" at the end. The rule I
suggested previously is actually complete:

iptables -A FORWARD -t mangle -i eth0 -o ppp0 -p tcp --dport ssh

You do not need to have a "-j target" at the end of a rule if you don't want
one. The above rule will still count the packets, and you can see the
packet/byte counters with "iptables -L FORWARD -t mangle -nvx".

Regards,

Antony.

Signature · 8 lines
"It would appear we have reached the limits of what it is possible to achieve 
with computer technology, although one should be careful with such 
statements; they tend to sound pretty silly in five years."

 - John von Neumann (1949)

                                                     Please reply to the list;
                                                           please don't CC me.
#5 lore raw

Witam

Quoted 6 lines · you
Quoted 2 lines

$IPTABLES -t mangle -A FORWARD -i $LOCAL_IF -o $INET_IF -j statsout $IPTABLES -t mangle -A FORWARD -i $INET_IF -o $LOCAL_IF -j statsin

You do not need to have a "-j target" at the end of a rule if you don't want
one. The above rule will still count the packets, and you can see the
packet/byte counters with "iptables -L FORWARD -t mangle -nvx".

Ok, i'll try this, but im still wondering why my rules works incorrectly. In theory (my theory :) first rule redirects packets lan -> internet to statsout, and in statsout chain they are counted by type of traffic.

The same goes to second rule, but there is internet -> lan traffic redirected.

But unfortunately this won't work that way. Why? Output form iptables -t mangle -L -vn is attached.

Signature · 2 lines
Pozdrawiam
 Marcin                            mailto:[email protected]
root@serwer:/etc/rc.d# iptables -t mangle -L -vn
Chain PREROUTING (policy ACCEPT 279K packets, 52M bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain INPUT (policy ACCEPT 19339 packets, 2118K bytes)
pkts bytes target prot opt in out source destination

Chain FORWARD (policy ACCEPT 190K packets, 16M bytes)

 pkts bytes target     prot opt in     out     source               destination
50145   25M CONNMARK   tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           CONNMARK restore
47938   25M ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match !0x0
    0     0 MARK       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           ipp2p v0.6 --ipp2p MARK set 0x1
    3   144 MARK       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:21 MARK set 0x4
    0     0 MARK       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:22 MARK set 0x3
    0     0 MARK       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:23 MARK set 0x3
    2    96 MARK       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:25 MARK set 0x5
 1491 71488 MARK       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:80 MARK set 0x2
    8   384 MARK       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:443 MARK set 0x2
  100  4800 MARK       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:110 MARK set 0x6
    0     0 MARK       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:6667 MARK set 0x7
 2207  122K CONNMARK   tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           CONNMARK save
 2150  107K statsout   all  --  eth0   ppp0    0.0.0.0/0            0.0.0.0/0
  562 81524 statsin    all  --  ppp0   eth0    0.0.0.0/0            0.0.0.0/0

Chain OUTPUT (policy ACCEPT 21282 packets, 1313K bytes)
pkts bytes target prot opt in out source destination

Chain POSTROUTING (policy ACCEPT 281K packets, 51M bytes)

 pkts bytes target     prot opt in     out     source               destination
61681   25M TTL        all  --  *      *       0.0.0.0/0            0.0.0.0/0           TTL set to 128

Chain statsin (1 references)

 pkts bytes target     prot opt in     out     source               destination
    0     0            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x1
    0     0            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x4
    0     0            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x3
    0     0            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x5
    0     0            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x2
    0     0            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x6
    0     0            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x7
  562 81524            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x0

Chain statsout (1 references)

 pkts bytes target     prot opt in     out     source               destination
    0     0            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x1
    3   144            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x4
    0     0            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x3
    2    96            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x5
 1499 71872            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x2
  100  4800            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x6
    0     0            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x7
  546 29999            all  --  *      *       0.0.0.0/0            0.0.0.0/0           MARK match 0x0
#6 lore raw
In reply to Antony Stone

Witam

Quoted 4 lines · you

No, don't put the "-j statsout" or "-j statsin" at the end. The rule I
suggested previously is actually complete:

iptables -A FORWARD -t mangle -i eth0 -o ppp0 -p tcp --dport ssh

Signature · 2 lines
Pozdrawiam
 Marcin                            mailto:[email protected]