Advertisement
Guest User

Untitled

a guest
Sep 8th, 2017
1,366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 17.61 KB | None | 0 0
  1. #!/bin/sh
  2. ##FreshJR_QOS v1.92 released 09/07/2017
  3. ##Script Tested on ASUS AC-68U, FW380.67, 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 rules copy applicable rule templates below, change filter parameters as desried, and then paste into custom rule start area.
  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.  
  10. #---------------------------------------------------------------------------------------------------------------
  11. #   VALID FLOW ID'S FOR CUSTOM RULES
  12. #    ${VOIP}, ${Gaming}, ${Others}, ${Web}, ${Streaming}, ${Downloads}, ${Default}
  13. #
  14. #   VALID MARKS FOR IPTABLES
  15. #    ${VOIP_mark}, ${Gaming_mark}, ${Others_mark}, ${Web_mark}, ${Streaming_mark}, ${Downloads_mark}, ${Default_mark}
  16. #
  17. #   DOWNLOAD/INCOMMING TRAFFIC rule templates.  See comments next to rule for details
  18. #    ${tc} filter add dev br0 protocol all prio 1 u32 match ip dport 1234 0xffff flowid ${Downloads}            #Template Rule 1 (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)
  19. #    ${tc} filter add dev br0 protocol all prio 1 u32 match ip dst 192.168.1.100/32 flowid ${VOIP}              #Template Rule 2 (All incomming traffic w/ LAN destination ip 192.168.1.100                 goes to "VOIP" Traffic Container)                                          
  20. #    ${tc} filter add dev br0 protocol all prio 1 u32 match u32 0xCCDDEEFF 0xffffffff at -16 flowid {VOIP}      #Template Rule 3 (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
  21. #    ${tc} filter add dev br0 protocol all prio 1 u32 match ip src 75.75.75.75/32 flowid ${Streaming}           #Template Rule 4 (All incomming traffic w/ WAN server 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)                                                                               
  22. #
  23. #   UPLOAD/OUTOING TRAFFIC rule templates.      See comments next to rule for details
  24. #    ${tc} filter add dev eth0 protocol all prio 1 u32 match ip sport 1234 0xffff flowid ${Downloads}           #Template Rule 1                                              (All outgoing traffic w/ LAN source 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)
  25. #    ${tc} filter add dev eth0 protocol all prio 1 u32 match ip src 192.168.1.100/32 flowid ${VOIP}             #Template Rule 2 -->NOT WORKING/USE IPTABLES ALTERNATIVE<--   (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.                                
  26. #    ${tc} filter add dev eth0 protocol all prio 1 u32 match u16 0xEEFF 0xffff  at -8 flowid {VOIP}             #Template Rule 3 -->NOT WORKING/USE IPTABLES ALTERNATIVE<--   (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.   
  27. #    ${tc} filter add dev eth0 protocol all prio 1 u32 match ip dst 75.75.75.75/32 flowid ${Streaming}          #Template Rule 4                                              (All outgoing traffic w/ WAN server 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)                                                                                                       
  28. #  
  29. #    iptables -D POSTROUTING -t mangle -o eth0 -s 192.168.1.100/32 -j MARK --set-mark ${VOIP_mark}                      #Template Rule 2 WORKING ALTERNATIVE (Line1/2)
  30. #    iptables -A POSTROUTING -t mangle -o eth0 -s 192.168.1.100/32 -j MARK --set-mark ${VOIP_mark}                      #Template Rule 2 WORKING ALTERNATIVE (Line1/2)
  31. #    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 3 WORKING ALTERNATIVE (Line1/2)
  32. #    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 3 WORKING ALTERNATIVE (Line1/2)
  33. #---------------------------------------------------------------------------------------------------------------       
  34.  
  35. cru a FreshJR_QOS "0 0 * * * /jffs/scripts/FreshJR_QOS"     ## schedules a daily check to see if modifcation is still persistant
  36.  
  37. if [ "$(nvram get qos_enable)" = "1" ] && [ "$(nvram get qos_type)" = "1" ] ; then
  38.     logger "Adaptive QOS: Modification Script Started"
  39.     sleep 30     
  40.  
  41.     if [ -e "/usr/sbin/realtc" ] ; then
  42.         tc="realtc"
  43.     else
  44.         tc="tc"
  45.     fi
  46.    
  47.     ####################  Variables Setup #####################
  48.  
  49.     #Percent of download speed guaranteed per QOS catagory, change below as desired (sum should equal 100)
  50.         NetControl_DownBandPercent=5                    #This value can be adjust as desired
  51.         VoIP_DownBandPercent=20                         #This value can be adjust as desired
  52.         Gaming_DownBandPercent=15                       #This value can be adjust as desired
  53.         Others_DownBandPercent=10                       #This value can be adjust as desired        #Note: New destination for all unidentified traffic per script default
  54.         WebSurfing_DownBandPercent=10                   #This value can be adjust as desired
  55.         Video_DownBandPercent=30                        #This value can be adjust as desired
  56.         FileTransfer_DownBandPercent=5                  #This value can be adjust as desired
  57.         Default_DownBandPercent=5                       #This value can be adjust as desired        #Note: Original destination all for unidentified traffic, no traffic should flow here
  58.    
  59.     #Percent of upload speed guaranteed per QOS catagory, change below as desired (sum should equal 100)
  60.         NetControl_UpBandPercent=5                      #This value can be adjust as desired
  61.         VoIP_UpBandPercent=20                           #This value can be adjust as desired
  62.         Gaming_UpBandPercent=15                         #This value can be adjust as desired
  63.         Others_UpBandPercent=30                         #This value can be adjust as desired        #Note: New destination for all unidentified traffic per script default
  64.         WebSurfing_UpBandPercent=10                     #This value can be adjust as desired
  65.         Video_UpBandPercent=10                          #This value can be adjust as desired
  66.         FileTransfer_UpBandPercent=5                    #This value can be adjust as desired
  67.         Default_UpBandPercent=5                         #This value can be adjust as desired        #Note: Original destination all for unidentified traffic, no traffic should flow here
  68.    
  69.    
  70.     while read -r line;                                                                         #reads all QOS rules, used to read user order of VOIP contianers
  71.     do
  72.             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.
  73.             if [ "${flowid}" != "" ] ; then                                                     #if valid flowID is found, read next line.  
  74.                     read line                                                                  
  75.                     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
  76.                                            
  77.  
  78.                     if [ "${mark}" = "0x80060000 0x803f0000" ] ; then                       #VOIP
  79.                         eval "Cat${flowid}DownBandPercent=${VoIP_DownBandPercent}"
  80.                         eval "Cat${flowid}UpBandPercent=${VoIP_UpBandPercent}"
  81.                         VOIP="1:1${flowid}"                                         #Use this variable for custom QOS rule flowID (case sensitive)
  82.                     fi
  83.                    
  84.                     if [ "${mark}" = "0x80080000 0x803f0000" ] ; then                       #Gaming
  85.                         eval "Cat${flowid}DownBandPercent=${Gaming_DownBandPercent}"
  86.                         eval "Cat${flowid}UpBandPercent=${Gaming_UpBandPercent}"
  87.                         Gaming="1:1${flowid}"                                       #Use this variable for custom QOS rule flowID (case sensitive)
  88.                     fi
  89.                    
  90.                     if [ "${mark}" = "0x800a0000 0x803f0000" ] ; then                       #Others
  91.                         eval "Cat${flowid}DownBandPercent=${Others_DownBandPercent}"
  92.                         eval "Cat${flowid}UpBandPercent=${Others_UpBandPercent}"
  93.                         Others="1:1${flowid}"                                       #Use this variable for custom QOS rule flowID (case sensitive)
  94.                     fi
  95.                    
  96.                     if [ "${mark}" = "0x800d0000 0x803f0000" ] ; then                       #Web Surfing
  97.                         eval "Cat${flowid}DownBandPercent=${WebSurfing_DownBandPercent}"
  98.                         eval "Cat${flowid}UpBandPercent=${WebSurfing_UpBandPercent}"
  99.                         Web="1:1${flowid}"                                          #Use this variable for custom QOS rule flowID (case sensitive)
  100.                     fi
  101.                    
  102.                     if [ "${mark}" = "0x80040000 0x803f0000" ] ; then                       #Streaming
  103.                         eval "Cat${flowid}DownBandPercent=${Video_DownBandPercent}"
  104.                         eval "Cat${flowid}UpBandPercent=${Video_UpBandPercent}"
  105.                         Streaming="1:1${flowid}"                                    #Use this variable for custom QOS rule flowID (case sensitive)
  106.                     fi
  107.                    
  108.                     if [ "${mark}" = "0x80030000 0x803f0000" ] ; then                       #Downloads
  109.                         eval "Cat${flowid}DownBandPercent=${FileTransfer_DownBandPercent}"
  110.                         eval "Cat${flowid}UpBandPercent=${FileTransfer_UpBandPercent}"
  111.                         Downloads="1:1${flowid}"                                    #Use this variable for custom QOS rule flowID (case sensitive)
  112.                     fi
  113.                    
  114.                     if [ "${mark}" = "0x80000000 0x8000ffff" ] ; then                       #Default (Unidentified traffic)
  115.                         Default="1:1${flowid}"                                      #Use this variable for custom QOS rule flowID (case sensitive)
  116.                     fi
  117.                        
  118.             fi
  119.     done <<EOF
  120.         $(${tc} filter show dev br0 | grep -o "flowid.*" -A1 | sed '/^--$/d')
  121. EOF
  122.    
  123.     Cat0DownBandPercent=${NetControl_DownBandPercent}
  124.     Cat0UpBandPercent=${NetControl_UpBandPercent}
  125.  
  126.     Cat7DownBandPercent=${Default_DownBandPercent}
  127.     Cat7UpBandPercent=${Default_UpBandPercent}
  128.    
  129.     ############################### Unidentified Traffic Priority & Custom Rules ##########################
  130.    
  131.      
  132.     if [ "${Default}" = "1:17" ] ; then
  133.         logger "Adaptive QOS: Changing container for Unidentified Traffic & Applying Custom Rules"
  134.  
  135.         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 caught by unidentified mask
  136.         Gaming_mark="0x40080001"
  137.         Others_mark="0x400a0001"
  138.         Web_mark="0x400d0001"
  139.         Streaming_mark="0x40040001"
  140.         Downloads_mark="0x40030001"
  141.         Default_mark="0x40000001"
  142.  
  143.         ####################  Custom Rules Setup #####################
  144.        
  145.         ${tc} filter del dev br0 parent 1: prio 1                                                                   #Deletes rule routing unidentified traffic into "Default" traffic container
  146.         ${tc} filter add dev br0 protocol all prio 2 u32 match mark 0x80000000 0x8000ffff flowid ${Others}          #Creates rule routing unidentified traffic into "Others" traffic container which user adjustable in webUI, instead of default reouting pf traffoc into non adjustable "Default" traffic container      
  147.             ##DOWNLOAD (INCOMMING TRAFFIC) CUSTOM RULES START HERE
  148.             ${tc} filter add dev br0 protocol all prio 1 u32 match ip dport 500 0xffff flowid ${VOIP}               #Wifi Calling   (All incomming traffic w/ LAN destination port 500              goes to "VOIP" Traffic Container)                              
  149.             ${tc} filter add dev br0 protocol all prio 1 u32 match ip dport 4500 0xffff flowid ${VOIP}              #Wifi Calling   (All incomming traffic w/ LAN destination port 4500             goes to "VOIP" Traffic Container)
  150.             ${tc} filter add dev br0 protocol all prio 1 u32 match ip dport 16384 0xffe0 flowid ${VOIP}             #Facetime       (All incomming traffic w/ LAN destination ports 16384 - 16415   goes to "VOIP" Traffic Container)
  151.             ${tc} filter add dev br0 protocol all prio 15 u32 match mark 0x80130000 0x803f0000 flowid ${Web}        #https traffic  (rule missing from ASUS default rule set)
  152.  
  153.            
  154.         ${tc} filter del dev eth0 parent 1: prio 1                                                                  #Deletes rule routing unidentified traffic into "Default" traffic container
  155.         ${tc} filter add dev eth0 protocol all prio 2 u32 match mark 0x40000000 0x4000ffff flowid ${Others}         #Creates rule routing unidentified traffic into "Others" traffic container which user adjustable in webUI, instead of default reouting pf traffoc into non adjustable "Default" traffic container      
  156.             ##UPLOAD (OUTGOING TRAFFIC) CUSTOM RULES START HERE                                            
  157.             ${tc} filter add dev eth0 protocol all prio 1 u32 match ip sport 500 0xffff flowid ${VOIP}              #Wifi Calling   (All outgoing traffic w/ LAN source port 500                goes to "VOIP" Traffic Container)                                      
  158.             ${tc} filter add dev eth0 protocol all prio 1 u32 match ip sport 4500 0xffff flowid ${VOIP}             #Wifi Calling   (All outgoing traffic w/ LAN source port 4500               goes to "VOIP" Traffic Container)
  159.             ${tc} filter add dev eth0 protocol all prio 1 u32 match ip sport 16384 0xffe0 flowid ${VOIP}            #Facetime       (All outgoing traffic w/ LAN source ports 16384 - 16415     goes to "VOIP" Traffic Container)
  160.             ${tc} filter add dev eth0 protocol all prio 15 u32 match mark 0x40130000 0x403f0000 flowid ${Web}       #https traffic  (rule missing from ASUS default rule set)
  161.     else
  162.         logger "Adaptive QOS: No change required for Unidentified Traffic Container or Custom Rules"
  163.     fi
  164.      
  165.     ######################## Minimum Alotted Bandwidth Per QOS Catagory ##########################
  166.    
  167.    
  168.     DownCeil="$(printf "%.0f" $(nvram get qos_ibw))"                                                            #Maximum download rate defined in WebUI
  169.     UpCeil="$(printf "%.0f" $(nvram get qos_obw))"
  170.    
  171.     DownRate0="$(expr ${DownCeil} \* ${Cat0DownBandPercent} / 100)"                                         #New rates that correspond to user defined percentages above
  172.     DownRate1="$(expr ${DownCeil} \* ${Cat1DownBandPercent} / 100)"
  173.     DownRate2="$(expr ${DownCeil} \* ${Cat2DownBandPercent} / 100)"
  174.     DownRate3="$(expr ${DownCeil} \* ${Cat3DownBandPercent} / 100)"
  175.     DownRate4="$(expr ${DownCeil} \* ${Cat4DownBandPercent} / 100)"
  176.     DownRate5="$(expr ${DownCeil} \* ${Cat5DownBandPercent} / 100)"
  177.     DownRate6="$(expr ${DownCeil} \* ${Cat6DownBandPercent} / 100)"
  178.     DownRate7="$(expr ${DownCeil} \* ${Cat7DownBandPercent} / 100)"
  179.  
  180.     UpRate0="$(expr ${UpCeil} \* ${Cat0UpBandPercent} / 100)"
  181.     UpRate1="$(expr ${UpCeil} \* ${Cat1UpBandPercent} / 100)"
  182.     UpRate2="$(expr ${UpCeil} \* ${Cat2UpBandPercent} / 100)"
  183.     UpRate3="$(expr ${UpCeil} \* ${Cat3UpBandPercent} / 100)"
  184.     UpRate4="$(expr ${UpCeil} \* ${Cat4UpBandPercent} / 100)"
  185.     UpRate5="$(expr ${UpCeil} \* ${Cat5UpBandPercent} / 100)"
  186.     UpRate6="$(expr ${UpCeil} \* ${Cat6UpBandPercent} / 100)"
  187.     UpRate7="$(expr ${UpCeil} \* ${Cat7UpBandPercent} / 100)"
  188.      
  189.      
  190.     CurrentDownRate1="$(${tc} class show dev br0 | grep -w "1:11" | tr ' ' '\n' | grep "rate" -A1 | tail -n 1)"
  191.     if [ "${CurrentDownRate1}" != "${DownRate1}" ] ; then
  192.         logger "Adaptive QOS: Changing minimum alloted bandwidth per QOS category to user defined percentages"
  193.  
  194.         DownBurst="$(tc class show dev br0 | grep -w "1:10" | tr ' ' '\n' | grep -w "burst" -A1 | tail -n1)"
  195.         DownCburst="$(tc class show dev br0 | grep -w "1:10" | tr ' ' '\n' | grep -w "cburst" -A1 | tail -n1)"
  196.        
  197.         UpBurst="$(tc class show dev eth0 | grep -w "1:10" | tr ' ' '\n' | grep -w "burst" -A1 | tail -n1)"
  198.         UpCburst="$(tc class show dev eth0 | grep -w "1:10" | tr ' ' '\n' | grep -w "cburst" -A1 | tail -n1)"
  199.        
  200.         tc class change dev br0 parent 1:1 classid 1:10 htb prio 0 rate ${DownRate0}Kbit ceil ${DownCeil}Kbit burst ${DownBurst} cburst ${DownCburst}
  201.         tc class change dev br0 parent 1:1 classid 1:11 htb prio 1 rate ${DownRate1}Kbit ceil ${DownCeil}Kbit burst ${DownBurst} cburst ${DownCburst}
  202.         tc class change dev br0 parent 1:1 classid 1:12 htb prio 2 rate ${DownRate2}Kbit ceil ${DownCeil}Kbit burst ${DownBurst} cburst ${DownCburst}
  203.         tc class change dev br0 parent 1:1 classid 1:13 htb prio 3 rate ${DownRate3}Kbit ceil ${DownCeil}Kbit burst ${DownBurst} cburst ${DownCburst}
  204.         tc class change dev br0 parent 1:1 classid 1:14 htb prio 4 rate ${DownRate4}Kbit ceil ${DownCeil}Kbit burst ${DownBurst} cburst ${DownCburst}
  205.         tc class change dev br0 parent 1:1 classid 1:15 htb prio 5 rate ${DownRate5}Kbit ceil ${DownCeil}Kbit burst ${DownBurst} cburst ${DownCburst}
  206.         tc class change dev br0 parent 1:1 classid 1:16 htb prio 6 rate ${DownRate6}Kbit ceil ${DownCeil}Kbit burst ${DownBurst} cburst ${DownCburst}
  207.         tc class change dev br0 parent 1:1 classid 1:17 htb prio 7 rate ${DownRate7}Kbit ceil ${DownCeil}Kbit burst ${DownBurst} cburst ${DownCburst}
  208.  
  209.         tc class change dev eth0 parent 1:1 classid 1:10 htb prio 0 rate ${UpRate0}Kbit ceil ${UpCeil}Kbit burst ${UpBurst} cburst ${UpCburst}
  210.         tc class change dev eth0 parent 1:1 classid 1:11 htb prio 1 rate ${UpRate1}Kbit ceil ${UpCeil}Kbit burst ${UpBurst} cburst ${UpCburst}
  211.         tc class change dev eth0 parent 1:1 classid 1:12 htb prio 2 rate ${UpRate2}Kbit ceil ${UpCeil}Kbit burst ${UpBurst} cburst ${UpCburst}
  212.         tc class change dev eth0 parent 1:1 classid 1:13 htb prio 3 rate ${UpRate3}Kbit ceil ${UpCeil}Kbit burst ${UpBurst} cburst ${UpCburst}
  213.         tc class change dev eth0 parent 1:1 classid 1:14 htb prio 4 rate ${UpRate4}Kbit ceil ${UpCeil}Kbit burst ${UpBurst} cburst ${UpCburst}
  214.         tc class change dev eth0 parent 1:1 classid 1:15 htb prio 5 rate ${UpRate5}Kbit ceil ${UpCeil}Kbit burst ${UpBurst} cburst ${UpCburst}
  215.         tc class change dev eth0 parent 1:1 classid 1:16 htb prio 6 rate ${UpRate6}Kbit ceil ${UpCeil}Kbit burst ${UpBurst} cburst ${UpCburst}
  216.         tc class change dev eth0 parent 1:1 classid 1:17 htb prio 7 rate ${UpRate7}Kbit ceil ${UpCeil}Kbit burst ${UpBurst} cburst ${UpCburst}
  217.     else
  218.         logger "Adaptive QOS: No change required for QOS category bandwidth percentages"
  219.     fi
  220. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement