Project

General

Profile

Download (14.3 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/* $Id$ */
4
/*
5
	status_interfaces.php
6
        part of pfSense
7
	Copyright (C) 2005 Scott Ullrich <sullrich@gmail.com>.
8
	All rights reserved.
9

    
10
	originally part of m0n0wall (http://m0n0.ch/wall)
11
	Copyright (C) 2003-2005 Manuel Kasper <mk@neon1.net>.
12
	All rights reserved.
13

    
14
	Redistribution and use in source and binary forms, with or without
15
	modification, are permitted provided that the following conditions are met:
16

    
17
	1. Redistributions of source code must retain the above copyright notice,
18
	   this list of conditions and the following disclaimer.
19

    
20
	2. Redistributions in binary form must reproduce the above copyright
21
	   notice, this list of conditions and the following disclaimer in the
22
	   documentation and/or other materials provided with the distribution.
23

    
24
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
26
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
28
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33
	POSSIBILITY OF SUCH DAMAGE.
34
*/
35

    
36
require_once("guiconfig.inc");
37

    
38
$wancfg = &$config['interfaces']['wan'];
39

    
40
if ($_POST) {
41
	$interface = $_POST['interface'];
42
	$ifcfg = &$config['interfaces'][$interface];
43
	if ($_POST['submit'] == "Disconnect" || $_POST['submit'] == "Release") {
44
		if ($ifcfg['ipaddr'] == "dhcp")
45
			interfaces_dhcp_down($interface);
46
		else if ($ifcfg['ipaddr'] == "pppoe")
47
			interfaces_wan_pppoe_down(); // FIXME: when we support multi-pppoe
48
		else if ($ifcfg['ipaddr'] == "pptp")
49
			interfaces_wan_pptp_down(); // FIXME: when we support multi-pptp
50
	} else if ($_POST['submit'] == "Connect" || $_POST['submit'] == "Renew") {
51
		if ($ifcfg['ipaddr'] == "dhcp")
52
			interfaces_dhcp_up($interface);
53
		else if ($ifcfg['ipaddr'] == "pppoe")
54
			interfaces_wan_pppoe_up(); // FIXME: when we support multi-pppoe
55
		else if ($ifcfg['ipaddr'] == "pptp")
56
			interfaces_wan_pptp_up(); // FIXME: when we support multi-pptp
57
	} else {
58
		header("Location: index.php");
59
		exit;
60
	}
61
}
62

    
63
function get_interface_info($ifdescr) {
64

    
65
	global $config, $linkinfo, $netstatrninfo;
66

    
67
	$ifinfo = array();
68

    
69
	/* find out interface name */
70
	$ifinfo['hwif'] = $config['interfaces'][$ifdescr]['if'];
71
	if ($ifdescr == "wan")
72
		$ifinfo['if'] = get_real_wan_interface();
73
	else
74
		$ifinfo['if'] = $ifinfo['hwif'];
75

    
76
	/* run netstat to determine link info */
77

    
78
	unset($linkinfo);
79
	exec("/usr/bin/netstat -I " . $ifinfo['hwif'] . " -nWb -f link", $linkinfo);
80
	$linkinfo = preg_split("/\s+/", $linkinfo[1]);
81
	if (preg_match("/\*$/", $linkinfo[0])) {
82
		$ifinfo['status'] = "down";
83
	} else {
84
		$ifinfo['status'] = "up";
85
	}
86

    
87
	if (!strstr($ifinfo['if'],'tun')) {
88
		$ifinfo['macaddr'] = $linkinfo[3];
89
		$ifinfo['inpkts'] = $linkinfo[4];
90
		$ifinfo['inerrs'] = $linkinfo[5];
91
		$ifinfo['inbytes'] = $linkinfo[6];
92
		$ifinfo['outpkts'] = $linkinfo[7];
93
		$ifinfo['outerrs'] = $linkinfo[8];
94
		$ifinfo['outbytes'] = $linkinfo[9];
95
		$ifinfo['collisions'] = $linkinfo[10];
96
	} else {
97
		$ifinfo['inpkts'] = $linkinfo[3];
98
		$ifinfo['inbytes'] = $linkinfo[5];
99
		$ifinfo['outpkts'] = $linkinfo[6];
100
		$ifinfo['outbytes'] = $linkinfo[8];
101
	}
102

    
103
	/* DHCP? -> see if dhclient is up */
104
	if (($ifdescr == "wan") && ($config['interfaces']['wan']['ipaddr'] == "dhcp")) {
105
		/* see if dhclient is up */
106
		if (is_dhcp_running("wan") == true)
107
			$ifinfo['dhcplink'] = "up";
108
		else
109
			$ifinfo['dhcplink'] = "down";
110
	}
111
	/* loop through optional interfaces looking to see if they are dhcp */
112
        for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
113
                $ifdescrs['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
114
                if (($ifdescr == "opt{$j}") && ($config['interfaces']['opt' . $j]['ipaddr'] == "dhcp")) {
115
                        /* see if dhclient is up */
116
                        if (is_dhcp_running("opt{$j}") == true)
117
                                $ifinfo['dhcplink'] = "up";
118
                        else
119
                                $ifinfo['dhcplink'] = "down";
120
                }
121
        }
122

    
123
	/* PPPoE interface? -> get status from virtual interface */
124
	if (($ifdescr == "wan") && ($config['interfaces']['wan']['ipaddr'] == "pppoe")) {
125
		unset($linkinfo);
126
		exec("/usr/bin/netstat -I " . $ifinfo['if'] . " -nWb -f link", $linkinfo);
127
		$linkinfo = preg_split("/\s+/", $linkinfo[1]);
128
		if (preg_match("/\*$/", $linkinfo[0])) {
129
			$ifinfo['pppoelink'] = "down";
130
		} else {
131
			/* get PPPoE link status for dial on demand */
132
			$ifconfiginfo = "";
133
			unset($ifconfiginfo);
134
			exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
135

    
136
			$ifinfo['pppoelink'] = "up";
137

    
138
			foreach ($ifconfiginfo as $ici) {
139
				if (strpos($ici, 'LINK0') !== false)
140
					$ifinfo['pppoelink'] = "down";
141
			}
142
		}
143
	}
144

    
145
	/* PPTP interface? -> get status from virtual interface */
146
	if (($ifdescr == "wan") && ($config['interfaces']['wan']['ipaddr'] == "pptp")) {
147
		unset($linkinfo);
148
		exec("/usr/bin/netstat -I " . $ifinfo['if'] . " -nWb -f link", $linkinfo);
149
		$linkinfo = preg_split("/\s+/", $linkinfo[1]);
150
		if (preg_match("/\*$/", $linkinfo[0])) {
151
			$ifinfo['pptplink'] = "down";
152
		} else {
153
			/* get PPTP link status for dial on demand */
154
			unset($ifconfiginfo);
155
			exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
156

    
157
			$ifinfo['pptplink'] = "up";
158

    
159
			foreach ($ifconfiginfo as $ici) {
160
				if (strpos($ici, 'LINK0') !== false)
161
					$ifinfo['pptplink'] = "down";
162
			}
163
		}
164
	}
165

    
166
	if ($ifinfo['status'] == "up") {
167
		/* try to determine media with ifconfig */
168
		unset($ifconfiginfo);
169
		exec("/sbin/ifconfig " . $ifinfo['hwif'], $ifconfiginfo);
170
		$matches = "";
171
		foreach ($ifconfiginfo as $ici) {
172

    
173
			/* don't list media/speed for wireless cards, as it always
174
			   displays 2 Mbps even though clients can connect at 11 Mbps */
175
			if (preg_match("/media: .*? \((.*?)\)/", $ici, $matches)) {
176
				$ifinfo['media'] = $matches[1];
177
			} else if (preg_match("/media: Ethernet (.*)/", $ici, $matches)) {
178
				$ifinfo['media'] = $matches[1];
179
			} else if (preg_match("/media: IEEE 802.11 Wireless Ethernet (.*)/", $ici, $matches)) {
180
				$ifinfo['media'] = $matches[1];
181
			}
182

    
183
			if (preg_match("/status: (.*)$/", $ici, $matches)) {
184
				if ($matches[1] != "active")
185
					$ifinfo['status'] = $matches[1];
186
			}
187
			if (preg_match("/channel (\S*)/", $ici, $matches)) {
188
				$ifinfo['channel'] = $matches[1];
189
			}
190
			if (preg_match("/ssid (\".*?\"|\S*)/", $ici, $matches)) {
191
				if ($matches[1][0] == '"')
192
					$ifinfo['ssid'] = substr($matches[1], 1, -1);
193
				else
194
					$ifinfo['ssid'] = $matches[1];
195
			}
196
		}
197

    
198
		if ($ifinfo['pppoelink'] != "down" && $ifinfo['pptplink'] != "down") {
199
			/* try to determine IP address and netmask with ifconfig */
200
			unset($ifconfiginfo);
201
			exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
202

    
203
			foreach ($ifconfiginfo as $ici) {
204
				if (preg_match("/inet (\S+)/", $ici, $matches)) {
205
					$ifinfo['ipaddr'] = $matches[1];
206
				}
207
				if (preg_match("/netmask (\S+)/", $ici, $matches)) {
208
					if (preg_match("/^0x/", $matches[1]))
209
						$ifinfo['subnet'] = long2ip(hexdec($matches[1]));
210
				}
211
			}
212

    
213
			if ($ifdescr == "wan") {
214
				/* run netstat to determine the default gateway */
215
				unset($netstatrninfo);
216
				exec("/usr/bin/netstat -rnf inet", $netstatrninfo);
217

    
218
				foreach ($netstatrninfo as $nsr) {
219
					if (preg_match("/^default\s*(\S+)/", $nsr, $matches)) {
220
						$ifinfo['gateway'] = $matches[1];
221
					}
222
				}
223
			}
224
		}
225
	}
226

    
227
	return $ifinfo;
228
}
229

    
230
$pgtitle = "Status: Interfaces";
231
include("head.inc");
232

    
233
?>
234

    
235
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
236
<?php include("fbegin.inc"); ?>
237
<p class="pgtitle"><?=$pgtitle?></p>
238
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
239
              <?php $i = 0; $ifdescrs = array('wan' => 'WAN', 'lan' => 'LAN');
240
		for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
241
			$ifdescrs['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
242
		}
243
		foreach ($ifdescrs as $ifdescr => $ifname):
244
			$ifinfo = get_interface_info($ifdescr);
245
		?>
246
		<form action="status_interfaces.php" method="post">
247
		<input type="hidden" name="interface" value="<?php echo $ifdescr; ?>">
248
              <?php if ($i): ?>
249
              <tr>
250
			<td colspan="8" class="list" height="12"></td>
251
			</tr>
252
			<?php endif; ?>
253
              <tr>
254
                <td colspan="2" class="listtopic">
255
                  <?=htmlspecialchars($ifname);?>
256
				  interface 
257
				  (<?=convert_friendly_interface_to_real_interface_name($ifname);?>)                  
258
				</td>
259
              </tr>
260
              <tr>
261
                <td width="22%" class="vncellt">Status</td>
262
                <td width="78%" class="listr">
263
                  <?=htmlspecialchars($ifinfo['status']);?>
264
                </td>
265
              </tr><?php if ($ifinfo['dhcplink']): ?>
266
			  <tr>
267
				<td width="22%" class="vncellt">DHCP</td>
268
				<td width="78%" class="listr">
269
				  <?=htmlspecialchars($ifinfo['dhcplink']);?>&nbsp;&nbsp;
270
				  <?php if ($ifinfo['dhcplink'] == "up"): ?>
271
				  <input type="submit" name="submit" value="Release" class="formbtns">
272
				  <?php else: ?>
273
				  <input type="submit" name="submit" value="Renew" class="formbtns">
274
				  <?php endif; ?>
275
				</td>
276
			  </tr><?php endif; if ($ifinfo['pppoelink']): ?>
277
              <tr>
278
                <td width="22%" class="vncellt">PPPoE</td>
279
                <td width="78%" class="listr">
280
                  <?=htmlspecialchars($ifinfo['pppoelink']);?>&nbsp;&nbsp;
281
				  <?php if ($ifinfo['pppoelink'] == "up"): ?>
282
				  <input type="submit" name="submit" value="Disconnect" class="formbtns">
283
				  <?php else: ?>
284
				  <input type="submit" name="submit" value="Connect" class="formbtns">
285
				  <?php endif; ?>
286
                </td>
287
              </tr><?php  endif; if ($ifinfo['pptplink']): ?>
288
              <tr>
289
                <td width="22%" class="vncellt">PPTP</td>
290
                <td width="78%" class="listr">
291
                  <?=htmlspecialchars($ifinfo['pptplink']);?>&nbsp;&nbsp;
292
				  <?php if ($ifinfo['pptplink'] == "up"): ?>
293
				  <input type="submit" name="submit" value="Disconnect" class="formbtns">
294
				  <?php else: ?>
295
				  <input type="submit" name="submit" value="Connect" class="formbtns">
296
				  <?php endif; ?>
297
                </td>
298
              </tr><?php  endif; if ($ifinfo['macaddr']): ?>
299
              <tr>
300
                <td width="22%" class="vncellt">MAC address</td>
301
                <td width="78%" class="listr">
302
                  <?=htmlspecialchars($ifinfo['macaddr']);?>
303
                </td>
304
              </tr>
305
	      </form>
306
		<?php endif; if ($ifinfo['status'] != "down"): ?>
307
			  <?php if ($ifinfo['dhcplink'] != "down" && $ifinfo['pppoelink'] != "down" && $ifinfo['pptplink'] != "down"): ?>
308
			  <?php if ($ifinfo['ipaddr']): ?>
309
              <tr>
310
                <td width="22%" class="vncellt">IP address</td>
311
                <td width="78%" class="listr">
312
                  <?=htmlspecialchars($ifinfo['ipaddr']);?>
313
                  &nbsp; </td>
314
              </tr><?php endif; ?><?php if ($ifinfo['subnet']): ?>
315
              <tr>
316
                <td width="22%" class="vncellt">Subnet mask</td>
317
                <td width="78%" class="listr">
318
                  <?=htmlspecialchars($ifinfo['subnet']);?>
319
                </td>
320
              </tr><?php endif; ?><?php if ($ifinfo['gateway']): ?>
321
              <tr>
322
                <td width="22%" class="vncellt">Gateway</td>
323
                <td width="78%" class="listr">
324
                  <?=htmlspecialchars($ifinfo['gateway']);?>
325
                </td>
326
              </tr><?php endif; if ($ifdescr == "wan" && file_exists("{$g['varetc_path']}/resolv.conf")): ?>
327
                <td width="22%" class="vncellt">ISP DNS servers</td>
328
                <td width="78%" class="listr">
329
		<?php
330
			$dns_servers = get_dns_servers();
331
			foreach($dns_servers as $dns) {
332
				echo "{$dns}<br>";
333
			}	
334
		?>
335
		</td>
336
			  <?php endif; endif; if ($ifinfo['media']): ?>
337
              <tr>
338
                <td width="22%" class="vncellt">Media</td>
339
                <td width="78%" class="listr">
340
                  <?=htmlspecialchars($ifinfo['media']);?>
341
                </td>
342
              </tr><?php endif; ?><?php if ($ifinfo['channel']): ?>
343
              <tr>
344
                <td width="22%" class="vncellt">Channel</td>
345
                <td width="78%" class="listr">
346
                  <?=htmlspecialchars($ifinfo['channel']);?>
347
                </td>
348
              </tr><?php endif; ?><?php if ($ifinfo['ssid']): ?>
349
              <tr>
350
                <td width="22%" class="vncellt">SSID</td>
351
                <td width="78%" class="listr">
352
                  <?=htmlspecialchars($ifinfo['ssid']);?>
353
                </td>
354
              </tr><?php endif; ?>
355
              <tr>
356
                <td width="22%" class="vncellt">In/out packets</td>
357
                <td width="78%" class="listr">
358
                  <?=htmlspecialchars($ifinfo['inpkts'] . "/" . $ifinfo['outpkts'] . " (" .
359
			format_bytes($ifinfo['inbytes']) . "/" . format_bytes($ifinfo['outbytes']) . ")");?>
360
                </td>
361
              </tr><?php if (isset($ifinfo['inerrs'])): ?>
362
              <tr>
363
                <td width="22%" class="vncellt">In/out errors</td>
364
                <td width="78%" class="listr">
365
                  <?=htmlspecialchars($ifinfo['inerrs'] . "/" . $ifinfo['outerrs']);?>
366
                </td>
367
              </tr><?php endif; ?><?php if (isset($ifinfo['collisions'])): ?>
368
              <tr>
369
                <td width="22%" class="vncellt">Collisions</td>
370
                <td width="78%" class="listr">
371
                  <?=htmlspecialchars($ifinfo['collisions']);?>
372
                </td>
373
              </tr><?php endif; ?>
374
	      <?php endif; ?>
375
              <?php $i++; endforeach; ?>
376
            </table>
377
<br/>
378
</strong>Using dial-on-demand will bring the connection up again if any packet
379
triggers it. To substantiate this point: disconnecting manually
380
will <strong>not</strong> prevent dial-on-demand from making connections
381
to the outside! Don't use dial-on-demand if you want to make sure that the line 
382
is kept disconnected.
383
<p>
384
<span class="red"><strong>Note:</strong></span> In/out counters will wrap at 32bit (4 Gigabyte) ! <br/>
385

    
386
<meta http-equiv="refresh" content="120;url=<?php print $_SERVER['SCRIPT_NAME']; ?>">
387

    
388
<?php include("fend.inc"); ?>
(120-120/163)