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'
24 logger
-t olsrd
-p daemon.info
-s "${initscript}: $@"
33 [ -z "$varname" -o "$varname" != "${varname%%[!A-Za-z0-9_]*}" ] && return 1
37 validate_olsrd_option
() {
39 [ -z "$str" -o "$str" != "${str%%[! 0-9A-Za-z./|:_-]*}" ] && return 1
45 local cfgt hostname latlon oldIFS
47 config_get cfgt
"$cfg" TYPE
49 if [ "$cfgt" = "system" ]; then
50 config_get hostname
"$cfg" hostname
51 hostname
="${hostname:-OpenWrt}"
52 SYSTEM_HOSTNAME
="$hostname"
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"
62 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
63 config_get latlon
"$cfg" latitude
65 config_get latlon
"$cfg" longitude
70 olsrd_find_config_file
() {
72 validate_varname
"$cfg" ||
return 0
74 config_get_bool ignore
"$cfg" ignore
0
75 [ "$ignore" -ne 0 ] && return 0
76 config_get OLSRD_CONFIG_FILE
"$cfg" config_file
81 warning_invalid_value
() {
82 local funcname
="warning_invalid_value"
84 validate_varname
"$package" || package
=
86 validate_varname
"$config" || config
=
88 validate_varname
"$option" || option
=
90 if [ -n "$package" -a -n "$config" ]; then
91 log
"$funcname() in option '$package.$config${option:+.}$option', skipped"
93 log
"$funcname() skipped"
99 olsrd_write_option
() {
102 validate_varname
"$cfg" ||
return 1
104 validate_varname
"$option" ||
return 1
106 local option_type
="$5"
108 if [ "$option_type" = bool
]; then
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;;
116 if ! validate_olsrd_option
"$value"; then
117 warning_invalid_value olsrd
"$cfg" "$option"
121 if [ "$value" != "${value%%[G-Zg-z_-]*}" ]; then
122 if [ "$option" != "Ip6AddrType" -a "$option" != "LinkQualityMult" -a "$value" != "yes" -a "$value" != "no" ]; then
127 echo -n "${N}$param$option $value"
132 olsrd_write_plparam
() {
133 local funcname
="olsrd_write_plparam"
136 validate_varname
"$cfg" ||
return 1
138 validate_varname
"$option" ||
return 1
140 local option_type
="$5"
143 if [ "$option_type" = bool
]; then
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;;
151 if ! validate_olsrd_option
"$value"; then
152 warning_invalid_value olsrd
"$cfg" "$option"
156 oldIFS
="$IFS"; IFS
='-_'; set -- $option; IFS
="$oldIFS"
160 if [ "$option" = 'hosts' ]; then
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"
172 log
"$funcname() Warning: mdns Interface '$value' not found, skipped"
175 warning_invalid_value olsrd
"$cfg" "NonOlsrIf"
177 [ -z "$ifname" ] || value
=$ifname
180 echo -n "${N}${param}PlParam \"$option\" \"$value\""
185 config_update_schema
() {
186 local schema_varname
="$1"
187 validate_varname
"$schema_varname" ||
return 1
189 validate_varname
"$command" ||
return 1
191 validate_varname
"$option" ||
return 1
201 eval "export -n -- \"schema=\${$schema_varname}\""
203 for cur_option
in $schema; do
204 [ "${cur_option%%[:=]*}" = "$option" ] && return 0
207 if [ "$command" = list
]; then
209 if [ "$#" -ge "3" ]; then
210 schema_entry
="$option:list3"
211 elif [ "$#" -ge "2" ]; then
212 schema_entry
="$option:list2"
214 schema_entry
="$option:list"
217 schema_entry
="$option"
220 append
"$schema_varname" "$schema_entry"
225 config_write_options
() {
226 local funcname
="config_write_options"
229 validate_varname
"$cfg" ||
return 1
230 local write_func
="$3"
231 [ -z "$write_func" ] && output_func
=echo
232 local write_param
="$4"
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"
237 get_value_for_entry
()
239 local schema_entry
="$1"
241 default
="${schema_entry#*[=]}"
242 [ "$default" = "$schema_entry" ] && default
=
243 option
="${schema_entry%%[=]*}"
245 oldIFS
="$IFS"; IFS
=':'; set -- $option; IFS
="$oldIFS"
249 validate_varname
"$option" ||
return 1
250 [ -z "$option_type" ] || validate_varname
"$option_type" ||
return 1
251 [ "$option_type" = internal
] && return 1
253 config_get value
"$cfg" "$option"
254 [ "$option" = "speed" ] && return 1
271 already_in_schema
"speed" && {
272 get_value_for_entry
"speed"
274 if [ 2>/dev
/null
$value -gt 0 -a $value -le 20 ]; then
277 log
"$funcname() Warning: invalid speed-value: '$value' - allowed integers: 1...20, fallback to 6"
281 for schema_entry
in $list_speed_vars; do {
282 already_in_schema
"$schema_entry" || schema
="$schema $schema_entry"
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
290 value
="$(( $speed / 2 + 1 )).0"
293 value
="$(( $speed * 25 )).0"
295 TcInterval
) # todo: not fisheye? -> $(( $speed * 2 ))
296 value
=$
(( $speed / 2 ))
297 [ $value -eq 0 ] && value
=1
301 value
="$(( $speed * 100 )).0"
304 value
="$(( $speed * 5 )).0"
307 value
="$(( $speed * 100 )).0"
310 value
="$(( $speed * 2 )).0"
313 value
="$(( $speed * 25 )).0"
316 get_value_for_entry
"$schema_entry" ||
continue
322 case " $list_speed_vars " in
332 is_speed_var
"$schema_entry" && option
="$schema_entry"
334 get_value_for_entry
"$schema_entry" ||
continue
337 if [ -z "$value" ]; then
338 oldIFS
="$IFS"; IFS
='+'; set -- $default; IFS
="$oldIFS"
340 elif [ "$value" = '-' -a -n "$default" ]; then
344 [ -z "$value" ] && continue
346 case "$option_type" in
353 if [ "$list_size" -gt 0 ]; then
354 config_get option_length
"$cfg" "${option}_LENGTH"
355 if [ -n "$option_length" ]; then
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
365 for list_item
in $value; do
366 append
"list_value" "$list_item"
368 position
=$
((i
% list_size
))
369 if [ "$position" -eq 0 ]; then
370 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" ||
break
374 [ "$position" -ne 0 ] && "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type"
377 "$write_func" "$write_param" "$cfg" "$option" "$value" "$option_type"
384 olsrd_write_olsrd
() {
386 validate_varname
"$cfg" ||
return 0
389 config_get_bool ignore
"$cfg" ignore
0
390 [ "$ignore" -ne 0 ] && return 0
392 [ "$OLSRD_COUNT" -gt 0 ] && return 0
394 config_get ipversion
"$cfg" IpVersion
395 if [ "$ipversion" = "6and4" ]; then
396 OLSRD_IPVERSION_6AND4
=1
397 config_set
"$cfg" IpVersion
'6'
399 config_get smartgateway
"$cfg" SmartGateway
400 config_get smartgatewayuplink
"$cfg" SmartGatewayUplink
402 config_write_options
"$OLSRD_OLSRD_SCHEMA" "$cfg" olsrd_write_option
404 OLSRD_COUNT
=$
((OLSRD_COUNT
+ 1))
408 olsrd_write_ipcconnect
() {
410 validate_varname
"$cfg" ||
return 0
413 config_get_bool ignore
"$cfg" ignore
0
414 [ "$ignore" -ne 0 ] && return 0
416 [ "$IPCCONNECT_COUNT" -gt 0 ] && return 0
418 echo -n "${N}IpcConnect${N}{"
419 config_write_options
"$OLSRD_IPCCONNECT_SCHEMA" "$cfg" olsrd_write_option
"${T}"
421 IPCCONNECT_COUNT
=$
((IPCCONNECT_COUNT
+ 1))
428 validate_varname
"$cfg" ||
return 0
431 config_get_bool ignore
"$cfg" ignore
0
432 [ "$ignore" -ne 0 ] && return 0
434 config_get netaddr
"$cfg" netaddr
435 if ! validate_olsrd_option
"$netaddr"; then
436 warning_invalid_value olsrd
"$cfg" "netaddr"
440 config_get netmask
"$cfg" netmask
441 if ! validate_olsrd_option
"$netmask"; then
442 warning_invalid_value olsrd
"$cfg" "netmask"
446 [ "$HNA4_COUNT" -le 0 ] && echo -n "${N}Hna4${N}{"
447 echo -n "${N}${T}${T}$netaddr $netmask"
448 HNA4_COUNT
=$
((HNA4_COUNT
+ 1))
455 validate_varname
"$cfg" ||
return 0
458 config_get_bool ignore
"$cfg" ignore
0
459 [ "$ignore" -ne 0 ] && return 0
461 config_get netaddr
"$cfg" netaddr
462 if ! validate_olsrd_option
"$netaddr"; then
463 warning_invalid_value olsrd
"$cfg" "netaddr"
467 config_get prefix
"$cfg" prefix
468 if ! validate_olsrd_option
"$prefix"; then
469 warning_invalid_value olsrd
"$cfg" "prefix"
473 [ "$HNA6_COUNT" -le 0 ] && echo -n "${N}Hna6${N}{"
474 echo -n "${N}${T}${T}$netaddr $prefix"
475 HNA6_COUNT
=$
((HNA6_COUNT
+ 1))
480 olsrd_write_loadplugin
() {
481 local funcname
="olsrd_write_loadplugin"
483 validate_varname
"$cfg" ||
return 0
491 config_get_bool ignore
"$cfg" ignore
0
492 [ "$ignore" -ne 0 ] && return 0
494 config_get library
"$cfg" library
495 if ! validate_olsrd_option
"$library"; then
496 warning_invalid_value olsrd
"$cfg" "library"
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"
506 config_get name
"$cfg" name
507 [ -z "$name" ] && config_set
"$cfg" name
$SYSTEM_HOSTNAME
509 config_get suffix
"$cfg" suffix
510 [ -z "$suffix" ] && config_set
"$cfg" suffix
'.olsr'
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'
521 config_set
"$cfg" lat
"$SYSTEM_LAT"
522 config_set
"$cfg" lon
"$SYSTEM_LON"
526 for f
in latlon_file hosts_file services_file resolv_file macs_file
; do
527 config_get
$f "$cfg" $f
530 [ -z "$latlon_file" ] && config_set
"$cfg" latlon_file
'/var/run/latlon.js'
533 config_get wd_file
"$cfg" file
537 echo -n "${N}LoadPlugin \"$library\"${N}{"
538 config_write_options
"$OLSRD_LOADPLUGIN_SCHEMA" "$cfg" olsrd_write_plparam
"${T}"
544 olsrd_write_interface
() {
545 local funcname
="olsrd_write_interface"
547 validate_varname
"$cfg" ||
return 0
553 config_get_bool ignore
"$cfg" ignore
0
554 [ "$ignore" -ne 0 ] && return 0
557 config_get interfaces
"$cfg" interface
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"
565 log
"$funcname() Warning: Interface '$interface' not found, skipped"
568 warning_invalid_value olsrd
"$cfg" "interface"
572 [ -z "$ifnames" ] && return 0
574 echo -n "${N}Interface$ifnames${N}{"
575 config_write_options
"$OLSRD_INTERFACE_SCHEMA" "$cfg" olsrd_write_option
"${T}"
577 INTERFACES_COUNT
=$
((INTERFACES_COUNT
+ 1))
582 olsrd_write_interface_defaults
() {
584 validate_varname
"$cfg" ||
return 0
586 echo -n "${N}InterfaceDefaults$ifnames${N}{"
587 config_write_options
"$OLSRD_INTERFACE_DEFAULTS_SCHEMA" "$cfg" olsrd_write_option
"${T}"
593 olsrd_update_schema
() {
595 validate_varname
"$command" ||
return 0
597 validate_varname
"$varname" ||
return 0
599 local cfg
="$CONFIG_SECTION"
603 config_get cfgt
"$cfg" TYPE
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";;
615 olsrd_write_config
() {
616 OLSRD_IPVERSION_6AND4
=0
618 config_foreach olsrd_write_olsrd olsrd
620 config_foreach olsrd_write_ipcconnect IpcConnect
622 config_foreach olsrd_write_hna4 Hna4
623 [ "$HNA4_COUNT" -gt 0 ] && echo "${N}}"
625 config_foreach olsrd_write_hna6 Hna6
626 [ "$HNA6_COUNT" -gt 0 ] && echo "${N}}"
627 config_foreach olsrd_write_loadplugin LoadPlugin
629 config_foreach olsrd_write_interface_defaults InterfaceDefaults
630 config_foreach olsrd_write_interface Interface
638 local wanifnames word catch_next
640 which ip
>/dev
/null ||
return 1
642 set -- $
( ip route list exact
0.0.0.0/0 table all
)
649 [ -n "$catch_next" ] && {
650 case "$wanifnames" in
654 wanifnames
="$wanifnames $word "
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."
675 local wanifnames
="$( get_wan_ifnames )"
677 if [ -z "$wanifnames" ]; then
683 IP4T
=$
(which iptables
)
684 IP6T
=$
(which ip6tables
)
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
692 for IFACE
in $ifsglobal; do
693 while $IPT -D input_rule
-i $IFACE -p 4 -j ACCEPT
2> /dev
/null
; do :; done
696 while $IP4T -t nat
-D postrouting_rule
-o tnl_
+ -j MASQUERADE
2> /dev
/null
; do :;done
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
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
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
722 for IFACE
in $ifsglobal; do
723 $IP6T -I input_rule
-i $IFACE -p 4 -j ACCEPT
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
734 for IFACE
in $ifsglobal; do
735 $IPT -I input_rule
-i $IFACE -p 4 -j ACCEPT
748 config_foreach system_config system
751 olsrd_update_schema
"option" "$@"
755 olsrd_update_schema
"list" "$@"
758 .
/lib
/functions
/network.sh
764 config_foreach olsrd_find_config_file olsrd
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
774 [ -z "$OLSRD_CONFIG_FILE" ] && return 1
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
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
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."
794 service_start
/usr
/sbin
/olsrd
-f /var
/etc
/olsrd.conf.ipv6
-nofork
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."
801 service_start
/usr
/sbin
/olsrd
-f /var
/etc
/olsrd.conf.ipv4
-nofork
805 sysctl
-w net.ipv6.bindv6only
="$bindv6only" > /dev
/null
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"
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."
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'"
825 olsrd_setup_smartgw_rules
829 SERVICE_PID_FILE
="$PID"
830 service_stop
/usr
/sbin
/olsrd
832 SERVICE_PID_FILE
="$PID6"
833 service_stop
/usr
/sbin
/olsrd