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.

79 lines
1.7 KiB

  1. #!/bin/sh
  2. # based on static.sh
  3. # Copyright (C) 2020 Tong Zhang<ztong0001@gmail.com>
  4. #
  5. . /lib/nft-qos/core.sh
  6. qosdef_validate_mac() {
  7. uci_load_validate nft-qos default "$1" "$2" \
  8. 'limit_mac_enable:bool:0'
  9. }
  10. # append rule for mac qos
  11. qosdef_append_rule_mac() { # <section> <operator>
  12. local macaddr unit rate
  13. local operator=$2
  14. config_get macaddr $1 macaddr
  15. if [ "$operator" = "saddr" ]; then
  16. config_get unit $1 urunit
  17. config_get rate $1 urate
  18. else
  19. config_get unit $1 drunit
  20. config_get rate $1 drate
  21. fi
  22. [ -z "$macaddr" ] && return
  23. qosdef_append_rule_mac_limit $macaddr $operator $unit $rate
  24. }
  25. # append chain for mac qos
  26. qosdef_append_chain_mac() { # <hook> <name> <section>
  27. local hook=$1 name=$2
  28. local config=$3 operator
  29. case "$name" in
  30. download) operator="daddr";;
  31. upload) operator="saddr";;
  32. esac
  33. qosdef_appendx "\tchain $name {\n"
  34. qosdef_append_chain_def filter $hook 0 accept
  35. config_foreach qosdef_append_rule_mac $config $operator
  36. qosdef_appendx "\t}\n"
  37. }
  38. qosdef_flush_mac() {
  39. if [ -n "$NFT_QOS_HAS_BRIDGE" ]; then
  40. qosdef_flush_table bridge nft-qos-mac
  41. else
  42. qosdef_flush_table "$NFT_QOS_INET_FAMILY" nft-qos-mac
  43. fi
  44. }
  45. # limit rate by mac address init
  46. qosdef_init_mac() {
  47. local hook_ul="prerouting" hook_dl="postrouting"
  48. [ "$2" = 0 ] || {
  49. logger -t nft-qos-mac "validation failed"
  50. return 1
  51. }
  52. [ $limit_mac_enable -eq 0 ] && return 1
  53. table_name=$NFT_QOS_INET_FAMILY
  54. if [ -z "$NFT_QOS_HAS_BRIDGE" ]; then
  55. hook_ul="postrouting"
  56. hook_dl="prerouting"
  57. else
  58. table_name="bridge"
  59. fi
  60. qosdef_appendx "table $table_name nft-qos-mac {\n"
  61. qosdef_append_chain_mac $hook_ul upload client
  62. qosdef_append_chain_mac $hook_dl download client
  63. qosdef_appendx "}\n"
  64. }