2011-03-10 12:08:39 +00:00
|
|
|
# This module enables Network Address Translation (NAT).
|
|
|
|
|
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
with pkgs.lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.networking.nat;
|
|
|
|
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2011-03-10 12:08:39 +00:00
|
|
|
networking.nat.enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Whether to enable Network Address Translation (NAT).
|
|
|
|
'';
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2011-03-10 12:08:39 +00:00
|
|
|
networking.nat.internalIPs = mkOption {
|
|
|
|
example = "192.168.1.0/24";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
The IP address range for which to perform NAT. Packets
|
|
|
|
coming from these addresses and destined for the external
|
|
|
|
interface will be rewritten.
|
|
|
|
'';
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2011-03-10 12:08:39 +00:00
|
|
|
networking.nat.externalInterface = mkOption {
|
|
|
|
example = "eth1";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
The name of the external network interface.
|
|
|
|
'';
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2011-03-10 12:08:39 +00:00
|
|
|
networking.nat.externalIP = mkOption {
|
|
|
|
default = "";
|
|
|
|
example = "203.0.113.123";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
The public IP address to which packets from the local
|
|
|
|
network are to be rewritten. If this is left empty, the
|
|
|
|
IP address associated with the external interface will be
|
|
|
|
used.
|
|
|
|
'';
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2011-03-10 12:08:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf config.networking.nat.enable {
|
|
|
|
|
|
|
|
environment.systemPackages = [ pkgs.iptables ];
|
|
|
|
|
2011-03-10 13:03:47 +00:00
|
|
|
boot.kernelModules = [ "nf_nat_ftp" ];
|
|
|
|
|
2011-03-10 12:08:39 +00:00
|
|
|
jobs.nat =
|
|
|
|
{ description = "Network Address Translation";
|
|
|
|
|
|
|
|
startOn = "started network-interfaces";
|
|
|
|
|
|
|
|
path = [ pkgs.iptables ];
|
|
|
|
|
|
|
|
preStart =
|
|
|
|
''
|
2011-08-04 15:33:40 +00:00
|
|
|
iptables -t nat -F POSTROUTING
|
2011-03-10 12:08:39 +00:00
|
|
|
iptables -t nat -X
|
|
|
|
|
|
|
|
iptables -t nat -A POSTROUTING \
|
|
|
|
-s ${cfg.internalIPs} -o ${cfg.externalInterface} \
|
|
|
|
${if cfg.externalIP == ""
|
|
|
|
then "-j MASQUERADE"
|
|
|
|
else "-j SNAT --to-source ${cfg.externalIP}"}
|
|
|
|
|
|
|
|
echo 1 > /proc/sys/net/ipv4/ip_forward
|
|
|
|
'';
|
|
|
|
|
|
|
|
postStop =
|
|
|
|
''
|
2011-08-04 15:33:40 +00:00
|
|
|
iptables -t nat -F POSTROUTING
|
2011-03-10 12:08:39 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|