I've been working for more than a week trying to get outbound packets nat'ed to fit a Security Association.

This is my (example) scenario:

  • LAN:
  • FreeBSD interface: xn0 (Amazon EC2 instance)
  • Virtual Address to nat traffic from:
  • Destination to reach:

Problem description

Despite the fact I've all the SA's up and running in the FreeBSD Box, trying to nat the packets using either pf or ipfw, doesn't work, the nat'ed packet keeps flowing using the default route interface and doesn't get into the ipsec tunnel.

Here is an example SA using:

$ ipsec status vpn
vpn{1}:  INSTALLED, TUNNEL, reqid 1, ESP in UDP SPIs: cca608fa_i d74355dc_o vpn{1}:  AES_CBC_256/HMAC_SHA2_256_128, 2688 bytes_i (32 pkts, 32s ago), 4992 bytes_o (32 pkts, 32s ago), rekeying in 43 minutes vpn{1}: ===

this is my nat rule on pf.conf:

nat on enc0 from to ->

This is my ipsec0 interface status:

ipsec0:  flags=8011<UP,POINTOPOINT,MULTICAST> metric 0 mtu 1400     inet --> netmask 0xffffffff options=23<PERFORMNUD,ACCEPT_RTADV,AUTO_LINKLOCAL>     reqid: 0    groups: ipsec

I can reach from smoothly.

This is a simple packet capture on xn0 (physical interface):

$ tcpdump -i xn0 -n -vvv host > ICMP echo request, id 15745, seq 818, length 64

and no traffic is seen on enc0.

Another try was to set via sysctl:


But I'm not completely sure what exactly this option does.

I've seen many people struggling to get this kind of nat working without success.


1 Answer 1


After several days struggling I've been able to handle this doing the following steps and will post the solution here to help others

Get the unique ID of the desired SA you want to nat source to, example:

setkey -DP

and find the SA unique ID:[any][any] any
    out ipsec
    created: Jul 22 22:29:34 2018  lastused: Jul 22 22:29:34 2018
    lifetime: 9223372036854775807(s) validtime: 0(s)
    spid=2715 seq=0 pid=13358 scope=global

in this case: 1

add a new policy:

setkey -v -c
spdadd -4 any -P out ipsec esp/tunnel/REALLOCALIP-REALREMOTEIP/unique:1;

This should be sufficient to route packets inside the tunnel and get them nat'ed to match the real SA installed.

Keep in mind this unique id must match the installed SA and if you change the order of the SA install in StrongSwan you must change this policy to fit the new UniqueID.

The nat rules must be applied on enc0 interface.



Good luck.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .