1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008-2013 OpenWrt.org
9 CONF
=/var
/etc
/olsrd.conf
10 PID
=/var
/run
/olsrd.pid
11 PID6
=/var
/run
/olsrd.ipv6.pid
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'
25 logger
-t olsrd
-p daemon.info
-s "$1"
30 [ -z "$varname" -o "$varname" != "${varname%%[!A-Za-z0-9_]*}" ] && return 1
34 validate_olsrd_option
() {
36 [ -z "$str" -o "$str" != "${str%%[! 0-9A-Za-z./|:_-]*}" ] && return 1
42 local cfgt hostname latlon oldIFS
44 config_get cfgt
"$cfg" TYPE
46 if [ "$cfgt" = "system" ]; then
47 config_get hostname
"$cfg" hostname
48 hostname
="${hostname:-OpenWrt}"
49 SYSTEM_HOSTNAME
="$hostname"
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"
59 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
60 config_get latlon
"$cfg" latitude
62 config_get latlon
"$cfg" longitude
67 olsrd_find_config_file
() {
69 validate_varname
"$cfg" ||
return 0
71 config_get_bool ignore
"$cfg" ignore
0
72 [ "$ignore" -ne 0 ] && return 0
73 config_get OLSRD_CONFIG_FILE
"$cfg" config_file
78 warning_invalid_value
() {
79 local funcname
="warning_invalid_value"
81 validate_varname
"$package" || package
=
83 validate_varname
"$config" || config
=
85 validate_varname
"$option" || option
=
87 if [ -n "$package" -a -n "$config" ]; then
88 log
"$funcname() in option '$package.$config${option:+.}$option', skipped"
90 log
"$funcname() skipped"
96 olsrd_write_option
() {
99 validate_varname
"$cfg" ||
return 1
101 validate_varname
"$option" ||
return 1
103 local option_type
="$5"
105 if [ "$option_type" = bool
]; then
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;;
113 if ! validate_olsrd_option
"$value"; then
114 warning_invalid_value olsrd
"$cfg" "$option"
118 if [ "$value" != "${value%%[G-Zg-z_-]*}" ]; then
119 if [ "$option" != "Ip6AddrType" -a "$option" != "LinkQualityMult" -a "$value" != "yes" -a "$value" != "no" ]; then
124 echo -n "${N}$param$option $value"
129 olsrd_write_plparam
() {
130 local funcname
="olsrd_write_plparam"
133 validate_varname
"$cfg" ||
return 1
135 validate_varname
"$option" ||
return 1
137 local option_type
="$5"
140 if [ "$option_type" = bool
]; then
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;;
148 if ! validate_olsrd_option
"$value"; then
149 warning_invalid_value olsrd
"$cfg" "$option"
153 oldIFS
="$IFS"; IFS
='-_'; set -- $option; IFS
="$oldIFS"
157 if [ "$option" = 'hosts' ]; then
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"
169 log
"$funcname() Warning: mdns Interface '$value' not found, skipped"
172 warning_invalid_value olsrd
"$cfg" "NonOlsrIf"
174 [ -z "$ifname" ] || value
=$ifname
177 echo -n "${N}${param}PlParam \"$option\" \"$value\""
182 config_update_schema
() {
183 local schema_varname
="$1"
184 validate_varname
"$schema_varname" ||
return 1
186 validate_varname
"$command" ||
return 1
188 validate_varname
"$option" ||
return 1
198 eval "export -n -- \"schema=\${$schema_varname}\""
200 for cur_option
in $schema; do
201 [ "${cur_option%%[:=]*}" = "$option" ] && return 0
204 if [ "$command" = list
]; then
206 if [ "$#" -ge "3" ]; then
207 schema_entry
="$option:list3"
208 elif [ "$#" -ge "2" ]; then
209 schema_entry
="$option:list2"
211 schema_entry
="$option:list"
214 schema_entry
="$option"
217 append
"$schema_varname" "$schema_entry"
222 config_write_options
() {
223 local funcname
="config_write_options"
226 validate_varname
"$cfg" ||
return 1
227 local write_func
="$3"
228 [ -z "$write_func" ] && output_func
=echo
229 local write_param
="$4"
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"
234 get_value_for_entry
()
236 local schema_entry
="$1"
238 default
="${schema_entry#*[=]}"
239 [ "$default" = "$schema_entry" ] && default
=
240 option
="${schema_entry%%[=]*}"
242 oldIFS
="$IFS"; IFS
=':'; set -- $option; IFS
="$oldIFS"
246 validate_varname
"$option" ||
return 1
247 [ -z "$option_type" ] || validate_varname
"$option_type" ||
return 1
248 [ "$option_type" = internal
] && return 1
250 config_get value
"$cfg" "$option"
251 [ "$option" = "speed" ] && return 1
268 already_in_schema
"speed" && {
269 get_value_for_entry
"speed"
271 if [ 2>/dev
/null
$value -gt 0 -a $value -le 20 ]; then
274 log
"$funcname() Warning: invalid speed-value: '$value' - allowed integers: 1...20, fallback to 6"
278 for schema_entry
in $list_speed_vars; do {
279 already_in_schema
"$schema_entry" || schema
="$schema $schema_entry"
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
287 value
="$(( $speed / 2 + 1 )).0"
290 value
="$(( $speed * 25 )).0"
292 TcInterval
) # todo: not fisheye? -> $(( $speed * 2 ))
293 value
=$
(( $speed / 2 ))
294 [ $value -eq 0 ] && value
=1
298 value
="$(( $speed * 100 )).0"
301 value
="$(( $speed * 5 )).0"
304 value
="$(( $speed * 100 )).0"
307 value
="$(( $speed * 2 )).0"
310 value
="$(( $speed * 25 )).0"
313 get_value_for_entry
"$schema_entry" ||
continue
319 case " $list_speed_vars " in
329 is_speed_var
"$schema_entry" && option
="$schema_entry"
331 get_value_for_entry
"$schema_entry" ||
continue
334 if [ -z "$value" ]; then
335 oldIFS
="$IFS"; IFS
='+'; set -- $default; IFS
="$oldIFS"
337 elif [ "$value" = '-' -a -n "$default" ]; then
341 [ -z "$value" ] && continue
343 case "$option_type" in
350 if [ "$list_size" -gt 0 ]; then
351 config_get option_length
"$cfg" "${option}_LENGTH"
352 if [ -n "$option_length" ]; then
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
362 for list_item
in $value; do
363 append
"list_value" "$list_item"
365 position
=$
((i
% list_size
))
366 if [ "$position" -eq 0 ]; then
367 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" ||
break
371 [ "$position" -ne 0 ] && "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type"
374 "$write_func" "$write_param" "$cfg" "$option" "$value" "$option_type"
381 olsrd_write_olsrd
() {
383 validate_varname
"$cfg" ||
return 0
386 config_get_bool ignore
"$cfg" ignore
0
387 [ "$ignore" -ne 0 ] && return 0
389 [ "$OLSRD_COUNT" -gt 0 ] && return 0
391 config_get ipversion
"$cfg" IpVersion
392 if [ "$ipversion" = "6and4" ]; then
393 OLSRD_IPVERSION_6AND4
=1
394 config_set
"$cfg" IpVersion
'6'
396 config_get smartgateway
"$cfg" SmartGateway
397 config_get smartgatewayuplink
"$cfg" SmartGatewayUplink
399 config_write_options
"$OLSRD_OLSRD_SCHEMA" "$cfg" olsrd_write_option
401 OLSRD_COUNT
=$
((OLSRD_COUNT
+ 1))
405 olsrd_write_ipcconnect
() {
407 validate_varname
"$cfg" ||
return 0
410 config_get_bool ignore
"$cfg" ignore
0
411 [ "$ignore" -ne 0 ] && return 0
413 [ "$IPCCONNECT_COUNT" -gt 0 ] && return 0
415 echo -n "${N}IpcConnect${N}{"
416 config_write_options
"$OLSRD_IPCCONNECT_SCHEMA" "$cfg" olsrd_write_option
"${T}"
418 IPCCONNECT_COUNT
=$
((IPCCONNECT_COUNT
+ 1))
425 validate_varname
"$cfg" ||
return 0
428 config_get_bool ignore
"$cfg" ignore
0
429 [ "$ignore" -ne 0 ] && return 0
431 config_get netaddr
"$cfg" netaddr
432 if ! validate_olsrd_option
"$netaddr"; then
433 warning_invalid_value olsrd
"$cfg" "netaddr"
437 config_get netmask
"$cfg" netmask
438 if ! validate_olsrd_option
"$netmask"; then
439 warning_invalid_value olsrd
"$cfg" "netmask"
443 [ "$HNA4_COUNT" -le 0 ] && echo -n "${N}Hna4${N}{"
444 echo -n "${N}${T}${T}$netaddr $netmask"
445 HNA4_COUNT
=$
((HNA4_COUNT
+ 1))
452 validate_varname
"$cfg" ||
return 0
455 config_get_bool ignore
"$cfg" ignore
0
456 [ "$ignore" -ne 0 ] && return 0
458 config_get netaddr
"$cfg" netaddr
459 if ! validate_olsrd_option
"$netaddr"; then
460 warning_invalid_value olsrd
"$cfg" "netaddr"
464 config_get prefix
"$cfg" prefix
465 if ! validate_olsrd_option
"$prefix"; then
466 warning_invalid_value olsrd
"$cfg" "prefix"
470 [ "$HNA6_COUNT" -le 0 ] && echo -n "${N}Hna6${N}{"
471 echo -n "${N}${T}${T}$netaddr $prefix"
472 HNA6_COUNT
=$
((HNA6_COUNT
+ 1))
477 olsrd_write_loadplugin
() {
478 local funcname
="olsrd_write_loadplugin"
480 validate_varname
"$cfg" ||
return 0
488 config_get_bool ignore
"$cfg" ignore
0
489 [ "$ignore" -ne 0 ] && return 0
491 config_get library
"$cfg" library
492 if ! validate_olsrd_option
"$library"; then
493 warning_invalid_value olsrd
"$cfg" "library"
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"
503 config_get name
"$cfg" name
504 [ -z "$name" ] && config_set
"$cfg" name
$SYSTEM_HOSTNAME
506 config_get suffix
"$cfg" suffix
507 [ -z "$suffix" ] && config_set
"$cfg" suffix
'.olsr'
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'
518 config_set
"$cfg" lat
"$SYSTEM_LAT"
519 config_set
"$cfg" lon
"$SYSTEM_LON"
523 for f
in latlon_file hosts_file services_file resolv_file macs_file
; do
524 config_get
$f "$cfg" $f
527 [ -z "$latlon_file" ] && config_set
"$cfg" latlon_file
'/var/run/latlon.js'
530 config_get wd_file
"$cfg" file
534 echo -n "${N}LoadPlugin \"$library\"${N}{"
535 config_write_options
"$OLSRD_LOADPLUGIN_SCHEMA" "$cfg" olsrd_write_plparam
"${T}"
541 olsrd_write_interface
() {
542 local funcname
="olsrd_write_interface"
544 validate_varname
"$cfg" ||
return 0
550 config_get_bool ignore
"$cfg" ignore
0
551 [ "$ignore" -ne 0 ] && return 0
554 config_get interfaces
"$cfg" interface
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"
562 log
"$funcname() Warning: Interface '$interface' not found, skipped"
565 warning_invalid_value olsrd
"$cfg" "interface"
569 [ -z "$ifnames" ] && return 0
571 echo -n "${N}Interface$ifnames${N}{"
572 config_write_options
"$OLSRD_INTERFACE_SCHEMA" "$cfg" olsrd_write_option
"${T}"
574 INTERFACES_COUNT
=$
((INTERFACES_COUNT
+ 1))
579 olsrd_write_interface_defaults
() {
581 validate_varname
"$cfg" ||
return 0
583 echo -n "${N}InterfaceDefaults$ifnames${N}{"
584 config_write_options
"$OLSRD_INTERFACE_DEFAULTS_SCHEMA" "$cfg" olsrd_write_option
"${T}"
590 olsrd_update_schema
() {
592 validate_varname
"$command" ||
return 0
594 validate_varname
"$varname" ||
return 0
596 local cfg
="$CONFIG_SECTION"
600 config_get cfgt
"$cfg" TYPE
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";;
612 olsrd_write_config
() {
613 OLSRD_IPVERSION_6AND4
=0
615 config_foreach olsrd_write_olsrd olsrd
617 config_foreach olsrd_write_ipcconnect IpcConnect
619 config_foreach olsrd_write_hna4 Hna4
620 [ "$HNA4_COUNT" -gt 0 ] && echo "${N}}"
622 config_foreach olsrd_write_hna6 Hna6
623 [ "$HNA6_COUNT" -gt 0 ] && echo "${N}}"
624 config_foreach olsrd_write_loadplugin LoadPlugin
626 config_foreach olsrd_write_interface_defaults InterfaceDefaults
627 config_foreach olsrd_write_interface Interface
635 local wanifnames word catch_next
637 which ip
>/dev
/null ||
return 1
639 set -- $
( ip route list exact
0.0.0.0/0 table all
)
646 [ -n "$catch_next" ] && {
647 case "$wanifnames" in
651 wanifnames
="$wanifnames $word "
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."
672 local wanifnames
="$( get_wan_ifnames )"
674 if [ -z "$wanifnames" ]; then
680 IP4T
=$
(which iptables
)
681 IP6T
=$
(which ip6tables
)
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
689 for IFACE
in $ifsglobal; do
690 while $IPT -D input_rule
-i $IFACE -p 4 -j ACCEPT
2> /dev
/null
; do :; done
693 while $IP4T -t nat
-D postrouting_rule
-o tnl_
+ -j MASQUERADE
2> /dev
/null
; do :;done
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
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
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
719 for IFACE
in $ifsglobal; do
720 $IP6T -I input_rule
-i $IFACE -p 4 -j ACCEPT
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
731 for IFACE
in $ifsglobal; do
732 $IPT -I input_rule
-i $IFACE -p 4 -j ACCEPT
741 log
"error() ${initscript}: $@"
749 config_foreach system_config system
752 olsrd_update_schema
"option" "$@"
756 olsrd_update_schema
"list" "$@"
759 .
/lib
/functions
/network.sh
765 config_foreach olsrd_find_config_file olsrd
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
775 [ -z "$OLSRD_CONFIG_FILE" ] && return 1
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
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
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."
795 service_start
/usr
/sbin
/olsrd
-f /var
/etc
/olsrd.conf.ipv6
-nofork
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."
802 service_start
/usr
/sbin
/olsrd
-f /var
/etc
/olsrd.conf.ipv4
-nofork
806 sysctl
-w net.ipv6.bindv6only
="$bindv6only" > /dev
/null
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"
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."
819 service_start
/usr
/sbin
/olsrd
-f "$OLSRD_CONFIG_FILE" -nofork
820 service_check
/usr
/sbin
/olsrd ||
{
821 log
"startup-error: check via: '/usr/sbin/olsrd -f \"$OLSRD_CONFIG_FILE\" -nofork'"
826 olsrd_setup_smartgw_rules
830 SERVICE_PID_FILE
="$PID"
831 service_stop
/usr
/sbin
/olsrd
833 SERVICE_PID_FILE
="$PID6"
834 service_stop
/usr
/sbin
/olsrd