#!/bin/sh
# vim: set tabstop=4 shiftwidth=4 expandtab:
#
# Configure routing and miscellaneous network tunables
#
# $Id$
#

# PROVIDE: ya-routing
# REQUIRE: netif ppp
# REQUIRE: ya-netif
# KEYWORD: nojail
# BEFORE: NETWORKING

. /etc/ya.subr
_init_variables

name="ya-routing"
start_cmd="ya_routing_start"
stop_cmd="ya_routing_stop"
reload_cmd="ya_routing_reload"
extra_commands="reload"


#
# ya_routing_stop
#   Removes all previously added routes
#

ya_routing_stop()
{
    ya_routing_common stop
}

#
# ya_routing_start
#   Populates routing table with static routes to our networks and enables jumbos
#

ya_routing_start()
{
    ya_routing_common start
}

#
# ya_routing_reload
#
#

ya_routing_reload()
{
    ya_routing_common stop
    if [ "${ya_ip}" != XXXX ]; then
        ya_route_manage change default ${ya_defrouter}
    fi
    if [ "${ya_ip6}" != XXXX ]; then
        ya_route_manage change default ${ya_defrouter6}%${_main_if}
    fi
    ya_routing_common start
}

ya_routing_common()
{
    local _if_exist _ip _n _mtu _action _route _dc _vlan _route6

    if [ $# -ne 1 ] || [ -z "$1" ]; then
        ya_err 1 "usage: ya_routing_common (start|stop)"
    fi
    _action=$1

    _main_if=$(ya_interface_get_active)
    _mtu=$(ya_interface_manage detect-safe-mtu $_main_if)

    if ya_checkyesno disable_jumbo; then
        _mtu=1500
    fi

    if [ "${ya_os}" = "Linux" ] ; then
        ip link set ${_main_if} group backbone
    fi

    # Do not change mtu if it's already set
    if [ $(ya_interface_manage get-mtu $_main_if) -ne $_mtu ]; then
        if ! ya_interface_manage set-mtu $_main_if $_mtu; then
            ya_warn "Can not set mtu [ $_mtu ] for interface [ $_main_if ]"
        fi
    fi

    load_networks
    for _ip in ${ipaddr}; do
        eval `ya_network_info ${_ip}`
        # Do not delete default route on restart
        if [ "${_action}" = "start" ]; then
            if [ "${ya_ip}" != XXXX ]; then
                ya_route_manage ${_action} default ${ya_defrouter} 1450
            fi
            if [ "${ya_ip6}" != XXXX ]; then
                ya_route_manage ${_action} default ${ya_defrouter6}%${_main_if} 1450
            fi
        fi
        _n=$ya_networks
        while [ "${_n}" -gt 0 ]; do
            eval _route=\$net_${_n}
            eval _dc=\$dc_${_n}
            eval _vlan=\$vlan_${_n}
            # Do not manage our own network route.
            if [ "${ya_ip}" != XXXX -a "${_route}" != XXXX ]; then
                if ! is_ipv4_belong_net ${_ip} ${_route}; then
                    if [ "${_dc}" = "$ya_DC" -a "${_vlan}" = "$ya_VLAN" ]; then
                        # Local route
                        ya_route_manage ${_action} ${_route} ${_main_if} ${_mtu}
                    else
                        ya_route_manage ${_action} ${_route} ${ya_defrouter} ${_mtu}
                    fi
                fi
            fi
            eval _route6=\$net6_${_n}
            if [ "${ya_ip6}" != XXXX -a "${_route6}" != XXXX ]; then
                if ! is_ipv6_belong_net ${ya_ip6} ${_route6}; then
                    ya_route_manage ${_action} ${_route6} ${ya_defrouter6}%${_main_if} ${_mtu}
                fi
            fi
            _n=$((${_n} - 1))
        done

        # We should now add/del all networks from YA_JUMBO_NETWORKS
        if [ "${ya_ip}" != XXXX ]; then
            for jumbo_network in $YA_JUMBO_NETWORKS; do
                ya_route_manage ${_action} ${jumbo_network} ${ya_defrouter} ${_mtu}
            done
        fi

        # We should now add/del all networks from YA_JUMBO_IPV6_NETWORKS
        if [ "${ya_ip6}" != XXXX ]; then
            for jumbo_ipv6_network in $YA_JUMBO_IPV6_NETWORKS; do
                ya_route_manage ${_action} ${jumbo_ipv6_network} ${ya_defrouter6}%${_main_if} ${_mtu}
            done
        fi

        # We need to add/del routes for the first address only, otherwise we'll get
        # a bunch of 'File exists'/'No such file' errors
        break
    done
}

ya_load_config_files $name
ya_run_rc_command "$1"
