Last active
February 25, 2019 17:47
-
-
Save rda0/2edc01632578b0bd59b8c486f8a94989 to your computer and use it in GitHub Desktop.
Script to create tc filters and classes
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
| #!/bin/bash | |
| dev="eth0" | |
| ip_local="10.8.0.0" | |
| cut_ip_local() { | |
| if [ -n "$ip_local" ]; then | |
| ip_local_byte1=`echo "$ip_local" | cut -d. -f1` | |
| ip_local_byte2=`echo "$ip_local" | cut -d. -f2` | |
| fi | |
| } | |
| create_identifiers() { | |
| if [ -n "$1" ]; then | |
| ip_byte3=`echo "$1" | cut -d. -f3` | |
| handle=`printf "%x\n" "$ip_byte3"` | |
| ip_byte4=`echo "$1" | cut -d. -f4` | |
| hash=`printf "%x\n" "$ip_byte4"` | |
| classid=`printf "%x\n" $((256*ip_byte3+ip_byte4))` | |
| fi | |
| } | |
| start_tc() { | |
| cut_ip_local | |
| tc qdisc add dev "$dev" root handle 1: htb | |
| tc filter add dev "$dev" parent 1:0 prio 1 protocol ip u32 | |
| tc filter add dev "$dev" parent 1:0 prio 1 handle 2: protocol ip u32 divisor 256 | |
| tc filter add dev "$dev" parent 1:0 prio 1 protocol ip u32 ht 800:: \ | |
| match ip dst "${ip_local_byte1}"."${ip_local_byte2}".0.0/16 \ | |
| hashkey mask 0x000000ff at 16 link 2: | |
| modprobe ifb numifbs=1 | |
| ip link set dev ifb0 up | |
| tc qdisc add dev "$dev" handle ffff: ingress | |
| tc filter add dev "$dev" parent ffff: protocol ip u32 match u32 0 0 action mirred egress redirect dev ifb0 | |
| tc qdisc add dev ifb0 root handle 1: htb | |
| tc filter add dev ifb0 parent 1:0 prio 1 protocol ip u32 | |
| tc filter add dev ifb0 parent 1:0 prio 1 handle 3: protocol ip u32 divisor 256 | |
| tc filter add dev ifb0 parent 1:0 prio 1 protocol ip u32 ht 800:: \ | |
| match ip src "${ip_local_byte1}"."${ip_local_byte2}".0.0/16 \ | |
| hashkey mask 0x000000ff at 12 link 3: | |
| } | |
| stop_tc() { | |
| tc qdisc del dev "$dev" root | |
| tc qdisc del dev "$dev" handle ffff: ingress | |
| tc qdisc del dev ifb0 root | |
| ip link set dev ifb0 down | |
| rmmod ifb | |
| } | |
| function add_ip() { | |
| user=$1 | |
| ip=$2 | |
| create_identifiers $ip | |
| if [ "$user" == "admin" ]; then | |
| downrate=10mbit | |
| uprate=10mbit | |
| elif [ "$user" == "client" ]; then | |
| downrate=1200kbit | |
| uprate=1200kbit | |
| else | |
| echo "error: unknown user" | |
| exit 1 | |
| fi | |
| # Limit traffic from VPN server to client | |
| tc class add dev "$dev" parent 1: classid 1:"$classid" htb rate "$downrate" | |
| tc filter add dev "$dev" parent 1:0 protocol ip prio 1 \ | |
| handle 2:"${hash}":"${handle}" \ | |
| u32 ht 2:"${hash}": match ip dst "$ip"/32 flowid 1:"$classid" | |
| # Limit traffic from client to VPN server | |
| tc class add dev ifb0 parent 1: classid 1:"$classid" htb rate "$uprate" | |
| tc filter add dev ifb0 parent 1:0 protocol ip prio 1 \ | |
| handle 3:"${hash}":"${handle}" \ | |
| u32 ht 3:"${hash}": match ip src "$ip"/32 flowid 1:"$classid" | |
| } | |
| case "$1" in | |
| start) | |
| start_tc | |
| ;; | |
| stop) | |
| stop_tc | |
| ;; | |
| add) | |
| add_ip $2 $3 | |
| ;; | |
| *) | |
| echo "$0: unknown operation [$1]" >&2 | |
| exit 1 | |
| ;; | |
| esac | |
| exit 0 |
Author
rda0
commented
Feb 8, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment