freifunk-p2pblock.init 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/bin/sh /etc/rc.common
  2. START=82
  3. ME="freifunk-p2pblock"
  4. LOCK='/var/run/p2pblock.lock'
  5. # helper-scripts
  6. ipt_add() {
  7. logger -t "$ME" "set 'iptables -I $1'"
  8. iptables -I $1
  9. echo "iptables -D $1" >> $LOCK
  10. }
  11. start() {
  12. /etc/init.d/freifunk-p2pblock enabled || return
  13. if [ ! -s "$LOCK" ]; then
  14. logger -s -t "$ME" 'starting p2pblock...'
  15. config_load network
  16. config_get wan wan ifname
  17. if [ -n "$wan" ]; then
  18. config_load freifunk_p2pblock
  19. config_get layer7 p2pblock layer7
  20. config_get ipp2p p2pblock ipp2p
  21. config_get portrange p2pblock portrange
  22. config_get blocktime p2pblock blocktime
  23. config_get whitelist p2pblock whitelist
  24. # load modules
  25. insmod ipt_ipp2p 2>&-
  26. insmod ipt_layer7 2>&-
  27. insmod ipt_recent ip_list_tot=400 ip_pkt_list_tot=3 2>&-
  28. # create new p2p-chain
  29. iptables -N p2pblock
  30. # pipe all incoming FORWARD with source-/destination-port 1024-65535 throu p2p-chain
  31. ipt_add "FORWARD -i $wan -p tcp --sport $portrange --dport $portrange -j p2pblock"
  32. ipt_add "FORWARD -i $wan -p udp --sport $portrange --dport $portrange -j p2pblock"
  33. # if p2p-traffic blocked 3 packages to a destination ip then block all traffic within the next 180 sec (port 1024-65535)
  34. ipt_add "p2pblock -m recent --rdest --rcheck --name P2PBLOCK --seconds $blocktime --hitcount 3 -j DROP"
  35. ipt_add "p2pblock -m recent --rdest --rcheck --name P2PBLOCK --seconds $blocktime --hitcount 3 -m limit --limit 1/minute -j LOG --log-prefix P2PBLOCK-DROP:"
  36. # create layer7-rules
  37. for proto in $layer7; do
  38. ipt_add "p2pblock -m layer7 --l7proto $proto -m recent --rdest --set --name P2PBLOCK"
  39. ipt_add "p2pblock -m layer7 --l7proto $proto -m limit --limit 1/minute -j LOG --log-prefix P2PBLOCK-seen-$proto:"
  40. done
  41. # create ipp2p-rules
  42. for proto in $ipp2p; do
  43. ipt_add "p2pblock -m ipp2p --$proto -m recent --rdest --set --name P2PBLOCK"
  44. ipt_add "p2pblock -m ipp2p --$proto -m limit --limit 1/minute -j LOG --log-prefix P2PBLOCK-seen-$proto:"
  45. done
  46. # insert whitelisted ips
  47. for ip in $whitelist; do
  48. ipt_add "p2pblock -d $ip -j RETURN"
  49. done
  50. logger -s -t "$ME" 'Done.'; return 0
  51. else
  52. logger -s -t "$ME" 'No wan interface present.'; return 0
  53. fi
  54. else
  55. logger -s -t "$ME" 'WARNING! already running - Aborting!'; return 2
  56. fi
  57. }
  58. stop() {
  59. if [ -s "$LOCK" ]; then
  60. logger -s -t "$ME" 'stopping p2pblock...'
  61. # unset all rules in $LOCK-file
  62. cat $LOCK | sed -ne '1!G;h;$p' | while read line; do
  63. logger -t "$ME" "unset $line"
  64. while eval $line 2>&-; do :; done
  65. done; : > "$LOCK"
  66. # flush and delete the p2p-chain
  67. iptables -F p2pblock
  68. iptables -X p2pblock
  69. logger -s -t "$ME" 'Done.'; return 0
  70. else
  71. logger -s -t "$ME" 'WARNING! not running - Aborting!'; return 2
  72. fi
  73. }
  74. restart() {
  75. stop; sleep 1; start
  76. }