Compare commits

...

6 Commits

Author SHA1 Message Date
Jakob Olsson
e9f6558f4e easy-qos: update script to use json over space seprator 2019-04-04 13:58:50 +02:00
Jakob Olsson
c642a8d3b2 easy-qos: update priority best to besteffort 2019-04-04 13:56:35 +02:00
Jakob Olsson
b37593d0e0 easy-qos: change config typo from list to option 2019-04-04 12:07:28 +02:00
vdutta
11d49945e6 Added easy_qos init script for ebtable rules 2019-04-03 10:11:26 +02:00
vdutta
5c9cc26758 Updated all in Protocol for easy_qos 2019-04-03 10:11:26 +02:00
vdutta
f76a298a89 Added easy_qos package for marking packets in mangle table 2019-04-03 10:11:26 +02:00
4 changed files with 400 additions and 0 deletions

31
easy-qos/Makefile Normal file
View File

@@ -0,0 +1,31 @@
include $(TOPDIR)/rules.mk
PKG_NAME:=easy-qos
PKG_VERSION:=1.0
PKG_RELEASE:=0
include $(INCLUDE_DIR)/package.mk
define Package/easy-qos
SECTION:=net
CATEGORY:=Network
TITLE:=Easy QoS
endef
define Package/easy-qos/description
This package contains Easy QoS utitie
endef
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./files/* $(PKG_BUILD_DIR)/
endef
define Build/Compile
endef
define Package/easy-qos/install
$(CP) ./files/* $(1)/
endef
$(eval $(call BuildPackage,easy-qos))

View File

@@ -0,0 +1,30 @@
config rule
option priority "high"
option macaddr "12:34:56:78:9a:bc"
list port 22
list port 53
option comment "SSH, DNS"
# -j MARK --set-xmark 0x7/0x7
config rule
option priority "medium"
option macaddr "08:00:27:db:2a:31"
option proto "tcp"
list port 21
list port 25
list port 80
option comment "FTP, SMTP, HTTP"
# -j MARK --set-xmark 0x5/0x5
config rule
option priority "normal"
option macaddr "1a:2b:3c:4d:5e:6f"
option comment "All Traffic"
# -j MARK --set-xmark 0x3/0x3
config rule
option priority "low"
option macaddr "6a:4b:2c:5d:1e:70"
option proto "icmp"
option comment "Ping"
# -j MARK --set-xmark 0x1/0x1

View File

@@ -0,0 +1,196 @@
#!/bin/sh /etc/rc.common
#
# Copyright (C) 2015 inteno.org
#
. /usr/share/libubox/jshn.sh
START=99
USE_PROCD=1
CLIENT_LIST="/tmp/easy_qos_client.list"
log() {
echo "${@}"|logger -t easy_qos -p debug
}
exec_log() {
${@}
if [ "${?}" -ne 0 ]; then
log "Failed to create ${@}";
fi
}
get_priority() {
local prio=$(echo $1|tr [A-Z] [a-z]);
case "${prio}" in
"lowest")
echo 0;;
"low")
echo 1;;
"besteffort")
echo 2;;
"normal")
echo 3;;
"video")
echo 4;;
"medium")
echo 5;;
"high")
echo 6;;
"highest")
echo 7;;
esac
}
clean_client_entries() {
[ -f ${CLIENT_LIST} ] && rm ${CLIENT_LIST}
}
map_client_entries() {
local clients ip mac host
json_load "$(ubus call router.network 'clients')"
json_get_keys keys
for key in ${keys};
do
json_select ${key}
json_get_vars ipaddr macaddr hostname
clients="${macaddr} ${ipaddr} ${hostname};${clients}"
json_select ..
done
json_init
json_add_array "clients"
IFS=";"
for client in ${clients};
do
json_add_object "NULL"
json_add_string "macaddr" "$(echo ${client} | cut -d" " -f1)"
json_add_string "ip" "$(echo ${client} | cut -d" " -f2)"
json_add_string "host" "$(echo ${client} | cut -d" " -f3)"
json_close_object
done
IFS=' '
echo `json_dump` > ${CLIENT_LIST}
json_cleanup
}
# Find the IP of a corresponding mac from arp table
get_ipaddress() {
local clients ip mac host
json_load "$(cat ${CLIENT_LIST})"
json_get_keys keys
json_select "clients"
local i="1"
while json_get_type type $i; do
json_get_var arrvar "$((i++))"
if [ "${1}" == "${macaddr}" ];
then
ip=${ipaddr}
break
fi
done
json_select ".."
json_cleanup
echo ${ip};
}
validate_rule_section()
{
uci_validate_section easy_qos rule "${1}" \
'priority:string' \
'macaddr:string' \
'proto:string:none' \
'port:list(uinteger)' \
'comment:string:none'
}
# Clear existing rules before applying new rules
clear_existing_rules() {
local rule=$(iptables -t mangle -S OUTPUT|grep -m 1 MARK |sed 's/-A/-D/1')
while [ -n "${rule}" ]; do
exec_log iptables -t mangle ${rule}
rule=$(iptables -t mangle -S OUTPUT|grep -m 1 MARK |sed 's/-A/-D/1')
done
}
check_and_create() {
iptables -t mangle -C OUTPUT ${@} 2>/dev/null
# Create rule if not exists
if [ ${?} -ne 0 ]; then
exec_log iptables -t mangle -A OUTPUT ${@}
else
log "Rule exists for ${@}"
fi
}
create_rule() {
local proto=$1; shift
local src_ip=$1; shift
local mark="0x$1/0x$1"; shift
local ports=$1;
local cmd="";
cmd="-j MARK --set-xmark ${mark}";
if [ -n "${ports}" ]; then
cmd="--match multiport --dports ${ports} ${cmd}";
fi
if [ "${proto}" == "icmp" ]; then
cmd="-p icmp -m icmp --icmp-type 8 $cmd"
elif [ "${proto}" == "all" ]; then
cmd="-p all $cmd"
else
cmd="-p ${proto} -m ${proto} $cmd"
fi
cmd="-s ${src_ip} $cmd"
check_and_create ${cmd}
}
manage_rule() {
local cfg="$1"
local priority macaddr proto port comment prio_num ip port_list
validate_rule_section "${1}" || {
log "Validation of section failed"
return 1;
}
prio_num=$(get_priority ${priority})
ip=$(get_ipaddress ${macaddr})
port_list=$(echo ${port}|sed 's/ /,/g')
if [ -n "${ip}" -a -n "${prio_num}" ]; then
if [ "${proto}" == "none" ]; then
create_rule tcp ${ip} ${prio_num} ${port_list}
create_rule udp ${ip} ${prio_num} ${port_list}
else
create_rule ${proto} ${ip} ${prio_num} ${port_list}
fi
fi
}
reload_service() {
clear_existing_rules
map_client_entries
config_load easy_qos
config_foreach manage_rule rule
clean_client_entries
}
start_service() {
reload_service
echo "Easy QoS installed">/dev/console;
}
service_triggers() {
procd_add_reload_trigger "easy_qos"
}

View File

@@ -0,0 +1,143 @@
#!/bin/sh /etc/rc.common
#
# Copyright (C) 2015 inteno.org
#
START=99
USE_PROCD=1
log() {
echo "${@}"|logger -t easy_qos.ebtable -p debug
}
exec_log() {
${@}
if [ "${?}" -ne 0 ]; then
log "Failed to create ${@}";
fi
}
get_priority() {
local prio=$(echo $1|tr [A-Z] [a-z]);
case "${prio}" in
"lowest")
echo 0;;
"low")
echo 1;;
"besteffort")
echo 2;;
"normal")
echo 3;;
"video")
echo 4;;
"medium")
echo 5;;
"high")
echo 6;;
"highest")
echo 7;;
esac
}
validate_rule_section()
{
uci_validate_section easy_qos rule "${1}" \
'priority:string' \
'macaddr:string' \
'proto:string:none' \
'port:list(uinteger)' \
'comment:string:none'
}
# Clear existing rules before applying new rules
clear_existing_rules() {
local rule=$(ebtables -t broute -L BROUTING|grep -m 1 mark)
while [ -n "${rule}" ]; do
exec_log ebtables -t broute -D BROUTING ${rule}
rule=$(ebtables -t broute -L BROUTING|grep -m 1 mark)
done
}
create_rule() {
local protocol=$1; shift
local mac=$1; shift
local mark="0x$1"; shift
local forward_port=$1;
local cmd="";
local protocol_number
cmd="-j mark --mark-or ${mark}";
if [ -n "${forward_port}" ]; then
cmd="--ip-destination-port ${forward_port} ${cmd}";
fi
case "${protocol}" in
"tcp")
protocol_number=6;;
"udp")
protocol_number=17;;
"dccp")
protocol_number=33;;
"sctp")
protocol_number=132;;
*)
log "Protocol ${protocol} not supported in ebtables"
return;;
esac
cmd="--ip-proto ${protocol_number} $cmd"
cmd="-p ip $cmd"
cmd="-s ${mac} $cmd"
exec_log ebtables -t broute -A BROUTING ${cmd}
}
manage_rule() {
local cfg="$1"
local priority macaddr proto port comment prio_num protocol
validate_rule_section "${1}" || {
log "Validation of section failed"
return 1;
}
protocol=$(echo ${proto}|tr [A-Z] [a-z])
prio_num=$(get_priority ${priority})
if [ -n "${macaddr}" -a -n "${prio_num}" ]; then
for p in ${port}; do
if [ "${protocol}" == "none" ]; then
create_rule tcp ${macaddr} ${prio_num} ${p}
create_rule udp ${macaddr} ${prio_num} ${p}
else
create_rule ${protocol} ${macaddr} ${prio_num} ${p}
fi
done
# Create rule for all ports if port is not mentioned in uci
if [ -z "${port}" ]; then
if [ "${protocol}" == "none" ]; then
create_rule tcp ${macaddr} ${prio_num}
create_rule udp ${macaddr} ${prio_num}
else
create_rule ${protocol} ${macaddr} ${prio_num}
fi
fi
fi
}
reload_service() {
# Do not apply rules if ebtables is not present in system
[ -x /usr/sbin/ebtables ] || return;
clear_existing_rules
config_load easy_qos
config_foreach manage_rule rule
}
start_service() {
reload_service
}
service_triggers() {
procd_add_reload_trigger "easy_qos"
}