netfilter: extensions: don't add nat by default
Subsystems (2)
Florian Westphal
maintainercover netfilter: extensions: don't add nat by default
The nat extension only holds information needed by pptp and the masquerade target (and in the latter case its not essential), so no need to attach it by default.
First two patches add the nat extension in masquerade and pptp case, 3rd patch gets rid of the default-add.
This saves 8 bytes in the extension blob unless a connection is subject to MASQERUADE and/or pptp nat helper.
Florian Westphal
maintainerPATCH 1/3 netfilter: masquerade: attach nat extension if not present
Currently the nat extension is always attached as soon as nat module is loaded. However, most NAT uses do not need the nat extension anymore.
Prepare to remove the add-nat-by-default by making those places that need it attach it if its not present yet.
--- net/ipv4/netfilter/nf_nat_masquerade_ipv4.c | 5 +++-- net/ipv6/netfilter/nf_nat_masquerade_ipv6.c | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-)
net/ipv4/netfilter/nf_nat_masquerade_ipv4.c +3 -2
@@ -37,7 +37,6 @@ nf_nat_masquerade_ipv4(struct sk_buff *skb, unsigned int hooknum,NF_CT_ASSERT (hooknum ==NF_INET_POST_ROUTING ); ct =nf_ct_get (skb, &ctinfo); - nat =nfct_nat (ct);NF_CT_ASSERT (ct && (ctinfo ==IP_CT_NEW || ctinfo ==IP_CT_RELATED || ctinfo ==IP_CT_RELATED_REPLY ));
@@ -56,7 +55,9 @@ nf_nat_masquerade_ipv4(struct sk_buff *skb, unsigned int hooknum,return NF_DROP ; } - nat->masq_index = out->ifindex; + nat =nf_ct_nat_ext_add (ct); +if (nat) + nat->masq_index = out->ifindex;/* Transfer from original range. */ memset (&newrange.min_addr,0 ,sizeof (newrange.min_addr));
net/ipv6/netfilter/nf_nat_masquerade_ipv6.c +4 -1
@@ -30,6 +30,7 @@ nf_nat_masquerade_ipv6(struct sk_buff *skb, const struct nf_nat_range *range, const struct net_device *out) {enum ip_conntrack_info ctinfo; +struct nf_conn_nat *nat;struct in6_addr src;struct nf_conn *ct;struct nf_nat_range newrange;
@@ -42,7 +43,9 @@ nf_nat_masquerade_ipv6(struct sk_buff *skb, const struct nf_nat_range *range, &ipv6_hdr(skb)->daddr, 0, &src) < 0)return NF_DROP ; -nfct_nat (ct)->masq_index = out->ifindex; + nat =nf_ct_nat_ext_add (ct); +if (nat) + nat->masq_index = out->ifindex; newrange.flags = range->flags |NF_NAT_RANGE_MAP_IPS ; newrange.min_addr.in6 = src;
Signature · 1 line
2.10.2
Florian Westphal
maintainerPATCH 2/3 netfilter: pptp: attach nat extension when needed
In reply to Florian Westphalmake sure nat extension gets added if the master conntrack is subject to NAT. This will be required once the nat core stops adding it by default.
--- net/ipv4/netfilter/nf_nat_pptp.c | 25 +++++++++++++++++++++---- net/netfilter/nf_conntrack_pptp.c | 12 ++++++++++-- 2 files changed, 31 insertions(+), 6 deletions(-)
net/ipv4/netfilter/nf_nat_pptp.c +21 -4
@@ -49,9 +49,14 @@ static void pptp_nat_expected(struct nf_conn *ct,const struct nf_ct_pptp_master *ct_pptp_info;const struct nf_nat_pptp *nat_pptp_info;struct nf_nat_range range; +struct nf_conn_nat *nat; + nat =nf_ct_nat_ext_add (ct); +if (WARN_ON_ONCE (!nat)) +return ; + + nat_pptp_info = &nat->help.nat_pptp_info; ct_pptp_info =nfct_help_data (master); - nat_pptp_info = &nfct_nat (master)->help.nat_pptp_info;/* And here goes the grand finale of corrosion... */ if (exp->dir ==IP_CT_DIR_ORIGINAL ) {
@@ -120,13 +125,17 @@ pptp_outbound_pkt(struct sk_buff *skb, {struct nf_ct_pptp_master *ct_pptp_info; +struct nf_conn_nat *nat =nfct_nat (ct);struct nf_nat_pptp *nat_pptp_info;u_int16_t msg;__be16 new_callid;unsigned int cid_off; +if (WARN_ON_ONCE (!nat)) +return NF_DROP ; + + nat_pptp_info = &nat->help.nat_pptp_info; ct_pptp_info =nfct_help_data (ct); - nat_pptp_info = &nfct_nat (ct)->help.nat_pptp_info; new_callid = ct_pptp_info->pns_call_id;
@@ -191,11 +200,15 @@ pptp_exp_gre(struct nf_conntrack_expect *expect_orig, struct nf_conntrack_expect *expect_reply) {const struct nf_conn *ct = expect_orig->master; +struct nf_conn_nat *nat =nfct_nat (ct);struct nf_ct_pptp_master *ct_pptp_info;struct nf_nat_pptp *nat_pptp_info; +if (WARN_ON_ONCE (!nat)) +return ; + + nat_pptp_info = &nat->help.nat_pptp_info; ct_pptp_info =nfct_help_data (ct); - nat_pptp_info = &nfct_nat (ct)->help.nat_pptp_info;/* save original PAC call ID in nat_info */ nat_pptp_info->pac_call_id = ct_pptp_info->pac_call_id;
@@ -223,11 +236,15 @@ pptp_inbound_pkt(struct sk_buff *skb, union pptp_ctrl_union *pptpReq) {const struct nf_nat_pptp *nat_pptp_info; +struct nf_conn_nat *nat =nfct_nat (ct);u_int16_t msg;__be16 new_pcid;unsigned int pcid_off; - nat_pptp_info = &nfct_nat (ct)->help.nat_pptp_info; +if (WARN_ON_ONCE (!nat)) +return NF_DROP ; + + nat_pptp_info = &nat->help.nat_pptp_info; new_pcid = nat_pptp_info->pns_call_id;switch (msg =ntohs (ctlh->messageType)) {
net/netfilter/nf_conntrack_pptp.c +10 -2
@@ -263,7 +263,7 @@ static int exp_gre(struct nf_conn *ct, __be16 callid, __be16 peer_callid) goto out_put_both; } -static inline int +static int pptp_inbound_pkt(struct sk_buff *skb, unsigned int protoff, struct PptpControlHeader *ctlh, union pptp_ctrl_union *pptpReq,
@@ -391,7 +391,7 @@ pptp_inbound_pkt(struct sk_buff *skb, unsigned int protoff,return NF_ACCEPT ; } -static inline int +static int pptp_outbound_pkt(struct sk_buff *skb, unsigned int protoff, struct PptpControlHeader *ctlh, union pptp_ctrl_union *pptpReq,
@@ -523,6 +523,14 @@ conntrack_pptp_help(struct sk_buff *skb, unsigned int protoff,int ret;u_int16_t msg; +#if IS_ENABLED (CONFIG_NF_NAT ) +if (!nf_ct_is_confirmed (ct) && (ct->status &IPS_NAT_MASK )) { +struct nf_conn_nat *nat =nf_ct_ext_find (ct,NF_CT_EXT_NAT ); + +if (!nat && !nf_ct_ext_add (ct,NF_CT_EXT_NAT ,GFP_ATOMIC )) +return NF_DROP ; + } +#endif /* don't do any tracking before tcp handshake complete */ if (ctinfo !=IP_CT_ESTABLISHED && ctinfo !=IP_CT_ESTABLISHED_REPLY )return NF_ACCEPT ;
Signature · 1 line
2.10.2
Florian Westphal
maintainerPATCH 3/3 netfilter: don't attach a nat extension by default
In reply to Florian Westphalnowadays the NAT extension only stores the interface index (used to purge connections that got masqueraded when interface goes down) and pptp nat information.
Previous patches moved nf_ct_nat_ext_add to those places that need it.
--- include/net/netfilter/nf_nat.h | 2 +- net/ipv4/netfilter/nf_nat_l3proto_ipv4.c | 4 +--- net/ipv6/netfilter/nf_nat_l3proto_ipv6.c | 4 +--- net/netfilter/nf_nat_core.c | 6 ------ 4 files changed, 3 insertions(+), 13 deletions(-)
include/net/netfilter/nf_nat.h +1 -1
@@ -67,7 +67,7 @@ static inline bool nf_nat_oif_changed(unsigned int hooknum, {#if IS_ENABLED (CONFIG_NF_NAT_MASQUERADE_IPV4 ) || \IS_ENABLED (CONFIG_NF_NAT_MASQUERADE_IPV6 ) -return nat->masq_index && hooknum ==NF_INET_POST_ROUTING && +return nat && nat->masq_index && hooknum ==NF_INET_POST_ROUTING &&CTINFO2DIR (ctinfo) ==IP_CT_DIR_ORIGINAL && nat->masq_index != out->ifindex;#else
net/ipv4/netfilter/nf_nat_l3proto_ipv4.c +1 -3
@@ -264,9 +264,7 @@ nf_nat_ipv4_fn(void *priv, struct sk_buff *skb,if (!ct)return NF_ACCEPT ; - nat =nf_ct_nat_ext_add (ct); -if (nat ==NULL ) -return NF_ACCEPT ; + nat =nfct_nat (ct);switch (ctinfo) {case IP_CT_RELATED :
net/ipv6/netfilter/nf_nat_l3proto_ipv6.c +1 -3
@@ -273,9 +273,7 @@ nf_nat_ipv6_fn(void *priv, struct sk_buff *skb,if (!ct)return NF_ACCEPT ; - nat =nf_ct_nat_ext_add (ct); -if (nat ==NULL ) -return NF_ACCEPT ; + nat =nfct_nat (ct);switch (ctinfo) {case IP_CT_RELATED :
net/netfilter/nf_nat_core.c +0 -6
@@ -408,12 +408,6 @@ nf_nat_setup_info(struct nf_conn *ct, enum nf_nat_manip_type maniptype) {struct nf_conntrack_tuple curr_tuple, new_tuple; -struct nf_conn_nat *nat; - -/* nat helper or nfctnetlink also setup binding */ - nat =nf_ct_nat_ext_add (ct); -if (nat ==NULL ) -return NF_ACCEPT ;NF_CT_ASSERT (maniptype ==NF_NAT_MANIP_SRC || maniptype ==NF_NAT_MANIP_DST );
Signature · 1 line
2.10.2
Pablo Neira Ayuso
maintainerQuoted 8 lines · Florian Westphal
The nat extension only holds information needed by pptp and the masquerade target (and in the latter case its not essential), so no need to attach it by default.
First two patches add the nat extension in masquerade and pptp case, 3rd patch gets rid of the default-add.
This saves 8 bytes in the extension blob unless a connection is subject to MASQERUADE and/or pptp nat helper.
Series applied, thanks Florian.