797ba2fafbe052a5a9459cd100dcebfff1280b09
[feed/routing.git] / babeld / files / babeld.init
1 #!/bin/sh /etc/rc.common
2
3 . $IPKG_INSTROOT/lib/functions/network.sh
4
5 START=70
6
7 pidfile='/var/run/babeld.pid'
8 CONFIGFILE='/var/etc/babeld.conf'
9 OTHERCONFIGFILE="/etc/babeld.conf"
10 OTHERCONFIGDIR="/tmp/babeld.d/"
11 EXTRA_COMMANDS="status"
12 EXTRA_HELP=" status Dump Babel's table to the log file."
13
14 # Options to ignore for the global section (old options that are translated
15 # for backward compatibility with old configuration files)
16 ignored_options="carrier_sense assume_wireless no_split_horizon random_router_id multicast_address port hello_interval wired_hello_interval smoothing_half_time duplication_priority local_server conf_file"
17
18 # Append a line to the configuration file
19 cfg_append() {
20 local value="$1"
21 echo "$value" >> $CONFIGFILE
22 }
23
24 cfg_append_option() {
25 local section="$1"
26 local option="$2"
27 local value
28 config_get value "$section" "$option"
29 # babeld convention for options is '-', not '_'
30 [ -n "$value" ] && cfg_append "${option//_/-} $value"
31 }
32
33 # Append to the "$buffer" variable
34 append_ifname() {
35 local section="$1"
36 local option="$2"
37 local switch="$3"
38 local _name
39 config_get _name "$section" "$option"
40 [ -z "$_name" ] && return 0
41 local ifname=$(uci_get_state network "$_name" ifname "$_name")
42 append buffer "$switch $ifname"
43 }
44
45 append_bool() {
46 local section="$1"
47 local option="$2"
48 local value="$3"
49 local _loctmp
50 config_get_bool _loctmp "$section" "$option" 0
51 [ "$_loctmp" -gt 0 ] && append buffer "$value"
52 }
53
54 append_parm() {
55 local section="$1"
56 local option="$2"
57 local switch="$3"
58 local _loctmp
59 config_get _loctmp "$section" "$option"
60 [ -z "$_loctmp" ] && return 0
61 append buffer "$switch $_loctmp"
62 }
63
64
65 # Provides backward compatibility for old option names in the global section.
66 translate_option() {
67 local section="$1"
68 local old_option="$2"
69 local new_option="$3"
70 local _value
71 config_get _value "$section" "$old_option"
72 [ -z "$_value" ] && return
73 cfg_append "${new_option//_/-} $_value"
74 }
75
76 translate_bool() {
77 local section="$1"
78 local old_option="$2"
79 local new_option="$3"
80 local _bool
81 local _value
82 config_get_bool _bool "$section" "$old_option" 0
83 [ "$_bool" -eq 0 ] && return
84 cfg_append "${new_option//_/-} true"
85 }
86
87 # Adds a new interface section for setting default interface options.
88 add_default_option() {
89 local option="$1"
90 local value="$2"
91 cfg_append "default ${option//_/-} $value"
92 }
93
94 # Global 'hello_interval' and 'wired_hello_interval' options are ignored,
95 # because they have no direct equivalent: you should use
96 # interface-specific settings.
97 parse_old_global_options() {
98 local section="$1"
99 translate_bool "$section" 'carrier_sense' 'link_detect'
100 translate_bool "$section" 'random_router_id' 'random_id'
101 translate_option "$section" 'multicast_address' 'protocol_group'
102 translate_option "$section" 'port' 'protocol_port'
103 translate_option "$section" 'local_server' 'local_port'
104 translate_option "$section" 'smoothing_half_time' 'smoothing_half_life'
105 translate_option "$section" 'duplication_priority' 'allow_duplicates'
106 # These two global options are turned into default interface options.
107 local _bool
108 config_get_bool _bool "$section" 'assume_wireless' 0
109 [ "$_bool" -eq 1 ] && add_default_option "wired" "false"
110 config_get_bool _bool "$section" 'no_split_horizon' 0
111 [ "$_bool" -eq 1 ] && add_default_option "split_horizon" "false"
112 }
113
114 babel_filter() {
115 local cfg="$1"
116 local _loctmp
117
118 local _ignored
119 config_get_bool _ignored "$cfg" 'ignore' 0
120 [ "$_ignored" -eq 1 ] && return 0
121
122 unset buffer
123 append_parm "$cfg" 'type' ''
124
125 append_bool "$cfg" 'local' 'local'
126
127 append_parm "$cfg" 'ip' 'ip'
128 append_parm "$cfg" 'eq' 'eq'
129 append_parm "$cfg" 'le' 'le'
130 append_parm "$cfg" 'ge' 'ge'
131 append_parm "$cfg" 'src_ip' 'src-ip'
132 append_parm "$cfg" 'src_eq' 'src-eq'
133 append_parm "$cfg" 'src_le' 'src-le'
134 append_parm "$cfg" 'src_ge' 'src-ge'
135 append_parm "$cfg" 'neigh' 'neigh'
136 append_parm "$cfg" 'id' 'id'
137 append_parm "$cfg" 'proto' 'proto'
138
139 append_ifname "$cfg" 'if' 'if'
140
141 append_parm "$cfg" 'action' ''
142
143 cfg_append "$buffer"
144 }
145
146 # Only one of babeld's options is allowed multiple times, "import-table".
147 # We just append it multiple times.
148 list_cb() {
149 option_cb "$@"
150 }
151
152 babel_config_cb() {
153 local type="$1"
154 local section="$2"
155 case "$type" in
156 "general")
157 option_cb() {
158 local option="$1"
159 local value="$2"
160 # Ignore old options
161 list_contains ignored_options "$option" && return
162 cfg_append "${option//_/-} $value"
163 }
164 ;;
165 "interface")
166 local _ifname
167 config_get _ifname "$section" 'ifname'
168 # Backward compatibility: try to use the section name
169 # if no "option ifname" was used.
170 [ -z "$_ifname" -a "${section:0:3}" != "cfg" ] && _ifname="$section"
171 # Try to resolve the logical interface name
172 unset interface
173 network_get_device interface "$_ifname" || interface="$_ifname"
174 option_cb() {
175 local option="$1"
176 local value="$2"
177 local _interface
178 # "option ifname" is a special option, don't actually
179 # generate configuration for it.
180 [ "$option" = "ifname" ] && return
181 [ -n "$interface" ] && _interface="interface $interface" || _interface="default"
182 cfg_append "$_interface ${option//_/-} $value"
183 }
184 # Handle ignore options.
185 local _ignored
186 # This works because we loaded the whole configuration
187 # beforehand (see config_load below).
188 config_get_bool _ignored "$section" 'ignore' 0
189 if [ "$_ignored" -eq 1 ]
190 then
191 option_cb() { return; }
192 else
193 # Also include an empty "interface $interface" statement,
194 # so that babeld operates on this interface.
195 [ -n "$interface" ] && cfg_append "interface $interface"
196 fi
197 ;;
198 *)
199 # Don't use reset_cb, this would also reset config_cb
200 option_cb() { return; }
201 ;;
202 esac
203 }
204
205 start() {
206 mkdir -p /var/lib
207 mkdir -p /var/etc
208 mkdir -p $OTHERCONFIGDIR
209
210 # Start by emptying the generated config file
211 >"$CONFIGFILE"
212 # Import dynamic config files
213 for f in $OTHERCONFIGDIR/*.conf; do
214 [ -f "$f" ] && cat $f >> $CONFIGFILE
215 done
216 # First load the whole config file, without callbacks, so that we are
217 # aware of all "ignore" options in the second pass.
218 config_load babeld
219 # Parse general and interface sections thanks to the "config_cb()"
220 # callback. This allows to loop over all options without having to
221 # know their name in advance.
222 config_cb() { babel_config_cb "$@"; }
223 config_load babeld
224 # Backward compatibility
225 config_foreach parse_old_global_options general
226 # Parse filters separately, since we know which options we expect
227 config_foreach babel_filter filter
228 # Using multiple config files is supported since babeld 1.5.1
229 /usr/sbin/babeld -D -I "$pidfile" -c "$OTHERCONFIGFILE" -c "$CONFIGFILE"
230 # Wait for the pidfile to appear
231 for i in 1 2
232 do
233 [ -f "$pidfile" ] || sleep 1
234 done
235 [ -f "$pidfile" ] || (echo "Failed to start babeld"; exit 42)
236 }
237
238 stop() {
239 [ -f "$pidfile" ] && kill $(cat $pidfile)
240 # avoid race-condition on restart: wait for
241 # babeld to die for real.
242 [ -f "$pidfile" ] && sleep 1
243 [ -f "$pidfile" ] && sleep 1
244 [ -f "$pidfile" ] && sleep 1
245 [ -f "$pidfile" ] && exit 42
246 }
247
248 status() {
249 [ -f "$pidfile" ] && kill -USR1 $(cat $pidfile)
250 }