olsrd: init: updated (c)-year to 2017
[feed/routing.git] / olsrd / files / olsrd.init
index 1172623023adf60a01fb3412152d9fa02c369e83..258c7a580116b8ee83af99169a98a62459951f7e 100644 (file)
@@ -1,28 +1,31 @@
 #!/bin/sh /etc/rc.common
-# Copyright (C) 2008-2012 OpenWrt.org
+# Copyright (C) 2008-2017 OpenWrt.org
 
 START=65
 
 SERVICE_DAEMONIZE=1
 SERVICE_WRITE_PID=1
 
-CONF=/var/etc/olsrd.conf
-PID=/var/run/olsrd.pid
-PID6=/var/run/olsrd.ipv6.pid
-
 OLSRD_OLSRD_SCHEMA='ignore:internal config_file:internal DebugLevel=0 AllowNoInt=yes'
 OLSRD_IPCCONNECT_SCHEMA='ignore:internal Host:list Net:list2'
-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'
-OLSRD_INTERFACE_SCHEMA='ignore:internal interface:internal AutoDetectChanges:bool'
+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'
+OLSRD_INTERFACE_SCHEMA='ignore:internal interface:internal AutoDetectChanges:bool LinkQualityMult:list2'
 OLSRD_INTERFACE_DEFAULTS_SCHEMA='AutoDetectChanges:bool'
 
 T='    '
 N='
 '
 
-log()
-{
-       logger -t olsrd -p daemon.info -s "$1"
+log() {
+       logger -t olsrd -p daemon.info -s "${initscript}: $@"
+}
+
+error() {
+        logger -t olsrd -p daemon.err -s "${initscript}: ERROR: $@"
+}
+
+warn() {
+        logger -t olsrd -p daemon.warn -s "${initscript}: WARNING: $@"
 }
 
 validate_varname() {
@@ -33,15 +36,13 @@ validate_varname() {
 
 validate_olsrd_option() {
        local str="$1"
-       [ -z "$str" -o "$str" != "${str%%[!     0-9A-Za-z./|:_-]*}" ] && return 1
+       [ -z "$str" -o "$str" != "${str%%[!     0-9A-Za-z.%/|:_-]*}" ] && return 1
        return 0
 }
 
 system_config() {
        local cfg="$1"
-       local cfgt
-       local hostname
-       local latlon
+       local cfgt hostname latlon oldIFS
 
        config_get cfgt "$cfg" TYPE
 
@@ -53,9 +54,7 @@ system_config() {
 
        if [ -z "$SYSTEM_LAT" -o -z "$SYSTEM_LON" ]; then
                config_get latlon "$cfg" latlon
-               IFS=" ${T}${N},"
-               set -- $latlon
-               unset IFS
+               oldIFS="$IFS"; IFS=" ${T}${N},"; set -- $latlon; IFS="$oldIFS"
                SYSTEM_LAT="$1"
                SYSTEM_LON="$2"
        fi
@@ -139,7 +138,7 @@ olsrd_write_plparam() {
        validate_varname "$option" || return 1
        local value="$4"
        local option_type="$5"
-       local _option
+       local _option oldIFS
 
        if [ "$option_type" = bool ]; then
                case "$value" in
@@ -154,17 +153,20 @@ olsrd_write_plparam() {
                return 1
        fi
 
+       oldIFS="$IFS"
        IFS='-_'
        set -- $option
        option="$*"
-       unset IFS
+       IFS="$oldIFS"
        _option="$option"
+
        if [ "$option" = 'hosts' ]; then
                set -- $value
                option="$1"
                shift
                value="$*"
        fi
+
        if [ "$option" = 'NonOlsrIf' ]; then
                if validate_varname "$value"; then
                        if network_get_device ifname "$value"; then
@@ -184,7 +186,6 @@ olsrd_write_plparam() {
 }
 
 config_update_schema() {
-       unset IFS
        local schema_varname="$1"
        validate_varname "$schema_varname" || return 1
        local command="$2"
@@ -226,7 +227,6 @@ config_update_schema() {
 
 config_write_options() {
        local funcname="config_write_options"
-       unset IFS
        local schema="$1"
        local cfg="$2"
        validate_varname "$cfg" || return 1
@@ -234,18 +234,7 @@ config_write_options() {
        [ -z "$write_func" ] && output_func=echo
        local write_param="$4"
 
-       local schema_entry
-       local option
-       local option_length
-       local option_type
-       local default
-       local value
-       local list_size
-       local list_item
-       local list_value
-       local i
-       local position
-       local speed
+       local schema_entry option option_length option_type default value list_size list_item list_value i position speed oldIFS
        local list_speed_vars="HelloInterval HelloValidityTime TcInterval TcValidityTime MidInterval MidValidityTime HnaInterval HnaValidityTime"
 
        get_value_for_entry()
@@ -256,7 +245,7 @@ config_write_options() {
                [ "$default" = "$schema_entry" ] && default=
                option="${schema_entry%%[=]*}"
 
-               IFS=':'; set -- $option; unset IFS
+               oldIFS="$IFS"; IFS=':'; set -- $option; IFS="$oldIFS"
                option="$1"
                option_type="$2"
 
@@ -349,9 +338,7 @@ config_write_options() {
                fi
 
                if [ -z "$value" ]; then
-                       IFS='+'
-                       set -- $default
-                       unset IFS
+                       oldIFS="$IFS"; IFS='+'; set -- $default; IFS="$oldIFS"
                        value=$*
                elif [ "$value" = '-' -a -n "$default" ]; then
                        continue
@@ -407,11 +394,6 @@ olsrd_write_olsrd() {
 
        [ "$OLSRD_COUNT" -gt 0 ] && return 0
 
-       config_get ipversion "$cfg" IpVersion
-       if [ "$ipversion" = "6and4" ]; then
-               OLSRD_IPVERSION_6AND4=1
-               config_set "$cfg" IpVersion '6'
-       fi
        config_get smartgateway "$cfg" SmartGateway
        config_get smartgatewayuplink "$cfg" SmartGatewayUplink
 
@@ -577,6 +559,12 @@ olsrd_write_interface() {
                        if network_get_device IFNAME "$interface"; then
                                ifnames="$ifnames \"$IFNAME\""
                                ifsglobal="$ifsglobal $IFNAME"
+                       elif network_get_physdev IFNAME "$interface"; then
+                               local proto="$(uci -q get network.${interface}.proto)"
+                               if [ "$proto" = "static" -o "$proto" = "none" ]; then
+                                       ifnames="$ifnames \"$IFNAME\""
+                                       ifsglobal="$ifsglobal $IFNAME"
+                               fi
                        else
                                log "$funcname() Warning: Interface '$interface' not found, skipped"
                        fi
@@ -629,7 +617,6 @@ olsrd_update_schema() {
 }
 
 olsrd_write_config() {
-       OLSRD_IPVERSION_6AND4=0
        OLSRD_COUNT=0
        config_foreach olsrd_write_olsrd olsrd
        IPCCONNECT_COUNT=0
@@ -700,21 +687,29 @@ olsrd_setup_smartgw_rules() {
        IP6T=$(which ip6tables)
 
        # Delete smartgw firewall rules first
-       for IPT in $IP4T $IP6T; do
-               while $IPT -D forwarding_rule -o tnl_+ -j ACCEPT 2> /dev/null; do :;done
+       if [ "$UCI_CONF_NAME" = "olsrd6" ]; then
+               while $IP6T -D forwarding_rule -o tnl_+ -j ACCEPT 2> /dev/null; do :;done
                for IFACE in $wanifnames; do
-                       while $IPT -D forwarding_rule -i tunl0 -o $IFACE -j ACCEPT 2> /dev/null; do :; done
+                       while $IP6T -D forwarding_rule -i tunl0 -o $IFACE -j ACCEPT 2> /dev/null; do :; done
                done
                for IFACE in $ifsglobal; do
-                       while $IPT -D input_rule -i $IFACE -p 4 -j ACCEPT 2> /dev/null; do :; done
+                       while $IP6T -D input_rule -i $IFACE -p 4 -j ACCEPT 2> /dev/null; do :; done
                done
-       done
-       while $IP4T -t nat -D postrouting_rule -o tnl_+ -j MASQUERADE 2> /dev/null; do :;done
+       else
+               while $IP4T -D forwarding_rule -o tnl_+ -j ACCEPT 2> /dev/null; do :;done
+               for IFACE in $wanifnames; do
+                       while $IP4T -D forwarding_rule -i tunl0 -o $IFACE -j ACCEPT 2> /dev/null; do :; done
+               done
+               for IFACE in $ifsglobal; do
+                       while $IP4T -D input_rule -i $IFACE -p 4 -j ACCEPT 2> /dev/null; do :; done
+               done
+               while $IP4T -t nat -D postrouting_rule -o tnl_+ -j MASQUERADE 2> /dev/null; do :;done
+       fi
 
-       if [ "$smartgateway" == "yes" ]; then
+       if [ "$smartgateway" = "yes" ]; then
                log "$funcname() Notice: Inserting firewall rules for SmartGateway"
-               if [ ! "$smartgatewayuplink" == "none" ]; then
-                       if [ "$smartgatewayuplink" == "ipv4" ]; then
+               if [ ! "$smartgatewayuplink" = "none" ]; then
+                       if [ "$smartgatewayuplink" = "ipv4" ]; then
                                # Allow everything to be forwarded to tnl_+ and use NAT for it
                                $IP4T -I forwarding_rule -o tnl_+ -j ACCEPT
                                $IP4T -t nat -I postrouting_rule -o tnl_+ -j MASQUERADE
@@ -728,7 +723,7 @@ olsrd_setup_smartgw_rules() {
                                for IFACE in $ifsglobal; do
                                        $IP4T -I input_rule -i $IFACE -p 4 -j ACCEPT
                                done
-                       elif [ "$smartgatewayuplink" == "ipv6" ]; then
+                       elif [ "$smartgatewayuplink" = "ipv6" ]; then
                                $IP6T -I forwarding_rule -o tnl_+ -j ACCEPT
                                if [ "$nowan"="0" ]; then
                                        for IFACE in $wanifnames; do
@@ -739,9 +734,9 @@ olsrd_setup_smartgw_rules() {
                                        $IP6T -I input_rule -i $IFACE -p 4 -j ACCEPT
                                done
                        else
+                               $IP4T -t nat -I postrouting_rule -o tnl_+ -j MASQUERADE
                                for IPT in $IP4T $IP6T; do
                                        $IPT -I forwarding_rule -o tnl_+ -j ACCEPT
-                                       $IPT -t nat -I postrouting_rule -o tnl_+ -j MASQUERADE
                                        if [ "$nowan"="0" ]; then
                                                for IFACE in $wanifnames; do
                                                        $IPT -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT
@@ -756,10 +751,6 @@ olsrd_setup_smartgw_rules() {
        fi
 }
 
-error() {
-       log "error() ${initscript}: $@"
-}
-
 start() {
        SYSTEM_HOSTNAME=
        SYSTEM_LAT=
@@ -777,7 +768,7 @@ start() {
 
        . /lib/functions/network.sh
 
-       config_load olsrd
+       config_load $UCI_CONF_NAME
        reset_cb
 
        OLSRD_CONFIG_FILE=
@@ -785,66 +776,30 @@ start() {
 
        if [ -z "$OLSRD_CONFIG_FILE" ]; then
                mkdir -p -- /var/etc/
-               olsrd_write_config > /var/etc/olsrd.conf
+               olsrd_write_config > /var/etc/$UCI_CONF_NAME.conf || return 1
                if [ "$INTERFACES_COUNT" -gt 0 -a "$OLSRD_COUNT" -gt 0 ]; then
-                       OLSRD_CONFIG_FILE=/var/etc/olsrd.conf
+                       OLSRD_CONFIG_FILE=/var/etc/$UCI_CONF_NAME.conf
                fi
        fi
 
        [ -z "$OLSRD_CONFIG_FILE" ] && return 1
 
-       local bindv6only='0'
-       if [ "$OLSRD_IPVERSION_6AND4" -ne 0 ]; then
-               bindv6only="$(sysctl -n net.ipv6.bindv6only)"
-               sysctl -w net.ipv6.bindv6only=1 > /dev/null
-               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
-               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
-               rm $OLSRD_CONFIG_FILE
-
-               # some filenames should get the suffix .ipv6
-               for file in $latlon_file $hosts_file $services_file $resolv_file $macs_file $wd_file;do
-                       f=$(echo $file|sed 's/\//\\\//g')
-                       sed -i "s/$f/$f.ipv6/g" /var/etc/olsrd.conf.ipv6
-               done
-
-               SERVICE_PID_FILE="$PID6"
-               if service_check /usr/sbin/olsrd; then
-                       error "there is already an IPv6 instance of olsrd running (pid: '$(cat $PID6)'), not starting."
-               else
-                       service_start /usr/sbin/olsrd -f /var/etc/olsrd.conf.ipv6 -nofork
-               fi
-
-               SERVICE_PID_FILE="$PID"
-               if service_check /usr/sbin/olsrd; then
-                       error "there is already an IPv4 instance of olsrd running (pid: '$(cat $PID)'), not starting."
-               else
-                       service_start /usr/sbin/olsrd -f /var/etc/olsrd.conf.ipv4 -nofork
-               fi
-
-               sleep 3
-               sysctl -w net.ipv6.bindv6only="$bindv6only" > /dev/null
-
+       SERVICE_PID_FILE="$PID"
+       if service_check /usr/sbin/olsrd; then
+               error "there is already an instance of $UCI_CONF_NAME running (pid: '$(cat $PID)'), not starting."
+               return 1
        else
-
-               if [ "$ipversion" = "6" ]; then
-                       sed -i '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/d' "$OLSRD_CONFIG_FILE"
-               fi
-
-               SERVICE_PID_FILE="$PID"
-               if service_check /usr/sbin/olsrd; then
-                       error "there is already an IPv4 instance of olsrd running (pid: '$(cat $PID)'), not starting."
-                       return 1
-               else
-                       service_start /usr/sbin/olsrd -f "$OLSRD_CONFIG_FILE" -nofork
-               fi
+               service_start /usr/sbin/olsrd -f "$OLSRD_CONFIG_FILE" -nofork
+               sleep 1
+               service_check /usr/sbin/olsrd || {
+                       log "startup-error: check via: '/usr/sbin/olsrd -f \"$OLSRD_CONFIG_FILE\" -nofork'"
+               }
        fi
+
        olsrd_setup_smartgw_rules
 }
 
 stop() {
        SERVICE_PID_FILE="$PID"
        service_stop /usr/sbin/olsrd
-
-       SERVICE_PID_FILE="$PID6"
-       service_stop /usr/sbin/olsrd
 }