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