olsrd: init: fix all work with is done via IFS (storing old value, set IFS, work...
[feed/routing.git] / olsrd / files / olsrd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008-2013 OpenWrt.org
3
4 START=65
5
6 SERVICE_DAEMONIZE=1
7 SERVICE_WRITE_PID=1
8
9 CONF=/var/etc/olsrd.conf
10 PID=/var/run/olsrd.pid
11 PID6=/var/run/olsrd.ipv6.pid
12
13 OLSRD_OLSRD_SCHEMA='ignore:internal config_file:internal DebugLevel=0 AllowNoInt=yes'
14 OLSRD_IPCCONNECT_SCHEMA='ignore:internal Host:list Net:list2'
15 OLSRD_LOADPLUGIN_SCHEMA='ignore:internal library:internal Host4:list Net4:list2 Host:list Net:list2 Host6:list Net6:list2 Ping:list redistribute:list NonOlsrIf:list name:list lat lon latlon_infile HNA:list2 hosts:list2'
16 OLSRD_INTERFACE_SCHEMA='ignore:internal interface:internal AutoDetectChanges:bool'
17 OLSRD_INTERFACE_DEFAULTS_SCHEMA='AutoDetectChanges:bool'
18
19 T=' '
20 N='
21 '
22
23 log()
24 {
25 logger -t olsrd -p daemon.info -s "$1"
26 }
27
28 validate_varname() {
29 local varname="$1"
30 [ -z "$varname" -o "$varname" != "${varname%%[!A-Za-z0-9_]*}" ] && return 1
31 return 0
32 }
33
34 validate_olsrd_option() {
35 local str="$1"
36 [ -z "$str" -o "$str" != "${str%%[! 0-9A-Za-z./|:_-]*}" ] && return 1
37 return 0
38 }
39
40 system_config() {
41 local cfg="$1"
42 local cfgt hostname latlon oldIFS
43
44 config_get cfgt "$cfg" TYPE
45
46 if [ "$cfgt" = "system" ]; then
47 config_get hostname "$cfg" hostname
48 hostname="${hostname:-OpenWrt}"
49 SYSTEM_HOSTNAME="$hostname"
50 fi
51
52 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
53 config_get latlon "$cfg" latlon
54 oldIFS="$IFS"; IFS=" ${T}${N},"; set -- $latlon; IFS="$oldIFS"
55 SYSTEM_LAT="$1"
56 SYSTEM_LON="$2"
57 fi
58
59 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
60 config_get latlon "$cfg" latitude
61 SYSTEM_LAT="$latlon"
62 config_get latlon "$cfg" longitude
63 SYSTEM_LON="$latlon"
64 fi
65 }
66
67 olsrd_find_config_file() {
68 local cfg="$1"
69 validate_varname "$cfg" || return 0
70
71 config_get_bool ignore "$cfg" ignore 0
72 [ "$ignore" -ne 0 ] && return 0
73 config_get OLSRD_CONFIG_FILE "$cfg" config_file
74
75 return 0
76 }
77
78 warning_invalid_value() {
79 local funcname="warning_invalid_value"
80 local package="$1"
81 validate_varname "$package" || package=
82 local config="$2"
83 validate_varname "$config" || config=
84 local option="$3"
85 validate_varname "$option" || option=
86
87 if [ -n "$package" -a -n "$config" ]; then
88 log "$funcname() in option '$package.$config${option:+.}$option', skipped"
89 else
90 log "$funcname() skipped"
91 fi
92
93 return 0
94 }
95
96 olsrd_write_option() {
97 local param="$1"
98 local cfg="$2"
99 validate_varname "$cfg" || return 1
100 local option="$3"
101 validate_varname "$option" || return 1
102 local value="$4"
103 local option_type="$5"
104
105 if [ "$option_type" = bool ]; then
106 case "$value" in
107 1|on|true|enabled|yes) value=yes;;
108 0|off|false|disabled|no) value=no;;
109 *) warning_invalid_value olsrd "$cfg" "$option"; return 1;;
110 esac
111 fi
112
113 if ! validate_olsrd_option "$value"; then
114 warning_invalid_value olsrd "$cfg" "$option"
115 return 1
116 fi
117
118 if [ "$value" != "${value%%[G-Zg-z_-]*}" ]; then
119 if [ "$option" != "Ip6AddrType" -a "$option" != "LinkQualityMult" -a "$value" != "yes" -a "$value" != "no" ]; then
120 value="\"$value\""
121 fi
122 fi
123
124 echo -n "${N}$param$option $value"
125
126 return 0
127 }
128
129 olsrd_write_plparam() {
130 local funcname="olsrd_write_plparam"
131 local param="$1"
132 local cfg="$2"
133 validate_varname "$cfg" || return 1
134 local option="$3"
135 validate_varname "$option" || return 1
136 local value="$4"
137 local option_type="$5"
138 local _option oldIFS
139
140 if [ "$option_type" = bool ]; then
141 case "$value" in
142 1|on|true|enabled|yes) value=yes;;
143 0|off|false|disabled|no) value=no;;
144 *) warning_invalid_value olsrd "$cfg" "$option"; return 1;;
145 esac
146 fi
147
148 if ! validate_olsrd_option "$value"; then
149 warning_invalid_value olsrd "$cfg" "$option"
150 return 1
151 fi
152
153 oldIFS="$IFS"; IFS='-_'; set -- $option; IFS="$oldIFS"
154 option="$*"
155 _option="$option"
156
157 if [ "$option" = 'hosts' ]; then
158 set -- $value
159 option="$1"
160 shift
161 value="$*"
162 fi
163
164 if [ "$option" = 'NonOlsrIf' ]; then
165 if validate_varname "$value"; then
166 if network_get_device ifname "$value"; then
167 log "$funcname() Info: mdns Interface '$value' ifname '$ifname' found"
168 else
169 log "$funcname() Warning: mdns Interface '$value' not found, skipped"
170 fi
171 else
172 warning_invalid_value olsrd "$cfg" "NonOlsrIf"
173 fi
174 [ -z "$ifname" ] || value=$ifname
175 fi
176
177 echo -n "${N}${param}PlParam \"$option\" \"$value\""
178
179 return 0
180 }
181
182 config_update_schema() {
183 local schema_varname="$1"
184 validate_varname "$schema_varname" || return 1
185 local command="$2"
186 validate_varname "$command" || return 1
187 local option="$3"
188 validate_varname "$option" || return 1
189 local value="$4"
190 local schema
191 local cur_option
192
193 case "$varname" in
194 *_LENGTH) return 0;;
195 *_ITEM*) return 0;;
196 esac
197
198 eval "export -n -- \"schema=\${$schema_varname}\""
199
200 for cur_option in $schema; do
201 [ "${cur_option%%[:=]*}" = "$option" ] && return 0
202 done
203
204 if [ "$command" = list ]; then
205 set -- $value
206 if [ "$#" -ge "3" ]; then
207 schema_entry="$option:list3"
208 elif [ "$#" -ge "2" ]; then
209 schema_entry="$option:list2"
210 else
211 schema_entry="$option:list"
212 fi
213 else
214 schema_entry="$option"
215 fi
216
217 append "$schema_varname" "$schema_entry"
218
219 return 0
220 }
221
222 config_write_options() {
223 local funcname="config_write_options"
224 local schema="$1"
225 local cfg="$2"
226 validate_varname "$cfg" || return 1
227 local write_func="$3"
228 [ -z "$write_func" ] && output_func=echo
229 local write_param="$4"
230
231 local schema_entry option option_length option_type default value list_size list_item list_value i position speed oldIFS
232 local list_speed_vars="HelloInterval HelloValidityTime TcInterval TcValidityTime MidInterval MidValidityTime HnaInterval HnaValidityTime"
233
234 get_value_for_entry()
235 {
236 local schema_entry="$1"
237
238 default="${schema_entry#*[=]}"
239 [ "$default" = "$schema_entry" ] && default=
240 option="${schema_entry%%[=]*}"
241
242 oldIFS="$IFS"; IFS=':'; set -- $option; IFS="$oldIFS"
243 option="$1"
244 option_type="$2"
245
246 validate_varname "$option" || return 1
247 [ -z "$option_type" ] || validate_varname "$option_type" || return 1
248 [ "$option_type" = internal ] && return 1
249
250 config_get value "$cfg" "$option"
251 [ "$option" = "speed" ] && return 1
252
253 return 0
254 }
255
256 already_in_schema()
257 {
258 case " $schema " in
259 *" $1 "*)
260 return 0
261 ;;
262 *)
263 return 1
264 ;;
265 esac
266 }
267
268 already_in_schema "speed" && {
269 get_value_for_entry "speed"
270
271 if [ 2>/dev/null $value -gt 0 -a $value -le 20 ]; then
272 speed="$value"
273 else
274 log "$funcname() Warning: invalid speed-value: '$value' - allowed integers: 1...20, fallback to 6"
275 speed=6
276 fi
277
278 for schema_entry in $list_speed_vars; do {
279 already_in_schema "$schema_entry" || schema="$schema $schema_entry"
280 } done
281 }
282
283 for schema_entry in $schema; do
284 if [ -n "$speed" ]; then # like sven-ola freifunk firmware fff-1.7.4
285 case "$schema_entry" in
286 HelloInterval)
287 value="$(( $speed / 2 + 1 )).0"
288 ;;
289 HelloValidityTime)
290 value="$(( $speed * 25 )).0"
291 ;;
292 TcInterval) # todo: not fisheye? -> $(( $speed * 2 ))
293 value=$(( $speed / 2 ))
294 [ $value -eq 0 ] && value=1
295 value="$value.0"
296 ;;
297 TcValidityTime)
298 value="$(( $speed * 100 )).0"
299 ;;
300 MidInterval)
301 value="$(( $speed * 5 )).0"
302 ;;
303 MidValidityTime)
304 value="$(( $speed * 100 )).0"
305 ;;
306 HnaInterval)
307 value="$(( $speed * 2 )).0"
308 ;;
309 HnaValidityTime)
310 value="$(( $speed * 25 )).0"
311 ;;
312 *)
313 get_value_for_entry "$schema_entry" || continue
314 ;;
315 esac
316
317 is_speed_var()
318 {
319 case " $list_speed_vars " in
320 *" $1 "*)
321 return 0
322 ;;
323 *)
324 return 1
325 ;;
326 esac
327 }
328
329 is_speed_var "$schema_entry" && option="$schema_entry"
330 else
331 get_value_for_entry "$schema_entry" || continue
332 fi
333
334 if [ -z "$value" ]; then
335 oldIFS="$IFS"; IFS='+'; set -- $default; IFS="$oldIFS"
336 value=$*
337 elif [ "$value" = '-' -a -n "$default" ]; then
338 continue
339 fi
340
341 [ -z "$value" ] && continue
342
343 case "$option_type" in
344 list) list_size=1;;
345 list2) list_size=2;;
346 list3) list_size=3;;
347 *) list_size=0;;
348 esac
349
350 if [ "$list_size" -gt 0 ]; then
351 config_get option_length "$cfg" "${option}_LENGTH"
352 if [ -n "$option_length" ]; then
353 i=1
354 while [ "$i" -le "$option_length" ]; do
355 config_get list_value "$cfg" "${option}_ITEM$i"
356 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" || break
357 i=$((i + 1))
358 done
359 else
360 list_value=
361 i=0
362 for list_item in $value; do
363 append "list_value" "$list_item"
364 i=$((i + 1))
365 position=$((i % list_size))
366 if [ "$position" -eq 0 ]; then
367 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" || break
368 list_value=
369 fi
370 done
371 [ "$position" -ne 0 ] && "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type"
372 fi
373 else
374 "$write_func" "$write_param" "$cfg" "$option" "$value" "$option_type"
375 fi
376 done
377
378 return 0
379 }
380
381 olsrd_write_olsrd() {
382 local cfg="$1"
383 validate_varname "$cfg" || return 0
384 local ignore
385
386 config_get_bool ignore "$cfg" ignore 0
387 [ "$ignore" -ne 0 ] && return 0
388
389 [ "$OLSRD_COUNT" -gt 0 ] && return 0
390
391 config_get ipversion "$cfg" IpVersion
392 if [ "$ipversion" = "6and4" ]; then
393 OLSRD_IPVERSION_6AND4=1
394 config_set "$cfg" IpVersion '6'
395 fi
396 config_get smartgateway "$cfg" SmartGateway
397 config_get smartgatewayuplink "$cfg" SmartGatewayUplink
398
399 config_write_options "$OLSRD_OLSRD_SCHEMA" "$cfg" olsrd_write_option
400 echo
401 OLSRD_COUNT=$((OLSRD_COUNT + 1))
402 return 0
403 }
404
405 olsrd_write_ipcconnect() {
406 local cfg="$1"
407 validate_varname "$cfg" || return 0
408 local ignore
409
410 config_get_bool ignore "$cfg" ignore 0
411 [ "$ignore" -ne 0 ] && return 0
412
413 [ "$IPCCONNECT_COUNT" -gt 0 ] && return 0
414
415 echo -n "${N}IpcConnect${N}{"
416 config_write_options "$OLSRD_IPCCONNECT_SCHEMA" "$cfg" olsrd_write_option "${T}"
417 echo "${N}}"
418 IPCCONNECT_COUNT=$((IPCCONNECT_COUNT + 1))
419
420 return 0
421 }
422
423 olsrd_write_hna4() {
424 local cfg="$1"
425 validate_varname "$cfg" || return 0
426 local ignore
427
428 config_get_bool ignore "$cfg" ignore 0
429 [ "$ignore" -ne 0 ] && return 0
430
431 config_get netaddr "$cfg" netaddr
432 if ! validate_olsrd_option "$netaddr"; then
433 warning_invalid_value olsrd "$cfg" "netaddr"
434 return 0
435 fi
436
437 config_get netmask "$cfg" netmask
438 if ! validate_olsrd_option "$netmask"; then
439 warning_invalid_value olsrd "$cfg" "netmask"
440 return 0
441 fi
442
443 [ "$HNA4_COUNT" -le 0 ] && echo -n "${N}Hna4${N}{"
444 echo -n "${N}${T}${T}$netaddr $netmask"
445 HNA4_COUNT=$((HNA4_COUNT + 1))
446
447 return 0
448 }
449
450 olsrd_write_hna6() {
451 local cfg="$1"
452 validate_varname "$cfg" || return 0
453 local ignore
454
455 config_get_bool ignore "$cfg" ignore 0
456 [ "$ignore" -ne 0 ] && return 0
457
458 config_get netaddr "$cfg" netaddr
459 if ! validate_olsrd_option "$netaddr"; then
460 warning_invalid_value olsrd "$cfg" "netaddr"
461 return 0
462 fi
463
464 config_get prefix "$cfg" prefix
465 if ! validate_olsrd_option "$prefix"; then
466 warning_invalid_value olsrd "$cfg" "prefix"
467 return 0
468 fi
469
470 [ "$HNA6_COUNT" -le 0 ] && echo -n "${N}Hna6${N}{"
471 echo -n "${N}${T}${T}$netaddr $prefix"
472 HNA6_COUNT=$((HNA6_COUNT + 1))
473
474 return 0
475 }
476
477 olsrd_write_loadplugin() {
478 local funcname="olsrd_write_loadplugin"
479 local cfg="$1"
480 validate_varname "$cfg" || return 0
481 local ignore
482 local name
483 local suffix
484 local lat
485 local lon
486 local latlon_infile
487
488 config_get_bool ignore "$cfg" ignore 0
489 [ "$ignore" -ne 0 ] && return 0
490
491 config_get library "$cfg" library
492 if ! validate_olsrd_option "$library"; then
493 warning_invalid_value olsrd "$cfg" "library"
494 return 0
495 fi
496 if ! [ -x "/lib/$library" -o -x "/usr/lib/$library" -o -x "/usr/local/lib/$library" ]; then
497 log "$funcname() Warning: Plugin library '$library' not found, skipped"
498 return 0
499 fi
500
501 case "$library" in
502 olsrd_nameservice.*)
503 config_get name "$cfg" name
504 [ -z "$name" ] && config_set "$cfg" name $SYSTEM_HOSTNAME
505
506 config_get suffix "$cfg" suffix
507 [ -z "$suffix" ] && config_set "$cfg" suffix '.olsr'
508
509 config_get lat "$cfg" lat
510 config_get lon "$cfg" lon
511 config_get latlon_infile "$cfg" latlon_infile
512 if [ \( -z "$lat" -o -z "$lat" \) -a -z "$latlon_infile" ]; then
513 if [ -f '/var/run/latlon.txt' ]; then
514 config_set "$cfg" lat ''
515 config_set "$cfg" lon ''
516 config_set "$cfg" latlon_infile '/var/run/latlon.txt'
517 else
518 config_set "$cfg" lat "$SYSTEM_LAT"
519 config_set "$cfg" lon "$SYSTEM_LON"
520 fi
521 fi
522
523 for f in latlon_file hosts_file services_file resolv_file macs_file; do
524 config_get $f "$cfg" $f
525 done
526
527 [ -z "$latlon_file" ] && config_set "$cfg" latlon_file '/var/run/latlon.js'
528 ;;
529 olsrd_watchdog.*)
530 config_get wd_file "$cfg" file
531 ;;
532 esac
533
534 echo -n "${N}LoadPlugin \"$library\"${N}{"
535 config_write_options "$OLSRD_LOADPLUGIN_SCHEMA" "$cfg" olsrd_write_plparam "${T}"
536 echo "${N}}"
537
538 return 0
539 }
540
541 olsrd_write_interface() {
542 local funcname="olsrd_write_interface"
543 local cfg="$1"
544 validate_varname "$cfg" || return 0
545 local ignore
546 local interfaces
547 local interface
548 local ifnames
549
550 config_get_bool ignore "$cfg" ignore 0
551 [ "$ignore" -ne 0 ] && return 0
552
553 ifnames=
554 config_get interfaces "$cfg" interface
555
556 for interface in $interfaces; do
557 if validate_varname "$interface"; then
558 if network_get_device IFNAME "$interface"; then
559 ifnames="$ifnames \"$IFNAME\""
560 ifsglobal="$ifsglobal $IFNAME"
561 else
562 log "$funcname() Warning: Interface '$interface' not found, skipped"
563 fi
564 else
565 warning_invalid_value olsrd "$cfg" "interface"
566 fi
567 done
568
569 [ -z "$ifnames" ] && return 0
570
571 echo -n "${N}Interface$ifnames${N}{"
572 config_write_options "$OLSRD_INTERFACE_SCHEMA" "$cfg" olsrd_write_option "${T}"
573 echo "${N}}"
574 INTERFACES_COUNT=$((INTERFACES_COUNT + 1))
575
576 return 0
577 }
578
579 olsrd_write_interface_defaults() {
580 local cfg="$1"
581 validate_varname "$cfg" || return 0
582
583 echo -n "${N}InterfaceDefaults$ifnames${N}{"
584 config_write_options "$OLSRD_INTERFACE_DEFAULTS_SCHEMA" "$cfg" olsrd_write_option "${T}"
585 echo "${N}}"
586
587 return 1
588 }
589
590 olsrd_update_schema() {
591 local command="$1"
592 validate_varname "$command" || return 0
593 local varname="$2"
594 validate_varname "$varname" || return 0
595 local value="$3"
596 local cfg="$CONFIG_SECTION"
597 local cfgt
598 local cur_varname
599
600 config_get cfgt "$cfg" TYPE
601 case "$cfgt" in
602 olsrd) config_update_schema OLSRD_OLSRD_SCHEMA "$command" "$varname" "$value";;
603 IpcConnect) config_update_schema OLSRD_IPCCONNECT_SCHEMA "$command" "$varname" "$value";;
604 LoadPlugin) config_update_schema OLSRD_LOADPLUGIN_SCHEMA "$command" "$varname" "$value";;
605 Interface) config_update_schema OLSRD_INTERFACE_SCHEMA "$command" "$varname" "$value";;
606 InterfaceDefaults) config_update_schema OLSRD_INTERFACE_DEFAULTS_SCHEMA "$command" "$varname" "$value";;
607 esac
608
609 return 0
610 }
611
612 olsrd_write_config() {
613 OLSRD_IPVERSION_6AND4=0
614 OLSRD_COUNT=0
615 config_foreach olsrd_write_olsrd olsrd
616 IPCCONNECT_COUNT=0
617 config_foreach olsrd_write_ipcconnect IpcConnect
618 HNA4_COUNT=0
619 config_foreach olsrd_write_hna4 Hna4
620 [ "$HNA4_COUNT" -gt 0 ] && echo "${N}}"
621 HNA6_COUNT=0
622 config_foreach olsrd_write_hna6 Hna6
623 [ "$HNA6_COUNT" -gt 0 ] && echo "${N}}"
624 config_foreach olsrd_write_loadplugin LoadPlugin
625 INTERFACES_COUNT=0
626 config_foreach olsrd_write_interface_defaults InterfaceDefaults
627 config_foreach olsrd_write_interface Interface
628 echo
629
630 return 0
631 }
632
633 get_wan_ifnames()
634 {
635 local wanifnames word catch_next
636
637 which ip >/dev/null || return 1
638
639 set -- $( ip route list exact 0.0.0.0/0 table all )
640 for word in $*; do
641 case "$word" in
642 dev)
643 catch_next="true"
644 ;;
645 *)
646 [ -n "$catch_next" ] && {
647 case "$wanifnames" in
648 *" $word "*)
649 ;;
650 *)
651 wanifnames="$wanifnames $word "
652 ;;
653 esac
654
655 catch_next=
656 }
657 ;;
658 esac
659 done
660
661 echo "$wanifnames"
662 }
663
664 olsrd_setup_smartgw_rules() {
665 local funcname="olsrd_setup_smartgw_rules"
666 # Check if ipip is installed
667 [ -e /etc/modules.d/[0-9]*-ipip ] || {
668 log "$funcname() Warning: kmod-ipip is missing. SmartGateway will not work until you install it."
669 return 1
670 }
671
672 local wanifnames="$( get_wan_ifnames )"
673
674 if [ -z "$wanifnames" ]; then
675 nowan=1
676 else
677 nowan=0
678 fi
679
680 IP4T=$(which iptables)
681 IP6T=$(which ip6tables)
682
683 # Delete smartgw firewall rules first
684 for IPT in $IP4T $IP6T; do
685 while $IPT -D forwarding_rule -o tnl_+ -j ACCEPT 2> /dev/null; do :;done
686 for IFACE in $wanifnames; do
687 while $IPT -D forwarding_rule -i tunl0 -o $IFACE -j ACCEPT 2> /dev/null; do :; done
688 done
689 for IFACE in $ifsglobal; do
690 while $IPT -D input_rule -i $IFACE -p 4 -j ACCEPT 2> /dev/null; do :; done
691 done
692 done
693 while $IP4T -t nat -D postrouting_rule -o tnl_+ -j MASQUERADE 2> /dev/null; do :;done
694
695 if [ "$smartgateway" == "yes" ]; then
696 log "$funcname() Notice: Inserting firewall rules for SmartGateway"
697 if [ ! "$smartgatewayuplink" == "none" ]; then
698 if [ "$smartgatewayuplink" == "ipv4" ]; then
699 # Allow everything to be forwarded to tnl_+ and use NAT for it
700 $IP4T -I forwarding_rule -o tnl_+ -j ACCEPT
701 $IP4T -t nat -I postrouting_rule -o tnl_+ -j MASQUERADE
702 # Allow forwarding from tunl0 to (all) wan-interfaces
703 if [ "$nowan"="0" ]; then
704 for IFACE in $wanifnames; do
705 $IP4T -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
706 done
707 fi
708 # Allow incoming ipip on all olsr-interfaces
709 for IFACE in $ifsglobal; do
710 $IP4T -I input_rule -i $IFACE -p 4 -j ACCEPT
711 done
712 elif [ "$smartgatewayuplink" == "ipv6" ]; then
713 $IP6T -I forwarding_rule -o tnl_+ -j ACCEPT
714 if [ "$nowan"="0" ]; then
715 for IFACE in $wanifnames; do
716 $IP6T -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
717 done
718 fi
719 for IFACE in $ifsglobal; do
720 $IP6T -I input_rule -i $IFACE -p 4 -j ACCEPT
721 done
722 else
723 for IPT in $IP4T $IP6T; do
724 $IPT -I forwarding_rule -o tnl_+ -j ACCEPT
725 $IPT -t nat -I postrouting_rule -o tnl_+ -j MASQUERADE
726 if [ "$nowan"="0" ]; then
727 for IFACE in $wanifnames; do
728 $IPT -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
729 done
730 fi
731 for IFACE in $ifsglobal; do
732 $IPT -I input_rule -i $IFACE -p 4 -j ACCEPT
733 done
734 done
735 fi
736 fi
737 fi
738 }
739
740 error() {
741 log "error() ${initscript}: $@"
742 }
743
744 start() {
745 SYSTEM_HOSTNAME=
746 SYSTEM_LAT=
747 SYSTEM_LON=
748 config_load system
749 config_foreach system_config system
750
751 option_cb() {
752 olsrd_update_schema "option" "$@"
753 }
754
755 list_cb() {
756 olsrd_update_schema "list" "$@"
757 }
758
759 . /lib/functions/network.sh
760
761 config_load olsrd
762 reset_cb
763
764 OLSRD_CONFIG_FILE=
765 config_foreach olsrd_find_config_file olsrd
766
767 if [ -z "$OLSRD_CONFIG_FILE" ]; then
768 mkdir -p -- /var/etc/
769 olsrd_write_config > /var/etc/olsrd.conf
770 if [ "$INTERFACES_COUNT" -gt 0 -a "$OLSRD_COUNT" -gt 0 ]; then
771 OLSRD_CONFIG_FILE=/var/etc/olsrd.conf
772 fi
773 fi
774
775 [ -z "$OLSRD_CONFIG_FILE" ] && return 1
776
777 local bindv6only='0'
778 if [ "$OLSRD_IPVERSION_6AND4" -ne 0 ]; then
779 bindv6only="$(sysctl -n net.ipv6.bindv6only)"
780 sysctl -w net.ipv6.bindv6only=1 > /dev/null
781 sed -e '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/d' < "$OLSRD_CONFIG_FILE" > /var/etc/olsrd.conf.ipv6
782 sed -e 's/^IpVersion[ ][ ]*6$/IpVersion 4/' -e 's/^\t\t[A-Fa-f0-9.:]*[:][A-Fa-f0-9.:]*[ ][0-9]*$//' < "$OLSRD_CONFIG_FILE" > /var/etc/olsrd.conf.ipv4
783 rm $OLSRD_CONFIG_FILE
784
785 # some filenames should get the suffix .ipv6
786 for file in $latlon_file $hosts_file $services_file $resolv_file $macs_file $wd_file;do
787 f=$(echo $file|sed 's/\//\\\//g')
788 sed -i "s/$f/$f.ipv6/g" /var/etc/olsrd.conf.ipv6
789 done
790
791 SERVICE_PID_FILE="$PID6"
792 if service_check /usr/sbin/olsrd; then
793 error "there is already an IPv6 instance of olsrd running (pid: '$(cat $PID6)'), not starting."
794 else
795 service_start /usr/sbin/olsrd -f /var/etc/olsrd.conf.ipv6 -nofork
796 fi
797
798 SERVICE_PID_FILE="$PID"
799 if service_check /usr/sbin/olsrd; then
800 error "there is already an IPv4 instance of olsrd running (pid: '$(cat $PID)'), not starting."
801 else
802 service_start /usr/sbin/olsrd -f /var/etc/olsrd.conf.ipv4 -nofork
803 fi
804
805 sleep 3
806 sysctl -w net.ipv6.bindv6only="$bindv6only" > /dev/null
807
808 else
809
810 if [ "$ipversion" = "6" ]; then
811 sed -i '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/d' "$OLSRD_CONFIG_FILE"
812 fi
813
814 SERVICE_PID_FILE="$PID"
815 if service_check /usr/sbin/olsrd; then
816 error "there is already an IPv4 instance of olsrd running (pid: '$(cat $PID)'), not starting."
817 return 1
818 else
819 service_start /usr/sbin/olsrd -f "$OLSRD_CONFIG_FILE" -nofork
820 fi
821 fi
822 olsrd_setup_smartgw_rules
823 }
824
825 stop() {
826 SERVICE_PID_FILE="$PID"
827 service_stop /usr/sbin/olsrd
828
829 SERVICE_PID_FILE="$PID6"
830 service_stop /usr/sbin/olsrd
831 }