Traffic shaping per IP based on cumulative time per day

Quick update... I have managed to achieve what I set out in the OP - yay. I now have the following:

  • SQLite database containing users, devices (of a user), schedule (per user), scheduleActiveTime (per schedule), config and logs
  • bash script that runs via cron (ever minute) that determines if a user is using the internet on one of their devices and logs appropriately

For completeness, the working code I have is:

function set_tcf() {
	TCF="${TC} filter add dev ${1} parent 1: protocol ip prio 5 u32 match u16 0x0800 0xFFFF at -2"
}

function create_chains() {
	DEV=$1
	
	if [[ `${TC} qdisc show dev ${DEV} | grep "qdisc htb 1" | wc -l` -eq 0 ]]; then
		set_tcf $DEV
		${TC} qdisc add dev ${DEV} root       handle 1:    htb default 0xA
		${TC} class add dev ${DEV} parent 1:  classid 1:1  htb rate ${MAX_BW}
		${TC} class add dev ${DEV} parent 1:1 classid 1:10 htb rate ${MAX_BW}
	 
		${TC} class add dev ${DEV} parent 1:1 classid 1:20 htb rate ${DNLD}
	 
		#${TCF} flowid 1:10
	fi
}

function filter_mac() {
	DEV=$3
	M0=$(echo $1 | cut -d : -f 1)$(echo $1 | cut -d : -f 2)
	M1=$(echo $1 | cut -d : -f 3)$(echo $1 | cut -d : -f 4)
	M2=$(echo $1 | cut -d : -f 5)$(echo $1 | cut -d : -f 6)
	
	if [[ `${TC} filter show dev ${DEV} | grep -i "${M0}${M1}/ffffffff at -8" | wc -l` -eq 0 ]]; then
		set_tcf $DEV
		${TCF} match u16 0x${M2} 0xFFFF at -4 match u32 0x${M0}${M1} 0xFFFFFFFF at -8 flowid $2
		${TCF} match u32 0x${M1}${M2} 0xFFFFFFFF at -12 match u16 0x${M0} 0xFFFF at -14 flowid $2
	fi
}

-----
for IF in ${INTERFACES}; do
	for MAC in ${MACS}; do
		create_chains ${IF}
		filter_mac ${MAC} "1:20" ${IF}
	done
done

Now I need to create a frontend UI.

Thanks all for your input :slight_smile:

2 Likes