Advertisement
Guest User

Untitled

a guest
Jul 16th, 2017
582
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.67 KB | None | 0 0
  1. #!/bin/sh
  2. ##FreshJR_QOS v1.9 released 07/17/2017
  3. ##Script Tested on ASUS AC-68U, FW380.67_beta4, using Adaptive QOS with Manual Bandwidth Settings
  4. ##Script Changes Unidentified Packet QOS destination from Default Traffic Container (Category7) into user definable (in WebUI) Other Traffic Container
  5. ##Script Changes Minimum Guarenteed Bandwidth per QOS category from 128Kbit into user defined percentages upload and download.
  6. ##Script supports custom QOS rules, to create new rules use included existing rules as template
  7. ## Included custom rule moves any TCP/UDP traffic on ports 500 & 4500 into VOIP traffic Container. (Wifi Calling)
  8. ## Included custom rule moves any TCP/UDP traffic on ports 16384 - 16415 into VOIP traffic Container. (Facetime)
  9. ## Included, but disabled / commented out, custom rule templates. See comments next to rule for details
  10.  
  11. cru a FreshJR_QOS "0 0 * * * /jffs/scripts/FreshJR_QOS" ## schedules a daily check to see if modifcation is still persistant
  12.  
  13. if [ "$(nvram get qos_enable)" = "1" ] && [ "$(nvram get qos_type)" = "1" ] ; then
  14. logger "Adaptive QOS: Modification Script Started"
  15. sleep 30
  16.  
  17. if [ -e "/usr/sbin/realtc" ] ; then
  18. tc="realtc"
  19. else
  20. tc="tc"
  21. fi
  22.  
  23. #################### Variables Setup #####################
  24.  
  25. #Percent of download speed guaranteed per QOS catagory, change below as desired (sum should equal 100)
  26. NetControl_DownBandPercent=5 #This value can be adjust as desired
  27. VoIP_DownBandPercent=20 #This value can be adjust as desired
  28. Gaming_DownBandPercent=15 #This value can be adjust as desired
  29. Others_DownBandPercent=10 #This value can be adjust as desired #Note: New destination for all unidentified traffic per script default
  30. WebSurfing_DownBandPercent=10 #This value can be adjust as desired
  31. Video_DownBandPercent=30 #This value can be adjust as desired
  32. FileTransfer_DownBandPercent=5 #This value can be adjust as desired
  33. Default_DownBandPercent=5 #This value can be adjust as desired #Note: Original destination all for unidentified traffic, no traffic should flow here
  34.  
  35. #Percent of upload speed guaranteed per QOS catagory, change below as desired (sum should equal 100)
  36. NetControl_UpBandPercent=5 #This value can be adjust as desired
  37. VoIP_UpBandPercent=20 #This value can be adjust as desired
  38. Gaming_UpBandPercent=15 #This value can be adjust as desired
  39. Others_UpBandPercent=30 #This value can be adjust as desired #Note: New destination for all unidentified traffic per script default
  40. WebSurfing_UpBandPercent=10 #This value can be adjust as desired
  41. Video_UpBandPercent=10 #This value can be adjust as desired
  42. FileTransfer_UpBandPercent=5 #This value can be adjust as desired
  43. Default_UpBandPercent=5 #This value can be adjust as desired #Note: Original destination all for unidentified traffic, no traffic should flow here
  44.  
  45.  
  46. while read -r line; #reads all QOS rules, used to read user order of VOIP contianers
  47. do
  48. flowid="$( echo -n ${line} | sed -n -e 's/.*flowid //p' | tail -c 1)" #check if individual rule line output has valid flowID which would then correspond to an individual rules Traffic Container / Catagory / Flowid.
  49. if [ "${flowid}" != "" ] ; then #if valid flowID is found, read next line.
  50. read line
  51. mark="$(echo ${line} | sed -n -e 's/.*mark \([a-zA-z0-9]* [a-zA-z0-9]*\).*/\1/p')" #This line reads which individual QOS traffic rule / mark corresponds to the Traffic Container / Catagory / Flowid read in the previous line
  52.  
  53.  
  54. if [ "${mark}" = "0x80060000 0x803f0000" ] ; then #VOIP
  55. eval "Cat${flowid}DownBandPercent=${VoIP_DownBandPercent}"
  56. eval "Cat${flowid}UpBandPercent=${VoIP_UpBandPercent}"
  57. VOIP="1:1${flowid}" #Use this variable for custom QOS rule flowID (case sensitive)
  58. fi
  59.  
  60. if [ "${mark}" = "0x80080000 0x803f0000" ] ; then #Gaming
  61. eval "Cat${flowid}DownBandPercent=${Gaming_DownBandPercent}"
  62. eval "Cat${flowid}UpBandPercent=${Gaming_UpBandPercent}"
  63. Gaming="1:1${flowid}" #Use this variable for custom QOS rule flowID (case sensitive)
  64. fi
  65.  
  66. if [ "${mark}" = "0x800a0000 0x803f0000" ] ; then #Others
  67. eval "Cat${flowid}DownBandPercent=${Others_DownBandPercent}"
  68. eval "Cat${flowid}UpBandPercent=${Others_UpBandPercent}"
  69. Others="1:1${flowid}" #Use this variable for custom QOS rule flowID (case sensitive)
  70. fi
  71.  
  72. if [ "${mark}" = "0x800d0000 0x803f0000" ] ; then #Web Surfing
  73. eval "Cat${flowid}DownBandPercent=${WebSurfing_DownBandPercent}"
  74. eval "Cat${flowid}UpBandPercent=${WebSurfing_UpBandPercent}"
  75. Web="1:1${flowid}" #Use this variable for custom QOS rule flowID (case sensitive)
  76. fi
  77.  
  78. if [ "${mark}" = "0x80040000 0x803f0000" ] ; then #Streaming
  79. eval "Cat${flowid}DownBandPercent=${Video_DownBandPercent}"
  80. eval "Cat${flowid}UpBandPercent=${Video_UpBandPercent}"
  81. Streaming="1:1${flowid}" #Use this variable for custom QOS rule flowID (case sensitive)
  82. fi
  83.  
  84. if [ "${mark}" = "0x80030000 0x803f0000" ] ; then #Downloads
  85. eval "Cat${flowid}DownBandPercent=${FileTransfer_DownBandPercent}"
  86. eval "Cat${flowid}UpBandPercent=${FileTransfer_UpBandPercent}"
  87. Downloads="1:1${flowid}" #Use this variable for custom QOS rule flowID (case sensitive)
  88. fi
  89.  
  90. if [ "${mark}" = "0x80000000 0x8000ffff" ] ; then #Default (Unidentified traffic)
  91. Default="1:1${flowid}" #Use this variable for custom QOS rule flowID (case sensitive)
  92. fi
  93.  
  94. fi
  95. done <<EOF
  96. $(${tc} filter show dev br0 | grep -o "flowid.*" -A1 | sed '/^--$/d')
  97. EOF
  98.  
  99. Cat0DownBandPercent=${NetControl_DownBandPercent}
  100. Cat0UpBandPercent=${NetControl_UpBandPercent}
  101.  
  102. Cat7DownBandPercent=${Default_DownBandPercent}
  103. Cat7UpBandPercent=${Default_UpBandPercent}
  104.  
  105. ############################### Unidentified Traffic Priority & Custom Rules ##########################
  106.  
  107.  
  108. if [ "${Default}" = "1:17" ] ; then
  109. logger "Adaptive QOS: Changing container for Unidentified Traffic & Applying Custom Rules"
  110.  
  111. VOIP_mark="0x40060001" #Note these marks are same as filter match/mask combo but have a 1 at the end. That trailing one prevents the filters from being marked unidentified
  112. Gaming_mark="0x40080001"
  113. Others_mark="0x400a0001"
  114. Web_mark="0x400d0001"
  115. Streaming_mark="0x40040001"
  116. Downloads_mark="0x40030001"
  117. Default_mark="0x40000001"
  118.  
  119. ${tc} filter del dev br0 parent 1: prio 1
  120. ${tc} filter add dev br0 protocol all prio 1 u32 match ip dport 500 0xffff flowid ${VOIP} #Custom Rule 1 --INCOMMING TRAFFIC-- (All incomming traffic w/ LAN destination port 500 goes to "VOIP" Traffic Container) --Wifi Calling
  121. ${tc} filter add dev br0 protocol all prio 1 u32 match ip dport 4500 0xffff flowid ${VOIP} #Custom Rule 2 --INCOMMING TRAFFIC-- (All incomming traffic w/ LAN destination port 4500 goes to "VOIP" Traffic Container)--Wifi Calling
  122. ${tc} filter add dev br0 protocol all prio 1 u32 match ip dport 16384 0xffe0 flowid ${VOIP} #Custom Rule 3 --INCOMMING TRAFFIC-- (All incomming traffic w/ LAN destination ports 16384 - 16415 go to "VOIP" Traffic Container) --Facetime
  123. #${tc} filter add dev br0 protocol all prio 1 u32 match ip dst 192.168.1.100/32 flowid ${VOIP} #Template Rule 1 --INCOMMING TRAFFIC-- (All incomming traffic w/ LAN destination ip 192.168.1.100 goes to "VOIP" Traffic Container)
  124. #${tc} filter add dev br0 protocol all prio 1 u32 match u32 0xCCDDEEFF 0xffffffff at -16 flowid {VOIP} #Template Rule 2 --INCOMMING TRAFFIC-- (All incomming traffic w/ LAN destination MAC Address AA:BB:CC:DD:EE:FF goes to "VOIP" Traffic Container) **RULE USES LAST 8 MAC DIGITS
  125. #${tc} filter add dev br0 protocol all prio 1 u32 match ip src 75.75.75.75/32 flowid ${Streaming} #Template Rule 2 --INCOMMING TRAFFIC-- (All incomming traffic w/ WAN source ip 75.75.75.75 goes to "Streaming" Traffic Container) (/32 CIDR mask defines only one ip, if IP range is desired see SNB forum post for guide)
  126. #${tc} filter add dev br0 protocol all prio 1 u32 match ip dport 1234 0xffff flowid ${Downloads} #Template Rule 3 --INCOMMING TRAFFIC-- (All incomming traffic w/ LAN destination port 1234 goes to "Downloads" Traffic Container) (0xFFFF port mask defines only one port, if port range is desired see SNB forum post for guide)
  127. ${tc} filter add dev br0 protocol all prio 2 u32 match mark 0x80000000 0x8000ffff flowid ${Others} #DO NOT DELETE/DISABLE Reroute incomming unidentified traffic into "Others" traffic container which user adjustable in webUI, instead of default reouting pf traffoc into non adjustable "Default" traffic container
  128.  
  129. ${tc} filter del dev eth0 parent 1: prio 1
  130. ${tc} filter add dev eth0 protocol all prio 1 u32 match ip sport 500 0xffff flowid ${VOIP} #Custom Rule 1 --OUTGOING TRAFFIC-- (All outgoing traffic w/ LAN source port 500 goes to "VOIP" Traffic Container) --Wifi Calling
  131. ${tc} filter add dev eth0 protocol all prio 1 u32 match ip sport 4500 0xffff flowid ${VOIP} #Custom Rule 2 --OUTGOING TRAFFIC-- (All outgoing traffic w/ LAN source port 4500 goes to "VOIP" Traffic Container) --Wifi Calling
  132. ${tc} filter add dev eth0 protocol all prio 1 u32 match ip sport 16384 0xffe0 flowid ${VOIP} #Custom Rule 3 --OUTGOING TRAFFIC-- (All outgoing traffic w/ LAN source ports 16384 - 16415 go to "VOIP" Traffic Container) --Facetime
  133. #${tc} filter add dev eth0 protocol all prio 1 u32 match ip src 192.168.1.100/32 flowid ${VOIP} #Template Rule 1 -->NOT WORKING<-- (All outgoing traffic w/ LAN source ip 192.168.1.123 goes to "VOIP" Traffic Container) **Reason this does not work is because you can only apply filters to egress traffic. The egress source IP of outgoing traffic is your Public WAN IP, not your Local LAN IP. So filter will not match on local IP.
  134. #${tc} filter add dev eth0 protocol all prio 1 u32 match u16 0xEEFF 0xffff at -8 flowid {VOIP} #Template Rule 2 -->NOT WORKING<-- (All outgoing traffic w/ LAN source MAC Address AA:BB:CC:DD:EE:FF goes to "VOIP" Traffic Container) **RULE USES LAST 4 MAC DIGITS **Reason this does not work is because you can only apply filters to egress traffic. The egress source MAC ADDRESS of outgoing traffic is your Router MAC ADDRESS, not your client MAC ADDRESS. So filter will not match on local MAC ADDRESS.
  135. #${tc} filter add dev eth0 protocol all prio 1 u32 match ip dst 75.75.75.75/32 flowid ${Streaming} #Template Rule 3 --OUTGOING TRAFFIC-- (All outgoing traffic w/ WAN destination ip 75.75.75.75 goes to "Streaming" Traffic Container) (/32 CIDR mask defines only one ip, if IP range is desired see SNB forum post for guide)
  136. #${tc} filter add dev eth0 protocol all prio 1 u32 match ip sport 1234 0xffff flowid ${Downloads} #Template Rule 4 --OUTGOING TRAFFIC-- (All outgoing traffic w/ LAN source port 1234 go to "Downloads" Traffic Container) (0xFFFF port mask defines only one port, if port range is desired see SNB forum post for guide)
  137. #iptables -D POSTROUTING -t mangle -o eth0 -s 192.168.1.100/32 -j MARK --set-mark ${VOIP_mark} #Template Rule 1 WORKING ALTERNATIVE --OUTGOING TRAFFIC-- (Line1/2)
  138. #iptables -A POSTROUTING -t mangle -o eth0 -s 192.168.1.100/32 -j MARK --set-mark ${VOIP_mark} #Template Rule 1 WORKING ALTERNATIVE --OUTGOING TRAFFIC-- (Line1/2)
  139. #iptables -D POSTROUTING -t mangle -o eth0 -m mac --mac-source AA:BB:CC:DD:EE:FF -j MARK --set-mark ${VOIP_mark} #Template Rule 2 WORKING ALTERNATIVE --OUTGOING TRAFFIC-- (Line1/2)
  140. #iptables -A POSTROUTING -t mangle -o eth0 -m mac --mac-source AA:BB:CC:DD:EE:FF -j MARK --set-mark ${VOIP_mark} #Template Rule 2 WORKING ALTERNATIVE --OUTGOING TRAFFIC-- (Line1/2)
  141. ${tc} filter add dev eth0 protocol all prio 2 u32 match mark 0x40000000 0x4000ffff flowid ${Others} #DO NOT DELETE/DISABLE Reroute outgoing unidentified traffic into "Others" traffic container which user adjustable in webUI, instead of default reouting pf traffoc into non adjustable "Default" traffic container
  142. else
  143. logger "Adaptive QOS: No change required for Unidentified Traffic Container or Custom Rules"
  144. fi
  145.  
  146. ######################## Minimum Alotted Bandwidth Per QOS Catagory ##########################
  147.  
  148.  
  149. DownCeil="$(printf "%.0f" $(nvram get qos_ibw))" #Maximum download rate defined in WebUI
  150. UpCeil="$(printf "%.0f" $(nvram get qos_obw))"
  151.  
  152. DownRate0="$(expr ${DownCeil} \* ${Cat0DownBandPercent} / 100)"Kbit #New rates that correspond to user defined percentages above
  153. DownRate1="$(expr ${DownCeil} \* ${Cat1DownBandPercent} / 100)"Kbit
  154. DownRate2="$(expr ${DownCeil} \* ${Cat2DownBandPercent} / 100)"Kbit
  155. DownRate3="$(expr ${DownCeil} \* ${Cat3DownBandPercent} / 100)"Kbit
  156. DownRate4="$(expr ${DownCeil} \* ${Cat4DownBandPercent} / 100)"Kbit
  157. DownRate5="$(expr ${DownCeil} \* ${Cat5DownBandPercent} / 100)"Kbit
  158. DownRate6="$(expr ${DownCeil} \* ${Cat6DownBandPercent} / 100)"Kbit
  159. DownRate7="$(expr ${DownCeil} \* ${Cat7DownBandPercent} / 100)"Kbit
  160.  
  161. UpRate0="$(expr ${UpCeil} \* ${Cat0UpBandPercent} / 100)"Kbit
  162. UpRate1="$(expr ${UpCeil} \* ${Cat1UpBandPercent} / 100)"Kbit
  163. UpRate2="$(expr ${UpCeil} \* ${Cat2UpBandPercent} / 100)"Kbit
  164. UpRate3="$(expr ${UpCeil} \* ${Cat3UpBandPercent} / 100)"Kbit
  165. UpRate4="$(expr ${UpCeil} \* ${Cat4UpBandPercent} / 100)"Kbit
  166. UpRate5="$(expr ${UpCeil} \* ${Cat5UpBandPercent} / 100)"Kbit
  167. UpRate6="$(expr ${UpCeil} \* ${Cat6UpBandPercent} / 100)"Kbit
  168. UpRate7="$(expr ${UpCeil} \* ${Cat7UpBandPercent} / 100)"Kbit
  169.  
  170.  
  171. CurrentDownRate1="$(${tc} class show dev br0 | grep -w "1:11" | tr ' ' '\n' | grep "rate" -A1 | tail -n 1)"
  172. if [ "${CurrentDownRate1}" != "${DownRate1}" ] ; then
  173. logger "Adaptive QOS: Changing minimum alloted bandwidth per QOS category to user defined percentages"
  174. ${tc} class change dev br0 parent 1:1 classid 1:10 htb prio 0 rate ${DownRate0} ceil ${DownCeil}Kbit
  175. ${tc} class change dev br0 parent 1:1 classid 1:11 htb prio 1 rate ${DownRate1} ceil ${DownCeil}Kbit
  176. ${tc} class change dev br0 parent 1:1 classid 1:12 htb prio 2 rate ${DownRate2} ceil ${DownCeil}Kbit
  177. ${tc} class change dev br0 parent 1:1 classid 1:13 htb prio 3 rate ${DownRate3} ceil ${DownCeil}Kbit
  178. ${tc} class change dev br0 parent 1:1 classid 1:14 htb prio 4 rate ${DownRate4} ceil ${DownCeil}Kbit
  179. ${tc} class change dev br0 parent 1:1 classid 1:15 htb prio 5 rate ${DownRate5} ceil ${DownCeil}Kbit
  180. ${tc} class change dev br0 parent 1:1 classid 1:16 htb prio 6 rate ${DownRate6} ceil ${DownCeil}Kbit
  181. ${tc} class change dev br0 parent 1:1 classid 1:17 htb prio 7 rate ${DownRate7} ceil ${DownCeil}Kbit
  182. ${tc} class change dev eth0 parent 1:1 classid 1:10 htb prio 0 rate ${UpRate0} ceil ${UpCeil}Kbit
  183. ${tc} class change dev eth0 parent 1:1 classid 1:11 htb prio 1 rate ${UpRate1} ceil ${UpCeil}Kbit
  184. ${tc} class change dev eth0 parent 1:1 classid 1:12 htb prio 2 rate ${UpRate2} ceil ${UpCeil}Kbit
  185. ${tc} class change dev eth0 parent 1:1 classid 1:13 htb prio 3 rate ${UpRate3} ceil ${UpCeil}Kbit
  186. ${tc} class change dev eth0 parent 1:1 classid 1:14 htb prio 4 rate ${UpRate4} ceil ${UpCeil}Kbit
  187. ${tc} class change dev eth0 parent 1:1 classid 1:15 htb prio 5 rate ${UpRate5} ceil ${UpCeil}Kbit
  188. ${tc} class change dev eth0 parent 1:1 classid 1:16 htb prio 6 rate ${UpRate6} ceil ${UpCeil}Kbit
  189. ${tc} class change dev eth0 parent 1:1 classid 1:17 htb prio 7 rate ${UpRate7} ceil ${UpCeil}Kbit
  190. else
  191. logger "Adaptive QOS: No change required for QOS category bandwidth percentages"
  192. fi
  193. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement