Project

General

Profile

Download (12.9 KB) Statistics
| Branch: | Tag: | Revision:
1
#!/usr/local/bin/php
2
<?php
3
/*
4
	status_interfaces.php
5
	part of m0n0wall (http://m0n0.ch/wall)
6

    
7
	Copyright (C) 2003-2005 Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9

    
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12

    
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15

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

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

    
32
require_once("guiconfig.inc");
33

    
34
$wancfg = &$config['interfaces']['wan'];
35

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

    
57
function get_interface_info($ifdescr) {
58

    
59
	global $config;
60

    
61
	$ifinfo = array();
62

    
63
	/* find out interface name */
64
	$ifinfo['hwif'] = $config['interfaces'][$ifdescr]['if'];
65
	if ($ifdescr == "wan")
66
		$ifinfo['if'] = get_real_wan_interface();
67
	else
68
		$ifinfo['if'] = $ifinfo['hwif'];
69

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

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

    
96
	/* DHCP? -> see if dhclient is up */
97
	if (($ifdescr == "wan") && ($config['interfaces']['wan']['ipaddr'] == "dhcp")) {
98
		/* see if dhclient is up */
99
		if (is_process_running("dhclient") == true)
100
			$ifinfo['dhcplink'] = "up";
101
		else
102
			$ifinfo['dhcplink'] = "down";
103
	}
104

    
105
	/* PPPoE interface? -> get status from virtual interface */
106
	if (($ifdescr == "wan") && ($config['interfaces']['wan']['ipaddr'] == "pppoe")) {
107
		unset($linkinfo);
108
		exec("/usr/bin/netstat -I " . $ifinfo['if'] . " -nWb -f link", $linkinfo);
109
		$linkinfo = preg_split("/\s+/", $linkinfo[1]);
110
		if (preg_match("/\*$/", $linkinfo[0])) {
111
			$ifinfo['pppoelink'] = "down";
112
		} else {
113
			/* get PPPoE link status for dial on demand */
114
			unset($ifconfiginfo);
115
			exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
116

    
117
			$ifinfo['pppoelink'] = "up";
118

    
119
			foreach ($ifconfiginfo as $ici) {
120
				if (strpos($ici, 'LINK0') !== false)
121
					$ifinfo['pppoelink'] = "down";
122
			}
123
		}
124
	}
125

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

    
138
			$ifinfo['pptplink'] = "up";
139

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

    
147
	if ($ifinfo['status'] == "up") {
148
		/* try to determine media with ifconfig */
149
		unset($ifconfiginfo);
150
		exec("/sbin/ifconfig " . $ifinfo['hwif'], $ifconfiginfo);
151

    
152
		foreach ($ifconfiginfo as $ici) {
153
			if (!isset($config['interfaces'][$ifdescr]['wireless'])) {
154
				/* don't list media/speed for wireless cards, as it always
155
				   displays 2 Mbps even though clients can connect at 11 Mbps */
156
				if (preg_match("/media: .*? \((.*?)\)/", $ici, $matches)) {
157
					$ifinfo['media'] = $matches[1];
158
				} else if (preg_match("/media: Ethernet (.*)/", $ici, $matches)) {
159
					$ifinfo['media'] = $matches[1];
160
				}
161
			}
162
			if (preg_match("/status: (.*)$/", $ici, $matches)) {
163
				if ($matches[1] != "active")
164
					$ifinfo['status'] = $matches[1];
165
			}
166
			if (preg_match("/channel (\S*)/", $ici, $matches)) {
167
				$ifinfo['channel'] = $matches[1];
168
			}
169
			if (preg_match("/ssid (\".*?\"|\S*)/", $ici, $matches)) {
170
				if ($matches[1][0] == '"')
171
					$ifinfo['ssid'] = substr($matches[1], 1, -1);
172
				else
173
					$ifinfo['ssid'] = $matches[1];
174
			}
175
		}
176

    
177
		if ($ifinfo['pppoelink'] != "down" && $ifinfo['pptplink'] != "down") {
178
			/* try to determine IP address and netmask with ifconfig */
179
			unset($ifconfiginfo);
180
			exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
181

    
182
			foreach ($ifconfiginfo as $ici) {
183
				if (preg_match("/inet (\S+)/", $ici, $matches)) {
184
					$ifinfo['ipaddr'] = $matches[1];
185
				}
186
				if (preg_match("/netmask (\S+)/", $ici, $matches)) {
187
					if (preg_match("/^0x/", $matches[1]))
188
						$ifinfo['subnet'] = long2ip(hexdec($matches[1]));
189
				}
190
			}
191

    
192
			if ($ifdescr == "wan") {
193
				/* run netstat to determine the default gateway */
194
				unset($netstatrninfo);
195
				exec("/usr/bin/netstat -rnf inet", $netstatrninfo);
196

    
197
				foreach ($netstatrninfo as $nsr) {
198
					if (preg_match("/^default\s*(\S+)/", $nsr, $matches)) {
199
						$ifinfo['gateway'] = $matches[1];
200
					}
201
				}
202
			}
203
		}
204
	}
205

    
206
	return $ifinfo;
207
}
208

    
209
$pgtitle = "Status: Interfaces";
210
include("head.inc");
211

    
212
?>
213

    
214
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
215
<?php include("fbegin.inc"); ?>
216
<p class="pgtitle"><?=$pgtitle?></p>
217
<form action="status_interfaces.php" method="post">
218
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
219
              <?php $i = 0; $ifdescrs = array('wan' => 'WAN', 'lan' => 'LAN');
220

    
221
		for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
222
			$ifdescrs['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
223
		}
224

    
225
		foreach ($ifdescrs as $ifdescr => $ifname):
226
			$ifinfo = get_interface_info($ifdescr);
227
		?>
228
              <?php if ($i): ?>
229
              <tr>
230
			<td colspan="8" class="list" height="12"></td>
231
			</tr>
232
			<?php endif; ?>
233
              <tr>
234
                <td colspan="2" class="listtopic">
235
                  <?=htmlspecialchars($ifname);?>
236
                  interface</td>
237
              </tr>
238
              <tr>
239
                <td width="22%" class="vncellt">Status</td>
240
                <td width="78%" class="listr">
241
                  <?=htmlspecialchars($ifinfo['status']);?>
242
                </td>
243
              </tr><?php if ($ifinfo['dhcplink']): ?>
244
			  <tr>
245
				<td width="22%" class="vncellt">DHCP</td>
246
				<td width="78%" class="listr">
247
				  <?=htmlspecialchars($ifinfo['dhcplink']);?>&nbsp;&nbsp;
248
				  <?php if ($ifinfo['dhcplink'] == "up"): ?>
249
				  <input type="submit" name="submit" value="Release" class="formbtns">
250
				  <?php else: ?>
251
				  <input type="submit" name="submit" value="Renew" class="formbtns">
252
				  <?php endif; ?>
253
				</td>
254
			  </tr><?php endif; if ($ifinfo['pppoelink']): ?>
255
              <tr>
256
                <td width="22%" class="vncellt">PPPoE</td>
257
                <td width="78%" class="listr">
258
                  <?=htmlspecialchars($ifinfo['pppoelink']);?>&nbsp;&nbsp;
259
				  <?php if ($ifinfo['pppoelink'] == "up"): ?>
260
				  <input type="submit" name="submit" value="Disconnect" class="formbtns">
261
				  <?php else: ?>
262
				  <input type="submit" name="submit" value="Connect" class="formbtns">
263
				  <?php endif; ?>
264
                </td>
265
              </tr><?php  endif; if ($ifinfo['pptplink']): ?>
266
              <tr>
267
                <td width="22%" class="vncellt">PPTP</td>
268
                <td width="78%" class="listr">
269
                  <?=htmlspecialchars($ifinfo['pptplink']);?>&nbsp;&nbsp;
270
				  <?php if ($ifinfo['pptplink'] == "up"): ?>
271
				  <input type="submit" name="submit" value="Disconnect" class="formbtns">
272
				  <?php else: ?>
273
				  <input type="submit" name="submit" value="Connect" class="formbtns">
274
				  <?php endif; ?>
275
                </td>
276
              </tr><?php  endif; if ($ifinfo['macaddr']): ?>
277
              <tr>
278
                <td width="22%" class="vncellt">MAC address</td>
279
                <td width="78%" class="listr">
280
                  <?=htmlspecialchars($ifinfo['macaddr']);?>
281
                </td>
282
              </tr><?php endif; if ($ifinfo['status'] != "down"): ?>
283
			  <?php if ($ifinfo['dhcplink'] != "down" && $ifinfo['pppoelink'] != "down" && $ifinfo['pptplink'] != "down"): ?>
284
			  <?php if ($ifinfo['ipaddr']): ?>
285
              <tr>
286
                <td width="22%" class="vncellt">IP address</td>
287
                <td width="78%" class="listr">
288
                  <?=htmlspecialchars($ifinfo['ipaddr']);?>
289
                  &nbsp; </td>
290
              </tr><?php endif; ?><?php if ($ifinfo['subnet']): ?>
291
              <tr>
292
                <td width="22%" class="vncellt">Subnet mask</td>
293
                <td width="78%" class="listr">
294
                  <?=htmlspecialchars($ifinfo['subnet']);?>
295
                </td>
296
              </tr><?php endif; ?><?php if ($ifinfo['gateway']): ?>
297
              <tr>
298
                <td width="22%" class="vncellt">Gateway</td>
299
                <td width="78%" class="listr">
300
                  <?=htmlspecialchars($ifinfo['gateway']);?>
301
                </td>
302
              </tr><?php endif; if ($ifdescr == "wan" && file_exists("{$g['varetc_path']}/nameservers.conf")): ?>
303
                <td width="22%" class="vncellt">ISP DNS servers</td>
304
                <td width="78%" class="listr"><?php echo nl2br(file_get_contents("{$g['varetc_path']}/nameservers.conf")); ?></td>
305
			  <?php endif; endif; if ($ifinfo['media']): ?>
306
              <tr>
307
                <td width="22%" class="vncellt">Media</td>
308
                <td width="78%" class="listr">
309
                  <?=htmlspecialchars($ifinfo['media']);?>
310
                </td>
311
              </tr><?php endif; ?><?php if ($ifinfo['channel']): ?>
312
              <tr>
313
                <td width="22%" class="vncellt">Channel</td>
314
                <td width="78%" class="listr">
315
                  <?=htmlspecialchars($ifinfo['channel']);?>
316
                </td>
317
              </tr><?php endif; ?><?php if ($ifinfo['ssid']): ?>
318
              <tr>
319
                <td width="22%" class="vncellt">SSID</td>
320
                <td width="78%" class="listr">
321
                  <?=htmlspecialchars($ifinfo['ssid']);?>
322
                </td>
323
              </tr><?php endif; ?>
324
              <tr>
325
                <td width="22%" class="vncellt">In/out packets</td>
326
                <td width="78%" class="listr">
327
                  <?=htmlspecialchars($ifinfo['inpkts'] . "/" . $ifinfo['outpkts'] . " (" .
328
				  		format_bytes($ifinfo['inbytes']) . "/" . format_bytes($ifinfo['outbytes']) . ")");?>
329
                </td>
330
              </tr><?php if (isset($ifinfo['inerrs'])): ?>
331
              <tr>
332
                <td width="22%" class="vncellt">In/out errors</td>
333
                <td width="78%" class="listr">
334
                  <?=htmlspecialchars($ifinfo['inerrs'] . "/" . $ifinfo['outerrs']);?>
335
                </td>
336
              </tr><?php endif; ?><?php if (isset($ifinfo['collisions'])): ?>
337
              <tr>
338
                <td width="22%" class="vncellt">Collisions</td>
339
                <td width="78%" class="listr">
340
                  <?=htmlspecialchars($ifinfo['collisions']);?>
341
                </td>
342
              </tr><?php endif; ?>
343
	      <?php endif; ?>
344
              <?php $i++; endforeach; ?>
345
            </table>
346
</form>
347
<br>
348
<strong class="red">Note:<br>
349
</strong>Using dial-on-demand will bring the connection up again if any packet
350
triggers it. To substantiate this point: disconnecting manually
351
will <strong>not</strong> prevent dial-on-demand from making connections
352
to the outside! Don't use dial-on-demand if you want to make sure that the line is kept disconnected.
353
<?php include("fend.inc"); ?>
(98-98/128)