1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008-2013 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'
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}: $@"
29 [ -z "$varname" -o "$varname" != "${varname%%[!A-Za-z0-9_]*}" ] && return 1
33 validate_olsrd_option
() {
35 [ -z "$str" -o "$str" != "${str%%[! 0-9A-Za-z./|:_-]*}" ] && return 1
41 local cfgt hostname latlon oldIFS
43 config_get cfgt
"$cfg" TYPE
45 if [ "$cfgt" = "system" ]; then
46 config_get hostname
"$cfg" hostname
47 hostname
="${hostname:-OpenWrt}"
48 SYSTEM_HOSTNAME
="$hostname"
51 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
52 config_get latlon
"$cfg" latlon
53 oldIFS
="$IFS"; IFS
=" ${T}${N},"; set -- $latlon; IFS
="$oldIFS"
58 if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
59 config_get latlon
"$cfg" latitude
61 config_get latlon
"$cfg" longitude
66 olsrd_find_config_file
() {
68 validate_varname
"$cfg" ||
return 0
70 config_get_bool ignore
"$cfg" ignore
0
71 [ "$ignore" -ne 0 ] && return 0
72 config_get OLSRD_CONFIG_FILE
"$cfg" config_file
77 warning_invalid_value
() {
78 local funcname
="warning_invalid_value"
80 validate_varname
"$package" || package
=
82 validate_varname
"$config" || config
=
84 validate_varname
"$option" || option
=
86 if [ -n "$package" -a -n "$config" ]; then
87 log
"$funcname() in option '$package.$config${option:+.}$option', skipped"
89 log
"$funcname() skipped"
95 olsrd_write_option
() {
98 validate_varname
"$cfg" ||
return 1
100 validate_varname
"$option" ||
return 1
102 local option_type
="$5"
104 if [ "$option_type" = bool
]; then
106 1|on|true|enabled|
yes) value
=yes;;
107 0|off|false|disabled|no
) value
=no
;;
108 *) warning_invalid_value olsrd
"$cfg" "$option"; return 1;;
112 if ! validate_olsrd_option
"$value"; then
113 warning_invalid_value olsrd
"$cfg" "$option"
117 if [ "$value" != "${value%%[G-Zg-z_-]*}" ]; then
118 if [ "$option" != "Ip6AddrType" -a "$option" != "LinkQualityMult" -a "$value" != "yes" -a "$value" != "no" ]; then
123 echo -n "${N}$param$option $value"
128 olsrd_write_plparam
() {
129 local funcname
="olsrd_write_plparam"
132 validate_varname
"$cfg" ||
return 1
134 validate_varname
"$option" ||
return 1
136 local option_type
="$5"
139 if [ "$option_type" = bool
]; then
141 1|on|true|enabled|
yes) value
=yes;;
142 0|off|false|disabled|no
) value
=no
;;
143 *) warning_invalid_value olsrd
"$cfg" "$option"; return 1;;
147 if ! validate_olsrd_option
"$value"; then
148 warning_invalid_value olsrd
"$cfg" "$option"
159 if [ "$option" = 'hosts' ]; then
166 if [ "$option" = 'NonOlsrIf' ]; then
167 if validate_varname
"$value"; then
168 if network_get_device ifname
"$value"; then
169 log
"$funcname() Info: mdns Interface '$value' ifname '$ifname' found"
171 log
"$funcname() Warning: mdns Interface '$value' not found, skipped"
174 warning_invalid_value olsrd
"$cfg" "NonOlsrIf"
176 [ -z "$ifname" ] || value
=$ifname
179 echo -n "${N}${param}PlParam \"$option\" \"$value\""
184 config_update_schema
() {
185 local schema_varname
="$1"
186 validate_varname
"$schema_varname" ||
return 1
188 validate_varname
"$command" ||
return 1
190 validate_varname
"$option" ||
return 1
200 eval "export -n -- \"schema=\${$schema_varname}\""
202 for cur_option
in $schema; do
203 [ "${cur_option%%[:=]*}" = "$option" ] && return 0
206 if [ "$command" = list
]; then
208 if [ "$#" -ge "3" ]; then
209 schema_entry
="$option:list3"
210 elif [ "$#" -ge "2" ]; then
211 schema_entry
="$option:list2"
213 schema_entry
="$option:list"
216 schema_entry
="$option"
219 append
"$schema_varname" "$schema_entry"
224 config_write_options
() {
225 local funcname
="config_write_options"
228 validate_varname
"$cfg" ||
return 1
229 local write_func
="$3"
230 [ -z "$write_func" ] && output_func
=echo
231 local write_param
="$4"
233 local schema_entry option option_length option_type default value list_size list_item list_value i position speed oldIFS
234 local list_speed_vars
="HelloInterval HelloValidityTime TcInterval TcValidityTime MidInterval MidValidityTime HnaInterval HnaValidityTime"
236 get_value_for_entry
()
238 local schema_entry
="$1"
240 default
="${schema_entry#*[=]}"
241 [ "$default" = "$schema_entry" ] && default
=
242 option
="${schema_entry%%[=]*}"
244 oldIFS
="$IFS"; IFS
=':'; set -- $option; IFS
="$oldIFS"
248 validate_varname
"$option" ||
return 1
249 [ -z "$option_type" ] || validate_varname
"$option_type" ||
return 1
250 [ "$option_type" = internal
] && return 1
252 config_get value
"$cfg" "$option"
253 [ "$option" = "speed" ] && return 1
270 already_in_schema
"speed" && {
271 get_value_for_entry
"speed"
273 if [ 2>/dev
/null
$value -gt 0 -a $value -le 20 ]; then
276 log
"$funcname() Warning: invalid speed-value: '$value' - allowed integers: 1...20, fallback to 6"
280 for schema_entry
in $list_speed_vars; do {
281 already_in_schema
"$schema_entry" || schema
="$schema $schema_entry"
285 for schema_entry
in $schema; do
286 if [ -n "$speed" ]; then # like sven-ola freifunk firmware fff-1.7.4
287 case "$schema_entry" in
289 value
="$(( $speed / 2 + 1 )).0"
292 value
="$(( $speed * 25 )).0"
294 TcInterval
) # todo: not fisheye? -> $(( $speed * 2 ))
295 value
=$
(( $speed / 2 ))
296 [ $value -eq 0 ] && value
=1
300 value
="$(( $speed * 100 )).0"
303 value
="$(( $speed * 5 )).0"
306 value
="$(( $speed * 100 )).0"
309 value
="$(( $speed * 2 )).0"
312 value
="$(( $speed * 25 )).0"
315 get_value_for_entry
"$schema_entry" ||
continue
321 case " $list_speed_vars " in
331 is_speed_var
"$schema_entry" && option
="$schema_entry"
333 get_value_for_entry
"$schema_entry" ||
continue
336 if [ -z "$value" ]; then
337 oldIFS
="$IFS"; IFS
='+'; set -- $default; IFS
="$oldIFS"
339 elif [ "$value" = '-' -a -n "$default" ]; then
343 [ -z "$value" ] && continue
345 case "$option_type" in
352 if [ "$list_size" -gt 0 ]; then
353 config_get option_length
"$cfg" "${option}_LENGTH"
354 if [ -n "$option_length" ]; then
356 while [ "$i" -le "$option_length" ]; do
357 config_get list_value
"$cfg" "${option}_ITEM$i"
358 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" ||
break
364 for list_item
in $value; do
365 append
"list_value" "$list_item"
367 position
=$
((i
% list_size
))
368 if [ "$position" -eq 0 ]; then
369 "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type" ||
break
373 [ "$position" -ne 0 ] && "$write_func" "$write_param" "$cfg" "$option" "$list_value" "$option_type"
376 "$write_func" "$write_param" "$cfg" "$option" "$value" "$option_type"
383 olsrd_write_olsrd
() {
385 validate_varname
"$cfg" ||
return 0
388 config_get_bool ignore
"$cfg" ignore
0
389 [ "$ignore" -ne 0 ] && return 0
391 [ "$OLSRD_COUNT" -gt 0 ] && return 0
393 config_get ipversion
"$cfg" IpVersion
394 if [ "$ipversion" = "6and4" ]; then
395 OLSRD_IPVERSION_6AND4
=1
396 config_set
"$cfg" IpVersion
'6'
398 config_get smartgateway
"$cfg" SmartGateway
399 config_get smartgatewayuplink
"$cfg" SmartGatewayUplink
401 config_write_options
"$OLSRD_OLSRD_SCHEMA" "$cfg" olsrd_write_option
403 OLSRD_COUNT
=$
((OLSRD_COUNT
+ 1))
407 olsrd_write_ipcconnect
() {
409 validate_varname
"$cfg" ||
return 0
412 config_get_bool ignore
"$cfg" ignore
0
413 [ "$ignore" -ne 0 ] && return 0
415 [ "$IPCCONNECT_COUNT" -gt 0 ] && return 0
417 echo -n "${N}IpcConnect${N}{"
418 config_write_options
"$OLSRD_IPCCONNECT_SCHEMA" "$cfg" olsrd_write_option
"${T}"
420 IPCCONNECT_COUNT
=$
((IPCCONNECT_COUNT
+ 1))
427 validate_varname
"$cfg" ||
return 0
430 config_get_bool ignore
"$cfg" ignore
0
431 [ "$ignore" -ne 0 ] && return 0
433 config_get netaddr
"$cfg" netaddr
434 if ! validate_olsrd_option
"$netaddr"; then
435 warning_invalid_value olsrd
"$cfg" "netaddr"
439 config_get netmask
"$cfg" netmask
440 if ! validate_olsrd_option
"$netmask"; then
441 warning_invalid_value olsrd
"$cfg" "netmask"
445 [ "$HNA4_COUNT" -le 0 ] && echo -n "${N}Hna4${N}{"
446 echo -n "${N}${T}${T}$netaddr $netmask"
447 HNA4_COUNT
=$
((HNA4_COUNT
+ 1))
454 validate_varname
"$cfg" ||
return 0
457 config_get_bool ignore
"$cfg" ignore
0
458 [ "$ignore" -ne 0 ] && return 0
460 config_get netaddr
"$cfg" netaddr
461 if ! validate_olsrd_option
"$netaddr"; then
462 warning_invalid_value olsrd
"$cfg" "netaddr"
466 config_get prefix
"$cfg" prefix
467 if ! validate_olsrd_option
"$prefix"; then
468 warning_invalid_value olsrd
"$cfg" "prefix"
472 [ "$HNA6_COUNT" -le 0 ] && echo -n "${N}Hna6${N}{"
473 echo -n "${N}${T}${T}$netaddr $prefix"
474 HNA6_COUNT
=$
((HNA6_COUNT
+ 1))
479 olsrd_write_loadplugin
() {
480 local funcname
="olsrd_write_loadplugin"
482 validate_varname
"$cfg" ||
return 0
490 config_get_bool ignore
"$cfg" ignore
0
491 [ "$ignore" -ne 0 ] && return 0
493 config_get library
"$cfg" library
494 if ! validate_olsrd_option
"$library"; then
495 warning_invalid_value olsrd
"$cfg" "library"
498 if ! [ -x "/lib/$library" -o -x "/usr/lib/$library" -o -x "/usr/local/lib/$library" ]; then
499 log
"$funcname() Warning: Plugin library '$library' not found, skipped"
505 config_get name
"$cfg" name
506 [ -z "$name" ] && config_set
"$cfg" name
$SYSTEM_HOSTNAME
508 config_get suffix
"$cfg" suffix
509 [ -z "$suffix" ] && config_set
"$cfg" suffix
'.olsr'
511 config_get lat
"$cfg" lat
512 config_get lon
"$cfg" lon
513 config_get latlon_infile
"$cfg" latlon_infile
514 if [ \
( -z "$lat" -o -z "$lat" \
) -a -z "$latlon_infile" ]; then
515 if [ -f '/var/run/latlon.txt' ]; then
516 config_set
"$cfg" lat
''
517 config_set
"$cfg" lon
''
518 config_set
"$cfg" latlon_infile
'/var/run/latlon.txt'
520 config_set
"$cfg" lat
"$SYSTEM_LAT"
521 config_set
"$cfg" lon
"$SYSTEM_LON"
525 for f
in latlon_file hosts_file services_file resolv_file macs_file
; do
526 config_get
$f "$cfg" $f
529 [ -z "$latlon_file" ] && config_set
"$cfg" latlon_file
'/var/run/latlon.js'
532 config_get wd_file
"$cfg" file
536 echo -n "${N}LoadPlugin \"$library\"${N}{"
537 config_write_options
"$OLSRD_LOADPLUGIN_SCHEMA" "$cfg" olsrd_write_plparam
"${T}"
543 olsrd_write_interface
() {
544 local funcname
="olsrd_write_interface"
546 validate_varname
"$cfg" ||
return 0
552 config_get_bool ignore
"$cfg" ignore
0
553 [ "$ignore" -ne 0 ] && return 0
556 config_get interfaces
"$cfg" interface
558 for interface
in $interfaces; do
559 if validate_varname
"$interface"; then
560 if network_get_device IFNAME
"$interface"; then
561 ifnames
="$ifnames \"$IFNAME\""
562 ifsglobal
="$ifsglobal $IFNAME"
563 elif network_get_physdev IFNAME
"$interface"; then
564 local proto
="$(uci -q get network.${interface}.proto)"
565 if [ "$proto" = "static" -o "$proto" = "none" ]; then
566 ifnames
="$ifnames \"$IFNAME\""
567 ifsglobal
="$ifsglobal $IFNAME"
570 log
"$funcname() Warning: Interface '$interface' not found, skipped"
573 warning_invalid_value olsrd
"$cfg" "interface"
577 [ -z "$ifnames" ] && return 0
579 echo -n "${N}Interface$ifnames${N}{"
580 config_write_options
"$OLSRD_INTERFACE_SCHEMA" "$cfg" olsrd_write_option
"${T}"
582 INTERFACES_COUNT
=$
((INTERFACES_COUNT
+ 1))
587 olsrd_write_interface_defaults
() {
589 validate_varname
"$cfg" ||
return 0
591 echo -n "${N}InterfaceDefaults$ifnames${N}{"
592 config_write_options
"$OLSRD_INTERFACE_DEFAULTS_SCHEMA" "$cfg" olsrd_write_option
"${T}"
598 olsrd_update_schema
() {
600 validate_varname
"$command" ||
return 0
602 validate_varname
"$varname" ||
return 0
604 local cfg
="$CONFIG_SECTION"
608 config_get cfgt
"$cfg" TYPE
610 olsrd
) config_update_schema OLSRD_OLSRD_SCHEMA
"$command" "$varname" "$value";;
611 IpcConnect
) config_update_schema OLSRD_IPCCONNECT_SCHEMA
"$command" "$varname" "$value";;
612 LoadPlugin
) config_update_schema OLSRD_LOADPLUGIN_SCHEMA
"$command" "$varname" "$value";;
613 Interface
) config_update_schema OLSRD_INTERFACE_SCHEMA
"$command" "$varname" "$value";;
614 InterfaceDefaults
) config_update_schema OLSRD_INTERFACE_DEFAULTS_SCHEMA
"$command" "$varname" "$value";;
620 olsrd_write_config
() {
621 OLSRD_IPVERSION_6AND4
=0
623 config_foreach olsrd_write_olsrd olsrd
625 config_foreach olsrd_write_ipcconnect IpcConnect
627 config_foreach olsrd_write_hna4 Hna4
628 [ "$HNA4_COUNT" -gt 0 ] && echo "${N}}"
630 config_foreach olsrd_write_hna6 Hna6
631 [ "$HNA6_COUNT" -gt 0 ] && echo "${N}}"
632 config_foreach olsrd_write_loadplugin LoadPlugin
634 config_foreach olsrd_write_interface_defaults InterfaceDefaults
635 config_foreach olsrd_write_interface Interface
643 local wanifnames word catch_next
645 which ip
>/dev
/null ||
return 1
647 set -- $
( ip route list exact
0.0.0.0/0 table all
)
654 [ -n "$catch_next" ] && {
655 case "$wanifnames" in
659 wanifnames
="$wanifnames $word "
672 olsrd_setup_smartgw_rules
() {
673 local funcname
="olsrd_setup_smartgw_rules"
674 # Check if ipip is installed
675 [ -e /etc
/modules.d
/[0-9]*-ipip ] ||
{
676 log
"$funcname() Warning: kmod-ipip is missing. SmartGateway will not work until you install it."
680 local wanifnames
="$( get_wan_ifnames )"
682 if [ -z "$wanifnames" ]; then
688 IP4T
=$
(which iptables
)
689 IP6T
=$
(which ip6tables
)
691 # Delete smartgw firewall rules first
692 for IPT
in $IP4T $IP6T; do
693 while $IPT -D forwarding_rule
-o tnl_
+ -j ACCEPT
2> /dev
/null
; do :;done
694 for IFACE
in $wanifnames; do
695 while $IPT -D forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
2> /dev
/null
; do :; done
697 for IFACE
in $ifsglobal; do
698 while $IPT -D input_rule
-i $IFACE -p 4 -j ACCEPT
2> /dev
/null
; do :; done
701 while $IP4T -t nat
-D postrouting_rule
-o tnl_
+ -j MASQUERADE
2> /dev
/null
; do :;done
703 if [ "$smartgateway" == "yes" ]; then
704 log
"$funcname() Notice: Inserting firewall rules for SmartGateway"
705 if [ ! "$smartgatewayuplink" == "none" ]; then
706 if [ "$smartgatewayuplink" == "ipv4" ]; then
707 # Allow everything to be forwarded to tnl_+ and use NAT for it
708 $IP4T -I forwarding_rule
-o tnl_
+ -j ACCEPT
709 $IP4T -t nat
-I postrouting_rule
-o tnl_
+ -j MASQUERADE
710 # Allow forwarding from tunl0 to (all) wan-interfaces
711 if [ "$nowan"="0" ]; then
712 for IFACE
in $wanifnames; do
713 $IP4T -A forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
716 # Allow incoming ipip on all olsr-interfaces
717 for IFACE
in $ifsglobal; do
718 $IP4T -I input_rule
-i $IFACE -p 4 -j ACCEPT
720 elif [ "$smartgatewayuplink" == "ipv6" ]; then
721 $IP6T -I forwarding_rule
-o tnl_
+ -j ACCEPT
722 if [ "$nowan"="0" ]; then
723 for IFACE
in $wanifnames; do
724 $IP6T -A forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
727 for IFACE
in $ifsglobal; do
728 $IP6T -I input_rule
-i $IFACE -p 4 -j ACCEPT
731 $IP4T -t nat
-I postrouting_rule
-o tnl_
+ -j MASQUERADE
732 for IPT
in $IP4T $IP6T; do
733 $IPT -I forwarding_rule
-o tnl_
+ -j ACCEPT
734 if [ "$nowan"="0" ]; then
735 for IFACE
in $wanifnames; do
736 $IPT -A forwarding_rule
-i tunl0
-o $IFACE -j ACCEPT
739 for IFACE
in $ifsglobal; do
740 $IPT -I input_rule
-i $IFACE -p 4 -j ACCEPT
753 config_foreach system_config system
756 olsrd_update_schema
"option" "$@"
760 olsrd_update_schema
"list" "$@"
763 .
/lib
/functions
/network.sh
765 config_load
$UCI_CONF_NAME
769 config_foreach olsrd_find_config_file olsrd
771 if [ -z "$OLSRD_CONFIG_FILE" ]; then
772 mkdir
-p -- /var
/etc
/
773 olsrd_write_config
> /var
/etc
/$UCI_CONF_NAME.conf
774 if [ "$INTERFACES_COUNT" -gt 0 -a "$OLSRD_COUNT" -gt 0 ]; then
775 OLSRD_CONFIG_FILE
=/var
/etc
/$UCI_CONF_NAME.conf
779 [ -z "$OLSRD_CONFIG_FILE" ] && return 1
782 if [ "$OLSRD_IPVERSION_6AND4" -ne 0 ]; then
783 bindv6only
="$(sysctl -n net.ipv6.bindv6only)"
784 sysctl
-w net.ipv6.bindv6only
=1 > /dev
/null
785 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
786 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
787 rm $OLSRD_CONFIG_FILE
789 # some filenames should get the suffix .ipv6
790 for file in $latlon_file $hosts_file $services_file $resolv_file $macs_file $wd_file;do
791 f
=$
(echo $file|
sed 's/\//\\\//g')
792 sed -i "s/$f/$f.ipv6/g" /var
/etc
/olsrd.conf.ipv6
795 SERVICE_PID_FILE
="$PID6"
796 if service_check
/usr
/sbin
/olsrd
; then
797 error
"there is already an IPv6 instance of olsrd running (pid: '$(cat $PID6)'), not starting."
799 service_start
/usr
/sbin
/olsrd
-f /var
/etc
/olsrd.conf.ipv6
-nofork
802 SERVICE_PID_FILE
="$PID"
803 if service_check
/usr
/sbin
/olsrd
; then
804 error
"there is already an IPv4 instance of olsrd running (pid: '$(cat $PID)'), not starting."
806 service_start
/usr
/sbin
/olsrd
-f /var
/etc
/olsrd.conf.ipv4
-nofork
810 sysctl
-w net.ipv6.bindv6only
="$bindv6only" > /dev
/null
814 if [ "$ipversion" = "6" ]; then
815 sed -i '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/d' "$OLSRD_CONFIG_FILE"
818 SERVICE_PID_FILE
="$PID"
819 if service_check
/usr
/sbin
/olsrd
; then
820 error
"there is already an IPv4 instance of olsrd running (pid: '$(cat $PID)'), not starting."
823 service_start
/usr
/sbin
/olsrd
-f "$OLSRD_CONFIG_FILE" -nofork
824 service_check
/usr
/sbin
/olsrd ||
{
825 log
"startup-error: check via: '/usr/sbin/olsrd -f \"$OLSRD_CONFIG_FILE\" -nofork'"
830 olsrd_setup_smartgw_rules
834 SERVICE_PID_FILE
="$PID"
835 service_stop
/usr
/sbin
/olsrd