Project

General

Profile

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

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

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

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

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

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

    
35
require_once("guiconfig.inc");
36

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

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

    
62
function get_interface_info($ifdescr) {
63

    
64
	global $config;
65

    
66
	$ifinfo = array();
67

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

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

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

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

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

    
133
			$ifinfo['pppoelink'] = "up";
134

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

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

    
154
			$ifinfo['pptplink'] = "up";
155

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

    
163
	if ($ifinfo['status'] == "up") {
164
		/* try to determine media with ifconfig */
165
		unset($ifconfiginfo);
166
		exec("/sbin/ifconfig " . $ifinfo['hwif'], $ifconfiginfo);
167

    
168
		foreach ($ifconfiginfo as $ici) {
169
			if (!isset($config['interfaces'][$ifdescr]['wireless'])) {
170
				/* don't list media/speed for wireless cards, as it always
171
				   displays 2 Mbps even though clients can connect at 11 Mbps */
172
				if (preg_match("/media: .*? \((.*?)\)/", $ici, $matches)) {
173
					$ifinfo['media'] = $matches[1];
174
				} else if (preg_match("/media: Ethernet (.*)/", $ici, $matches)) {
175
					$ifinfo['media'] = $matches[1];
176
				}
177
			}
178
			if (preg_match("/status: (.*)$/", $ici, $matches)) {
179
				if ($matches[1] != "active")
180
					$ifinfo['status'] = $matches[1];
181
			}
182
			if (preg_match("/channel (\S*)/", $ici, $matches)) {
183
				$ifinfo['channel'] = $matches[1];
184
			}
185
			if (preg_match("/ssid (\".*?\"|\S*)/", $ici, $matches)) {
186
				if ($matches[1][0] == '"')
187
					$ifinfo['ssid'] = substr($matches[1], 1, -1);
188
				else
189
					$ifinfo['ssid'] = $matches[1];
190
			}
191
		}
192

    
193
		if ($ifinfo['pppoelink'] != "down" && $ifinfo['pptplink'] != "down") {
194
			/* try to determine IP address and netmask with ifconfig */
195
			unset($ifconfiginfo);
196
			exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
197

    
198
			foreach ($ifconfiginfo as $ici) {
199
				if (preg_match("/inet (\S+)/", $ici, $matches)) {
200
					$ifinfo['ipaddr'] = $matches[1];
201
				}
202
				if (preg_match("/netmask (\S+)/", $ici, $matches)) {
203
					if (preg_match("/^0x/", $matches[1]))
204
						$ifinfo['subnet'] = long2ip(hexdec($matches[1]));
205
				}
206
			}
207

    
208
			if ($ifdescr == "wan") {
209
				/* run netstat to determine the default gateway */
210
				unset($netstatrninfo);
211
				exec("/usr/bin/netstat -rnf inet", $netstatrninfo);
212

    
213
				foreach ($netstatrninfo as $nsr) {
214
					if (preg_match("/^default\s*(\S+)/", $nsr, $matches)) {
215
						$ifinfo['gateway'] = $matches[1];
216
					}
217
				}
218
			}
219
		}
220
	}
221

    
222
	return $ifinfo;
223
}
224

    
225
$pgtitle = "Status: Interfaces";
226
include("head.inc");
227

    
228
?>
229

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

    
378
<meta http-equiv="refresh" content="120;url=<?php print $_SERVER['PHP_SELF']; ?>">
379

    
380
<?php include("fend.inc"); ?>
(110-110/144)