igmpproxy.init 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2010-2014 OpenWrt.org
  3. START=99
  4. USE_PROCD=1
  5. PROG=/usr/sbin/igmpproxy
  6. CONFIGFILE=/var/etc/igmpproxy.conf
  7. igmp_header() {
  8. local quickleave verbose
  9. config_get_bool quickleave "$1" quickleave 0
  10. config_get verbose "$1" verbose 0
  11. [ $verbose = "1" ] && logopts="-v"
  12. [ $verbose = "2" ] && logopts="-v -v"
  13. mkdir -p /var/etc
  14. rm -f /var/etc/igmpproxy.conf
  15. [ $quickleave -gt 0 ] && echo "quickleave" >> /var/etc/igmpproxy.conf
  16. [ -L /etc/igmpproxy.conf ] || ln -nsf /var/etc/igmpproxy.conf /etc/igmpproxy.conf
  17. }
  18. igmp_add_phyint() {
  19. local network direction altnets device up
  20. config_get network $1 network
  21. config_get direction $1 direction
  22. config_get altnets $1 altnet
  23. local status="$(ubus -S call "network.interface.$network" status)"
  24. [ -n "$status" ] || return
  25. json_load "$status"
  26. json_get_var device l3_device
  27. json_get_var up up
  28. [ -n "$device" -a "$up" = "1" ] || {
  29. procd_append_param error "$network is not up"
  30. return;
  31. }
  32. append netdevs "$device"
  33. [[ "$direction" = "upstream" ]] && has_upstream=1
  34. echo -e "\nphyint $device $direction ratelimit 0 threshold 1" >> /var/etc/igmpproxy.conf
  35. if [ -n "$altnets" ]; then
  36. local altnet
  37. for altnet in $altnets; do
  38. echo -e "\taltnet $altnet" >> /var/etc/igmpproxy.conf
  39. done
  40. fi
  41. }
  42. igmp_add_network() {
  43. local network
  44. config_get network $1 network
  45. procd_add_interface_trigger "interface.*" $network /etc/init.d/igmpproxy reload
  46. }
  47. igmp_add_firewall_routing() {
  48. config_get network $1 network
  49. config_get direction $1 direction
  50. [[ "$direction" = "downstream" ]] || return 0
  51. json_add_object ""
  52. json_add_string type rule
  53. json_add_string src "$upstream"
  54. json_add_string dest "$network"
  55. json_add_string family ipv4
  56. json_add_string proto udp
  57. json_add_string dest_ip "224.0.0.0/4"
  58. json_add_string target ACCEPT
  59. json_close_object
  60. }
  61. igmp_add_firewall_network() {
  62. config_get network $1 network
  63. config_get direction $1 direction
  64. json_add_object ""
  65. json_add_string type rule
  66. json_add_string src "$network"
  67. json_add_string proto igmp
  68. json_add_string target ACCEPT
  69. json_close_object
  70. [[ "$direction" = "upstream" ]] && {
  71. upstream="$network"
  72. config_foreach igmp_add_firewall_routing phyint
  73. }
  74. }
  75. service_triggers() {
  76. procd_add_reload_trigger "igmpproxy"
  77. config_foreach igmp_add_network phyint
  78. }
  79. start_service() {
  80. has_upstream=
  81. netdevs=
  82. logopts=
  83. config_load igmpproxy
  84. config_foreach igmp_header igmpproxy
  85. config_foreach igmp_add_phyint phyint
  86. [ -n "$has_upstream" ] || return
  87. procd_open_instance
  88. procd_set_param command $PROG
  89. [ -n "$logopts" ] && procd_append_param command $logopts
  90. procd_append_param command $CONFIGFILE
  91. procd_set_param file $CONFIGFILE
  92. procd_set_param netdev $netdevs
  93. procd_set_param respawn
  94. procd_open_data
  95. json_add_array firewall
  96. config_foreach igmp_add_firewall_network phyint
  97. json_close_array
  98. procd_close_data
  99. procd_close_instance
  100. }
  101. service_started() {
  102. procd_set_config_changed firewall
  103. }
  104. stop_service() {
  105. procd_set_config_changed firewall
  106. }