I need to masquerade a set of IP addresses when they try to reach a certain (internal) IP address on another VLAN.
I found no way to do this in Luci and my attempts with /etc/firewall.user have failed:
nft add rule nat postrouting ip saddr @lgcastdevices ip daddr 10.1.103.102 masquerade
When I do a service firewall restart the answer is
Error: No such file or directory
add rule nat postrouting ip saddr @lgcastdevices ip daddr 10.1.103.102 masquerade
^^^
Include '/etc/firewall.user' failed with exit code 1
Is there a way to use an ipset for masquerading or do I have to create a rule for every single IP?
Ah, I think I see what you mean, the problem is that the subnets are in different firewall zones.
Why I need masquerade:
My TV (LG) is in a different VLAN than my/our phones. I can see the TV and send packages to it (all from the phone), but I can't cast to it because the TV itself rejects packets that come from another subnet (403 Forbidden). Everything works when I masquerade the phones IP addresses.
I created an ipset in LuCI containing the IPs of the phones. I'd like to use that ipset now somehow in a rule so that these IPs get masqueraded so that the TV accepts them.
I apologize for overlooking this point.
Absolutely no need to apologize, I am happy for any help!
OK. I'll respond like this: there's no direct method to masquerade an IP set. This is because - Your DST (i.e. the IPs in the set) must be considered before the routing decision is made (i.e. PREROUTING) - and you want this to have an effect on a POSTROUTING config (i.e. masquerading).
It should be possible to somehow mark these packets and masquerade all such packets destined for foo.
The problem is here. You are still thinking in terms of iptables (not helped by the very confusing use of the term "ipset" in fw4 as this implies iptables in all other Linux distros).
There is no "nat" table and postrouting chain to add your rule to, these are legacy table/chain names.
Try this instead, on the command line at first so that a reboot will clear it if something breaks:
nft insert rule inet fw4 srcnat_wan ip saddr @lgcastdevices ip daddr 10.1.103.102 masquerade
An SNAT rule (in NAT rules) would be more appropriate there. It provides more granular settings compared to the MASQUERADE of the whole traffic leaving a zone.
I barely know what I am doing with the masquerading, so this might be a stupid question, but why would a SNAT be better? From checking it out in LuCI it seems the only additional option would be to specify the IP address?
With masquerade I masquerade a bunch of IPs only when they connect to a single IP in another subnet, not the whole source subnet?
Masquerade and SNAT do the same, nat the source IP of the packet.
Masquerade is more common on edge routers where you can NAT all traffic exiting an interface, using the IP address of the interface and is used when we want to alter all traffic.
SNAT is used for more refined cases, like yours, when only specific traffic needs to be translated.
Apologies if I am still missing something obvious, but doesn't this rule mean that not all traffic is masqueraded but only traffic going to 10.1.103.102.
That would be fine for me, but I wanted to make sure that I am not misinterpreting the rule.