Skip to content

Commit 610a431

Browse files
Florian Westphalummakynes
authored andcommitted
netfilter: nf_nat_masquerade: unify ipv4/6 notifier registration
Only reason for having two different register functions was because of ipt_MASQUERADE and ip6t_MASQUERADE being two different modules. Previous patch merged those into xt_MASQUERADE, so we can merge this too. Signed-off-by: Florian Westphal <[email protected]> Signed-off-by: Pablo Neira Ayuso <[email protected]>
1 parent adf82ac commit 610a431

File tree

4 files changed

+44
-95
lines changed

4 files changed

+44
-95
lines changed

include/net/netfilter/nf_nat_masquerade.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ nf_nat_masquerade_ipv4(struct sk_buff *skb, unsigned int hooknum,
99
const struct nf_nat_range2 *range,
1010
const struct net_device *out);
1111

12-
int nf_nat_masquerade_ipv4_register_notifier(void);
13-
void nf_nat_masquerade_ipv4_unregister_notifier(void);
12+
int nf_nat_masquerade_inet_register_notifiers(void);
13+
void nf_nat_masquerade_inet_unregister_notifiers(void);
1414

1515
unsigned int
1616
nf_nat_masquerade_ipv6(struct sk_buff *skb, const struct nf_nat_range2 *range,
1717
const struct net_device *out);
18-
int nf_nat_masquerade_ipv6_register_notifier(void);
19-
void nf_nat_masquerade_ipv6_unregister_notifier(void);
2018

2119
#endif /*_NF_NAT_MASQUERADE_H_ */

net/netfilter/nf_nat_masquerade.c

Lines changed: 37 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
#include <net/netfilter/nf_nat_masquerade.h>
1111

1212
static DEFINE_MUTEX(masq_mutex);
13-
static unsigned int masq_refcnt4 __read_mostly;
14-
static unsigned int masq_refcnt6 __read_mostly;
13+
static unsigned int masq_refcnt __read_mostly;
1514

1615
unsigned int
1716
nf_nat_masquerade_ipv4(struct sk_buff *skb, unsigned int hooknum,
@@ -136,56 +135,6 @@ static struct notifier_block masq_inet_notifier = {
136135
.notifier_call = masq_inet_event,
137136
};
138137

139-
int nf_nat_masquerade_ipv4_register_notifier(void)
140-
{
141-
int ret = 0;
142-
143-
mutex_lock(&masq_mutex);
144-
if (WARN_ON_ONCE(masq_refcnt4 == UINT_MAX)) {
145-
ret = -EOVERFLOW;
146-
goto out_unlock;
147-
}
148-
149-
/* check if the notifier was already set */
150-
if (++masq_refcnt4 > 1)
151-
goto out_unlock;
152-
153-
/* Register for device down reports */
154-
ret = register_netdevice_notifier(&masq_dev_notifier);
155-
if (ret)
156-
goto err_dec;
157-
/* Register IP address change reports */
158-
ret = register_inetaddr_notifier(&masq_inet_notifier);
159-
if (ret)
160-
goto err_unregister;
161-
162-
mutex_unlock(&masq_mutex);
163-
return ret;
164-
165-
err_unregister:
166-
unregister_netdevice_notifier(&masq_dev_notifier);
167-
err_dec:
168-
masq_refcnt4--;
169-
out_unlock:
170-
mutex_unlock(&masq_mutex);
171-
return ret;
172-
}
173-
EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv4_register_notifier);
174-
175-
void nf_nat_masquerade_ipv4_unregister_notifier(void)
176-
{
177-
mutex_lock(&masq_mutex);
178-
/* check if the notifier still has clients */
179-
if (--masq_refcnt4 > 0)
180-
goto out_unlock;
181-
182-
unregister_netdevice_notifier(&masq_dev_notifier);
183-
unregister_inetaddr_notifier(&masq_inet_notifier);
184-
out_unlock:
185-
mutex_unlock(&masq_mutex);
186-
}
187-
EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv4_unregister_notifier);
188-
189138
#if IS_ENABLED(CONFIG_IPV6)
190139
static atomic_t v6_worker_count __read_mostly;
191140

@@ -321,44 +270,68 @@ static struct notifier_block masq_inet6_notifier = {
321270
.notifier_call = masq_inet6_event,
322271
};
323272

324-
int nf_nat_masquerade_ipv6_register_notifier(void)
273+
static int nf_nat_masquerade_ipv6_register_notifier(void)
274+
{
275+
return register_inet6addr_notifier(&masq_inet6_notifier);
276+
}
277+
#else
278+
static inline int nf_nat_masquerade_ipv6_register_notifier(void) { return 0; }
279+
#endif
280+
281+
int nf_nat_masquerade_inet_register_notifiers(void)
325282
{
326283
int ret = 0;
327284

328285
mutex_lock(&masq_mutex);
329-
if (WARN_ON_ONCE(masq_refcnt6 == UINT_MAX)) {
286+
if (WARN_ON_ONCE(masq_refcnt == UINT_MAX)) {
330287
ret = -EOVERFLOW;
331288
goto out_unlock;
332289
}
333290

334-
/* check if the notifier is already set */
335-
if (++masq_refcnt6 > 1)
291+
/* check if the notifier was already set */
292+
if (++masq_refcnt > 1)
336293
goto out_unlock;
337294

338-
ret = register_inet6addr_notifier(&masq_inet6_notifier);
295+
/* Register for device down reports */
296+
ret = register_netdevice_notifier(&masq_dev_notifier);
339297
if (ret)
340298
goto err_dec;
299+
/* Register IP address change reports */
300+
ret = register_inetaddr_notifier(&masq_inet_notifier);
301+
if (ret)
302+
goto err_unregister;
303+
304+
ret = nf_nat_masquerade_ipv6_register_notifier();
305+
if (ret)
306+
goto err_unreg_inet;
341307

342308
mutex_unlock(&masq_mutex);
343309
return ret;
310+
err_unreg_inet:
311+
unregister_inetaddr_notifier(&masq_inet_notifier);
312+
err_unregister:
313+
unregister_netdevice_notifier(&masq_dev_notifier);
344314
err_dec:
345-
masq_refcnt6--;
315+
masq_refcnt--;
346316
out_unlock:
347317
mutex_unlock(&masq_mutex);
348318
return ret;
349319
}
350-
EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv6_register_notifier);
320+
EXPORT_SYMBOL_GPL(nf_nat_masquerade_inet_register_notifiers);
351321

352-
void nf_nat_masquerade_ipv6_unregister_notifier(void)
322+
void nf_nat_masquerade_inet_unregister_notifiers(void)
353323
{
354324
mutex_lock(&masq_mutex);
355-
/* check if the notifier still has clients */
356-
if (--masq_refcnt6 > 0)
325+
/* check if the notifiers still have clients */
326+
if (--masq_refcnt > 0)
357327
goto out_unlock;
358328

329+
unregister_netdevice_notifier(&masq_dev_notifier);
330+
unregister_inetaddr_notifier(&masq_inet_notifier);
331+
#if IS_ENABLED(CONFIG_IPV6)
359332
unregister_inet6addr_notifier(&masq_inet6_notifier);
333+
#endif
360334
out_unlock:
361335
mutex_unlock(&masq_mutex);
362336
}
363-
EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv6_unregister_notifier);
364-
#endif
337+
EXPORT_SYMBOL_GPL(nf_nat_masquerade_inet_unregister_notifiers);

net/netfilter/nft_masq.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,12 @@ static struct nft_expr_type nft_masq_ipv6_type __read_mostly = {
195195

196196
static int __init nft_masq_module_init_ipv6(void)
197197
{
198-
int ret = nft_register_expr(&nft_masq_ipv6_type);
199-
200-
if (ret)
201-
return ret;
202-
203-
ret = nf_nat_masquerade_ipv6_register_notifier();
204-
if (ret < 0)
205-
nft_unregister_expr(&nft_masq_ipv6_type);
206-
207-
return ret;
198+
return nft_register_expr(&nft_masq_ipv6_type);
208199
}
209200

210201
static void nft_masq_module_exit_ipv6(void)
211202
{
212203
nft_unregister_expr(&nft_masq_ipv6_type);
213-
nf_nat_masquerade_ipv6_unregister_notifier();
214204
}
215205
#else
216206
static inline int nft_masq_module_init_ipv6(void) { return 0; }
@@ -293,7 +283,7 @@ static int __init nft_masq_module_init(void)
293283
return ret;
294284
}
295285

296-
ret = nf_nat_masquerade_ipv4_register_notifier();
286+
ret = nf_nat_masquerade_inet_register_notifiers();
297287
if (ret < 0) {
298288
nft_masq_module_exit_ipv6();
299289
nft_masq_module_exit_inet();
@@ -309,7 +299,7 @@ static void __exit nft_masq_module_exit(void)
309299
nft_masq_module_exit_ipv6();
310300
nft_masq_module_exit_inet();
311301
nft_unregister_expr(&nft_masq_ipv4_type);
312-
nf_nat_masquerade_ipv4_unregister_notifier();
302+
nf_nat_masquerade_inet_unregister_notifiers();
313303
}
314304

315305
module_init(nft_masq_module_init);

net/netfilter/xt_MASQUERADE.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,32 +107,20 @@ static int __init masquerade_tg_init(void)
107107
if (ret)
108108
return ret;
109109

110-
ret = nf_nat_masquerade_ipv4_register_notifier();
110+
ret = nf_nat_masquerade_inet_register_notifiers();
111111
if (ret) {
112112
xt_unregister_targets(masquerade_tg_reg,
113113
ARRAY_SIZE(masquerade_tg_reg));
114114
return ret;
115115
}
116116

117-
#if IS_ENABLED(CONFIG_IPV6)
118-
ret = nf_nat_masquerade_ipv6_register_notifier();
119-
if (ret) {
120-
xt_unregister_targets(masquerade_tg_reg,
121-
ARRAY_SIZE(masquerade_tg_reg));
122-
nf_nat_masquerade_ipv4_unregister_notifier();
123-
return ret;
124-
}
125-
#endif
126117
return ret;
127118
}
128119

129120
static void __exit masquerade_tg_exit(void)
130121
{
131122
xt_unregister_targets(masquerade_tg_reg, ARRAY_SIZE(masquerade_tg_reg));
132-
nf_nat_masquerade_ipv4_unregister_notifier();
133-
#if IS_ENABLED(CONFIG_IPV6)
134-
nf_nat_masquerade_ipv6_unregister_notifier();
135-
#endif
123+
nf_nat_masquerade_inet_unregister_notifiers();
136124
}
137125

138126
module_init(masquerade_tg_init);

0 commit comments

Comments
 (0)