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.

93 lines
2.1 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}_{POLICY}
  30. qosdef_append_rule_ip_policy() { # <operator> <ipaddr> <policy>
  31. qosdef_appendx "\t\tip $1 $2 $3\n"
  32. }
  33. _handle_limit_whitelist() { # <value> <chain>
  34. local ipaddr=$1
  35. local operator
  36. [ -z "$ipaddr" ] && return
  37. case "$2" in
  38. download) operator="daddr";;
  39. upload) operator="saddr";;
  40. esac
  41. qosdef_append_rule_ip_policy $operator $ipaddr accept
  42. }
  43. qosdef_append_rule_limit_whitelist() { # <chain>
  44. config_list_foreach default limit_whitelist _handle_limit_whitelist $1
  45. }
  46. qosdef_flush_table() { # <family> <table>
  47. nft flush table $1 $2 2>/dev/null
  48. }
  49. qosdef_remove_table() { # <family> <table>
  50. nft delete table $1 $2 2>/dev/null
  51. }
  52. qosdef_init_header() { # add header for nft script
  53. qosdef_appendx "#!/usr/sbin/nft -f\n"
  54. qosdef_appendx "# Copyright (C) 2018 rosysong@rosinson.com\n"
  55. qosdef_appendx "#\n\n"
  56. }
  57. qosdef_init_env() {
  58. # check interface type of lan
  59. local lt="$(uci_get "network.lan.type")"
  60. [ "$lt" = "bridge" ] && export NFT_QOS_HAS_BRIDGE="y"
  61. # check if ipv6 support
  62. [ -e /proc/sys/net/ipv6 ] && export NFT_QOS_INET_FAMILY="inet"
  63. }
  64. qosdef_clean_cache() {
  65. rm -f $NFT_QOS_SCRIPT_FILE
  66. }
  67. qosdef_init_done() {
  68. echo -e $NFT_QOS_SCRIPT_TEXT > $NFT_QOS_SCRIPT_FILE 2>/dev/null
  69. }
  70. qosdef_start() {
  71. nft -f $NFT_QOS_SCRIPT_FILE 2>/dev/null
  72. }