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 printf '%s' "${N}$param$option $value"
130 olsrd_write_plparam
() {
131 local funcname
="olsrd_write_plparam"
136 local option_type
="$5"
139 validate_varname
"$cfg" ||
return 1
140 validate_varname
"$option" ||
return 1
142 if [ "$option_type" = bool
]; then
144 1|on|true|enabled|
yes) value
=yes;;
145 0|off|false|disabled|no
) value
=no
;;
146 *) warning_invalid_value olsrd
"$cfg" "$option"; return 1;;
150 if ! validate_olsrd_option
"$value"; then
151 warning_invalid_value olsrd
"$cfg" "$option"
162 if [ "$option" = 'hosts' ]; then
169 if [ "$option" = 'NonOlsrIf' ]; then
170 if validate_varname
"$value"; then
171 if network_get_device ifname
"$value"; then
172 log
"$funcname() Info: mdns Interface '$value' ifname '$ifname' found"
174 log
"$funcname() Warning: mdns Interface '$value' not found, skipped"
177 warning_invalid_value olsrd
"$cfg" "NonOlsrIf"
179 [ -z "$ifname" ] || value
=$ifname
182 printf '%s' "${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 test 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 smartgateway
"$cfg" SmartGateway
395 config_get smartgatewayuplink
"$cfg" SmartGatewayUplink
397 config_write_options
"$OLSRD_OLSRD_SCHEMA" "$cfg" olsrd_write_option
399 OLSRD_COUNT
=$
((OLSRD_COUNT
+ 1))
403 olsrd_write_ipcconnect
() {
405 validate_varname
"$cfg" ||
return 0
408 config_get_bool ignore
"$cfg" ignore
0
409 [ "$ignore" -ne 0 ] && return 0
411 [ "$IPCCONNECT_COUNT" -gt 0 ] && return 0
413 printf '%s' "${N}IpcConnect${N}{"
414 config_write_options
"$OLSRD_IPCCONNECT_SCHEMA" "$cfg" olsrd_write_option
"${T}"
416 IPCCONNECT_COUNT
=$
((IPCCONNECT_COUNT
+ 1))
421 validate_varname
"$cfg" ||
return 0
424 config_get_bool ignore
"$cfg" ignore
0
425 [ "$ignore" -ne 0 ] && return 0
427 config_get netaddr
"$cfg" netaddr
428 if ! validate_olsrd_option
"$netaddr"; then
429 warning_invalid_value olsrd
"$cfg" "netaddr"
433 config_get netmask
"$cfg" netmask
434 if ! validate_olsrd_option
"$netmask"; then
435 warning_invalid_value olsrd
"$cfg" "netmask"
439 [ "$HNA4_COUNT" -le 0 ] && printf '%s' "${N}Hna4${N}{"
440 printf '%s' "${N}${T}${T}$netaddr $netmask"
441 HNA4_COUNT
=$
((HNA4_COUNT
+ 1))
446 validate_varname
"$cfg" ||
return 0
449 config_get_bool ignore
"$cfg" ignore
0
450 [ "$ignore" -ne 0 ] && return 0
452 config_get netaddr
"$cfg" netaddr
453 if ! validate_olsrd_option
"$netaddr"; then
454 warning_invalid_value olsrd
"$cfg" "netaddr"
458 config_get prefix
"$cfg" prefix
459 if ! validate_olsrd_option
"$prefix"; then
460 warning_invalid_value olsrd
"$cfg" "prefix"
464 [ "$HNA6_COUNT" -le 0 ] && printf '%s' "${N}Hna6${N}{"
465 printf '%s' "${N}${T}${T}$netaddr $prefix"
466 HNA6_COUNT
=$
((HNA6_COUNT
+ 1))
469 find_most_recent_plugin_libary
()
474 for file in "/lib/$library"* "/usr/lib/$library"* "/usr/local/lib/$library"*; do {
476 unixtime
="$( date +%s -r "$file" )"
477 echo "$unixtime $file"
479 } done |
sort -n |
tail -n1 | cut
-d' ' -f2
482 olsrd_write_loadplugin
()
484 local funcname
='olsrd_write_loadplugin'
486 local ignore name suffix lat lon latlon_infile
488 validate_varname
"$cfg" ||
return 0
490 config_get_bool ignore
"$cfg" ignore
0
491 [ "$ignore" -ne 0 ] && return 0
493 # e.g. olsrd_txtinfo.so.1.1 or 'olsrd_txtinfo'
494 config_get library
"$cfg" library
496 library
="$( find_most_recent_plugin_libary "$library" )"
497 if [ -z "$library" ]; then
498 log
"$funcname() Warning: Plugin library '$library' not found, skipped"
501 library
="$( basename "$library" )"
504 validate_olsrd_option
"$library" ||
{
505 warning_invalid_value olsrd
"$cfg" 'library'
510 'olsrd_nameservice.'*)
511 config_get name
"$cfg" name
512 [ -z "$name" ] && config_set
"$cfg" name
$SYSTEM_HOSTNAME
514 config_get suffix
"$cfg" suffix
515 [ -z "$suffix" ] && config_set
"$cfg" suffix
'.olsr'
517 config_get lat
"$cfg" lat
518 config_get lon
"$cfg" lon
519 config_get latlon_infile
"$cfg" latlon_infile
520 if [ \
( -z "$lat" -o -z "$lat" \
) -a -z "$latlon_infile" ]; then
521 if [ -f '/var/run/latlon.txt' ]; then
522 config_set
"$cfg" lat
''
523 config_set
"$cfg" lon
''
524 config_set
"$cfg" latlon_infile
'/var/run/latlon.txt'
526 config_set
"$cfg" lat
"$SYSTEM_LAT"
527 config_set
"$cfg" lon
"$SYSTEM_LON"
531 for f
in latlon_file hosts_file services_file resolv_file macs_file
; do
532 config_get
$f "$cfg" $f
535 [ -z "$latlon_file" ] && config_set
"$cfg" latlon_file
'/var/run/latlon.js'
538 config_get wd_file
"$cfg" file
542 printf '%s' "${N}LoadPlugin \"$library\"${N}{"
543 config_write_options
"$OLSRD_LOADPLUGIN_SCHEMA" "$cfg" olsrd_write_plparam
"${T}"
547 olsrd_write_interface
() {
548 local funcname
="olsrd_write_interface"
550 validate_varname
"$cfg" ||
return 0
556 config_get_bool ignore
"$cfg" ignore
0
557 [ "$ignore" -ne 0 ] && return 0
560 config_get interfaces
"$cfg" interface
562 for interface
in $interfaces; do
563 if validate_varname
"$interface"; then
564 if network_get_device IFNAME
"$interface"; then
565 ifnames
="$ifnames \"$IFNAME\""
566 ifsglobal
="$ifsglobal $IFNAME"
567 elif network_get_physdev IFNAME
"$interface"; then
568 local proto
="$(uci -q get network.${interface}.proto)"
569 if [ "$proto" = "static" -o "$proto" = "none" ]; then
570 ifnames
="$ifnames \"$IFNAME\""
571 ifsglobal
="$ifsglobal $IFNAME"
574 log
"$funcname() Warning: Interface '$interface' not found, skipped"
577 warning_invalid_value olsrd
"$cfg" "interface"
581 [ -z "$ifnames" ] && return 0
583 printf '%s' "${N}Interface$ifnames${N}{"
584 config_write_options
"$OLSRD_INTERFACE_SCHEMA" "$cfg" olsrd_write_option
"${T}"
586 INTERFACES_COUNT
=$
((INTERFACES_COUNT
+ 1))
589 olsrd_write_interface_defaults
() {
591 validate_varname
"$cfg" ||
return 0
593 printf '%s' "${N}InterfaceDefaults$ifnames${N}{"
594 config_write_options
"$OLSRD_INTERFACE_DEFAULTS_SCHEMA" "$cfg" olsrd_write_option
"${T}"
600 olsrd_update_schema
() {
602 validate_varname
"$command" ||
return 0
604 validate_varname
"$varname" ||
return 0
606 local cfg
="$CONFIG_SECTION"
610 config_get cfgt
"$cfg" TYPE
612 olsrd
) config_update_schema OLSRD_OLSRD_SCHEMA
"$command" "$varname" "$value";;
613 IpcConnect
) config_update_schema OLSRD_IPCCONNECT_SCHEMA
"$command" "$varname" "$value";;
614 LoadPlugin
) config_update_schema OLSRD_LOADPLUGIN_SCHEMA
"$command" "$varname" "$value";;
615 Interface
) config_update_schema OLSRD_INTERFACE_SCHEMA
"$command" "$varname" "$value";;
616 InterfaceDefaults
) config_update_schema OLSRD_INTERFACE_DEFAULTS_SCHEMA
"$command" "$varname" "$value";;
622 olsrd_write_config
() {
624 config_foreach olsrd_write_olsrd olsrd
626 config_foreach olsrd_write_ipcconnect IpcConnect
628 config_foreach olsrd_write_hna4 Hna4
629 [ "$HNA4_COUNT" -gt 0 ] && echo "${N}}"
631 config_foreach olsrd_write_hna6 Hna6
632 [ "$HNA6_COUNT" -gt 0 ] && echo "${N}}"
633 config_foreach olsrd_write_loadplugin LoadPlugin
635 config_foreach olsrd_write_interface_defaults InterfaceDefaults
636 config_foreach olsrd_write_interface Interface
644 local wanifnames word catch_next
646 which ip
>/dev
/null ||
return 1
648 set -- $
( ip route list exact
0.0.0.0/0 table all
)
655 [ -n "$catch_next" ] && {
656 case "$wanifnames" in
660 wanifnames
="$wanifnames $word "
673 olsrd_setup_smartgw_rules
() {
674 local funcname
="olsrd_setup_smartgw_rules"
675 # Check if ipip is installed
676 [ -e /etc
/modules.d
/[0-9]*-ipip ] ||
{
677 log
"$funcname() Warning: kmod-ipip is missing. SmartGateway will not work until you install it."
681 local wanifnames
="$( get_wan_ifnames )"
683 if [ -z "$wanifnames" ]; then
689 IP4T
=$
(which iptables
)
690 IP6T
=$
(which ip6tables
)
692 # Delete smartgw firewall rules first
693 if [ "$UCI_CONF_NAME" = "olsrd6" ]; then
694 while $IP6T -D forwarding_rule
-o tnl_
+ -j ACCEPT
2> /dev
/null
; do :;done
695 for IFACE
in $wanifnames; do
696 while $IP6T -D forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
2> /dev
/null
; do :; done
698 for IFACE
in $ifsglobal; do
699 while $IP6T -D input_rule
-i $IFACE -p 4 -j ACCEPT
2> /dev
/null
; do :; done
702 while $IP4T -D forwarding_rule
-o tnl_
+ -j ACCEPT
2> /dev
/null
; do :;done
703 for IFACE
in $wanifnames; do
704 while $IP4T -D forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
2> /dev
/null
; do :; done
706 for IFACE
in $ifsglobal; do
707 while $IP4T -D input_rule
-i $IFACE -p 4 -j ACCEPT
2> /dev
/null
; do :; done
709 while $IP4T -t nat
-D postrouting_rule
-o tnl_
+ -j MASQUERADE
2> /dev
/null
; do :;done
712 if [ "$smartgateway" = "yes" ]; then
713 log
"$funcname() Notice: Inserting firewall rules for SmartGateway"
714 if [ ! "$smartgatewayuplink" = "none" ]; then
715 if [ "$smartgatewayuplink" = "ipv4" ]; then
716 # Allow everything to be forwarded to tnl_+ and use NAT for it
717 $IP4T -I forwarding_rule
-o tnl_
+ -j ACCEPT
718 $IP4T -t nat
-I postrouting_rule
-o tnl_
+ -j MASQUERADE
719 # Allow forwarding from tunl0 to (all) wan-interfaces
720 if [ "$nowan" = '0' ]; then
721 for IFACE
in $wanifnames; do
722 $IP4T -A forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
725 # Allow incoming ipip on all olsr-interfaces
726 for IFACE
in $ifsglobal; do
727 $IP4T -I input_rule
-i $IFACE -p 4 -j ACCEPT
729 elif [ "$smartgatewayuplink" = "ipv6" ]; then
730 $IP6T -I forwarding_rule
-o tnl_
+ -j ACCEPT
731 if [ "$nowan" = '0' ]; then
732 for IFACE
in $wanifnames; do
733 $IP6T -A forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
736 for IFACE
in $ifsglobal; do
737 $IP6T -I input_rule
-i $IFACE -p 4 -j ACCEPT
740 $IP4T -t nat
-I postrouting_rule
-o tnl_
+ -j MASQUERADE
741 for IPT
in $IP4T $IP6T; do
742 $IPT -I forwarding_rule
-o tnl_
+ -j ACCEPT
743 if [ "$nowan" = '0' ]; then
744 for IFACE
in $wanifnames; do
745 $IPT -A forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
748 for IFACE
in $ifsglobal; do
749 $IPT -I input_rule
-i $IFACE -p 4 -j ACCEPT
762 config_foreach system_config system
765 olsrd_update_schema
"option" "$@"
769 olsrd_update_schema
"list" "$@"
772 .
/lib
/functions
/network.sh
774 config_load
$UCI_CONF_NAME
778 config_foreach olsrd_find_config_file olsrd
780 if [ -z "$OLSRD_CONFIG_FILE" ]; then
781 mkdir
-p -- /var
/etc
/
782 olsrd_write_config
> /var
/etc
/$UCI_CONF_NAME.conf ||
return 1
783 if [ "$INTERFACES_COUNT" -gt 0 -a "$OLSRD_COUNT" -gt 0 ]; then
784 OLSRD_CONFIG_FILE
=/var
/etc
/$UCI_CONF_NAME.conf
788 [ -z "$OLSRD_CONFIG_FILE" ] && return 1
790 SERVICE_PID_FILE
="$PID"
791 if service_check
/usr
/sbin
/olsrd
; then
792 error
"there is already an instance of $UCI_CONF_NAME running (pid: '$(cat $PID)'), not starting."
795 service_start
/usr
/sbin
/olsrd
-f "$OLSRD_CONFIG_FILE" -nofork
797 service_check
/usr
/sbin
/olsrd ||
{
798 log
"startup-error: check via: '/usr/sbin/olsrd -f \"$OLSRD_CONFIG_FILE\" -nofork'"
802 olsrd_setup_smartgw_rules
806 SERVICE_PID_FILE
="$PID"
807 service_stop
/usr
/sbin
/olsrd