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