summaryrefslogtreecommitdiffstats
path: root/etc/inc/lb.inc
blob: 8bb4ca2e392e1022edc76848445d0a3ea9b4966c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/* $Id$ */
/*
	lb.php
	part of pfSense (www.pfSense.com)

	Copyright (C) 2005 Chris Dionissopoulos <chdio@bug.gr>
	All rights reserved.

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

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

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

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

/*
global config data
~~~~~~~~~~~~~~~~~~
['lb'] --> unique,required
['lb']['enable'] --> unique , required, int(0|1)
['lb']['gateway'] --> ARRAY , optional
['lb']['gateway']['enable'] --> unique , required, int(0|1)
['lb']['gateway']['state']  --> unique , required, int(0|1)
['lb']['gateway']['gateway']  --> unique , required, string
['lb']['gateway']['monitor']  --> unique , required, string
['lb']['gateway']['weight']  --> unique , required, int(1..10)
['lb']['gateway']['default'] --> unique , optional, int(0|1)

*/

require_once("functions.inc");

	function addgateway($enable,$state,$gateway,$monitor,$weight,$default){
		global $config,$g;
		$exists = false;
		$lb = &$config['lb'];
		
		foreach($lb['gateway'] as $tgate)
			if ($gateway == $tgate['gateway']){
				$exists = true;
				break;
			}
		}
		if (!$exists) {
			$tgate = array();
			$tgate['enable'] = $enable;
			if ($enable) 
				$tgate['state'] = $this->getstate($monitor);
			else 
				$tgate['state'] = 0;
			$tgate['gateway'] = $gateway;
			$tgate['monitor'] = $monitor;
			$tgate['weight'] = $weight;
			if ($default==1) {
				$i=0;
				foreach($lb['gateway'] as $ttgate) {
					if (isset($this->lb['gateway'][$i]['default']))
						unset($this->lb['gateway'][$i]['default']);
					$i++;
				}
				$tgate['default'] = 1;
			} else 
				$tgate['default'] = 0;
			$lb['gateway'][] = $tgate;
			return true;
		} else {
			return "error: gateway exists!";
	}
		
	function edit_gateway($num,$enable,$state,$gateway,$monitor,$weight,$default){
		global $config,$g;
		$i=0;
		$exists = false;
		$lb = &$config['lb'];
		
		foreach($lb['gateway'] as $tgate)
			if ($gateway == $tgate['gateway'] && $i!=$num){
				$exists = true;
				break;
			}
			$i++;
		}
		if (!$exists) {
			$tgate = array();
			$tgate['enable'] = $enable;
			if ($enable) 
				$tgate['state'] = get_state($monitor);
			else 
				$tgate['state'] = 0;
			$tgate['gateway'] = $gateway;
			$tgate['monitor'] = $monitor;
			$tgate['weight'] = $weight;
			if ($default==1) {
				$i=0;
				foreach($lb['gateway'] as $ttgate){
					if (isset($lb['gateway'][$i]['default']))
						unset($lb['gateway'][$i]['default']);
					$i++;
				}
				$tgate['default'] = 1;
			} else 
				$tgate['default'] = 0;
			$lb['gateway'][$num] = $tgate;
			return true;
		} else {
			return "error: gateway exists!";
	}
	
	function get_state($ip){
		$pingcmd = exec("/usr/local/bin/sudo /sbin/ping -c1 -t1 -n ".$ip,$result);
		if (preg_match_all('/.*100\%\spacket\sloss.*/',$result[count($result)-1], $match))
			return 0;
		if (preg_match_all('/.*0\%\spacket\sloss.*/',$result[count($result)-2], $match))
			return 1;
	}
	
	function rules(){
		global $config,$g;
		$lb = &$config['lb'];
		$lancfg = $config['interfaces']['lan'];
		$wancfg = $config['interfaces']['wan'];
		$out_flow = ' ip from '.$lancfg['ipaddr'].'/'.$lancfg['subnet']
					.' to not '. $lancfg['ipaddr'].'/'.$lancfg['subnet'];
		$in_flow = 'ip from not '.$lancfg['ipaddr'].'/'.$lancfg['subnet']
					.' to '.$lancfg['ipaddr'].'/'.$lancfg['subnet'];
					
		$rules ="";
		$num = 1;
		$i=0;
		$gates = count($lb['gateway']);
		foreach($lb['gateway'] as $tgate){
			$num++;
			$in_num = 10000+$num;
			$out_num = 11000+$num;
			$skipto = 20000 + $num*10;
			$sw[$i] = 0;
			for($j=$i;$j<$gates;$j++)
				$sw[$i] +=$lb['gateway'][$j]['weight'];
			$prob = round($tgate['weight']/$sw[$i], 2);
					
			$rules .='/sbin/ipfw add $in_num set 5 skipto '.$skipto
					.' '.$in_flow.' mac any '.arp_get_mac_by_ip($tgate['gateway'])
					.' in recv '.$wancfg['if'].' keep-state\n';
					
			$rules .='/sbin/ipfw add $out_num set 6 prob '.$prob.' skipto '.$skipto
				.' '.$out_flow.' in recv '. $lancfg['if'].' keep-state\n';
					
			$rules .='/sbin/ipfw add $skipto set 6 fwd '.$tgate['gateway']
					.' '.$out_flow.' in recv '.$lancfg['if'].'\n';
					
			$skipto++;
			$rule .= '/sbin/ipfw add $skipto set 6 skipto 65535 ip from any to any\n';
			$i++;
		}
		return $rules;
	}
	
	


?>
OpenPOWER on IntegriCloud