igmpproxy.init 3.4 KB

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