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.

39 lines
1.4 KiB

  1. #!/bin/sh
  2. #
  3. # Copyright (C) 2018 rosysong@rosinson.com
  4. #
  5. . /lib/nft-qos/core.sh
  6. qosdef_monitor_get_ip_handle() { # <family> <chain> <ip>
  7. echo $(nft list chain $1 nft-qos-monitor $2 -a 2>/dev/null | grep $3 | awk '{print $11}')
  8. }
  9. qosdef_monitor_add() { # <mac> <ip> <hostname>
  10. handle_dl=$(qosdef_monitor_get_ip_handle $NFT_QOS_INET_FAMILY download $2)
  11. [ -z "$handle_dl" ] && nft add rule $NFT_QOS_INET_FAMILY nft-qos-monitor download ip daddr $2 counter
  12. handle_ul=$(qosdef_monitor_get_ip_handle $NFT_QOS_INET_FAMILY upload $2)
  13. [ -z "$handle_ul" ] && nft add rule $NFT_QOS_INET_FAMILY nft-qos-monitor upload ip saddr $2 counter
  14. }
  15. qosdef_monitor_del() { # <mac> <ip> <hostname>
  16. local handle_dl handle_ul
  17. handle_dl=$(qosdef_monitor_get_ip_handle $NFT_QOS_INET_FAMILY download $2)
  18. handle_ul=$(qosdef_monitor_get_ip_handle $NFT_QOS_INET_FAMILY upload $2)
  19. [ -n "$handle_dl" ] && nft delete handle $handle_dl
  20. [ -n "$handle_ul" ] && nft delete handle $handle_ul
  21. }
  22. # init qos monitor
  23. qosdef_init_monitor() {
  24. local hook_ul="prerouting" hook_dl="postrouting"
  25. [ -z "$NFT_QOS_HAS_BRIDGE" ] && {
  26. hook_ul="postrouting"
  27. hook_dl="prerouting"
  28. }
  29. nft add table $NFT_QOS_INET_FAMILY nft-qos-monitor
  30. nft add chain $NFT_QOS_INET_FAMILY nft-qos-monitor upload { type filter hook $hook_ul priority 0\; }
  31. nft add chain $NFT_QOS_INET_FAMILY nft-qos-monitor download { type filter hook $hook_dl priority 0\; }
  32. }