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
$pgtitle = array("Status", "Interfaces");
33

    
34
require_once("guiconfig.inc");
35

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

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

    
59
function get_interface_info($ifdescr) {
60

    
61
	global $config, $g;
62

    
63
	$ifinfo = array();
64

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

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

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

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

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

    
119
			$ifinfo['pppoelink'] = "up";
120

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

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

    
140
			$ifinfo['pptplink'] = "up";
141

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

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

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

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

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

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

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

    
208
	return $ifinfo;
209
}
210

    
211
$pgtitle = "Status: Interfaces";
212
include("head.inc");
213

    
214
?>
215

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

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

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