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