Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>

authored by

Florian Westphal and committed by
Pablo Neira Ayuso
610a4314 adf82acc

+44 -95
+2 -4
include/net/netfilter/nf_nat_masquerade.h
··· 9 9 const struct nf_nat_range2 *range, 10 10 const struct net_device *out); 11 11 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); 14 14 15 15 unsigned int 16 16 nf_nat_masquerade_ipv6(struct sk_buff *skb, const struct nf_nat_range2 *range, 17 17 const struct net_device *out); 18 - int nf_nat_masquerade_ipv6_register_notifier(void); 19 - void nf_nat_masquerade_ipv6_unregister_notifier(void); 20 18 21 19 #endif /*_NF_NAT_MASQUERADE_H_ */
+37 -64
net/netfilter/nf_nat_masquerade.c
··· 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, ··· 134 135 static struct notifier_block masq_inet_notifier = { 135 136 .notifier_call = masq_inet_event, 136 137 }; 137 - 138 - int nf_nat_masquerade_ipv4_register_notifier(void) 139 - { 140 - int ret = 0; 141 - 142 - mutex_lock(&masq_mutex); 143 - if (WARN_ON_ONCE(masq_refcnt4 == UINT_MAX)) { 144 - ret = -EOVERFLOW; 145 - goto out_unlock; 146 - } 147 - 148 - /* check if the notifier was already set */ 149 - if (++masq_refcnt4 > 1) 150 - goto out_unlock; 151 - 152 - /* Register for device down reports */ 153 - ret = register_netdevice_notifier(&masq_dev_notifier); 154 - if (ret) 155 - goto err_dec; 156 - /* Register IP address change reports */ 157 - ret = register_inetaddr_notifier(&masq_inet_notifier); 158 - if (ret) 159 - goto err_unregister; 160 - 161 - mutex_unlock(&masq_mutex); 162 - return ret; 163 - 164 - err_unregister: 165 - unregister_netdevice_notifier(&masq_dev_notifier); 166 - err_dec: 167 - masq_refcnt4--; 168 - out_unlock: 169 - mutex_unlock(&masq_mutex); 170 - return ret; 171 - } 172 - EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv4_register_notifier); 173 - 174 - void nf_nat_masquerade_ipv4_unregister_notifier(void) 175 - { 176 - mutex_lock(&masq_mutex); 177 - /* check if the notifier still has clients */ 178 - if (--masq_refcnt4 > 0) 179 - goto out_unlock; 180 - 181 - unregister_netdevice_notifier(&masq_dev_notifier); 182 - unregister_inetaddr_notifier(&masq_inet_notifier); 183 - out_unlock: 184 - mutex_unlock(&masq_mutex); 185 - } 186 - EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv4_unregister_notifier); 187 138 188 139 #if IS_ENABLED(CONFIG_IPV6) 189 140 static atomic_t v6_worker_count __read_mostly; ··· 270 321 .notifier_call = masq_inet6_event, 271 322 }; 272 323 273 - int nf_nat_masquerade_ipv6_register_notifier(void) 324 + static int nf_nat_masquerade_ipv6_register_notifier(void) 325 + { 326 + return register_inet6addr_notifier(&masq_inet6_notifier); 327 + } 328 + #else 329 + static inline int nf_nat_masquerade_ipv6_register_notifier(void) { return 0; } 330 + #endif 331 + 332 + int nf_nat_masquerade_inet_register_notifiers(void) 274 333 { 275 334 int ret = 0; 276 335 277 336 mutex_lock(&masq_mutex); 278 - if (WARN_ON_ONCE(masq_refcnt6 == UINT_MAX)) { 337 + if (WARN_ON_ONCE(masq_refcnt == UINT_MAX)) { 279 338 ret = -EOVERFLOW; 280 339 goto out_unlock; 281 340 } 282 341 283 - /* check if the notifier is already set */ 284 - if (++masq_refcnt6 > 1) 342 + /* check if the notifier was already set */ 343 + if (++masq_refcnt > 1) 285 344 goto out_unlock; 286 345 287 - ret = register_inet6addr_notifier(&masq_inet6_notifier); 346 + /* Register for device down reports */ 347 + ret = register_netdevice_notifier(&masq_dev_notifier); 288 348 if (ret) 289 349 goto err_dec; 350 + /* Register IP address change reports */ 351 + ret = register_inetaddr_notifier(&masq_inet_notifier); 352 + if (ret) 353 + goto err_unregister; 354 + 355 + ret = nf_nat_masquerade_ipv6_register_notifier(); 356 + if (ret) 357 + goto err_unreg_inet; 290 358 291 359 mutex_unlock(&masq_mutex); 292 360 return ret; 361 + err_unreg_inet: 362 + unregister_inetaddr_notifier(&masq_inet_notifier); 363 + err_unregister: 364 + unregister_netdevice_notifier(&masq_dev_notifier); 293 365 err_dec: 294 - masq_refcnt6--; 366 + masq_refcnt--; 295 367 out_unlock: 296 368 mutex_unlock(&masq_mutex); 297 369 return ret; 298 370 } 299 - EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv6_register_notifier); 371 + EXPORT_SYMBOL_GPL(nf_nat_masquerade_inet_register_notifiers); 300 372 301 - void nf_nat_masquerade_ipv6_unregister_notifier(void) 373 + void nf_nat_masquerade_inet_unregister_notifiers(void) 302 374 { 303 375 mutex_lock(&masq_mutex); 304 - /* check if the notifier still has clients */ 305 - if (--masq_refcnt6 > 0) 376 + /* check if the notifiers still have clients */ 377 + if (--masq_refcnt > 0) 306 378 goto out_unlock; 307 379 380 + unregister_netdevice_notifier(&masq_dev_notifier); 381 + unregister_inetaddr_notifier(&masq_inet_notifier); 382 + #if IS_ENABLED(CONFIG_IPV6) 308 383 unregister_inet6addr_notifier(&masq_inet6_notifier); 384 + #endif 309 385 out_unlock: 310 386 mutex_unlock(&masq_mutex); 311 387 } 312 - EXPORT_SYMBOL_GPL(nf_nat_masquerade_ipv6_unregister_notifier); 313 - #endif 388 + EXPORT_SYMBOL_GPL(nf_nat_masquerade_inet_unregister_notifiers);
+3 -13
net/netfilter/nft_masq.c
··· 195 195 196 196 static int __init nft_masq_module_init_ipv6(void) 197 197 { 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); 208 199 } 209 200 210 201 static void nft_masq_module_exit_ipv6(void) 211 202 { 212 203 nft_unregister_expr(&nft_masq_ipv6_type); 213 - nf_nat_masquerade_ipv6_unregister_notifier(); 214 204 } 215 205 #else 216 206 static inline int nft_masq_module_init_ipv6(void) { return 0; } ··· 283 293 return ret; 284 294 } 285 295 286 - ret = nf_nat_masquerade_ipv4_register_notifier(); 296 + ret = nf_nat_masquerade_inet_register_notifiers(); 287 297 if (ret < 0) { 288 298 nft_masq_module_exit_ipv6(); 289 299 nft_masq_module_exit_inet(); ··· 299 309 nft_masq_module_exit_ipv6(); 300 310 nft_masq_module_exit_inet(); 301 311 nft_unregister_expr(&nft_masq_ipv4_type); 302 - nf_nat_masquerade_ipv4_unregister_notifier(); 312 + nf_nat_masquerade_inet_unregister_notifiers(); 303 313 } 304 314 305 315 module_init(nft_masq_module_init);
+2 -14
net/netfilter/xt_MASQUERADE.c
··· 107 107 if (ret) 108 108 return ret; 109 109 110 - ret = nf_nat_masquerade_ipv4_register_notifier(); 110 + ret = nf_nat_masquerade_inet_register_notifiers(); 111 111 if (ret) { 112 112 xt_unregister_targets(masquerade_tg_reg, 113 113 ARRAY_SIZE(masquerade_tg_reg)); 114 114 return ret; 115 115 } 116 116 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 126 117 return ret; 127 118 } 128 119 129 120 static void __exit masquerade_tg_exit(void) 130 121 { 131 122 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(); 136 124 } 137 125 138 126 module_init(masquerade_tg_init);