#----------------------------------------------------------------------------
# /etc/init.d/dsl - configuration of PPPoE
#
# Creation: 	20.06.2002  fm
# Last Update:	19.04.2004  fm
#
# Copyright (c) 2002-2004 Frank Meyer <frank@eisfair.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

#! /bin/sh

. /etc/config.d/dsl
. /etc/config.d/imond

case $1
in
    start)
	if [ "$START_PPPOE" = "yes" ]
	then
	    /usr/local/bin/colecho "starting dsl (pppoe) ..." gn

	    clampmss="-m 1412"				    # clamp the MSS
	    lcp_interval=30				    # LCP interval
	    lcp_failure=5				    # LCP failure count
	    pppoe_timeout=120				    # should be 4 * lcp_interval
	    usepeerdns="$PPPOE_USEPEERDNS"                  # get configuration usepeerdns from config

	    ifconfig $PPPOE_ETH up mtu 1500		    # set mtu to 1500

	    pppoe_synchronous=no			    # fm: "yes" not possible
	    pppoe_sync=''
	    pppd_sync=''

	    if [ "$PPPOE_DEBUG" = "yes" ]
	    then
		debug='debug'
	    else
		debug=''
	    fi

	    if [ "$usepeerdns" = "yes" ]
	    then
		usepeerdns='usepeerdns'
	    else
		usepeerdns=''
	    fi

	    # PPPoE invocation
	    PPPOE_CMD="exec /usr/sbin/pppoe -p /var/run/pppoe.pid -I $PPPOE_ETH -T $pppoe_timeout $pppoe_sync $clampmss"

	    grep '^slhc ' /proc/modules >/dev/null
	    if [ $? != 0 ]
	    then
	       /sbin/insmod slhc
	    fi

	    kernel_version=`uname -r`
	    case "$kernel_version"
	    in
		2.2.*)
		    ppp_modules='ppp'
		    pppd_cmd='/usr/sbin/pppd'
		    ;;
		*)
		    ppp_modules='ppp_generic ppp_async'
		    pppd_cmd='/usr/sbin/pppd-2.4'
		    ;;
	    esac

	    grep '^ppp ' /proc/modules >/dev/null
	    if [ $? != 0 ]
	    then
		for j in $ppp_modules
		do
		    /sbin/insmod $j
		done
	    fi

	    route del default 2>/dev/null

	    if [ "$START_IMOND" = "yes" ]
	    then
		defaultroute=''
	    else
		defaultroute='defaultroute'
	    fi
	    echo pppoe >/etc/default-route-interface

	    local=10.112.112.112
	    remote=10.112.112.113
	    netmask=255.255.255.0

	    echo $local >/var/run/pppoe.conf
	    echo $remote >>/var/run/pppoe.conf
	    echo $netmask >>/var/run/pppoe.conf

	    echo 7 >/proc/sys/net/ipv4/ip_dynaddr

	    while [ 1 ]
	    do
		sec=`date +%s`
		$pppd_cmd       $usepeerdns                         \
				pty "$PPPOE_CMD"		    \
				noipdefault			    \
				noauth				    \
				$defaultroute			    \
				hide-password			    \
				nodetach			    \
				local				    \
				$debug				    \
				mtu 1492			    \
				mru 1492			    \
				noaccomp			    \
				noccp				    \
				nobsdcomp			    \
				nodeflate			    \
				nopcomp				    \
				novj				    \
				novjccomp			    \
				user "$PPPOE_USER"		    \
				lcp-echo-interval $lcp_interval	    \
				lcp-echo-failure $lcp_failure	    \
				demand				    \
				persist				    \
				maxfail 0			    \
				idle $PPPOE_HUP_TIMEOUT		    \
				$local:$remote			    \
				ipcp-accept-remote		    \
				ipcp-accept-local		    \
				connect true			    \
				ktune				    \
				$pppd_sync >/var/log/dsl-$sec 2>&1
		sleep 1
	    done &

	    echo 7 >/proc/sys/net/ipv4/ip_dynaddr   # enable dyn ip addresses
	    sleep 5
	    route add default dev ppp0
	fi
	;;
    stop)
	killall -TERM pppoe
	killall -TERM pppd
	rmmod ppp
	rmmod slhc
	;;
esac