History

netfilter: conntrack: handle ->destroy hook via nat_ops instead

The nat module already exposes a few functions to the conntrack core. Move the nat extension destroy hook to it. After this, no conntrack extension needs a destroy hook. 'struct nf_ct_ext_type' and the register/unregister api can be removed in a followup patch.

Florian Westphal committed 4 years ago

Commit 1bc91a51 parent 5f31edc5 files · +16 −36Browse files

Unified · Split

5 files
  1. include/linux/netfilter.h +1 −0
  2. include/net/netfilter/nf_conntrack_extend.h +0 −3
  3. net/netfilter/nf_conntrack_core.c +12 −2
  4. net/netfilter/nf_conntrack_extend.c +0 −21
  5. net/netfilter/nf_nat_core.c +3 −10
include/linux/netfilter.h+1−0
@@ -379,6 +379,7 @@ struct nf_nat_hook { · 378 unmodified lines
379379 unsigned int (*manip_pkt)(struct sk_buff *skb, struct nf_conn *ct,
380380 enum nf_nat_manip_type mtype,
381381 enum ip_conntrack_dir dir);
382+ void (*remove_nat_bysrc)(struct nf_conn *ct);
382383 };
383384
384385 extern const struct nf_nat_hook __rcu *nf_nat_hook;
include/net/netfilter/nf_conntrack_extend.h+0−3
@@ -79,9 +79,6 @@ void nf_ct_ext_destroy(struct nf_conn *ct); · 78 unmodified lines
7979 void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp);
8080
8181 struct nf_ct_ext_type {
82− /* Destroys relationships (can be NULL). */
83− void (*destroy)(struct nf_conn *ct);
84−
8582 enum nf_ct_ext_id id;
8683 };
8784
net/netfilter/nf_conntrack_core.c+12−2
@@ -594,7 +594,7 @@ EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc); · 593 unmodified lines
594594
595595 void nf_ct_tmpl_free(struct nf_conn *tmpl)
596596 {
597− nf_ct_ext_destroy(tmpl);
597+ kfree(tmpl->ext);
598598
599599 if (ARCH_KMALLOC_MINALIGN <= NFCT_INFOMASK)
600600 kfree((char *)tmpl - tmpl->proto.tmpl_padto);
@@ -1597,7 +1597,17 @@ void nf_conntrack_free(struct nf_conn *ct) · 996 unmodified lines
15971597 */
15981598 WARN_ON(refcount_read(&ct->ct_general.use) != 0);
15991599
1600− nf_ct_ext_destroy(ct);
1600+ if (ct->status & IPS_SRC_NAT_DONE) {
1601+ const struct nf_nat_hook *nat_hook;
1602+
1603+ rcu_read_lock();
1604+ nat_hook = rcu_dereference(nf_nat_hook);
1605+ if (nat_hook)
1606+ nat_hook->remove_nat_bysrc(ct);
1607+ rcu_read_unlock();
1608+ }
1609+
1610+ kfree(ct->ext);
16011611 kmem_cache_free(nf_conntrack_cachep, ct);
16021612 cnet = nf_ct_pernet(net);
16031613
net/netfilter/nf_conntrack_extend.c+0−21
@@ -89,27 +89,6 @@ static __always_inline unsigned int total_extension_size(void) · 88 unmodified lines
8989 ;
9090 }
9191
92−void nf_ct_ext_destroy(struct nf_conn *ct)
93−{
94− unsigned int i;
95− struct nf_ct_ext_type *t;
96−
97− for (i = 0; i < NF_CT_EXT_NUM; i++) {
98− rcu_read_lock();
99− t = rcu_dereference(nf_ct_ext_types[i]);
100−
101− /* Here the nf_ct_ext_type might have been unregisterd.
102− * I.e., it has responsible to cleanup private
103− * area in all conntracks when it is unregisterd.
104− */
105− if (t && t->destroy)
106− t->destroy(ct);
107− rcu_read_unlock();
108− }
109−
110− kfree(ct->ext);
111−}
112−
11392 void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp)
11493 {
11594 unsigned int newlen, newoff, oldlen, alloc;
net/netfilter/nf_nat_core.c+3−10
@@ -838,7 +838,7 @@ static int nf_nat_proto_remove(struct nf_conn *i, void *data) · 837 unmodified lines
838838 return i->status & IPS_NAT_MASK ? 1 : 0;
839839 }
840840
841−static void __nf_nat_cleanup_conntrack(struct nf_conn *ct)
841+static void nf_nat_cleanup_conntrack(struct nf_conn *ct)
842842 {
843843 unsigned int h;
844844
@@ -860,7 +860,7 @@ static int nf_nat_proto_clean(struct nf_conn *ct, void *data) · 15 unmodified lines
860860 * will delete entry from already-freed table.
861861 */
862862 if (test_and_clear_bit(IPS_SRC_NAT_DONE_BIT, &ct->status))
863− __nf_nat_cleanup_conntrack(ct);
863+ nf_nat_cleanup_conntrack(ct);
864864
865865 /* don't delete conntrack. Although that would make things a lot
866866 * simpler, we'd end up flushing all conntracks on nat rmmod.
@@ -868,15 +868,7 @@ static int nf_nat_proto_clean(struct nf_conn *ct, void *data) · 1 unmodified line
868868 return 0;
869869 }
870870
871−/* No one using conntrack by the time this called. */
872−static void nf_nat_cleanup_conntrack(struct nf_conn *ct)
873−{
874− if (ct->status & IPS_SRC_NAT_DONE)
875− __nf_nat_cleanup_conntrack(ct);
876−}
877−
878871 static struct nf_ct_ext_type nat_extend __read_mostly = {
879− .destroy = nf_nat_cleanup_conntrack,
880872 .id = NF_CT_EXT_NAT,
881873 };
882874
@@ -1171,6 +1163,7 @@ static const struct nf_nat_hook nat_hook = { · 288 unmodified lines
11711163 .decode_session = __nf_nat_decode_session,
11721164 #endif
11731165 .manip_pkt = nf_nat_manip_pkt,
1166+ .remove_nat_bysrc = nf_nat_cleanup_conntrack,
11741167 };
11751168
11761169 static int __init nf_nat_init(void)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t