X-Git-Url: http://git.openwrt.org/?p=feed%2Frouting.git;a=blobdiff_plain;f=files%2Folsrd.init;h=6ed369fc473ece892417b1e81c81436537976896;hp=52ab0ca713fa1abc3592d537fedce7d8e0546618;hb=2dd66c6b82cebaa8613c3beb9e8394788d4676af;hpb=2e65838e5f09635685a177dea49dee3d72cb6952 diff --git a/files/olsrd.init b/files/olsrd.init index 52ab0ca..6ed369f 100644 --- a/files/olsrd.init +++ b/files/olsrd.init @@ -1,45 +1,39 @@ #!/bin/sh /etc/rc.common -# Copyright (C) 2008 Alina Friedrichsen -# Special thanks to bittorf wireless )) -START=50 +# Copyright (C) 2008-2012 OpenWrt.org -OLSRD_OLSRD_SCHEMA='ignore:internal config_file:internal DebugLevel=0 IpVersion=4 AllowNoInt:bool=1 Pollrate=0.025 TcRedundancy=2 MprCoverage=3 LinkQualityFishEye=1 LinkQualityWinSize=100 LinkQualityDijkstraLimit=0+9.0 LinkQualityLevel=2 UseHysteresis:bool=0 FIBMetric=flat ClearScreen:bool=1 Willingness=3 LinkQualityAging=0.1 LinkQualityAlgorithm=etx_fpm' +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 Ip4Broadcast=255.255.255.255 HelloInterval=2.0 HelloValidityTime=40.0 TcInterval=5.0 TcValidityTime=100.0 MidInterval=18.0 MidValidityTime=324.0 HnaInterval=18.0 HnaValidityTime=108.0' +OLSRD_INTERFACE_SCHEMA='ignore:internal interface:internal AutoDetectChanges:bool' +OLSRD_INTERFACE_DEFAULTS_SCHEMA='AutoDetectChanges:bool' T=' ' N=' ' +log() +{ + logger -t olsrd -p daemon.info -s "$1" +} + validate_varname() { local varname="$1" [ -z "$varname" -o "$varname" != "${varname%%[!A-Za-z0-9_]*}" ] && return 1 return 0 } -validate_ifname() { - local ifname="$1" - [ -z "$ifname" -o "$ifname" != "${ifname%%[!A-Za-z0-9.:_-]*}" ] && return 1 - return 0 -} - validate_olsrd_option() { local str="$1" - [ -z "$str" -o "$str" != "${str%%[! 0-9A-Za-z./:_-]*}" ] && return 1 - return 0 -} - -get_ifname() { - IFNAME= - local interface="$1" - validate_varname "$interface" || return 1 - local ifname - - config_get ifname "$interface" ifname - validate_ifname "$ifname" || return 1 - IFNAME="$ifname" - + [ -z "$str" -o "$str" != "${str%%[! 0-9A-Za-z./|:_-]*}" ] && return 1 return 0 } @@ -86,6 +80,7 @@ olsrd_find_config_file() { } warning_invalid_value() { + local funcname="warning_invalid_value" local package="$1" validate_varname "$package" || package= local config="$2" @@ -93,14 +88,12 @@ warning_invalid_value() { local option="$3" validate_varname "$option" || option= - echo -n "Warning: Invalid value" 1>&2 - if [ -n "$package" -a -n "$config" ]; then - echo -n " in option '$package.$config${option:+.}$option'" 1>&2 + log "$funcname() in option '$package.$config${option:+.}$option', skipped" + else + log "$funcname() skipped" fi - echo ", skipped" 1>&2 - return 0 } @@ -127,7 +120,7 @@ olsrd_write_option() { fi if [ "$value" != "${value%%[G-Zg-z_-]*}" ]; then - if [ "$option" != "Ip6AddrType" -a "$value" != "yes" -a "$value" != "no" ]; then + if [ "$option" != "Ip6AddrType" -a "$option" != "LinkQualityMult" -a "$value" != "yes" -a "$value" != "no" ]; then value="\"$value\"" fi fi @@ -138,6 +131,7 @@ olsrd_write_option() { } olsrd_write_plparam() { + local funcname="olsrd_write_plparam" local param="$1" local cfg="$2" validate_varname "$cfg" || return 1 @@ -171,6 +165,18 @@ olsrd_write_plparam() { shift value="$*" fi + if [ "$option" = 'NonOlsrIf' ]; then + if validate_varname "$value"; then + if network_get_device ifname "$value"; then + log "$funcname() Info: mdns Interface '$value' ifname '$ifname' found" + else + log "$funcname() Warning: mdns Interface '$value' not found, skipped" + fi + else + warning_invalid_value olsrd "$cfg" "NonOlsrIf" + fi + [ -z "$ifname" ] || value=$ifname + fi echo -n "${N}${param}PlParam \"$option\" \"$value\"" @@ -226,6 +232,7 @@ config_write_options() { local write_func="$3" [ -z "$write_func" ] && output_func=echo local write_param="$4" + local schema_entry local option local option_length @@ -238,20 +245,30 @@ config_write_options() { local i local position - for schema_entry in $schema; do + get_value_for_entry() + { + local schema_entry="$1" + default="${schema_entry#*[=]}" [ "$default" = "$schema_entry" ] && default= option="${schema_entry%%[=]*}" - IFS=':' - set -- $option - unset IFS + + IFS=':'; set -- $option; unset IFS option="$1" option_type="$2" - validate_varname "$option" || continue - [ -z "$option_type" ] || validate_varname "$option_type" || continue - [ "$option_type" = internal ] && continue + + validate_varname "$option" || return 1 + [ -z "$option_type" ] || validate_varname "$option_type" || return 1 + [ "$option_type" = internal ] && return 1 + config_get value "$cfg" "$option" + return 0 + } + + for schema_entry in $schema; do + get_value_for_entry "$schema_entry" || continue + if [ -z "$value" ]; then IFS='+' set -- $default @@ -305,7 +322,6 @@ olsrd_write_olsrd() { local cfg="$1" validate_varname "$cfg" || return 0 local ignore - local ipversion config_get_bool ignore "$cfg" ignore 0 [ "$ignore" -ne 0 ] && return 0 @@ -317,11 +333,12 @@ olsrd_write_olsrd() { OLSRD_IPVERSION_6AND4=1 config_set "$cfg" IpVersion '6' fi + config_get smartgateway "$cfg" SmartGateway + config_get smartgatewayuplink "$cfg" SmartGatewayUplink config_write_options "$OLSRD_OLSRD_SCHEMA" "$cfg" olsrd_write_option echo OLSRD_COUNT=$((OLSRD_COUNT + 1)) - return 0 } @@ -364,7 +381,7 @@ olsrd_write_hna4() { fi [ "$HNA4_COUNT" -le 0 ] && echo -n "${N}Hna4${N}{" - echo -n "${N}${T}$netaddr $netmask" + echo -n "${N}${T}${T}$netaddr $netmask" HNA4_COUNT=$((HNA4_COUNT + 1)) return 0 @@ -391,13 +408,14 @@ olsrd_write_hna6() { fi [ "$HNA6_COUNT" -le 0 ] && echo -n "${N}Hna6${N}{" - echo -n "${N}${T}$netaddr $prefix" + echo -n "${N}${T}${T}$netaddr $prefix" HNA6_COUNT=$((HNA6_COUNT + 1)) return 0 } olsrd_write_loadplugin() { + local funcname="olsrd_write_loadplugin" local cfg="$1" validate_varname "$cfg" || return 0 local ignore @@ -416,7 +434,7 @@ olsrd_write_loadplugin() { return 0 fi if ! [ -x "/lib/$library" -o -x "/usr/lib/$library" -o -x "/usr/local/lib/$library" ]; then - echo "Warning: Plugin library '$library' not found, skipped" 1>&2 + log "$funcname() Warning: Plugin library '$library' not found, skipped" return 0 fi @@ -442,9 +460,15 @@ olsrd_write_loadplugin() { fi fi - config_get latlon_file "$cfg" latlon_file + for f in latlon_file hosts_file services_file resolv_file macs_file; do + config_get $f "$cfg" $f + done + [ -z "$latlon_file" ] && config_set "$cfg" latlon_file '/var/run/latlon.js' ;; + olsrd_watchdog.*) + config_get wd_file "$cfg" file + ;; esac echo -n "${N}LoadPlugin \"$library\"${N}{" @@ -455,6 +479,7 @@ olsrd_write_loadplugin() { } olsrd_write_interface() { + local funcname="olsrd_write_interface" local cfg="$1" validate_varname "$cfg" || return 0 local ignore @@ -467,12 +492,14 @@ olsrd_write_interface() { ifnames= config_get interfaces "$cfg" interface + for interface in $interfaces; do if validate_varname "$interface"; then - if get_ifname "$interface"; then + if network_get_device IFNAME "$interface"; then ifnames="$ifnames \"$IFNAME\"" + ifsglobal="$ifsglobal $IFNAME" else - echo "Warning: Interface '$interface' not found, skipped" 1>&2 + log "$funcname() Warning: Interface '$interface' not found, skipped" fi else warning_invalid_value olsrd "$cfg" "interface" @@ -489,6 +516,17 @@ olsrd_write_interface() { return 0 } +olsrd_write_interface_defaults() { + local cfg="$1" + validate_varname "$cfg" || return 0 + + echo -n "${N}InterfaceDefaults$ifnames${N}{" + config_write_options "$OLSRD_INTERFACE_DEFAULTS_SCHEMA" "$cfg" olsrd_write_option "${T}" + echo "${N}}" + + return 1 +} + olsrd_update_schema() { local command="$1" validate_varname "$command" || return 0 @@ -505,6 +543,7 @@ olsrd_update_schema() { IpcConnect) config_update_schema OLSRD_IPCCONNECT_SCHEMA "$command" "$varname" "$value";; LoadPlugin) config_update_schema OLSRD_LOADPLUGIN_SCHEMA "$command" "$varname" "$value";; Interface) config_update_schema OLSRD_INTERFACE_SCHEMA "$command" "$varname" "$value";; + InterfaceDefaults) config_update_schema OLSRD_INTERFACE_DEFAULTS_SCHEMA "$command" "$varname" "$value";; esac return 0 @@ -524,12 +563,124 @@ olsrd_write_config() { [ "$HNA6_COUNT" -gt 0 ] && echo "${N}}" config_foreach olsrd_write_loadplugin LoadPlugin INTERFACES_COUNT=0 + config_foreach olsrd_write_interface_defaults InterfaceDefaults config_foreach olsrd_write_interface Interface echo return 0 } +get_wan_ifnames() +{ + local wanifnames word catch_next + + which ip >/dev/null || return 1 + + set -- $( ip route list exact 0.0.0.0/0 table all ) + for word in $*; do + case "$word" in + dev) + catch_next="true" + ;; + *) + [ -n "$catch_next" ] && { + case "$wanifnames" in + *" $word "*) + ;; + *) + wanifnames="$wanifnames $word " + ;; + esac + + catch_next= + } + ;; + esac + done + + echo "$wanifnames" +} + +olsrd_setup_smartgw_rules() { + local funcname="olsrd_setup_smartgw_rules" + # Check if ipip is installed + [ -e /etc/modules.d/[0-9]*-ipip ] || { + log "$funcname() Warning: kmod-ipip is missing. SmartGateway will not work until you install it." + return 1 + } + + local wanifnames="$( get_wan_ifnames )" + + if [ -z "$wanifnames" ]; then + nowan=1 + else + nowan=0 + fi + + IP4T=$(which iptables) + 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 + for IFACE in $wanifnames; do + while $IPT -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 + done + done + while $IP4T -t nat -D postrouting_rule -o tnl_+ -j MASQUERADE 2> /dev/null; do :;done + + if [ "$smartgateway" == "yes" ]; then + log "$funcname() Notice: Inserting firewall rules for SmartGateway" + 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 + # Allow forwarding from tunl0 to (all) wan-interfaces + if [ "$nowan"="0" ]; then + for IFACE in $wanifnames; do + $IP4T -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT + done + fi + # Allow incoming ipip on all olsr-interfaces + for IFACE in $ifsglobal; do + $IP4T -I input_rule -i $IFACE -p 4 -j ACCEPT + done + elif [ "$smartgatewayuplink" == "ipv6" ]; then + $IP6T -I forwarding_rule -o tnl_+ -j ACCEPT + if [ "$nowan"="0" ]; then + for IFACE in $wanifnames; do + $IP6T -A forwarding_rule -i tunl0 -o $IFACE -j ACCEPT + done + fi + for IFACE in $ifsglobal; do + $IP6T -I input_rule -i $IFACE -p 4 -j ACCEPT + done + else + 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 + done + fi + for IFACE in $ifsglobal; do + $IPT -I input_rule -i $IFACE -p 4 -j ACCEPT + done + done + fi + fi + fi +} + +error() { + log "error() ${initscript}: $@" +} + start() { SYSTEM_HOSTNAME= SYSTEM_LAT= @@ -545,8 +696,8 @@ start() { olsrd_update_schema "list" "$@" } - include /lib/network - scan_interfaces + . /lib/functions/network.sh + config_load olsrd reset_cb @@ -566,19 +717,55 @@ start() { local bindv6only='0' if [ "$OLSRD_IPVERSION_6AND4" -ne 0 ]; then bindv6only="$(sysctl -n net.ipv6.bindv6only)" - sysctl -w net.ipv6.bindv6only=1 - fi + 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 - olsrd -f "$OLSRD_CONFIG_FILE" -nofork < /dev/null > /dev/null & + 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 - if [ "$OLSRD_IPVERSION_6AND4" -ne 0 ]; then - sed -e 's/^IpVersion[ ][ ]*6$/IpVersion 4/' < "$OLSRD_CONFIG_FILE" > /var/etc/olsrd.conf.ipv4 - olsrd -f /var/etc/olsrd.conf.ipv4 -nofork < /dev/null > /dev/null & sleep 3 - sysctl -w net.ipv6.bindv6only="$bindv6only" + sysctl -w net.ipv6.bindv6only="$bindv6only" > /dev/null + + 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 fi + olsrd_setup_smartgw_rules } stop() { - killall olsrd + SERVICE_PID_FILE="$PID" + service_stop /usr/sbin/olsrd + + SERVICE_PID_FILE="$PID6" + service_stop /usr/sbin/olsrd }