nf conntrack protos pernet functionality v3
Subsystems (2)
cover nf conntrack protos pernet functionality v3
Hi,
here is a new attempt of bringing in per-net functionality into nf conntrack protos. Please review and test if possible.
It's quite far from ideal solution Alexey proposed (to try to shrink sysctl's manipulation in one place with all #ifdef guardians we need) but I didn't manage to implement it right way yet. So mostly at moment it looks like a code _bloat_ so rejecting this series would be acceptable solution :)
ICMP v6 is not covered yet as well. Will be done.
Cyrill
PATCH 1/5 net: netfilter conntrack - add per-net functionality for ICMP protocol
Protocol specific data moved into per-net site and being allocated/freed during net namespace creation/deletion.
--- include/net/netfilter/ipv4/nf_conntrack_ipv4.h | 3 net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c | 11 ++ net/ipv4/netfilter/nf_conntrack_proto_icmp.c | 125 +++++++++++++++++++++++-- 3 files changed, 130 insertions(+), 9 deletions(-)
include/net/netfilter/ipv4/nf_conntrack_ipv4.h +3 -0
@@ -21,4 +21,7 @@ extern void nf_conntrack_ipv4_compat_finextern void need_ipv4_conntrack (void ); +extern int nf_ct_icmp_proto_init (void ); +extern void nf_ct_icmp_proto_fini (void ); +#endif /*_NF_CONNTRACK_IPV4_H*/
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c +10 -1
@@ -395,10 +395,16 @@ static int __init nf_conntrack_l3proto_i goto cleanup_tcp; } + ret =nf_ct_icmp_proto_init (); +if (ret <0 ) { +printk ("nf_conntrack_ipv4: can't initialize icmp\n" ); + goto cleanup_udp; + } + ret =nf_conntrack_l4proto_register (&nf_conntrack_l4proto_icmp);if (ret <0 ) {printk ("nf_conntrack_ipv4: can't register icmp.\n" ); - goto cleanup_udp; + goto uninit_icmp; } ret =nf_conntrack_l3proto_register (&nf_conntrack_l3proto_ipv4);
@@ -427,6 +433,8 @@ static int __init nf_conntrack_l3proto_inf_conntrack_l3proto_unregister (&nf_conntrack_l3proto_ipv4); cleanup_icmp:nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_icmp); + uninit_icmp: +nf_ct_icmp_proto_fini (); cleanup_udp:nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_udp4); cleanup_tcp:
@@ -445,6 +453,7 @@ static void __exit nf_conntrack_l3proto_nf_unregister_hooks (ipv4_conntrack_ops,ARRAY_SIZE (ipv4_conntrack_ops));nf_conntrack_l3proto_unregister (&nf_conntrack_l3proto_ipv4);nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_icmp); +nf_ct_icmp_proto_fini ();nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_udp4);nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_tcp4);nf_unregister_sockopt (&so_getorigdst);
net/ipv4/netfilter/nf_conntrack_proto_icmp.c +117 -8
162 lines not on this page · raw message
PATCH 2/5 net: netfilter conntrack - add per-net functionality for SCTP protocol
In reply to Cyrill GorcunovProtocol specific data moved into per-net site and being allocated/freed during net namespace creation/deletion.
--- net/netfilter/nf_conntrack_proto_sctp.c | 179 ++++++++++++++++++++++++-------- 1 file changed, 139 insertions(+), 40 deletions(-)
net/netfilter/nf_conntrack_proto_sctp.c +139 -40
@@ -21,6 +21,9 @@#include <linux/spinlock.h> #include <linux/interrupt.h> +#include <net/net_namespace.h> +#include <net/netns/generic.h> +#include <net/netfilter/nf_conntrack.h> #include <net/netfilter/nf_conntrack_l4proto.h> #include <net/netfilter/nf_conntrack_ecache.h>
@@ -49,16 +52,6 @@ static const char *const sctp_conntrack_#define HOURS * 60 MINS#define DAYS * 24 HOURS -static unsigned int sctp_timeouts[SCTP_CONNTRACK_MAX ]__read_mostly = { - [SCTP_CONNTRACK_CLOSED] = 10 SECS, - [SCTP_CONNTRACK_COOKIE_WAIT] = 3 SECS, - [SCTP_CONNTRACK_COOKIE_ECHOED] = 3 SECS, - [SCTP_CONNTRACK_ESTABLISHED] = 5 DAYS, - [SCTP_CONNTRACK_SHUTDOWN_SENT] = 300 SECS / 1000, - [SCTP_CONNTRACK_SHUTDOWN_RECD] = 300 SECS / 1000, - [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = 3 SECS, -}; -#define sNO SCTP_CONNTRACK_NONE#define sCL SCTP_CONNTRACK_CLOSED#define sCW SCTP_CONNTRACK_COOKIE_WAIT
@@ -130,6 +123,25 @@ static const u8 sctp_conntracks[2][9][SC } }; +/* this module per-net specifics */ +static int sctp_net_id; +struct sctp_net { +unsigned int sctp_timeouts[SCTP_CONNTRACK_MAX ]; +#ifdef CONFIG_SYSCTL +struct ctl_table_header *sysctl_header; +struct ctl_table *sysctl_table; +#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT +struct ctl_table_header *compat_sysctl_header; +struct ctl_table *compat_sysctl_table; +#endif +#endif +}; + +static inline struct sctp_net *sctp_pernet (struct net *net) +{ +return net_generic (net, sctp_net_id); +} +static bool sctp_pkt_to_tuple (const struct sk_buff *skb,unsigned int dataoff,struct nf_conntrack_tuple *tuple) {
@@ -297,6 +309,7 @@ static int sctp_packet(struct nf_conn *cconst struct sctp_chunkhdr *sch;struct sctp_chunkhdr _sch;u_int32_t offset, count; +struct sctp_net *sn;unsigned long map[256 /sizeof (unsigned long )] = {0 }; sh =skb_header_pointer (skb, dataoff,sizeof (_sctph), &_sctph);
@@ -373,7 +386,8 @@ static int sctp_packet(struct nf_conn *c }write_unlock_bh (&sctp_lock); -nf_ct_refresh_acct (ct, ctinfo, skb, sctp_timeouts[new_state]); + sn =sctp_pernet (nf_ct_net (ct)); +nf_ct_refresh_acct (ct, ctinfo, skb, sn->sctp_timeouts[new_state]); if (old_state == SCTP_CONNTRACK_COOKIE_ECHOED && dir == IP_CT_DIR_REPLY &&
@@ -546,54 +560,46 @@ static int sctp_nlattr_size(void)#endif #ifdef CONFIG_SYSCTL -static unsigned int sctp_sysctl_table_users; -static struct ctl_table_header *sctp_sysctl_header; +/* templates, data assigned later */ static struct ctl_table sctp_sysctl_table[] = { { .procname = "nf_conntrack_sctp_timeout_closed", - .data = &sctp_timeouts[SCTP_CONNTRACK_CLOSED], .maxlen = sizeof(unsigned int), .mode = 0644, .proc_handler = proc_dointvec_jiffies, }, { .procname = "nf_conntrack_sctp_timeout_cookie_wait", - .data = &sctp_timeouts[SCTP_CONNTRACK_COOKIE_WAIT], .maxlen = sizeof(unsigned int), .mode = 0644, .proc_handler = proc_dointvec_jiffies, }, { .procname = "nf_conntrack_sctp_timeout_cookie_echoed", - .data = &sctp_timeouts[SCTP_CONNTRACK_COOKIE_ECHOED], .maxlen = sizeof(unsigned int), .mode = 0644, .proc_handler = proc_dointvec_jiffies, }, { .procname = "nf_conntrack_sctp_timeout_established", - .data = &sctp_timeouts[SCTP_CONNTRACK_ESTABLISHED], .maxlen = sizeof(unsigned int), .mode = 0644, .proc_handler = proc_dointvec_jiffies, }, { .procname = "nf_conntrack_sctp_timeout_shutdown_sent", - .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT], .maxlen = sizeof(unsigned int), .mode = 0644, .proc_handler = proc_dointvec_jiffies, }, { .procname = "nf_conntrack_sctp_timeout_shutdown_recd", - .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD], .maxlen = sizeof(unsigned int), .mode = 0644, .proc_handler = proc_dointvec_jiffies, }, { .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent", - .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT], .maxlen = sizeof(unsigned int), .mode = 0644, .proc_handler = proc_dointvec_jiffies,
@@ -607,49 +613,42 @@ static struct ctl_table sctp_sysctl_tablstatic struct ctl_table sctp_compat_sysctl_table[] = { { .procname ="ip_conntrack_sctp_timeout_closed" , - .data = &sctp_timeouts[SCTP_CONNTRACK_CLOSED ], .maxlen =sizeof (unsigned int ), .mode =0644 , .proc_handler = proc_dointvec_jiffies, }, { .procname ="ip_conntrack_sctp_timeout_cookie_wait" , - .data = &sctp_timeouts[SCTP_CONNTRACK_COOKIE_WAIT ], .maxlen =sizeof (unsigned int ), .mode =0644 , .proc_handler = proc_dointvec_jiffies, }, { .procname ="ip_conntrack_sctp_timeout_cookie_echoed" , - .data = &sctp_timeouts[SCTP_CONNTRACK_COOKIE_ECHOED ], .maxlen =sizeof (unsigned int ), .mode =0644 , .proc_handler = proc_dointvec_jiffies, }, { .procname ="ip_conntrack_sctp_timeout_established" , - .data = &sctp_timeouts[SCTP_CONNTRACK_ESTABLISHED ], .maxlen =sizeof (unsigned int ), .mode =0644 , .proc_handler = proc_dointvec_jiffies, }, { .procname ="ip_conntrack_sctp_timeout_shutdown_sent" , - .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT ], .maxlen =sizeof (unsigned int ), .mode =0644 , .proc_handler = proc_dointvec_jiffies, }, { .procname ="ip_conntrack_sctp_timeout_shutdown_recd" , - .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD ], .maxlen =sizeof (unsigned int ), .mode =0644 , .proc_handler = proc_dointvec_jiffies, }, { .procname ="ip_conntrack_sctp_timeout_shutdown_ack_sent" , - .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT ], .maxlen =sizeof (unsigned int ), .mode =0644 , .proc_handler = proc_dointvec_jiffies,
@@ -659,7 +658,7 @@ static struct ctl_table sctp_compat_sysc } };#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */ -#endif +#endif /* CONFIG_SYSCTL */ static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp4__read_mostly = { .l3proto =PF_INET ,
@@ -681,14 +680,6 @@ static struct nf_conntrack_l4proto nf_co .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, .nla_policy = nf_ct_port_nla_policy,#endif -#ifdef CONFIG_SYSCTL - .ctl_table_users = &sctp_sysctl_table_users, - .ctl_table_header = &sctp_sysctl_header, - .ctl_table = sctp_sysctl_table, -#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT - .ctl_compat_table = sctp_compat_sysctl_table, -#endif -#endif };static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp6__read_mostly = {
@@ -711,21 +702,126 @@ static struct nf_conntrack_l4proto nf_co .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, .nla_policy = nf_ct_port_nla_policy,#endif +}; + +static __net_init int sctp_net_init (struct net *net) +{ +struct sctp_net *sn; +int err; + + sn =kmalloc (sizeof (*sn),GFP_KERNEL ); +if (!sn) +return -ENOMEM ; + +/* default values */ + sn->sctp_timeouts[SCTP_CONNTRACK_CLOSED] = 10 SECS; + sn->sctp_timeouts[SCTP_CONNTRACK_COOKIE_WAIT] = 3 SECS; + sn->sctp_timeouts[SCTP_CONNTRACK_COOKIE_ECHOED] = 3 SECS; + sn->sctp_timeouts[SCTP_CONNTRACK_ESTABLISHED] = 5 DAYS; + sn->sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT] = 300 SECS / 1000; + sn->sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD] = 300 SECS / 1000; + sn->sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = 3 SECS; + + err =net_assign_generic (net, sctp_net_id, sn); +if (err) + goto out; + +/* +* Pin per-net data to sysctl tables +* +* We allocate new ctrl tables from predefined templates +* and then assign .data fields iteratively, we allowed +* to do so since SCTP_CONNTRACK_... (enum sctp_conntrack) +* is a part of userspace ABI and it's hardly that the enum +* entries will be rearranged +*/ +#ifdef CONFIG_SYSCTL - .ctl_table_users = &sctp_sysctl_table_users, - .ctl_table_header = &sctp_sysctl_header, - .ctl_table = sctp_sysctl_table, + { +int i; + err = -ENOMEM ; + sn->sysctl_table =kmemdup (sctp_sysctl_table, +sizeof (sctp_sysctl_table),GFP_KERNEL ); +if (!sn->sysctl_table) + goto out; + +for (i =SCTP_CONNTRACK_CLOSED ; i <SCTP_CONNTRACK_MAX ; i++) + sn->sysctl_table[i -1 ].data = &sn->sctp_timeouts[i]; + + sn->sysctl_header =register_net_sysctl_table (net, + nf_net_netfilter_sysctl_path, sn->sysctl_table); +if (!sn->sysctl_header) + goto out_free; + +#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT + sn->compat_sysctl_table =kmemdup (sctp_compat_sysctl_table, +sizeof (sctp_compat_sysctl_table),GFP_KERNEL ); +if (!sn->compat_sysctl_table) + goto out_sysctl; + +for (i =SCTP_CONNTRACK_CLOSED ; i <SCTP_CONNTRACK_MAX ; i++) + sn->compat_sysctl_table[i -1 ].data = &sn->sctp_timeouts[i]; + + sn->compat_sysctl_header =register_net_sysctl_table (net, + nf_net_ipv4_netfilter_sysctl_path, sn->compat_sysctl_table); +if (!sn->compat_sysctl_header) + goto out_free_compat; +#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */ + } +#endif /* CONFIG_SYSCTL */ + +return 0 ; + +#ifdef CONFIG_SYSCTL + +#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT +out_free_compat: +kfree (sn->compat_sysctl_table); +#endif +out_sysctl: +unregister_net_sysctl_table (sn->sysctl_header); +out_free: +kfree (sn->sysctl_table); +#endif + +out: +kfree (sn); +return err; +} + +static __net_exit void sctp_net_exit(struct net *net) +{ +struct sctp_net *sn =sctp_pernet (net); +#ifdef CONFIG_SYSCTL +unregister_net_sysctl_table (sn->sysctl_header); +#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT +unregister_net_sysctl_table (sn->compat_sysctl_header); +kfree (sn->compat_sysctl_table);#endif +kfree (sn->sysctl_table); +#endif +kfree (sn); + +net_assign_generic (net, sctp_net_id,NULL ); +} + +static struct pernet_operations sctp_net_ops = { + .init = sctp_net_init, + .exit = sctp_net_exit, };static int __init nf_conntrack_proto_sctp_init (void ) {int ret; + ret =register_pernet_gen_subsys (&sctp_net_id, &sctp_net_ops); +if (ret <0 ) + goto out; + ret =nf_conntrack_l4proto_register (&nf_conntrack_l4proto_sctp4);if (ret) {printk ("nf_conntrack_l4proto_sctp4: protocol register failed\n" ); - goto out; + goto cleanup_net; } ret =nf_conntrack_l4proto_register (&nf_conntrack_l4proto_sctp6);if (ret) {
@@ -737,6 +833,8 @@ static int __init nf_conntrack_proto_sct cleanup_sctp4:nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_sctp4); + cleanup_net: +unregister_pernet_gen_subsys (sctp_net_id, &sctp_net_ops); out:return ret; }
@@ -745,6 +843,7 @@ static void __exit nf_conntrack_proto_sc {nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_sctp6);nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_sctp4); +unregister_pernet_gen_subsys (sctp_net_id, &sctp_net_ops); }module_init (nf_conntrack_proto_sctp_init);
PATCH 3/5 net: netfilter conntrack - add per-net functionality for TCP protocol
In reply to Cyrill GorcunovProtocol specific data moved into per-net site and being allocated/freed during net namespace creation/deletion.
--- include/net/netfilter/ipv4/nf_conntrack_ipv4.h | 3 include/net/netfilter/ipv6/nf_conntrack_ipv6.h | 3 net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c | 11 net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c | 12 - net/netfilter/nf_conntrack_proto_tcp.c | 292 ++++++++++++++++++------- 5 files changed, 241 insertions(+), 80 deletions(-)
include/net/netfilter/ipv4/nf_conntrack_ipv4.h +3 -0
@@ -24,4 +24,7 @@ extern void need_ipv4_conntrack(void);extern int nf_ct_icmp_proto_init (void );extern void nf_ct_icmp_proto_fini (void ); +extern int nf_ct_tcp_proto_init (void ); +extern void nf_ct_tcp_proto_fini (void ); +#endif /*_NF_CONNTRACK_IPV4_H*/
include/net/netfilter/ipv6/nf_conntrack_ipv6.h +3 -0
@@ -20,4 +20,7 @@ struct inet_frags_ctl;#include <linux/sysctl.h> extern struct ctl_table nf_ct_ipv6_sysctl_table[]; +extern int nf_ct_tcp_proto_init (void ); +extern void nf_ct_tcp_proto_fini (void ); +#endif /* _NF_CONNTRACK_IPV6_H*/
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c +10 -1
@@ -383,10 +383,16 @@ static int __init nf_conntrack_l3proto_ireturn ret; } + ret =nf_ct_tcp_proto_init (); +if (ret <0 ) { +printk ("nf_conntrack_ipv4: can't initialize tcp.\n" ); + goto cleanup_sockopt; + } + ret =nf_conntrack_l4proto_register (&nf_conntrack_l4proto_tcp4);if (ret <0 ) {printk ("nf_conntrack_ipv4: can't register tcp.\n" ); - goto cleanup_sockopt; + goto uninit_tcp; } ret =nf_conntrack_l4proto_register (&nf_conntrack_l4proto_udp4);
@@ -439,6 +445,8 @@ static int __init nf_conntrack_l3proto_inf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_udp4); cleanup_tcp:nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_tcp4); + uninit_tcp: +nf_ct_tcp_proto_fini (); cleanup_sockopt:nf_unregister_sockopt (&so_getorigdst);return ret;
@@ -456,6 +464,7 @@ static void __exit nf_conntrack_l3proto_nf_ct_icmp_proto_fini ();nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_udp4);nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_tcp4); +nf_ct_tcp_proto_fini ();nf_unregister_sockopt (&so_getorigdst); }
net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c +11 -1
@@ -384,10 +384,17 @@ static int __init nf_conntrack_l3proto_iprintk ("nf_conntrack_ipv6: can't initialize frag6.\n" );return ret; } + + ret =nf_ct_tcp_proto_init (); +if (ret <0 ) { +printk ("nf_conntrack_ipv6: can't initialize tcp.\n" ); + goto cleanup_frag6; + } + ret =nf_conntrack_l4proto_register (&nf_conntrack_l4proto_tcp6);if (ret <0 ) {printk ("nf_conntrack_ipv6: can't register tcp.\n" ); - goto cleanup_frag6; + goto uninit_tcp; } ret =nf_conntrack_l4proto_register (&nf_conntrack_l4proto_udp6);
@@ -425,6 +432,8 @@ static int __init nf_conntrack_l3proto_inf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_udp6); cleanup_tcp:nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_tcp6); + uninit_tcp: +nf_ct_tcp_proto_fini (); cleanup_frag6:nf_ct_frag6_cleanup ();return ret;
@@ -438,6 +447,7 @@ static void __exit nf_conntrack_l3proto_nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_icmpv6);nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_udp6);nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_tcp6); +nf_ct_tcp_proto_fini ();nf_ct_frag6_cleanup (); }
net/netfilter/nf_conntrack_proto_tcp.c +214 -78
533 lines not on this page · raw message
PATCH 4/5 net: netfilter conntrack - add per-net functionality for UDP protocol
In reply to Cyrill GorcunovProtocol specific data moved into per-net site and being allocated/freed during net namespace creation/deletion.
--- include/net/netfilter/ipv4/nf_conntrack_ipv4.h | 3 include/net/netfilter/ipv6/nf_conntrack_ipv6.h | 3 net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c | 11 + net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c | 11 + net/netfilter/nf_conntrack_proto_udp.c | 163 +++++++++++++++++++++---- 5 files changed, 167 insertions(+), 24 deletions(-)
include/net/netfilter/ipv4/nf_conntrack_ipv4.h +3 -0
@@ -27,4 +27,7 @@ extern void nf_ct_icmp_proto_fini(void);extern int nf_ct_tcp_proto_init (void );extern void nf_ct_tcp_proto_fini (void ); +extern int nf_ct_udp_proto_init (void ); +extern void nf_ct_udp_proto_fini (void ); +#endif /*_NF_CONNTRACK_IPV4_H*/
include/net/netfilter/ipv6/nf_conntrack_ipv6.h +3 -0
@@ -23,4 +23,7 @@ extern struct ctl_table nf_ct_ipv6_sysctextern int nf_ct_tcp_proto_init (void );extern void nf_ct_tcp_proto_fini (void ); +extern int nf_ct_udp_proto_init (void ); +extern void nf_ct_udp_proto_fini (void ); +#endif /* _NF_CONNTRACK_IPV6_H*/
net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c +10 -1
@@ -395,10 +395,16 @@ static int __init nf_conntrack_l3proto_i goto uninit_tcp; } + ret =nf_ct_udp_proto_init (); +if (ret <0 ) { +printk ("nf_conntrack_ipv4: can't initialize udp.\n" ); + goto cleanup_tcp; + } + ret =nf_conntrack_l4proto_register (&nf_conntrack_l4proto_udp4);if (ret <0 ) {printk ("nf_conntrack_ipv4: can't register udp.\n" ); - goto cleanup_tcp; + goto uninit_udp; } ret =nf_ct_icmp_proto_init ();
@@ -443,6 +449,8 @@ static int __init nf_conntrack_l3proto_inf_ct_icmp_proto_fini (); cleanup_udp:nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_udp4); + uninit_udp: +nf_ct_udp_proto_fini (); cleanup_tcp:nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_tcp4); uninit_tcp:
@@ -463,6 +471,7 @@ static void __exit nf_conntrack_l3proto_nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_icmp);nf_ct_icmp_proto_fini ();nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_udp4); +nf_ct_udp_proto_fini ();nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_tcp4);nf_ct_tcp_proto_fini ();nf_unregister_sockopt (&so_getorigdst);
net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c +10 -1
@@ -397,10 +397,16 @@ static int __init nf_conntrack_l3proto_i goto uninit_tcp; } + ret =nf_ct_udp_proto_init (); +if (ret <0 ) { +printk ("nf_conntrack_ipv6: can't initialize udp.\n" ); + goto cleanup_tcp; + } + ret =nf_conntrack_l4proto_register (&nf_conntrack_l4proto_udp6);if (ret <0 ) {printk ("nf_conntrack_ipv6: can't register udp.\n" ); - goto cleanup_tcp; + goto uninit_udp; } ret =nf_conntrack_l4proto_register (&nf_conntrack_l4proto_icmpv6);
@@ -430,6 +436,8 @@ static int __init nf_conntrack_l3proto_inf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_icmpv6); cleanup_udp:nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_udp6); + uninit_udp: +nf_ct_udp_proto_fini (); cleanup_tcp:nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_tcp6); uninit_tcp:
@@ -446,6 +454,7 @@ static void __exit nf_conntrack_l3proto_nf_conntrack_l3proto_unregister (&nf_conntrack_l3proto_ipv6);nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_icmpv6);nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_udp6); +nf_ct_udp_proto_fini ();nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_tcp6);nf_ct_tcp_proto_fini ();nf_ct_frag6_cleanup ();
net/netfilter/nf_conntrack_proto_udp.c +141 -22
227 lines not on this page · raw message
PATCH 5/5 net: netfilter conntrack - add per-net functionality for UDPLITE protocol
In reply to Cyrill GorcunovProtocol specific data moved into per-net site and being allocated/freed during net namespace creation/deletion.
--- net/netfilter/nf_conntrack_proto_udplite.c | 115 ++++++++++++++++++++++++----- 1 file changed, 96 insertions(+), 19 deletions(-)
net/netfilter/nf_conntrack_proto_udplite.c +96 -19
@@ -17,6 +17,9 @@#include <net/ip6_checksum.h> #include <net/checksum.h> +#include <net/net_namespace.h> +#include <net/netns/generic.h> +#include <linux/netfilter.h> #include <linux/netfilter_ipv4.h> #include <linux/netfilter_ipv6.h>
@@ -24,8 +27,21 @@#include <net/netfilter/nf_conntrack_ecache.h> #include <net/netfilter/nf_log.h> -static unsigned int nf_ct_udplite_timeout__read_mostly =30 *HZ ; -static unsigned int nf_ct_udplite_timeout_stream__read_mostly =180 *HZ ; +/* this module per-net specifics */ +static int udplite_net_id; +struct udplite_net { +unsigned int udplite_timeout; +unsigned int udplite_timeout_stream; +#ifdef CONFIG_SYSCTL +struct ctl_table_header *sysctl_header; +struct ctl_table *sysctl_table; +#endif +}; + +static inline struct udplite_net *udplite_pernet (struct net *net) +{ +return net_generic (net, udplite_net_id); +} static bool udplite_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
@@ -68,16 +84,18 @@ static int udplite_packet(struct nf_conn u_int8_t pf, unsigned int hooknum) { +struct udplite_net *un =udplite_pernet (nf_ct_net (ct)); +/* If we've seen traffic both ways, this is some kind of UDP stream. Extend timeout. */ if (test_bit (IPS_SEEN_REPLY_BIT , &ct->status)) {nf_ct_refresh_acct (ct, ctinfo, skb, - nf_ct_udplite_timeout_stream); + un->udplite_timeout_stream);/* Also, more likely to be important, and not a probe */ if (!test_and_set_bit (IPS_ASSURED_BIT , &ct->status))nf_conntrack_event_cache (IPCT_STATUS , ct); }else -nf_ct_refresh_acct (ct, ctinfo, skb, nf_ct_udplite_timeout); +nf_ct_refresh_acct (ct, ctinfo, skb, un->udplite_timeout);return NF_ACCEPT ; }
@@ -142,13 +160,11 @@ static int udplite_error(struct net *net } #ifdef CONFIG_SYSCTL -static unsigned int udplite_sysctl_table_users; -static struct ctl_table_header *udplite_sysctl_header; +/* template, data assigned later */ static struct ctl_table udplite_sysctl_table[] = { { .ctl_name = CTL_UNNUMBERED, .procname = "nf_conntrack_udplite_timeout", - .data = &nf_ct_udplite_timeout, .maxlen = sizeof(unsigned int), .mode = 0644, .proc_handler = proc_dointvec_jiffies,
@@ -156,7 +172,6 @@ static struct ctl_table udplite_sysctl_t { .ctl_name = CTL_UNNUMBERED, .procname = "nf_conntrack_udplite_timeout_stream", - .data = &nf_ct_udplite_timeout_stream, .maxlen = sizeof(unsigned int), .mode = 0644, .proc_handler = proc_dointvec_jiffies,
@@ -184,11 +199,6 @@ static struct nf_conntrack_l4proto nf_co .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, .nla_policy = nf_ct_port_nla_policy,#endif -#ifdef CONFIG_SYSCTL - .ctl_table_users = &udplite_sysctl_table_users, - .ctl_table_header = &udplite_sysctl_header, - .ctl_table = udplite_sysctl_table, -#endif }; static struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite6 __read_mostly =
@@ -207,26 +217,92 @@ static struct nf_conntrack_l4proto nf_co .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, .nla_policy = nf_ct_port_nla_policy,#endif +}; + +static __net_init int udplite_net_init (struct net *net) +{ +struct udplite_net *un; +int err; + + un =kmalloc (sizeof (*un),GFP_KERNEL ); +if (!un) +return -ENOMEM ; + +/* default values */ + un->udplite_timeout =30 *HZ ; + un->udplite_timeout_stream =180 *HZ ; + + err =net_assign_generic (net, udplite_net_id, un); +if (err) + goto out; + +#ifdef CONFIG_SYSCTL + err = -ENOMEM ; + un->sysctl_table =kmemdup (udplite_sysctl_table, +sizeof (udplite_sysctl_table),GFP_KERNEL ); +if (!un->sysctl_table) + goto out; + + un->sysctl_table[0 ].data = &un->udplite_timeout; + un->sysctl_table[1 ].data = &un->udplite_timeout_stream; + + un->sysctl_header =register_net_sysctl_table (net, + nf_net_netfilter_sysctl_path, un->sysctl_table); +if (!un->sysctl_header) + goto out_free; +#endif /* CONFIG_SYSCTL */ + +return 0 ; + +#ifdef CONFIG_SYSCTL +out_free: +kfree (un->sysctl_table); +#endif + +out: +kfree (un); +return err; +} + +static __net_exit void udplite_net_exit(struct net *net) +{ +struct udplite_net *un =udplite_pernet (net);#ifdef CONFIG_SYSCTL - .ctl_table_users = &udplite_sysctl_table_users, - .ctl_table_header = &udplite_sysctl_header, - .ctl_table = udplite_sysctl_table, +unregister_net_sysctl_table (un->sysctl_header); +kfree (un->sysctl_table);#endif +kfree (un); + +net_assign_generic (net, udplite_net_id,NULL ); +} + +static struct pernet_operations udplite_net_ops = { + .init = udplite_net_init, + .exit = udplite_net_exit, };static int __init nf_conntrack_proto_udplite_init (void ) {int err; - err =nf_conntrack_l4proto_register (&nf_conntrack_l4proto_udplite4); + err =register_pernet_gen_subsys (&udplite_net_id, &udplite_net_ops);if (err <0 ) goto err1; - err =nf_conntrack_l4proto_register (&nf_conntrack_l4proto_udplite6); + + err =nf_conntrack_l4proto_register (&nf_conntrack_l4proto_udplite4);if (err <0 ) goto err2; + + err =nf_conntrack_l4proto_register (&nf_conntrack_l4proto_udplite6); +if (err <0 ) + goto err3; +return 0 ; -err2: + +err3:nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_udplite4); +err2: +unregister_pernet_gen_subsys (udplite_net_id, &udplite_net_ops); err1:return err; }
@@ -235,6 +311,7 @@ static void __exit nf_conntrack_proto_ud {nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_udplite6);nf_conntrack_l4proto_unregister (&nf_conntrack_l4proto_udplite4); +unregister_pernet_gen_subsys (udplite_net_id, &udplite_net_ops); }module_init (nf_conntrack_proto_udplite_init);