Skip to content

Commit 1bc91a5

Browse files
Florian Westphalummakynes
authored andcommitted
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. Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent 5f31edc commit 1bc91a5

File tree

5 files changed

+16
-36
lines changed

5 files changed

+16
-36
lines changed

include/linux/netfilter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ struct nf_nat_hook {
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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ void nf_ct_ext_destroy(struct nf_conn *ct);
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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ EXPORT_SYMBOL_GPL(nf_ct_tmpl_alloc);
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)
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

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,6 @@ static __always_inline unsigned int total_extension_size(void)
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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ static int nf_nat_proto_remove(struct nf_conn *i, void *data)
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,23 +860,15 @@ static int nf_nat_proto_clean(struct nf_conn *ct, void *data)
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.
867867
*/
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 = {
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)

0 commit comments

Comments
 (0)