|
10 | 10 | #include <net/netfilter/nf_nat_masquerade.h>
|
11 | 11 |
|
12 | 12 | 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; |
15 | 14 |
|
16 | 15 | unsigned int
|
17 | 16 | nf_nat_masquerade_ipv4(struct sk_buff *skb, unsigned int hooknum,
|
@@ -136,56 +135,6 @@ static struct notifier_block masq_inet_notifier = {
|
136 | 135 | .notifier_call = masq_inet_event,
|
137 | 136 | };
|
138 | 137 |
|
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 |
| - |
189 | 138 | #if IS_ENABLED(CONFIG_IPV6)
|
190 | 139 | static atomic_t v6_worker_count __read_mostly;
|
191 | 140 |
|
@@ -321,44 +270,68 @@ static struct notifier_block masq_inet6_notifier = {
|
321 | 270 | .notifier_call = masq_inet6_event,
|
322 | 271 | };
|
323 | 272 |
|
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) |
325 | 282 | {
|
326 | 283 | int ret = 0;
|
327 | 284 |
|
328 | 285 | mutex_lock(&masq_mutex);
|
329 |
| - if (WARN_ON_ONCE(masq_refcnt6 == UINT_MAX)) { |
| 286 | + if (WARN_ON_ONCE(masq_refcnt == UINT_MAX)) { |
330 | 287 | ret = -EOVERFLOW;
|
331 | 288 | goto out_unlock;
|
332 | 289 | }
|
333 | 290 |
|
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) |
336 | 293 | goto out_unlock;
|
337 | 294 |
|
338 |
| - ret = register_inet6addr_notifier(&masq_inet6_notifier); |
| 295 | + /* Register for device down reports */ |
| 296 | + ret = register_netdevice_notifier(&masq_dev_notifier); |
339 | 297 | if (ret)
|
340 | 298 | 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; |
341 | 307 |
|
342 | 308 | mutex_unlock(&masq_mutex);
|
343 | 309 | return ret;
|
| 310 | +err_unreg_inet: |
| 311 | + unregister_inetaddr_notifier(&masq_inet_notifier); |
| 312 | +err_unregister: |
| 313 | + unregister_netdevice_notifier(&masq_dev_notifier); |
344 | 314 | err_dec:
|
345 |
| - masq_refcnt6--; |
| 315 | + masq_refcnt--; |
346 | 316 | out_unlock:
|
347 | 317 | mutex_unlock(&masq_mutex);
|
348 | 318 | return ret;
|
349 | 319 | }
|
350 |
| -EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv6_register_notifier); |
| 320 | +EXPORT_SYMBOL_GPL(nf_nat_masquerade_inet_register_notifiers); |
351 | 321 |
|
352 |
| -void nf_nat_masquerade_ipv6_unregister_notifier(void) |
| 322 | +void nf_nat_masquerade_inet_unregister_notifiers(void) |
353 | 323 | {
|
354 | 324 | 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) |
357 | 327 | goto out_unlock;
|
358 | 328 |
|
| 329 | + unregister_netdevice_notifier(&masq_dev_notifier); |
| 330 | + unregister_inetaddr_notifier(&masq_inet_notifier); |
| 331 | +#if IS_ENABLED(CONFIG_IPV6) |
359 | 332 | unregister_inet6addr_notifier(&masq_inet6_notifier);
|
| 333 | +#endif |
360 | 334 | out_unlock:
|
361 | 335 | mutex_unlock(&masq_mutex);
|
362 | 336 | }
|
363 |
| -EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv6_unregister_notifier); |
364 |
| -#endif |
| 337 | +EXPORT_SYMBOL_GPL(nf_nat_masquerade_inet_unregister_notifiers); |
0 commit comments