2011-03-10 13:08:39 +01:00
|
|
|
# This module enables Network Address Translation (NAT).
|
2012-10-06 07:11:57 +02:00
|
|
|
# XXX: todo: support multiple upstream links
|
|
|
|
# see http://yesican.chsoft.biz/lartc/MultihomedLinuxNetworking.html
|
2011-03-10 13:08:39 +01:00
|
|
|
|
|
|
|
{ config, pkgs, ... }:
|
|
|
|
|
|
|
|
with pkgs.lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.networking.nat;
|
|
|
|
|
2014-04-10 14:23:38 +02:00
|
|
|
dest = if cfg.externalIP == null then "-j MASQUERADE" else "-j SNAT --to-source ${cfg.externalIP}";
|
|
|
|
|
2011-03-10 13:08:39 +01:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2011-03-10 13:08:39 +01:00
|
|
|
networking.nat.enable = mkOption {
|
2013-10-30 17:37:45 +01:00
|
|
|
type = types.bool;
|
2011-03-10 13:08:39 +01:00
|
|
|
default = false;
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
Whether to enable Network Address Translation (NAT).
|
|
|
|
'';
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2014-04-10 14:23:38 +02:00
|
|
|
networking.nat.internalInterfaces = mkOption {
|
|
|
|
type = types.listOf types.str;
|
|
|
|
default = [];
|
|
|
|
example = [ "eth0" ];
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
The interfaces for which to perform NAT. Packets coming from
|
|
|
|
these interface and destined for the external interface will
|
|
|
|
be rewritten.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2011-03-10 13:08:39 +01:00
|
|
|
networking.nat.internalIPs = mkOption {
|
2013-10-30 17:37:45 +01:00
|
|
|
type = types.listOf types.str;
|
2014-04-10 14:23:38 +02:00
|
|
|
default = [];
|
|
|
|
example = [ "192.168.1.0/24" ];
|
2011-03-10 13:08:39 +01:00
|
|
|
description =
|
|
|
|
''
|
2012-10-06 07:11:57 +02:00
|
|
|
The IP address ranges for which to perform NAT. Packets
|
2014-04-10 14:23:38 +02:00
|
|
|
coming from these addresses (on any interface) and destined
|
|
|
|
for the external interface will be rewritten.
|
2011-03-10 13:08:39 +01:00
|
|
|
'';
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2011-03-10 13:08:39 +01:00
|
|
|
networking.nat.externalInterface = mkOption {
|
2013-10-30 17:37:45 +01:00
|
|
|
type = types.str;
|
2011-03-10 13:08:39 +01:00
|
|
|
example = "eth1";
|
|
|
|
description =
|
|
|
|
''
|
|
|
|
The name of the external network interface.
|
|
|
|
'';
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2011-03-10 13:08:39 +01:00
|
|
|
networking.nat.externalIP = mkOption {
|
2013-10-30 17:37:45 +01:00
|
|
|
type = types.nullOr types.str;
|
|
|
|
default = null;
|
2011-03-10 13:08:39 +01:00
|
|
|
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 20:20:50 +02:00
|
|
|
|
2011-03-10 13:08:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkIf config.networking.nat.enable {
|
|
|
|
|
|
|
|
environment.systemPackages = [ pkgs.iptables ];
|
|
|
|
|
2011-03-10 14:03:47 +01:00
|
|
|
boot.kernelModules = [ "nf_nat_ftp" ];
|
|
|
|
|
2011-03-10 13:08:39 +01:00
|
|
|
jobs.nat =
|
|
|
|
{ description = "Network Address Translation";
|
|
|
|
|
|
|
|
startOn = "started network-interfaces";
|
|
|
|
|
|
|
|
path = [ pkgs.iptables ];
|
|
|
|
|
|
|
|
preStart =
|
|
|
|
''
|
2014-04-11 16:29:45 +02:00
|
|
|
iptables -w -t nat -F PREROUTING
|
|
|
|
iptables -w -t nat -F POSTROUTING
|
|
|
|
iptables -w -t nat -X
|
2014-04-10 14:23:38 +02:00
|
|
|
|
|
|
|
# We can't match on incoming interface in POSTROUTING, so
|
|
|
|
# mark packets coming from the external interfaces.
|
|
|
|
${concatMapStrings (iface: ''
|
2014-04-11 16:29:45 +02:00
|
|
|
iptables -w -t nat -A PREROUTING \
|
2014-04-10 14:23:38 +02:00
|
|
|
-i '${iface}' -j MARK --set-mark 1
|
|
|
|
'') cfg.internalInterfaces}
|
|
|
|
|
|
|
|
# NAT the marked packets.
|
|
|
|
${optionalString (cfg.internalInterfaces != []) ''
|
2014-04-11 16:29:45 +02:00
|
|
|
iptables -w -t nat -A POSTROUTING -m mark --mark 1 \
|
2014-04-10 14:23:38 +02:00
|
|
|
-o ${cfg.externalInterface} ${dest}
|
|
|
|
''}
|
|
|
|
|
|
|
|
# NAT packets coming from the internal IPs.
|
|
|
|
${concatMapStrings (range: ''
|
2014-04-11 16:29:45 +02:00
|
|
|
iptables -w -t nat -A POSTROUTING \
|
2014-04-11 16:26:00 +02:00
|
|
|
-s '${range}' -o ${cfg.externalInterface} ${dest}
|
2014-04-10 14:23:38 +02:00
|
|
|
'') cfg.internalIPs}
|
|
|
|
|
2011-03-10 13:08:39 +01:00
|
|
|
echo 1 > /proc/sys/net/ipv4/ip_forward
|
|
|
|
'';
|
|
|
|
|
|
|
|
postStop =
|
|
|
|
''
|
2014-04-11 16:29:45 +02:00
|
|
|
iptables -w -t nat -F PREROUTING
|
|
|
|
iptables -w -t nat -F POSTROUTING
|
|
|
|
iptables -w -t nat -X
|
2011-03-10 13:08:39 +01:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|