Project

General

Profile

« Previous | Next » 

Revision 611ae852

Added by Ermal LUÇI over 15 years ago

Redo most of the ppp interface handling to be consistent with the other mpd5 configurations. This way there is no more special handling for ppp around.

View differences:

etc/inc/interfaces.inc
873 873
	return;
874 874
}
875 875

  
876
function interface_ppp_configure($ppp_int,$edit=false) {
876
function interface_ppp_configure($interface) {
877 877
	global $config, $g;
878 878
	
879
	$serial_device = 0;
880
	if ($ppp_int <> -1)
881
		if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
882
			foreach ($config['ppps']['ppp'] as $ppp) {
883
				if ($ppp_int == "ppp".$ppp['pppid'])
884
					$serial_device = $ppp['port'];
885
			}
879
	$wancfg =& $config['interfaces'][$interface];
880
	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
881
		foreach ($config['ppps']['ppp'] as $ppp) {
882
			if ($wancfg['if'] == basename($ppp['port']))
883
				break;
886 884
		}
887
	
885
	}
886
	if (!$ppp || empty($ppp['port']))
887
		return;	
888

  
889
	if ($ifname == "wan")
890
		$pppid = "0";
891
	else
892
		$pppid = substr($ifname, 3);
893

  
894
	$pppif = "ppp{$pppid}";
895

  
888 896
	// mpd5 requires a /var/spool/lock directory
889 897
	if(!is_dir("/var/spool/lock")) {
890 898
		exec("/bin/mkdir -p /var/spool/lock");
891 899
		exec("/bin/chmod a+rw /var/spool/lock/.");
892 900
	}
893 901
	if (!file_exists("{$g['varetc_path']}/mpd.script"))
894
		if($g['booting'])
895
			mwexec("/bin/ln -s /usr/local/sbin/mpd.script {$g['varetc_path']}/.");
902
		mwexec("/bin/ln -s /usr/local/sbin/mpd.script {$g['varetc_path']}/.");
896 903
		
897
	if ($edit || $g['booting']){
898
		if($g['booting'])
899
			echo " configuring PPP on {$ppp_int} interface...\n";
900
		/* generate mpd.conf */
901
		$fd = fopen("{$g['varetc_path']}/mpd-ppp.conf", "w");
902
		if (!$fd) {
903
			printf("Error: cannot open mpd-ppp.conf in interface_ppp_configure().\n");
904
			return 1;
905
		}
906
		/* generate mpd.secret */
907
		$fds = fopen("{$g['varetc_path']}/mpd.secret", "w");
908
		if (!$fds) {
909
			printf("Error: cannot open mpd.secret in interface_ppp_configure().\n");
910
			return 1;
911
		}
912

  
913
		// Create mpd.secret file
914
		
915
		$secret = <<<EOD
916
#################################################################
917
#
918
# MPD secrets file
919
# Entries are for authentication in either or both directions, as well 
920
# as telnet console login. Auto generated from /etc/inc/interfaces.inc
921
# {$g['varetc_path']}/mpd.secret
922
#
923
##################################################################
924
Mylogin	""
904
	if($g['booting'])
905
		echo " configuring PPP on {$pppif} interface...\n";
925 906

  
926
EOD;
927
		// Construct the mpd.conf file
928
		$mpdconf = <<<EOD
929
#################################################################
930
#
931
# MPD Configuration file
932
# Auto generated by pfSense from /etc/inc/interfaces.inc
933
# {$g['varetc_path']}/mpd_ppp.conf
934
#
935
##################################################################
907
	/* generate mpd.conf */
908
	$fd = fopen("{$g['varetc_path']}/mpd_{$interface}.conf", "w");
909
	if (!$fd) {
910
		log_error("Error: cannot open mpd_{$interface}.conf in interface_ppp_configure().\n");
911
		return 1;
912
	}
936 913

  
914
	// Construct the mpd.conf file
915
	$mpdconf = <<<EOD
937 916
startup:
938 917
	# configure mpd users
939 918
	set user admin pfsense admin
......
942 921
	set console self 127.0.0.1 5005
943 922
	set console open
944 923
	# configure the web server
924
	set web close
945 925
	#set web self 0.0.0.0 5006
946 926
	#set web open
947 927

  
948
default:
928
EOD;
949 929

  
930
	if (is_ipaddr($ppp['localip']))
931
		$localip = $ppp['localip'];
932
	else
933
		$localip = '0.0.0.0';
934
	if (is_ipaddr($ppp['gateway']))
935
		$localgw = $ppp['gateway'];
936
	else
937
		$localgw = "10.0.0.{$pppid}";
938
			
939
	$mpdconf .= <<<EOD
940
default:
941
pppclient:
942
	create bundle static {$interface}
943
	set iface name {$pppif}
944
	set iface up-script /usr/local/sbin/ppp-linkup
945
	set iface down-script /usr/local/sbin/ppp-linkdown
946
	set ipcp ranges {$localip}/0 {$localgw}/0
947
	
950 948
EOD;
951 949

  
952
		$ip = 1;
953

  
954
 	   	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
955
			foreach ($config['ppps']['ppp'] as $ppp) {
956
				//$dev = substr($ppp['port'], 5); //Device sans path
957
				$id = $ppp['pppid'];
958
				$ppp_port = "ppp{$id}";
959

  
960
				$endingip = $ip+1;
961
				if($ppp['localip'] && $ppp['gateway'])
962
					$ranges = "set ipcp ranges {$ppp['localip']}/0 {$ppp['gateway']}/0";
963
				if(!$ppp['localip'] && $ppp['gateway'])
964
					$ranges = "set ipcp ranges 10.0.0.{$ip}/0 {$ppp['gateway']}/0";
965
				if($ppp['localip'] and !$ppp['gateway'])
966
					$ranges = "set ipcp ranges {$ppp['localip']}/0 10.0.0.{$endingip}/0";
967
				if(!$ppp['localip'] and !$ppp['gateway'])	
968
					$ranges = "set ipcp ranges 10.0.0.{$ip}/0 10.0.0.{$endingip}/0";
969
				$ip++;
970
				if ($ppp['username'] <> ""){
971
					$user = "{$ppp['username']}";
972
				} else {
973
					$user = "Mylogin";
974
				}
975
				
976
				$mpdconf .= <<<EOD
977
{$ppp_port}:
978
	create bundle static B{$id}
979
	{$ranges}
950
	if (isset($config['system']['dnsallowoverride'])) {
951
		$mpdconf .= <<<EOD
980 952
	set ipcp yes req-pri-dns
981 953
	set ipcp yes req-sec-dns
982
	set iface name {$ppp_port}
983 954

  
984 955
EOD;
985
				if (isset($ppp['defaultgw'])){
986
					$mpdconf .= <<<EOD
956
	}
957

  
958
	if (isset($ppp['defaultgw'])) {
959
		$mpdconf .= <<<EOD
987 960
	set iface route default
988 961

  
989 962
EOD;
990
				}
991
				$mpdconf .= <<<EOD
992
	set iface up-script /usr/local/sbin/ppp-linkup
993
	set iface down-script /usr/local/sbin/ppp-linkdown
963
	}
964

  
965
	$mpdconf .= <<<EOD
994 966
# Create link.
995
	create link static lnk-{$ppp_port} modem
967
	create link static lnk{$interface} modem
968
# We expect to be authenticated by peer using any protocol.
969
	set link disable chap pap
970
	set link accept chap pap eap
971
	set link enable no-orig-auth
972
# To make Ringback work we should specify how to handle incoming calls originated by it.
973
	#set link enable incoming
974
	set link action bundle {$interface}
975

  
976
EOD;
977

  
978
	if (!empty($ppp['username'])) {
979
		$mpdconf .= <<<EOD
980
# Configure the account name. Password will be taken from mpd.secret.
981
	set auth authname "{$ppp['username']}"
982
        set auth password "{$ppp['password']}"
983

  
984
EOD;
985
	}
986

  
987
	$mpdconf .= <<<EOD
996 988
	set modem device {$ppp['port']}
989
	set modem script DialPeer
990
	set modem idle-script Ringback
991
	set modem watch -cd
997 992
	set modem var \$DialPrefix "DT"
998 993
	set modem var \$Telephone "{$ppp['phone']}"
999 994

  
1000 995
EOD;
1001
				if (isset($ppp['connect-timeout'])){
1002
					$mpdconf .= <<<EOD
996
	if (isset($ppp['connect-timeout'])) {
997
		$mpdconf .= <<<EOD
1003 998
	set modem var \$ConnectTimeout "{$ppp['connect-timeout']}"
1004 999

  
1005 1000
EOD;
1006
				}
1007
				if (isset($ppp['initstr'])){
1008
					$initstr = base64_decode($ppp['initstr']);
1009
					$mpdconf .= <<<EOD
1001
	}
1002
	if (isset($ppp['initstr'])) {
1003
		$initstr = base64_decode($ppp['initstr']);
1004
		$mpdconf .= <<<EOD
1010 1005
	set modem var \$InitString "{$initstr}"
1011 1006

  
1012 1007
EOD;
1013
				}
1014
				if (isset($ppp['simpin'])){
1015
					$mpdconf .= <<<EOD
1008
	}
1009
	if (isset($ppp['simpin'])) {
1010
		$mpdconf .= <<<EOD
1016 1011
	set modem var \$SimPin "{$ppp['simpin']}"
1017 1012
	set modem var \$PinWait "{$ppp['pin-wait']}"
1018 1013

  
1019 1014
EOD;
1020
				}
1021
				if (isset($ppp['apn'])){
1022
					$mpdconf .= <<<EOD
1015
	}
1016
	if (isset($ppp['apn'])) {
1017
		$mpdconf .= <<<EOD
1023 1018
	set modem var \$APN "{$ppp['apn']}"
1024 1019
	set modem var \$APNum "{$ppp['apnum']}"
1025 1020

  
1026 1021
EOD;
1027
				}
1028
				$mpdconf .= <<<EOD
1029
	set modem script DialPeer
1030
	set modem idle-script Ringback
1031
	set modem watch -cd
1032
# We expect to be authenticated by peer using any protocol.
1033
	set link disable chap pap
1034
	set link accept chap pap eap
1035
	set link enable no-orig-auth
1036
# Configure the account name. Password will be taken from mpd.secret.
1037
	set auth authname {$user}
1038
# To make Ringback work we should specify how to handle incoming calls originated by it.
1039
	#set link enable incoming
1040
	set link action bundle B{$id}
1041
	open
1022
	}
1042 1023

  
1043
EOD;
1024
	$mpdconf .= "\topen";
1044 1025

  
1045
				$fdlnkq = fopen("{$g['varetc_path']}/{$ppp_port}.query", "w");
1046
				if (!$fdlnkq) {
1047
					printf("Error: cannot open {$ppp_port}.query in interface_ppp_configure().\n");
1048
					return 1;
1049
				}
1050
				$linkquery = <<<EOD
1026
	// Write out configuration for mpd_ppp.conf and mpd.secret
1027
	fwrite($fd, $mpdconf);
1028
	fclose($fd);
1029

  
1030
	$fdlnkq = fopen("{$g['varetc_path']}/mpd_{$interface}.query", "w");
1031
	if (!$fdlnkq) {
1032
		/* NOTE: It is not fatal if we cannot write the query.");
1033
		log_error("Error: cannot open mpd_{$interface}.query in interface_ppp_configure().\n");
1034
	} else {
1035
	$linkquery = <<<EOD
1051 1036
admin
1052 1037
pfsense
1053
link lnk-{$ppp_port}
1038
link lnk{$interface}
1054 1039
show iface
1055 1040
exit
1056 1041

  
1057 1042
EOD;
1058 1043

  
1059
				// Write out linkquery file for each configured PPP interface.
1060
				fwrite($fdlnkq, $linkquery);
1061
				fclose($fdlnkq);
1062
				
1063
				
1064
				if ($ppp['username'] <> ""){
1065
					$secret .= <<<EOD
1066
{$ppp['username']}	"{$ppp['password']}"
1067

  
1068
EOD;
1069

  
1070
				}
1071
			}	
1072
    	}
1073

  
1074
		// Write out configuration for mpd_ppp.conf and mpd.secret
1075
		fwrite($fd, $mpdconf);
1076
		fclose($fd);
1077
		fwrite($fds, $secret);
1078
		fclose($fds);
1079
		sleep(2);
1044
		// Write out linkquery file for each configured PPP interface.
1045
		fwrite($fdlnkq, $linkquery);
1046
		fclose($fdlnkq);
1080 1047
	}
1048

  
1081 1049
	// Launch specified ppp instance
1082
	if( (!$edit || $g['booting']) && file_exists("{$serial_device}")){
1083
		$timeout = 15;
1084
		/* if mpd is active, lets take it down -> accomplished by starting mpd with the -k flag*/
1050
	if( (file_exists("{$ppp['port']}")) {
1085 1051
		/* fire up mpd */
1086
		if (file_exists("{$g['tmp_path']}/{$ppp_int}_router"))
1087
			mwexec("/bin/rm -r {$g['tmp_path']}/{$ppp_int}_router");
1088
		mwexec("/usr/local/sbin/mpd5 -b -k -d {$g['varetc_path']} -f mpd-ppp.conf -p {$g['varrun_path']}/{$ppp_int}.pid -s ppp {$ppp_int}");
1089
		/* Wait for mpd5 to bring up connection, but don't wait longer than $timeout */
1090
		for ($i=0; $i < $timeout; $i++){
1091
			sleep(1);
1092
			if (file_exists("{$g['tmp_path']}/{$ppp_int}_router"))
1093
				break;
1094
		}
1095
		
1096

  
1097
	}
1052
		mwexec("/usr/local/sbin/mpd5 -b -k -d {$g['varetc_path']} -f mpd_{$interface}.conf -p {$g['varrun_path']}/ppp_{$interface}.pid -s {$interface} pppclient");
1053
	} else
1054
		log_error("Device {$ppp['port']} has disappeared.");
1098 1055
}
1099 1056

  
1100 1057
function interfaces_carp_setup() {
......
2002 1959
			interface_pptp_configure($interface);
2003 1960
			break;
2004 1961
		case 'ppp':
2005
			interface_ppp_configure($realif);
1962
			interface_ppp_configure($interface);
2006 1963
			break;
2007 1964
		default:
2008 1965
			if ($wancfg['ipaddr'] <> "" && $wancfg['subnet'] <> "") {
2009
				if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
2010
					mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " " .
2011
						escapeshellarg($wancfg['ipaddr'] . "/" . $wancfg['subnet']) .
2012
						" " . escapeshellarg($wancfg['pointtopoint']) . " up");
2013
				} else {
2014
					if($wancfg['ipaddr'] && $wancfg['subnet'])
2015
						mwexec("/sbin/ifconfig " . escapeshellarg($realif) .
2016
							" " . escapeshellarg($wancfg['ipaddr'] . "/" . 
2017
							$wancfg['subnet']));
2018
				}
1966
				if($wancfg['ipaddr'] && $wancfg['subnet'])
1967
					mwexec("/sbin/ifconfig " . escapeshellarg($realif) .
1968
						" " . escapeshellarg($wancfg['ipaddr'] . "/" . 
1969
						$wancfg['subnet']));
2019 1970
			}
2020 1971

  
2021 1972
			if (is_ipaddr($wancfg['gateway']))
......
2288 2239
	if(file_exists("{$g['varrun_path']}/pppoe_{$interface}.pid") and $g['booting']) {
2289 2240
		/* if we are booting and mpd has already been started then don't start again. */
2290 2241
	} else {
2291
		/* if mpd is active, lets take it down */
2292
		if(file_exists("{$g['varrun_path']}/pppoe_{$interface}.pid")) {
2293
			killbypid("{$g['varrun_path']}/pppoe_{$interface}.pid");
2294
			sleep(3);
2295
		}
2296

  
2297 2242
		/* Bring the parent interface up */
2298 2243
		if($wancfg['if'])
2299 2244
			interfaces_bring_up($wancfg['if']);
......
2301 2246
			log_error("Could not bring wancfg['if'] up in interface_pppoe_configure()");
2302 2247

  
2303 2248
		/* fire up mpd */
2304
		mwexec("/usr/local/sbin/mpd5 -b -d {$g['varetc_path']} -f mpd_{$interface}.conf -p {$g['varrun_path']}/pppoe_{$interface}.pid pppoeclient");
2249
		mwexec("/usr/local/sbin/mpd5 -b -k -d {$g['varetc_path']} -f mpd_{$interface}.conf -p {$g['varrun_path']}/pppoe_{$interface}.pid -s {$interface} pppoeclient");
2305 2250
	}
2306 2251

  
2307 2252
	/* sleep until wan is up - or 30 seconds, whichever comes first */
......
2435 2380
	else 
2436 2381
		log_error("Could not bring interface wancfg['if'] up in interface_pptp_configure()");
2437 2382
	/* fire up mpd */
2438
	mwexec("/usr/local/sbin/mpd5 -b -d {$g['varetc_path']} -f mpd_{$interface}.conf -p {$g['varrun_path']}/pptp_{$interface}.pid pptp");
2383
	mwexec("/usr/local/sbin/mpd5 -b -k -d {$g['varetc_path']} -f mpd_{$interface}.conf -p {$g['varrun_path']}/pptp_{$interface}.pid -s {$interface} pptp");
2439 2384

  
2440 2385
	return 0;
2441 2386
}
......
2710 2655
						$wanif = "pptp" . substr($if, 3);
2711 2656
					break;
2712 2657
				case "ppp":
2713
					$wanif = $cfg['if'];
2658
					if ($if == "wan")
2659
						$wanif = "ppp0";
2660
					else
2661
						$wanif = "ppp" . substr($if, 3);
2714 2662
					break;
2715 2663
				default:
2716 2664
					$wanif = $cfg['if'];
......
3021 2969
				$ints[] = $ifdescr;
3022 2970
			break;
3023 2971
			default:
3024
			if ($ifname['pointtopoint'])
3025
				$ints[] = $ifdescr;
3026
			else if (!empty($ifname['gateway']))
3027
				$ints[] = $ifdescr;
2972
				if (!empty($ifname['gateway']))
2973
					$ints[] = $ifdescr;
3028 2974
			break;
3029 2975
		}
3030 2976
	}
......
3072 3018
                return false;
3073 3019
}
3074 3020

  
3075
/****f* interfaces/is_interface_ppp
3076
 * NAME
3077
 *   is_interface_ppp - Returns if an interface is ppp
3078
 * RESULT
3079
 *   $tmp       - Returns if an interface is ppp
3080
 ******/
3081
function is_interface_ppp($interface) {
3082
	global $config, $g;
3083

  
3084
	$friendly = convert_real_interface_to_friendly_interface_name($interface);
3085
	if(isset($config['interfaces'][$friendly]['serialport'])) 
3086
		return true;
3087

  
3088
	return false;
3089
}
3090

  
3091 3021
/****f* interfaces/is_interface_wireless
3092 3022
 * NAME
3093 3023
 *   is_interface_wireless - Returns if an interface is wireless

Also available in: Unified diff