Project

General

Profile

Download (13.7 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
        for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
105
                $ifdescrs['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
106
                if (($ifdescr == "opt$j") && ($config['interfaces']['opt' . $j]['ipaddr'] == "dhcp")) {
107
                        /* see if dhclient is up */
108
                        if (is_process_running("dhclient") == true)
109
                                $ifinfo['dhcplink'] = "up";
110
                        else
111
                                $ifinfo['dhcplink'] = "down";
112
                }
113
        }
114

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

    
127
			$ifinfo['pppoelink'] = "up";
128

    
129
			foreach ($ifconfiginfo as $ici) {
130
				if (strpos($ici, 'LINK0') !== false)
131
					$ifinfo['pppoelink'] = "down";
132
			}
133
		}
134
	}
135

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

    
148
			$ifinfo['pptplink'] = "up";
149

    
150
			foreach ($ifconfiginfo as $ici) {
151
				if (strpos($ici, 'LINK0') !== false)
152
					$ifinfo['pptplink'] = "down";
153
			}
154
		}
155
	}
156

    
157
	if ($ifinfo['status'] == "up") {
158
		/* try to determine media with ifconfig */
159
		unset($ifconfiginfo);
160
		exec("/sbin/ifconfig " . $ifinfo['hwif'], $ifconfiginfo);
161

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

    
187
		if ($ifinfo['pppoelink'] != "down" && $ifinfo['pptplink'] != "down") {
188
			/* try to determine IP address and netmask with ifconfig */
189
			unset($ifconfiginfo);
190
			exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
191

    
192
			foreach ($ifconfiginfo as $ici) {
193
				if (preg_match("/inet (\S+)/", $ici, $matches)) {
194
					$ifinfo['ipaddr'] = $matches[1];
195
				}
196
				if (preg_match("/netmask (\S+)/", $ici, $matches)) {
197
					if (preg_match("/^0x/", $matches[1]))
198
						$ifinfo['subnet'] = long2ip(hexdec($matches[1]));
199
				}
200
			}
201

    
202
			if ($ifdescr == "wan") {
203
				/* run netstat to determine the default gateway */
204
				unset($netstatrninfo);
205
				exec("/usr/bin/netstat -rnf inet", $netstatrninfo);
206

    
207
				foreach ($netstatrninfo as $nsr) {
208
					if (preg_match("/^default\s*(\S+)/", $nsr, $matches)) {
209
						$ifinfo['gateway'] = $matches[1];
210
					}
211
				}
212
			}
213
		}
214
	}
215

    
216
	return $ifinfo;
217
}
218

    
219
$pgtitle = "Status: Interfaces";
220
include("head.inc");
221

    
222
?>
223

    
224
<body link="#0000CC" vlink="#0000CC" alink="#0000CC">
225
<?php include("fbegin.inc"); ?>
226
<p class="pgtitle"><?=$pgtitle?></p>
227
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
228
              <?php $i = 0; $ifdescrs = array('wan' => 'WAN', 'lan' => 'LAN');
229

    
230
		for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
231
			$ifdescrs['opt' . $j] = $config['interfaces']['opt' . $j]['descr'];
232
		}
233

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

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

    
376
<?php include("fend.inc"); ?>
(109-109/143)