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