1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008-2017 OpenWrt.org
9 OLSRD_OLSRD_SCHEMA
='ignore:internal config_file:internal DebugLevel=0 AllowNoInt=yes'
10 OLSRD_IPCCONNECT_SCHEMA
='ignore:internal Host:list Net:list2'
11 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 ipv6only:bool'
12 OLSRD_INTERFACE_SCHEMA
='ignore:internal interface:internal AutoDetectChanges:bool LinkQualityMult:list2'
13 OLSRD_INTERFACE_DEFAULTS_SCHEMA
='AutoDetectChanges:bool'
20 logger
-t olsrd
-p daemon.info
-s "${initscript}: $@"
24 logger
-t olsrd
-p daemon.err
-s "${initscript}: ERROR: $@"
28 logger
-t olsrd
-p daemon.warn
-s "${initscript}: WARNING: $@"
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"
163 if [ "$option" = 'hosts' ]; then
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"
175 log
"$funcname() Warning: mdns Interface '$value' not found, skipped"
178 warning_invalid_value olsrd
"$cfg" "NonOlsrIf"
180 [ -z "$ifname" ] || value
=$ifname
183 echo -n "${N}${param}PlParam \"$option\" \"$value\""
188 config_update_schema
() {
189 local schema_varname
="$1"
190 validate_varname
"$schema_varname" ||
return 1
192 validate_varname
"$command" ||
return 1
194 validate_varname
"$option" ||
return 1
204 eval "export -n -- \"schema=\${$schema_varname}\""
206 for cur_option
in $schema; do
207 [ "${cur_option%%[:=]*}" = "$option" ] && return 0
210 if [ "$command" = list
]; then
212 if [ "$#" -ge "3" ]; then
213 schema_entry
="$option:list3"
214 elif [ "$#" -ge "2" ]; then
215 schema_entry
="$option:list2"
217 schema_entry
="$option:list"
220 schema_entry
="$option"
223 append
"$schema_varname" "$schema_entry"
228 config_write_options
() {
229 local funcname
="config_write_options"
232 validate_varname
"$cfg" ||
return 1
233 local write_func
="$3"
234 [ -z "$write_func" ] && output_func
=echo
235 local write_param
="$4"
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"
240 get_value_for_entry
()
242 local schema_entry
="$1"
244 default
="${schema_entry#*[=]}"
245 [ "$default" = "$schema_entry" ] && default
=
246 option
="${schema_entry%%[=]*}"
248 oldIFS
="$IFS"; IFS
=':'; set -- $option; IFS
="$oldIFS"
252 validate_varname
"$option" ||
return 1
253 [ -z "$option_type" ] || validate_varname
"$option_type" ||
return 1
254 [ "$option_type" = internal
] && return 1
256 config_get value
"$cfg" "$option"
257 [ "$option" = "speed" ] && return 1
274 already_in_schema
"speed" && {
275 get_value_for_entry
"speed"
277 if [ 2>/dev
/null
$value -gt 0 -a $value -le 20 ]; then
280 log
"$funcname() Warning: invalid speed-value: '$value' - allowed integers: 1...20, fallback to 6"
284 for schema_entry
in $list_speed_vars; do {
285 already_in_schema
"$schema_entry" || schema
="$schema $schema_entry"
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
293 value
="$(( $speed / 2 + 1 )).0"
296 value
="$(( $speed * 25 )).0"
298 TcInterval
) # todo: not fisheye? -> $(( $speed * 2 ))
299 value
=$
(( $speed / 2 ))
300 [ $value -eq 0 ] && value
=1
304 value
="$(( $speed * 100 )).0"
307 value
="$(( $speed * 5 )).0"
310 value
="$(( $speed * 100 )).0"
313 value
="$(( $speed * 2 )).0"
316 value
="$(( $speed * 25 )).0"
319 get_value_for_entry
"$schema_entry" ||
continue
325 case " $list_speed_vars " in
335 is_speed_var
"$schema_entry" && option
="$schema_entry"
337 get_value_for_entry
"$schema_entry" ||
continue
340 if [ -z "$value" ]; then
341 oldIFS
="$IFS"; IFS
='+'; set -- $default; IFS
="$oldIFS"
343 elif [ "$value" = '-' -a -n "$default" ]; then
347 [ -z "$value" ] && continue
349 case "$option_type" in
356 if [ "$list_size" -gt 0 ]; then
357 config_get option_length
"$cfg" "${option}_LENGTH"
358 if [ -n "$option_length" ]; then
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
368 for list_item
in $value; do
369 append
"list_value" "$list_item"
371 position
=$
((i
% list_size
))
372 if [ "$position" -eq 0 ]; then
373 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" ||
break
377 [ "$position" -ne 0 ] && "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type"
380 "$write_func" "$write_param" "$cfg" "$option" "$value" "$option_type"
387 olsrd_write_olsrd
() {
389 validate_varname
"$cfg" ||
return 0
392 config_get_bool ignore
"$cfg" ignore
0
393 [ "$ignore" -ne 0 ] && return 0
395 [ "$OLSRD_COUNT" -gt 0 ] && return 0
397 config_get smartgateway
"$cfg" SmartGateway
398 config_get smartgatewayuplink
"$cfg" SmartGatewayUplink
400 config_write_options
"$OLSRD_OLSRD_SCHEMA" "$cfg" olsrd_write_option
402 OLSRD_COUNT
=$
((OLSRD_COUNT
+ 1))
406 olsrd_write_ipcconnect
() {
408 validate_varname
"$cfg" ||
return 0
411 config_get_bool ignore
"$cfg" ignore
0
412 [ "$ignore" -ne 0 ] && return 0
414 [ "$IPCCONNECT_COUNT" -gt 0 ] && return 0
416 echo -n "${N}IpcConnect${N}{"
417 config_write_options
"$OLSRD_IPCCONNECT_SCHEMA" "$cfg" olsrd_write_option
"${T}"
419 IPCCONNECT_COUNT
=$
((IPCCONNECT_COUNT
+ 1))
426 validate_varname
"$cfg" ||
return 0
429 config_get_bool ignore
"$cfg" ignore
0
430 [ "$ignore" -ne 0 ] && return 0
432 config_get netaddr
"$cfg" netaddr
433 if ! validate_olsrd_option
"$netaddr"; then
434 warning_invalid_value olsrd
"$cfg" "netaddr"
438 config_get netmask
"$cfg" netmask
439 if ! validate_olsrd_option
"$netmask"; then
440 warning_invalid_value olsrd
"$cfg" "netmask"
444 [ "$HNA4_COUNT" -le 0 ] && echo -n "${N}Hna4${N}{"
445 echo -n "${N}${T}${T}$netaddr $netmask"
446 HNA4_COUNT
=$
((HNA4_COUNT
+ 1))
453 validate_varname
"$cfg" ||
return 0
456 config_get_bool ignore
"$cfg" ignore
0
457 [ "$ignore" -ne 0 ] && return 0
459 config_get netaddr
"$cfg" netaddr
460 if ! validate_olsrd_option
"$netaddr"; then
461 warning_invalid_value olsrd
"$cfg" "netaddr"
465 config_get prefix
"$cfg" prefix
466 if ! validate_olsrd_option
"$prefix"; then
467 warning_invalid_value olsrd
"$cfg" "prefix"
471 [ "$HNA6_COUNT" -le 0 ] && echo -n "${N}Hna6${N}{"
472 echo -n "${N}${T}${T}$netaddr $prefix"
473 HNA6_COUNT
=$
((HNA6_COUNT
+ 1))
478 olsrd_write_loadplugin
() {
479 local funcname
="olsrd_write_loadplugin"
481 validate_varname
"$cfg" ||
return 0
489 config_get_bool ignore
"$cfg" ignore
0
490 [ "$ignore" -ne 0 ] && return 0
492 config_get library
"$cfg" library
493 if ! validate_olsrd_option
"$library"; then
494 warning_invalid_value olsrd
"$cfg" "library"
497 if ! [ -x "/lib/$library" -o -x "/usr/lib/$library" -o -x "/usr/local/lib/$library" ]; then
498 log
"$funcname() Warning: Plugin library '$library' not found, skipped"
504 config_get name
"$cfg" name
505 [ -z "$name" ] && config_set
"$cfg" name
$SYSTEM_HOSTNAME
507 config_get suffix
"$cfg" suffix
508 [ -z "$suffix" ] && config_set
"$cfg" suffix
'.olsr'
510 config_get lat
"$cfg" lat
511 config_get lon
"$cfg" lon
512 config_get latlon_infile
"$cfg" latlon_infile
513 if [ \
( -z "$lat" -o -z "$lat" \
) -a -z "$latlon_infile" ]; then
514 if [ -f '/var/run/latlon.txt' ]; then
515 config_set
"$cfg" lat
''
516 config_set
"$cfg" lon
''
517 config_set
"$cfg" latlon_infile
'/var/run/latlon.txt'
519 config_set
"$cfg" lat
"$SYSTEM_LAT"
520 config_set
"$cfg" lon
"$SYSTEM_LON"
524 for f
in latlon_file hosts_file services_file resolv_file macs_file
; do
525 config_get
$f "$cfg" $f
528 [ -z "$latlon_file" ] && config_set
"$cfg" latlon_file
'/var/run/latlon.js'
531 config_get wd_file
"$cfg" file
535 echo -n "${N}LoadPlugin \"$library\"${N}{"
536 config_write_options
"$OLSRD_LOADPLUGIN_SCHEMA" "$cfg" olsrd_write_plparam
"${T}"
542 olsrd_write_interface
() {
543 local funcname
="olsrd_write_interface"
545 validate_varname
"$cfg" ||
return 0
551 config_get_bool ignore
"$cfg" ignore
0
552 [ "$ignore" -ne 0 ] && return 0
555 config_get interfaces
"$cfg" interface
557 for interface
in $interfaces; do
558 if validate_varname
"$interface"; then
559 if network_get_device IFNAME
"$interface"; then
560 ifnames
="$ifnames \"$IFNAME\""
561 ifsglobal
="$ifsglobal $IFNAME"
562 elif network_get_physdev IFNAME
"$interface"; then
563 local proto
="$(uci -q get network.${interface}.proto)"
564 if [ "$proto" = "static" -o "$proto" = "none" ]; then
565 ifnames
="$ifnames \"$IFNAME\""
566 ifsglobal
="$ifsglobal $IFNAME"
569 log
"$funcname() Warning: Interface '$interface' not found, skipped"
572 warning_invalid_value olsrd
"$cfg" "interface"
576 [ -z "$ifnames" ] && return 0
578 echo -n "${N}Interface$ifnames${N}{"
579 config_write_options
"$OLSRD_INTERFACE_SCHEMA" "$cfg" olsrd_write_option
"${T}"
581 INTERFACES_COUNT
=$
((INTERFACES_COUNT
+ 1))
586 olsrd_write_interface_defaults
() {
588 validate_varname
"$cfg" ||
return 0
590 echo -n "${N}InterfaceDefaults$ifnames${N}{"
591 config_write_options
"$OLSRD_INTERFACE_DEFAULTS_SCHEMA" "$cfg" olsrd_write_option
"${T}"
597 olsrd_update_schema
() {
599 validate_varname
"$command" ||
return 0
601 validate_varname
"$varname" ||
return 0
603 local cfg
="$CONFIG_SECTION"
607 config_get cfgt
"$cfg" TYPE
609 olsrd
) config_update_schema OLSRD_OLSRD_SCHEMA
"$command" "$varname" "$value";;
610 IpcConnect
) config_update_schema OLSRD_IPCCONNECT_SCHEMA
"$command" "$varname" "$value";;
611 LoadPlugin
) config_update_schema OLSRD_LOADPLUGIN_SCHEMA
"$command" "$varname" "$value";;
612 Interface
) config_update_schema OLSRD_INTERFACE_SCHEMA
"$command" "$varname" "$value";;
613 InterfaceDefaults
) config_update_schema OLSRD_INTERFACE_DEFAULTS_SCHEMA
"$command" "$varname" "$value";;
619 olsrd_write_config
() {
621 config_foreach olsrd_write_olsrd olsrd
623 config_foreach olsrd_write_ipcconnect IpcConnect
625 config_foreach olsrd_write_hna4 Hna4
626 [ "$HNA4_COUNT" -gt 0 ] && echo "${N}}"
628 config_foreach olsrd_write_hna6 Hna6
629 [ "$HNA6_COUNT" -gt 0 ] && echo "${N}}"
630 config_foreach olsrd_write_loadplugin LoadPlugin
632 config_foreach olsrd_write_interface_defaults InterfaceDefaults
633 config_foreach olsrd_write_interface Interface
641 local wanifnames word catch_next
643 which ip
>/dev
/null ||
return 1
645 set -- $
( ip route list exact
0.0.0.0/0 table all
)
652 [ -n "$catch_next" ] && {
653 case "$wanifnames" in
657 wanifnames
="$wanifnames $word "
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."
678 local wanifnames
="$( get_wan_ifnames )"
680 if [ -z "$wanifnames" ]; then
686 IP4T
=$
(which iptables
)
687 IP6T
=$
(which ip6tables
)
689 # Delete smartgw firewall rules first
690 if [ "$UCI_CONF_NAME" = "olsrd6" ]; then
691 while $IP6T -D forwarding_rule
-o tnl_
+ -j ACCEPT
2> /dev
/null
; do :;done
692 for IFACE
in $wanifnames; do
693 while $IP6T -D forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
2> /dev
/null
; do :; done
695 for IFACE
in $ifsglobal; do
696 while $IP6T -D input_rule
-i $IFACE -p 4 -j ACCEPT
2> /dev
/null
; do :; done
699 while $IP4T -D forwarding_rule
-o tnl_
+ -j ACCEPT
2> /dev
/null
; do :;done
700 for IFACE
in $wanifnames; do
701 while $IP4T -D forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
2> /dev
/null
; do :; done
703 for IFACE
in $ifsglobal; do
704 while $IP4T -D input_rule
-i $IFACE -p 4 -j ACCEPT
2> /dev
/null
; do :; done
706 while $IP4T -t nat
-D postrouting_rule
-o tnl_
+ -j MASQUERADE
2> /dev
/null
; do :;done
709 if [ "$smartgateway" = "yes" ]; then
710 log
"$funcname() Notice: Inserting firewall rules for SmartGateway"
711 if [ ! "$smartgatewayuplink" = "none" ]; then
712 if [ "$smartgatewayuplink" = "ipv4" ]; then
713 # Allow everything to be forwarded to tnl_+ and use NAT for it
714 $IP4T -I forwarding_rule
-o tnl_
+ -j ACCEPT
715 $IP4T -t nat
-I postrouting_rule
-o tnl_
+ -j MASQUERADE
716 # Allow forwarding from tunl0 to (all) wan-interfaces
717 if [ "$nowan"="0" ]; then
718 for IFACE
in $wanifnames; do
719 $IP4T -A forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
722 # Allow incoming ipip on all olsr-interfaces
723 for IFACE
in $ifsglobal; do
724 $IP4T -I input_rule
-i $IFACE -p 4 -j ACCEPT
726 elif [ "$smartgatewayuplink" = "ipv6" ]; then
727 $IP6T -I forwarding_rule
-o tnl_
+ -j ACCEPT
728 if [ "$nowan"="0" ]; then
729 for IFACE
in $wanifnames; do
730 $IP6T -A forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
733 for IFACE
in $ifsglobal; do
734 $IP6T -I input_rule
-i $IFACE -p 4 -j ACCEPT
737 $IP4T -t nat
-I postrouting_rule
-o tnl_
+ -j MASQUERADE
738 for IPT
in $IP4T $IP6T; do
739 $IPT -I forwarding_rule
-o tnl_
+ -j ACCEPT
740 if [ "$nowan"="0" ]; then
741 for IFACE
in $wanifnames; do
742 $IPT -A forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
745 for IFACE
in $ifsglobal; do
746 $IPT -I input_rule
-i $IFACE -p 4 -j ACCEPT
759 config_foreach system_config system
762 olsrd_update_schema
"option" "$@"
766 olsrd_update_schema
"list" "$@"
769 .
/lib
/functions
/network.sh
771 config_load
$UCI_CONF_NAME
775 config_foreach olsrd_find_config_file olsrd
777 if [ -z "$OLSRD_CONFIG_FILE" ]; then
778 mkdir
-p -- /var
/etc
/
779 olsrd_write_config
> /var
/etc
/$UCI_CONF_NAME.conf ||
return 1
780 if [ "$INTERFACES_COUNT" -gt 0 -a "$OLSRD_COUNT" -gt 0 ]; then
781 OLSRD_CONFIG_FILE
=/var
/etc
/$UCI_CONF_NAME.conf
785 [ -z "$OLSRD_CONFIG_FILE" ] && return 1
787 SERVICE_PID_FILE
="$PID"
788 if service_check
/usr
/sbin
/olsrd
; then
789 error
"there is already an instance of $UCI_CONF_NAME running (pid: '$(cat $PID)'), not starting."
792 service_start
/usr
/sbin
/olsrd
-f "$OLSRD_CONFIG_FILE" -nofork
794 service_check
/usr
/sbin
/olsrd ||
{
795 log
"startup-error: check via: '/usr/sbin/olsrd -f \"$OLSRD_CONFIG_FILE\" -nofork'"
799 olsrd_setup_smartgw_rules
803 SERVICE_PID_FILE
="$PID"
804 service_stop
/usr
/sbin
/olsrd