d03f90f0c870b628303aa6e5edbd85bcf0089ec9
[lede.git] / package / network / services / igmpproxy / files / igmpproxy.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2010-2014 OpenWrt.org
3
4 START=99
5 USE_PROCD=1
6 PROG=/usr/sbin/igmpproxy
7 CONFIGFILE=/var/etc/igmpproxy.conf
8
9 # igmpproxy supports both a debug mode and verbosity, which are very useful
10 # when something isn't working.
11 #
12 # Debug mode will print everything to stdout instead of syslog. Generally
13 # verbosity should NOT be set as it will quickly fill your syslog.
14 #
15 # Put any debug or verbosity options into IGMP_OPTS
16 #
17 # Examples:
18 # OPTIONS="-d -v -v" - debug mode and very verbose, this will land in
19 #                        stdout and not in syslog
20 # OPTIONS="-v" - be verbose, this will write aditional information to syslog
21
22 OPTIONS=""
23
24 igmp_header() {
25         local quickleave
26         config_get_bool quickleave "$1" quickleave 0
27
28         mkdir -p /var/etc
29         rm -f /var/etc/igmpproxy.conf
30         [ $quickleave -gt 0 ] && echo "quickleave" >> /var/etc/igmpproxy.conf
31
32         [ -L /etc/igmpproxy.conf ] || ln -nsf /var/etc/igmpproxy.conf /etc/igmpproxy.conf
33 }
34
35 igmp_add_phyint() {
36         local network direction altnets device up
37
38         config_get network $1 network
39         config_get direction $1 direction
40         config_get altnets $1 altnet
41
42         local status="$(ubus -S call "network.interface.$network" status)"
43         [ -n "$status" ] || return
44
45         json_load "$status"
46         json_get_var device l3_device
47         json_get_var up up
48
49         [ -n "$device" -a "$up" = "1" ] || {
50                 procd_append_param error "$network is not up"
51                 return;
52         }
53
54         append netdevs "$device"
55
56         [[ "$direction" = "upstream" ]] && has_upstream=1
57
58         echo -e "\nphyint $device $direction ratelimit 0 threshold 1" >> /var/etc/igmpproxy.conf
59
60         if [ -n "$altnets" ]; then
61                 local altnet
62                 for altnet in $altnets; do
63                         echo -e "\taltnet $altnet" >> /var/etc/igmpproxy.conf
64                 done
65         fi
66 }
67
68 igmp_add_network() {
69         local network
70
71         config_get network $1 network
72         procd_add_interface_trigger "interface.*" $network /etc/init.d/igmpproxy reload
73 }
74
75 igmp_add_firewall_routing() {
76         config_get network $1 network
77         config_get direction $1 direction
78
79         [[ "$direction" = "downstream" ]] || return 0
80
81         json_add_object ""
82         json_add_string type rule
83         json_add_string src "$upstream"
84         json_add_string dest "$network"
85         json_add_string family ipv4
86         json_add_string proto udp
87         json_add_string dest_ip "224.0.0.0/4"
88         json_add_string target ACCEPT
89         json_close_object
90 }
91
92 igmp_add_firewall_network() {
93         config_get network $1 network
94         config_get direction $1 direction
95
96         json_add_object ""
97         json_add_string type rule
98         json_add_string src "$network"
99         json_add_string proto igmp
100         json_add_string target ACCEPT
101         json_close_object
102
103         [[ "$direction" = "upstream" ]] && {
104                 upstream="$network"
105                 config_foreach igmp_add_firewall_routing phyint
106         }
107 }
108
109 service_triggers() {
110         procd_add_reload_trigger "igmpproxy"
111 }
112
113 start_service() {
114         has_upstream=
115         netdevs=
116         config_load igmpproxy
117
118         config_foreach igmp_header igmpproxy
119         config_foreach igmp_add_phyint phyint
120         [ -n "$has_upstream" ] || return
121
122         procd_open_instance
123         procd_set_param command $PROG
124         [ -n "$OPTIONS" ] && procd_append_param $OPTIONS
125         procd_append_param command $CONFIGFILE
126         procd_set_param file $CONFIGFILE
127         procd_set_param netdev $netdevs
128         procd_set_param respawn
129         procd_open_trigger
130         config_foreach igmp_add_network phyint
131         procd_close_trigger
132
133         procd_open_data
134
135         json_add_array firewall
136         config_foreach igmp_add_firewall_network phyint
137         json_close_array
138
139         procd_close_data
140
141         procd_close_instance
142 }
143
144 service_started() {
145         procd_set_config_changed firewall
146 }