Project

General

Profile

« Previous | Next » 

Revision a28d40cb

Added by Jim Pingle over 12 years ago

Allow specifying multiple local/remote networks for OpenVPN separated by commas. While I'm here, fix up the IPv6 tunnel/remote/local network input validation. Simplify some code using functions.

View differences:

etc/inc/openvpn.inc
209 209
	return false;
210 210
}
211 211

  
212
function openvpn_validate_cidr($value, $name) {
212
function openvpn_validate_cidr($value, $name, $multiple = false, $ipproto = "ipv4") {
213
	$value = trim($value);
214
	$error = false;
215
	if (empty($value))
216
		return false;
217
	$networks = explode(',', $value);
218

  
219
	if (!$multiple && (count($networks) > 1))
220
		return sprintf(gettext("The field '%s' must contain a single valid %s CIDR range."), $name, $ipproto);
221

  
222
	foreach ($networks as $network) {
223
		if ($ipproto == "ipv4")
224
			$error = !openvpn_validate_cidr_ipv4($network);
225
		else
226
			$error = !openvpn_validate_cidr_ipv6($network);
227
		if ($error)
228
			break;
229
	}
230

  
231
	if ($error)
232
		return sprintf(gettext("The field '%s' must contain only valid %s CIDR range(s) separated by commas."), $name, $ipproto);
233
	else
234
		return false;
235
}
236

  
237
function openvpn_validate_cidr_ipv4($value) {
213 238
	$value = trim($value);
214 239
	if (!empty($value)) {
215 240
		list($ip, $mask) = explode('/', $value);
216
		if (!is_ipaddr($ip) or !is_numeric($mask) or ($mask > 32) or ($mask < 0))
217
			return sprintf(gettext("The field '%s' must contain a valid CIDR range."), $name);
241
		if (!is_ipaddrv4($ip) or !is_numeric($mask) or ($mask > 32) or ($mask < 0))
242
			return false;
218 243
	}
219
	return false;
244
	return true;
245
}
246

  
247
function openvpn_validate_cidr_ipv6($value) {
248
	$value = trim($value);
249
	if (!empty($value)) {
250
		list($ipv6, $prefix) = explode('/', $value);
251
		if (empty($prefix))
252
			$prefix = "128";
253
		if (!is_ipaddrv6($ipv6) or !is_numeric($prefix) or ($prefix > 128) or ($prefix < 0))
254
			return false;
255
	}
256
	return true;
220 257
}
221 258

  
222 259
function openvpn_add_dhcpopts(& $settings, & $conf) {
......
523 560

  
524 561
		// Can we push routes
525 562
		if ($settings['local_network']) {
526
			list($ip, $mask) = explode('/', $settings['local_network']);
527
			$mask = gen_subnet_mask($mask);
528
			$conf .= "push \"route $ip $mask\"\n";
563
			$conf .= openvpn_gen_routes($settings['local_network'], "ipv4", true);
529 564
		}
530 565
		if ($settings['local_networkv6']) {
531
			list($ipv6, $prefix) = explode('/', $settings['local_networkv6']);
532
			if (empty($prefix))
533
				$prefix = "128";
534
			$conf .= "push \"route-ipv6 $ipv6/$prefix\"\n";
566
			$conf .= openvpn_gen_routes($settings['local_networkv6'], "ipv6", true);
535 567
		}
536 568

  
537 569
		switch($settings['mode']) {
......
613 645

  
614 646
	// Add a remote network route if set, and only for p2p modes.
615 647
	if ((substr($settings['mode'], 0, 3) == "p2p") && is_subnet($settings['remote_network'])) {
616
		list($ip, $mask) = explode('/', $settings['remote_network']);
617
		$mask = gen_subnet_mask($mask);
618
		$conf .= "route $ip $mask\n";
648
		$conf .= openvpn_gen_routes($settings['remote_network'], "ipv4", false);
619 649
	}
620 650
	// Add a remote network route if set, and only for p2p modes.
621 651
	if ((substr($settings['mode'], 0, 3) == "p2p") && is_subnet($settings['remote_networkv6'])) {
622
		list($ipv6, $prefix) = explode('/', $settings['remote_networkv6']);
623
		if (empty($prefix))
624
			$prefix = "128";
625
		$conf .= "route-ipv6 ${ipv6}/${prefix}\n";
652
		$conf .= openvpn_gen_routes($settings['remote_networkv6'], "ipv6", false);
626 653
	}
627 654

  
628 655
	// Write the settings for the keys
......
1173 1200
	}
1174 1201
}
1175 1202

  
1203
function openvpn_gen_routes($value, $ipproto = "ipv4", $push = false) {
1204
	$routes = "";
1205
	if (empty($value))
1206
		return "";
1207
	$networks = explode(',', $value);
1208

  
1209
	foreach ($networks as $network) {
1210
		if ($ipproto == "ipv4")
1211
			$route = openvpn_gen_route_ipv4($network);
1212
		else
1213
			$route = openvpn_gen_route_ipv6($network);
1214

  
1215
		if ($push)
1216
			$routes .= "push \"{$route}\"\n";
1217
		else
1218
			$routes .= "{$route}\n";
1219
	}
1220
	return $routes;
1221
}
1222

  
1223
function openvpn_gen_route_ipv4($network) {
1224
	list($ip, $mask) = explode('/', trim($network));
1225
	$mask = gen_subnet_mask($mask);
1226
	return "route $ip $mask";
1227
}
1228

  
1229
function openvpn_gen_route_ipv6($network) {
1230
	list($ipv6, $prefix) = explode('/', trim($network));
1231
	if (empty($prefix))
1232
		$prefix = "128";
1233
	return "route-ipv6 ${ipv6}/${prefix}";
1234
}
1235

  
1176 1236
?>
usr/local/www/vpn_openvpn_client.php
203 203
	}
204 204

  
205 205
	if($pconfig['tunnel_network'])
206
		if ($result = openvpn_validate_cidr($pconfig['tunnel_network'], 'Tunnel network'))
206
		if ($result = openvpn_validate_cidr($pconfig['tunnel_network'], 'IPv4 Tunnel Network', false, "ipv4"))
207 207
			$input_errors[] = $result;
208 208

  
209
	if ($result = openvpn_validate_cidr($pconfig['remote_network'], 'Remote network'))
209
	if($pconfig['tunnel_networkv6'])
210
		if ($result = openvpn_validate_cidr($pconfig['tunnel_networkv6'], 'IPv6 Tunnel Network', false, "ipv6"))
211
			$input_errors[] = $result;
212

  
213
	if ($result = openvpn_validate_cidr($pconfig['remote_network'], 'IPv4 Remote Network', true, "ipv4"))
214
		$input_errors[] = $result;
215

  
216
	if ($result = openvpn_validate_cidr($pconfig['remote_networkv6'], 'IPv6 Remote Network', true, "ipv6"))
210 217
		$input_errors[] = $result;
211 218

  
212 219
	if (!empty($pconfig['use_shaper']) && (!is_numeric($pconfig['use_shaper']) || ($pconfig['use_shaper'] <= 0)))
usr/local/www/vpn_openvpn_server.php
231 231
	if ($result = openvpn_validate_port($pconfig['local_port'], 'Local port'))
232 232
		$input_errors[] = $result;
233 233

  
234
	if ($result = openvpn_validate_cidr($pconfig['tunnel_network'], 'Tunnel network'))
234
	if ($result = openvpn_validate_cidr($pconfig['tunnel_network'], 'IPv4 Tunnel Network', false, "ipv4"))
235 235
		$input_errors[] = $result;
236 236

  
237
	if ($result = openvpn_validate_cidr($pconfig['remote_network'], 'Remote network'))
237
	if ($result = openvpn_validate_cidr($pconfig['tunnel_networkv6'], 'IPv6 Tunnel Network', false, "ipv6"))
238 238
		$input_errors[] = $result;
239 239

  
240
	if ($result = openvpn_validate_cidr($pconfig['local_network'], 'Local network'))
240
	if ($result = openvpn_validate_cidr($pconfig['remote_network'], 'IPv4 Remote Network', true, "ipv4"))
241
		$input_errors[] = $result;
242

  
243
	if ($result = openvpn_validate_cidr($pconfig['remote_networkv6'], 'IPv6 Remote Network', true, "ipv6"))
244
		$input_errors[] = $result;
245

  
246
	if ($result = openvpn_validate_cidr($pconfig['local_network'], 'IPv4 Local Network', true, "ipv4"))
247
		$input_errors[] = $result;
248

  
249
	if ($result = openvpn_validate_cidr($pconfig['local_networkv6'], 'IPv6 Local Network', true, "ipv6"))
241 250
		$input_errors[] = $result;
242 251

  
243 252
	$portused = openvpn_port_used($pconfig['protocol'], $pconfig['local_port']);

Also available in: Unified diff