Project

General

Profile

Feature #447 » interfaces.inc.patch.txt

Marcus Brown, 03/24/2010 08:16 AM

 
1
--- backup-stock-image-20100320/inc/interfaces.inc	2010-03-23 13:42:51.000000000 +0700
2
+++ interfaces.inc	2010-03-24 14:41:27.000000000 +0700
3
@@ -687,7 +687,7 @@
4
 	interfaces_qinq_configure();
5
 
6
 	/* Set up PPP interfaces */
7
-	interfaces_ppp_configure();
8
+	//interfaces_ppp_configure();
9
 
10
 	$iflist = get_configured_interface_with_descr();
11
 	$delayed_list = array();
12
@@ -824,8 +824,6 @@
13
 	unlink_if_exists("{$g['tmp_path']}/{$realif}up");
14
 	unlink_if_exists("{$g['vardb_path']}/{$interface}ip");
15
 	unlink_if_exists("{$g['tmp_path']}/{$realif}_router");
16
-	
17
-	interface_ppp_bring_down($realif);
18
 
19
 	switch ($ifcfg['ipaddr']) {
20
 	case "pppoe":
21
@@ -857,6 +855,10 @@
22
 			mwexec("/usr/sbin/arp -d -i {$realif} -a");
23
 		}
24
 		break;
25
+	case "ppp":
26
+		killbypid("{$g['varrun_path']}/{$realif}.pid");
27
+		sleep(2);
28
+		break;
29
 	default:
30
 		if(does_interface_exist("$realif")) {
31
 			mwexec("/sbin/ifconfig " . escapeshellarg($realif) . " delete", true);
32
@@ -873,7 +875,9 @@
33
 
34
 	return;
35
 }
36
-
37
+/* Now PPP functions are integrated into the structure of other functions better we don't need this
38
+	i.e. interface_ppp_configure is called by interface_configure, interface_bring_down, etc.
39
+	
40
 function interfaces_ppp_configure($write_config=true) {
41
 	global $config, $g;
42
 	if(!$g['booting']) 
43
@@ -882,8 +886,8 @@
44
 		echo "Configuring PPP interfaces...";
45
 	if($config['ppps']['ppp']) {
46
 		foreach($config['ppps']['ppp'] as $ppp) {
47
-			$dev = substr($ppp['port'], 5);
48
-			interface_ppp_configure($dev,$write_config);
49
+			$id = $ppp['id'];
50
+			interface_ppp_configure($id,$write_config);
51
 		}
52
 	}
53
 	if(!$g['booting']) 
54
@@ -891,110 +895,224 @@
55
 	if($g['booting'])
56
 		echo "done.\n";
57
 }
58
+*/
59
 
60
-function interface_ppp_configure($ifcfg,$edit=false) {
61
+function interface_ppp_configure($ppp_int,$edit=false) {
62
 	global $config, $g;
63
 	
64
-	/* Remove  the /dev/ from the device name. */
65
-	$orig_dev = $ifcfg;
66
-
67
-	// ppp (userland) requires a /var/spool/lock directory
68
+	$serial_device = 0;
69
+	if ($ppp_int <> -1)
70
+		if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
71
+			foreach ($config['ppps']['ppp'] as $ppp) {
72
+				if ($ppp_int == "ppp".$ppp['pppid'])
73
+					$serial_device = $ppp['port'];
74
+			}
75
+		}
76
+	
77
+	// mpd5 requires a /var/spool/lock directory
78
 	if(!is_dir("/var/spool/lock")) {
79
-		exec("mkdir -p /var/spool/lock");
80
-		exec("chmod a+rw /var/spool/lock/.");
81
+		exec("/bin/mkdir -p /var/spool/lock");
82
+		exec("/bin/chmod a+rw /var/spool/lock/.");
83
 	}
84
-	if ($edit){
85
-		// Construct the ppp.conf file
86
-		$peerfile .= "default:\n";
87
-		$peerfile .= " set log Phase Chat LCP IPCP CCP tun command\n";
88
-		$peerfile .= " ident user-ppp VERSION (built COMPILATIONDATE)\n";
89
-		$peerfile .= " set dial \"ABORT BUSY ABORT NO\\\sCARRIER TIMEOUT 5 \\\n";
90
-		$peerfile .= " 		\\\"\\\" AT OK-AT-OK ATE1Q0 OK \\\dATDT\\\T TIMEOUT 40 CONNECT\"\n";
91
-		$peerfile .= " enable dns\n";
92
-		$peerfile .= " nat enable yes\n";
93
-		$peerfile .= " set reconnect {$ifcfg['connect-max-attempts']} 5\n";
94
-		$peerfile .= " allow users root\n\n";
95
-	
96
-		// Loop variables
97
-    		$i = 0;
98
-		$startingip = 1;
99
-
100
-		// Start ppp.linkup file
101
-		$rclinkup = "default:\n";
102
-		// Start ppp.linkdown file
103
-		$rclinkdown = "default:\n";
104
+	if (!file_exists("{$g['varetc_path']}/mpd.script"))
105
+		if($g['booting'])
106
+		// Maybe make a symlink instead?
107
+			mwexec("/bin/cp /etc/ppp/mpd.script {$g['varetc_path']}/mpd.script");
108
+		
109
+	if ($edit || $g['booting']){
110
+		if($g['booting'])
111
+			echo " configuring PPP on {$ppp_int} interface...\n";
112
+		/* generate mpd.conf */
113
+		$fd = fopen("{$g['varetc_path']}/mpd-ppp.conf", "w");
114
+		if (!$fd) {
115
+			printf("Error: cannot open mpd-ppp.conf in interface_ppp_configure().\n");
116
+			return 1;
117
+		}
118
+		/* generate mpd.secret */
119
+		$fds = fopen("{$g['varetc_path']}/mpd.secret", "w");
120
+		if (!$fds) {
121
+			printf("Error: cannot open mpd.secret in interface_ppp_configure().\n");
122
+			return 1;
123
+		}
124
+		$fdlnkdn = fopen("{$g['varetc_path']}/ppp.linkdn", "w");
125
+		if (!$fdlnkdn) {
126
+			printf("Error: cannot open ppp.linkdn in interface_ppp_configure().\n");
127
+			return 1;
128
+		}
129
+		// Create mpd.secret file
130
+		
131
+		$secret = <<<EOD
132
+#################################################################
133
+#
134
+# MPD secrets file
135
+# Entries are for authentication in either or both directions, as well 
136
+# as telnet console login. Auto generated from /etc/inc/interfaces.inc
137
+# {$g['varetc_path']}/mpd.secret
138
+#
139
+##################################################################
140
+Mylogin	""
141
+
142
+EOD;
143
+		// Construct the mpd.conf file
144
+		$mpdconf = <<<EOD
145
+#################################################################
146
+#
147
+# MPD Configuration file
148
+# Auto generated by pfSense from /etc/inc/interfaces.inc
149
+# {$g['varetc_path']}/mpd_ppp.conf
150
+#
151
+##################################################################
152
+
153
+startup:
154
+	# configure mpd users
155
+	set user admin pfsense admin
156
+	set user user pfsense
157
+	# configure the console
158
+	set console self 127.0.0.1 5005
159
+	set console open
160
+	# configure the web server
161
+	#set web self 0.0.0.0 5006
162
+	#set web open
163
+
164
+default:
165
+
166
+EOD;
167
+
168
+		$ip = 1;
169
 
170
  	   	if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
171
 			foreach ($config['ppps']['ppp'] as $ppp) {
172
-				$dev = substr($ppp['port'], 5);
173
-				$realif  = $ppp['port'];
174
-				$peerfile .= "{$dev}:\n";
175
-				$peerfile .= " set device {$realif}\n";
176
-				if($ppp['dialcmd']) {
177
-					$peerfile .= " set dial " . base64_decode($ppp['dialcmd']) . "\n";
178
-				} else
179
-					$peerfile .= " set dial \"\"\n";
180
-				$peerfile .= " set speed {$ppp['linespeed']}\n";
181
-				if (isset($ppp['defaultgw']))
182
-					$peerfile .= " add default HISADDR\n";
183
-				$peerfile .= " set timeout 0\n";
184
-				$peerfile .= " enable dns\n";
185
-				$endingip = $startingip+1;
186
+				//$dev = substr($ppp['port'], 5); //Device sans path
187
+				$id = $ppp['pppid'];
188
+				$ppp_port = "ppp{$id}";
189
+
190
+				$endingip = $ip+1;
191
 				if($ppp['localip'] && $ppp['gateway'])
192
-					$peerfile .= " set ifaddr {$ppp['localip']}/0 {$ppp['gateway']}/0 255.255.255.0 0.0.0.0\n";
193
+					$ranges = "set ipcp ranges {$ppp['localip']}/0 {$ppp['gateway']}/0";
194
 				if(!$ppp['localip'] && $ppp['gateway'])
195
-					$peerfile .= " set ifaddr 10.0.0.{$startingip}/0 {$ppp['gateway']}/0 255.255.255.0 0.0.0.0\n";
196
+					$ranges = "set ipcp ranges 10.0.0.{$ip}/0 {$ppp['gateway']}/0";
197
 				if($ppp['localip'] and !$ppp['gateway'])
198
-					$peerfile .= " set ifaddr {$ppp['localip']}/0 10.0.0.{$endingip}/0 255.255.255.0 0.0.0.0\n";
199
+					$ranges = "set ipcp ranges {$ppp['localip']}/0 10.0.0.{$endingip}/0";
200
 				if(!$ppp['localip'] and !$ppp['gateway'])	
201
-					$peerfile .= " set ifaddr 10.0.0.{$startingip}/0 10.0.0.{$endingip}/0 255.255.255.0 0.0.0.0\n";
202
-				//$peerfile .= " iface name ppp_{$orig_dev}\n";
203
-				$peerfile .= " set phone \"{$ppp['phone']}\"\n";
204
-				$peerfile .= " set authname \"{$ppp['username']}\"\n";
205
-				$peerfile .= " set authkey \"{$ppp['password']}\"\n";
206
-				// Add a local socket for the daemon so we can query it later
207
-				$peerfile .= " set server /var/run/{$dev}.sock \"\" 0177\n";
208
-				$peerfile .= "\n";
209
-				$i++;
210
-				$startingip++;
211
-				$rclinkup .= "{$dev}:\n";
212
-				$rclinkup .= "	! sh -c \"/etc/rc.conf_mount_rw\"\n";
213
- 				$rclinkup .= "	! sh -c \"/bin/echo `date -j +%Y.%m.%d-%H:%M:%S` 00:00:00 >> /conf/ppp-up.{$dev}.log\"\n";
214
-				$rclinkup .= "	! sh -c \"/sbin/ppp-script HISADDR INTERFACE DNS0 DNS1\"\n";
215
-				$rclinkup .= "	! sh -c \"/etc/rc.linkup INTERFACE start\"\n";
216
-				$rclinkup .= "	! sh -c \"/etc/rc.conf_mount_ro\"\n";
217
-				$rclinkup.= "	! sh -c \"/etc/rc.filter_configure_sync\"\n";
218
-				// Link down file
219
-				$rclinkdown .= "{$dev}:\n";
220
-				$rclinkdown .= "	! sh -c \"/etc/rc.conf_mount_rw\"\n";
221
-				$rclinkdown .= "	! sh -c \"/bin/echo `date -j +%Y.%m.%d-%H:%M:%S` UPTIME >> /conf/ppp-up.{$dev}.log\"\n";
222
-				$rclinkdown .= "	! sh -c \"/etc/rc.conf_mount_ro\"\n";
223
-				$rclinkdown .= "	! sh -c \"/bin/rm -f /var/run/{$dev}.if\"\n";
224
-			}	
225
-    		}
226
+					$ranges = "set ipcp ranges 10.0.0.{$ip}/0 10.0.0.{$endingip}/0";
227
+				if ($ppp['username'] <> ""){
228
+					$user = "{$ppp['username']}";
229
+				} else {
230
+					$user = "Mylogin";
231
+				}
232
+				
233
+				$mpdconf .= <<<EOD
234
+{$ppp_port}:
235
+	create bundle static B{$id}
236
+	{$ranges}
237
+	set ipcp yes req-pri-dns
238
+	set ipcp yes req-sec-dns
239
+	set iface name {$ppp_port}
240
 
241
-		// Write out configuration for ppp.conf
242
-		file_put_contents("/etc/ppp/ppp.conf", $peerfile);
243
+EOD;
244
+				if (isset($ppp['defaultgw'])){
245
+					$mpdconf .= <<<EOD
246
+	set iface route default
247
+
248
+EOD;
249
+				}
250
+				$mpdconf .= <<<EOD
251
+	set iface up-script /usr/local/sbin/ppp-linkup
252
+	set iface down-script /usr/local/sbin/ppp-linkdn
253
+# Create link.
254
+	create link static L{$ppp_port} modem
255
+	set modem device {$ppp['port']}
256
+	set modem var \$DialPrefix "DT"
257
+	set modem var \$Telephone "{$ppp['phone']}"
258
+
259
+EOD;
260
+				$ip++;
261
+				if (isset($ppp['connecttimeout'])){
262
+					$mpdconf .= <<<EOD
263
+	set modem var \$ConnectTimeout "25"
264
+
265
+EOD;
266
+				}
267
+				if (isset($ppp['initstr'])){
268
+					$mpdconf .= <<<EOD
269
+	set modem var \$InitString "{$ppp['initstr']}"
270
+
271
+EOD;
272
+				}
273
+				if (isset($ppp['apn'])){
274
+					$mpdconf .= <<<EOD
275
+	set modem var \$APN "{$ppp['apn']}"
276
+	set modem var \$APNum "{$ppp['apnum']}"
277
 
278
-		// Write out linkup file
279
-		file_put_contents("/etc/ppp/ppp.linkup", $rclinkup);
280
-		file_put_contents("/etc/ppp/ppp.linkdown", $rclinkdown);
281
-		// Make executable
282
-		exec("chmod a+rx /etc/ppp/ppp.linkup");
283
-		exec("chmod a+rx /etc/ppp/ppp.linkdown");
284
+EOD;
285
+				}
286
+				$mpdconf .= <<<EOD
287
+	set modem script DialPeer
288
+	set modem idle-script Ringback
289
+	set modem watch -cd
290
+# We expect to be authenticated by peer using any protocol.
291
+	set link disable chap pap
292
+	set link accept chap pap eap
293
+	set link enable no-orig-auth
294
+# Configure the account name. Password will be taken from mpd.secret.
295
+	set auth authname {$user}
296
+# To make Ringback work we should specify how to handle incoming calls originated by it.
297
+	#set link enable incoming
298
+	set link action bundle B{$id}
299
+	open
300
+
301
+EOD;
302
+
303
+				// Start interface down-script file - make new file for each configured PPP interface. 
304
+				// Then it's easy to associate an interface with a serial device.
305
+
306
+				$rclinkdown = <<<EOD
307
+#!/bin/sh
308
+
309
+# delete the node just in case mpd cannot do that
310
+/usr/sbin/ngctl shutdown $1:
311
+
312
+#rm -f /tmp/$1_route
313
+rm -f /tmp/$1up
314
+rm -f {$g['varetc_path']}/nameserver_$1
315
+#/etc/rc.conf_mount_rw
316
+#/bin/echo `date -j +%Y.%m.%d-%H:%M:%S` UPTIME >> /conf/$1.log
317
+#/etc/rc.conf_mount_ro
318
+
319
+EOD;
320
+				
321
+				if ($ppp['username'] <> ""){
322
+					$secret .= <<<EOD
323
+{$ppp['username']}	"{$ppp['password']}"
324
+
325
+EOD;
326
+				}
327
+
328
+				// Write out linkup file for each configured PPP interface.
329
+
330
+				fwrite($fdlnkdn, $rclinkdown);
331
+				fclose($fdlnkdn);		
332
+				// Make executable
333
+				exec("chmod a+rx {$g['varetc_path']}/ppp.linkdn");
334
+			}	
335
+    	}
336
+
337
+		// Write out configuration for mpd_ppp.conf and mpd.secret
338
+		fwrite($fd, $mpdconf);
339
+		fclose($fd);
340
+		log_error("wrote {$g['varetc_path']}/mpd-ppp.conf");
341
+		fwrite($fds, $secret);
342
+		fclose($fds);
343
+		sleep(1);
344
 	}
345
 	// Launch specified ppp instance
346
-	if( (!$edit || $g['booting']) && file_exists("/dev/{$orig_dev}")){
347
-		$running = `/bin/pgrep -f "ppp .* $orig_dev"`;
348
-		if(!$running)
349
-			mwexec_bg("/usr/sbin/ppp -background {$orig_dev}");
350
-	}
351
-}
352
+	if( (!$edit || $g['booting']) && file_exists("{$serial_device}")){
353
+		/* if mpd is active, lets take it down -> accomplished by starting mpd with the -k flag*/
354
+		/* fire up mpd */
355
+		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}");
356
+		sleep(2);
357
+		
358
 
359
-function interface_ppp_bring_down($if) {
360
-	if(file_exists("/var/run/{$if}.pid")) {
361
-		$pid = trim(file_get_contents("/var/run/{$if}.pid"));
362
-		mwexec("kill -QUIT {$pid}");
363
 	}
364
 }
365
 
366
@@ -1904,7 +2022,9 @@
367
 		case 'pptp':
368
 			interface_pptp_configure($interface);
369
 			break;
370
-
371
+		case 'ppp':
372
+			interface_ppp_configure($realif);
373
+			break;
374
 		default:
375
 			if ($wancfg['ipaddr'] <> "" && $wancfg['subnet'] <> "") {
376
 				if (isset($wancfg['ispointtopoint']) && $wancfg['pointtopoint']) {
377
@@ -1929,9 +2049,6 @@
378
 	if (!$g['booting'])
379
 		interface_reload_carps($realif);
380
 	
381
-	if($wancfg['serialport'])
382
-		interface_ppp_configure($wancfg['serialport']);
383
-	
384
 	if (!$g['booting']) {
385
 		if (link_interface_to_gre($interface)) {
386
 			foreach ($config['gres']['gre'] as $gre)
387
@@ -2391,7 +2508,7 @@
388
  */
389
 function convert_real_interface_to_friendly_interface_name($interface = "wan") {
390
         global $config;
391
-
392
+		
393
         if (stristr($interface, "pppoe")) {
394
                 $index = substr($interface, 5);
395
                 if (intval($index) > 0)
396
@@ -2404,7 +2521,7 @@
397
                         return "opt{$index}";
398
                 else
399
                         return "wan";
400
-	} else if (stristr($interface, "vip")) {
401
+		} else if (stristr($interface, "vip")) {
402
                 $index = substr($interface, 3);
403
                 $counter = 0;
404
                 foreach ($config['virtualip']['vip'] as $vip) {
405
@@ -2446,7 +2563,7 @@
406
 
407
 /* attempt to resolve interface to friendly descr */
408
 function convert_friendly_interface_to_friendly_descr($interface) {
409
-        global $config;
410
+        global $config; 
411
 
412
         switch ($interface) {
413
                 case "l2tp":
414
@@ -2570,17 +2687,6 @@
415
 
416
 			if ($interface == $if || $interface == $ifdesc) {
417
 
418
-			// PPP Support
419
-			if($config['interfaces'][$if]['serialport']) {
420
-				$dev = $config['interfaces'][$if]['serialport'];
421
-				if(file_exists("/var/run/{$dev}.if")) {
422
-					$wanif = trim(file_get_contents("/var/run/{$dev}.if"));
423
-				} else
424
-					$wanif = "Not connected";
425
-
426
-				break;
427
-			}
428
-
429
 			$cfg = $config['interfaces'][$if];
430
 
431
 			// Wireless cloned NIC support (FreeBSD 8+)
432
@@ -2624,6 +2730,9 @@
433
 					else
434
 						$wanif = "pptp" . substr($if, 3);
435
 					break;
436
+				case "ppp":
437
+					$wanif = $cfg['if'];
438
+					break;
439
 				default:
440
 					$wanif = $cfg['if'];
441
 					break;
442
@@ -2923,16 +3032,14 @@
443
 
444
 	/* loop interfaces, check config for outbound */
445
 	foreach($config['interfaces'] as $ifdescr => $ifname) {
446
-		if($ifname['serialport']) {
447
-			$ints[] = $ifdescr;
448
-			continue;
449
-		}
450
+
451
 		switch ($ifname['ipaddr']) {
452
 			case "dhcp":
453
 			case "carpdev-dhcp":
454
 			case "pppoe":
455
 			case "pptp":
456
-			$ints[] = $ifdescr;
457
+			case "ppp";
458
+				$ints[] = $ifdescr;
459
 			break;
460
 			default:
461
 			if ($ifname['pointtopoint'])
(6-6/10)