30816e34fde86e79f13c071aee4784c8a498ff0d
[openwrt/staging/jogo.git] / package / network / services / omcproxy / files / omcproxy.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2018 OpenWrt.org
3
4 START=99
5 USE_PROCD=1
6 PROG=/usr/sbin/omcproxy
7
8 # Uncomment to enable verbosity
9 #OPTIONS="-v"
10 PROXIES=""
11
12 omcproxy_add_proxy() {
13 local proxy scope uplink updevice downlinks
14
15 config_get uplink $1 uplink
16 [ -n "$uplink" ] || return
17
18 network_get_device updevice "$uplink" || {
19 procd_append_param error "$uplink is not up"
20 return;
21 }
22
23 config_get downlinks $1 downlink
24 for downlink in $downlinks; do
25 local device
26
27 network_get_device device "$downlink" || {
28 procd_append_param error "$downlink is not up"
29 continue;
30 }
31
32 proxy="$proxy,$device"
33
34 # Disable in-kernel querier while ours is active, default is 1.
35 [ -f /sys/class/net/$device/bridge/multicast_querier ] && \
36 echo 0 > /sys/class/net/$device/bridge/multicast_querier
37 done
38
39 [ -n "$proxy" ] || return 0
40
41 config_get scope $1 scope
42 [ -n "$scope" ] && proxy="$proxy,scope=$scope"
43
44 PROXIES="$PROXIES $updevice$proxy"
45 }
46
47 omcproxy_add_network_triggers() {
48 local uplink downlinks
49
50 config_get uplink $1 uplink
51 config_get downlinks $1 downlink
52
53 for link in $uplink $downlinks; do
54 procd_add_interface_trigger "interface.*" $link /etc/init.d/omcproxy restart
55 done
56 }
57
58 omcproxy_add_firewall_rules() {
59 local uplink downlinks
60
61 config_get uplink $1 uplink
62 config_get downlinks $1 downlink
63
64 upzone=$(fw3 -q network $uplink 2>/dev/null)
65 [ -n "$upzone" ] || return 0
66
67 json_add_object ""
68 json_add_string type rule
69 json_add_string src "$upzone"
70 json_add_string family ipv4
71 json_add_string proto igmp
72 json_add_string target ACCEPT
73 json_close_object
74
75 json_add_object ""
76 json_add_string type rule
77 json_add_string family ipv6
78 json_add_string src "$upzone"
79 json_add_string proto icmp
80 json_add_string src_ip fe80::/10
81 json_add_array icmp_type
82 json_add_string "" 130/0
83 json_add_string "" 131/0
84 json_add_string "" 132/0
85 json_add_string "" 143/0
86 json_close_array
87 json_add_string target ACCEPT
88 json_close_object
89
90 for downlink in $downlinks; do
91 downzone=$(fw3 -q network $downlink 2>/dev/null)
92 [ -n "$downzone" ] || continue
93
94 json_add_object ""
95 json_add_string type rule
96 json_add_string src "$upzone"
97 json_add_string dest "$downzone"
98 json_add_string family ipv4
99 json_add_string proto udp
100 json_add_string dest_ip "224.0.0.0/4"
101 json_add_string target ACCEPT
102 json_close_object
103
104 json_add_object ""
105 json_add_string type rule
106 json_add_string src "$upzone"
107 json_add_string dest "$downzone"
108 json_add_string family ipv6
109 json_add_string proto udp
110 json_add_string dest_ip "ff00::/8"
111 json_add_string target ACCEPT
112 json_close_object
113 done
114 }
115
116 service_triggers() {
117 procd_add_reload_trigger "omcproxy"
118 config_foreach omcproxy_add_network_triggers proxy
119 }
120
121 start_service() {
122 . /lib/functions/network.sh
123
124 config_load omcproxy
125
126 config_foreach omcproxy_add_proxy proxy
127 [ -n "$PROXIES" ] || return 0
128
129 procd_open_instance
130 procd_set_param command $PROG
131 [ -n "$OPTIONS" ] && procd_append_param command $OPTIONS
132 procd_append_param command $PROXIES
133 procd_set_param respawn
134
135 procd_open_data
136
137 json_add_array firewall
138 config_foreach omcproxy_add_firewall_rules proxy
139 json_close_array
140
141 procd_close_data
142
143 procd_close_instance
144
145 # Increase maximum IPv4 group memberships per socket, default is 100.
146 echo 128 > /proc/sys/net/ipv4/igmp_max_memberships
147 }
148
149 service_started() {
150 procd_set_config_changed firewall
151 }
152
153 stop_service() {
154 procd_set_config_changed firewall
155 }