You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.4 KiB

  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2018 rosysong@rosinson.com
  4. #
  5. # for uci_validate_section()
  6. . /lib/functions/procd.sh
  7. NFT_QOS_HAS_BRIDGE=
  8. NFT_QOS_INET_FAMILY=ip
  9. NFT_QOS_SCRIPT_TEXT=
  10. NFT_QOS_SCRIPT_FILE=/tmp/qos.nft
  11. qosdef_appendx() { # <string to be appended>
  12. NFT_QOS_SCRIPT_TEXT="$NFT_QOS_SCRIPT_TEXT""$1"
  13. }
  14. qosdef_append_chain_def() { # <type> <hook> <priority> <policy>
  15. qosdef_appendx "\t\ttype $1 hook $2 priority $3; policy $4;\n"
  16. }
  17. qosdef_append_chain_ingress() { # <type> <device> <priority> <policy>
  18. qosdef_appendx "\t\ttype $1 hook ingress device $2 priority $3; policy $4;\n"
  19. }
  20. # qosdef_append_rule_{MATCH}_{STATEMENT}
  21. qosdef_append_rule_ip_limit() { # <ipaddr> <operator> <unit> <rate>
  22. local ipaddr=$1
  23. local operator=$2
  24. local unit=$3
  25. local rate=$4
  26. qosdef_appendx \
  27. "\t\tip $operator $ipaddr limit rate over $rate $unit/second drop\n"
  28. }
  29. # qosdef_append_rule_{MATCH}_{STATEMENT}
  30. qosdef_append_rule_mac_limit() { # <macaddr> <operator> <unit> <rate>
  31. local macaddr=$1
  32. local operator=$2
  33. local unit=$3
  34. local rate=$4
  35. qosdef_appendx \
  36. "\t\tether $operator $macaddr limit rate over $rate $unit/second drop\n"
  37. }
  38. # qosdef_append_rule_{MATCH}_{POLICY}
  39. qosdef_append_rule_ip_policy() { # <operator> <ipaddr> <policy>
  40. qosdef_appendx "\t\tip $1 $2 $3\n"
  41. }
  42. _handle_limit_whitelist() { # <value> <chain>
  43. local ipaddr=$1
  44. local operator
  45. [ -z "$ipaddr" ] && return
  46. case "$2" in
  47. download) operator="daddr";;
  48. upload) operator="saddr";;
  49. esac
  50. qosdef_append_rule_ip_policy $operator $ipaddr accept
  51. }
  52. qosdef_append_rule_limit_whitelist() { # <chain>
  53. config_list_foreach default limit_whitelist _handle_limit_whitelist $1
  54. }
  55. qosdef_flush_table() { # <family> <table>
  56. nft flush table $1 $2 2>/dev/null
  57. }
  58. qosdef_remove_table() { # <family> <table>
  59. nft delete table $1 $2 2>/dev/null
  60. }
  61. qosdef_init_header() { # add header for nft script
  62. qosdef_appendx "#!/usr/sbin/nft -f\n"
  63. qosdef_appendx "# Copyright (C) 2018 rosysong@rosinson.com\n"
  64. qosdef_appendx "#\n\n"
  65. }
  66. qosdef_init_env() {
  67. # check interface type of lan
  68. local lt="$(uci_get "network.lan.type")"
  69. [ "$lt" = "bridge" ] && export NFT_QOS_HAS_BRIDGE="y"
  70. # check if ipv6 support
  71. [ -e /proc/sys/net/ipv6 ] && export NFT_QOS_INET_FAMILY="inet"
  72. }
  73. qosdef_clean_cache() {
  74. rm -f $NFT_QOS_SCRIPT_FILE
  75. }
  76. qosdef_init_done() {
  77. echo -e $NFT_QOS_SCRIPT_TEXT > $NFT_QOS_SCRIPT_FILE 2>/dev/null
  78. }
  79. qosdef_start() {
  80. nft -f $NFT_QOS_SCRIPT_FILE 2>/dev/null
  81. }