Default mark for packets using iptables

5,434

I was able to declare a 'default action' by adding a first rule that matches every packet. It seems that the 'mangle' table does not act like the others, and tries to match every rule instead of stopping at the first matching rule. This makes sense because one may want to do several modifications to the same packet...

# Everything goes into the default queue, except if another rule matches...
iptables -t mangle -A PREROUTING -s 192.168.1.0/24 -j MARK --set-mark 12
iptables -t mangle -A PREROUTING -s 192.168.1.70   -j MARK --set-mark 10 
iptables -t mangle -A PREROUTING -j IMQ --todev 0
Share:
5,434

Related videos on Youtube

Sid
Author by

Sid

Updated on September 17, 2022

Comments

  • Sid
    Sid over 1 year

    I am trying to set up a QOS script on my Tomato-enabled WRT54G router. The script does the basics of what I need (make sure that a particular machine on my network has lower priority than anything else), but I am not satisfied with the fact that I had to define a specific rule for the outgoing traffic from all other machines instead of using some 'default' behavior.

    Specifically, I would like to get rid of the two lines that mark packets with marks 11 and 12.

    iptables -t mangle -A PREROUTING -s 192.168.1.70 -j MARK --set-mark 10 
    iptables -t mangle -A PREROUTING -s 192.168.1.70 -j RETURN
    iptables -t mangle -A PREROUTING -m iprange --src-range 192.168.1.2-192.168.1.69 -j MARK --set-mark 11 
    iptables -t mangle -A PREROUTING -m iprange --src-range 192.168.1.71-192.168.1.254 -j MARK --set-mark 12 
    iptables -t mangle -A PREROUTING -j IMQ --todev 0 
    

    I tried to just remove them (adding a 'default 12') at the creation of the qdisk, but throughput dropped down dramatically. I also tried to mark all packets with either

    iptables -t mangle -A PREROUTING -j MARK --set-mark 12
    

    or

    iptables -t mangle -A PREROUTING -m mark --mark 0 -j MARK --set-mark 12
    

    as the first or last filtering rules, but this did not work either.

    Here is the complete script...

    TCA="tc class add dev br0" 
    TFA="tc filter add dev br0" 
    TQA="tc qdisc add dev br0" 
    SFQ="sfq perturb 10" 
    tc qdisc del dev br0 root 
    tc qdisc add dev br0 root handle 1: htb default 10 
    tc class add dev br0 parent 1: classid 1:1 htb rate 2400kbit 
    $TCA parent 1:1 classid 1:10 htb rate 2300kbit ceil 2400kbit prio 0 
    $TCA parent 1:1 classid 1:11 htb rate   10kbit ceil 2400kbit prio 1 
    $TCA parent 1:1 classid 1:12 htb rate   10kbit ceil 2400kbit prio 2 
    $TCA parent 1:1 classid 1:13 htb rate   10kbit ceil 2000kbit prio 4 
    $TQA parent 1:10 handle 10: $SFQ 
    $TQA parent 1:11 handle 11: $SFQ 
    $TQA parent 1:12 handle 12: $SFQ 
    $TQA parent 1:13 handle 13: $SFQ 
    $TFA parent 1:0 prio 0 protocol ip handle 10 fw flowid 1:10 
    $TFA parent 1:0 prio 1 protocol ip handle 11 fw flowid 1:11 
    $TFA parent 1:0 prio 2 protocol ip handle 12 fw flowid 1:12 
    $TFA parent 1:0 prio 4 protocol ip handle 13 fw flowid 1:13 
    iptables -t mangle -A POSTROUTING -p tcp --sport    80 -j MARK --set-mark 10 
    iptables -t mangle -A POSTROUTING -p tcp --sport   443 -j MARK --set-mark 11 
    iptables -t mangle -A POSTROUTING -p tcp --sport   995 -j MARK --set-mark 12 
    iptables -t mangle -A POSTROUTING -d 192.168.1.70 -j MARK --set-mark 13 
    
    
    TCAU="tc class add dev imq0" 
    TFAU="tc filter add dev imq0" 
    TQAU="tc qdisc add dev imq0" 
    modprobe imq 
    modprobe ipt_IMQ 
    ip link set imq0 up 
    tc qdisc del dev imq0 root 
    tc qdisc add dev imq0 root handle 1: htb
    tc class add dev imq0 parent 1: classid 1:1 htb rate 700kbit 
    $TCAU parent 1:1 classid 1:10 htb rate 7kbit ceil 500kbit prio 4 
    $TCAU parent 1:1 classid 1:11 htb rate 30kbit ceil 700kbit prio 2 
    $TCAU parent 1:1 classid 1:12 htb rate 663kbit ceil 700kbit prio 2 
    $TQAU parent 1:10 handle 10: $SFQ 
    $TQAU parent 1:11 handle 11: $SFQ 
    $TQAU parent 1:12 handle 12: $SFQ 
    $TFAU parent 1:0 prio 4 protocol ip handle 10 fw flowid 1:10 
    $TFAU parent 1:0 prio 2 protocol ip handle 11 fw flowid 1:11 
    $TFAU parent 1:0 prio 2 protocol ip handle 12 fw flowid 1:12 
    
    iptables -t mangle -A PREROUTING -s 192.168.1.70 -j MARK --set-mark 10 
    
    #
    # What should I do to avoid these two lines and mark everything else as '12'?
    #
    iptables -t mangle -A PREROUTING -m iprange --src-range 192.168.1.2-192.168.1.69 -j MARK --set-mark 11 
    iptables -t mangle -A PREROUTING -m iprange --src-range 192.168.1.71-192.168.1.254 -j MARK --set-mark 12 
    iptables -t mangle -A PREROUTING -j IMQ --todev 0