Move some `struct nf_conntrack` code from linux/skbuff.h to linux/nf_conntrack_common.h. Together with a couple of helpers for getting and setting skb->_nfct, it allows us to remove CONFIG_NF_CONNTRACK checks from net/netfilter/nf_conntrack.h.
Unified · Split
| @@ -2,6 +2,7 @@ · 1 unmodified line | |||
| 2 | #ifndef _NF_CONNTRACK_COMMON_H | 2 | #ifndef _NF_CONNTRACK_COMMON_H |
| 3 | #define _NF_CONNTRACK_COMMON_H | 3 | #define _NF_CONNTRACK_COMMON_H |
| 4 | 4 | ||
| 5 | #include <linux/atomic.h> | ||
| 5 | #include <uapi/linux/netfilter/nf_conntrack_common.h> | 6 | #include <uapi/linux/netfilter/nf_conntrack_common.h> |
| 6 | 7 | ||
| 7 | struct ip_conntrack_stat { | 8 | struct ip_conntrack_stat { |
| @@ -19,4 +20,23 @@ struct ip_conntrack_stat { · 11 unmodified lines | |||
| 19 | unsigned int search_restart; | 20 | unsigned int search_restart; |
| 20 | }; | 21 | }; |
| 21 | 22 | ||
| 23 | #define NFCT_INFOMASK 7UL | ||
| 24 | #define NFCT_PTRMASK ~(NFCT_INFOMASK) | ||
| 25 | |||
| 26 | struct nf_conntrack { | ||
| 27 | atomic_t use; | ||
| 28 | }; | ||
| 29 | |||
| 30 | void nf_conntrack_destroy(struct nf_conntrack *nfct); | ||
| 31 | static inline void nf_conntrack_put(struct nf_conntrack *nfct) | ||
| 32 | { | ||
| 33 | if (nfct && atomic_dec_and_test(&nfct->use)) | ||
| 34 | nf_conntrack_destroy(nfct); | ||
| 35 | } | ||
| 36 | static inline void nf_conntrack_get(struct nf_conntrack *nfct) | ||
| 37 | { | ||
| 38 | if (nfct) | ||
| 39 | atomic_inc(&nfct->use); | ||
| 40 | } | ||
| 41 | |||
| 22 | #endif /* _NF_CONNTRACK_COMMON_H */ | 42 | #endif /* _NF_CONNTRACK_COMMON_H */ |
| @@ -37,6 +37,9 @@ · 36 unmodified lines | |||
| 37 | #include <linux/in6.h> | 37 | #include <linux/in6.h> |
| 38 | #include <linux/if_packet.h> | 38 | #include <linux/if_packet.h> |
| 39 | #include <net/flow.h> | 39 | #include <net/flow.h> |
| 40 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) | ||
| 41 | #include <linux/netfilter/nf_conntrack_common.h> | ||
| 42 | #endif | ||
| 40 | 43 | ||
| 41 | /* The interface for checksum offload between the stack and networking drivers | 44 | /* The interface for checksum offload between the stack and networking drivers |
| 42 | * is as follows... | 45 | * is as follows... |
| @@ -244,12 +247,6 @@ struct bpf_prog; · 201 unmodified lines | |||
| 244 | union bpf_attr; | 247 | union bpf_attr; |
| 245 | struct skb_ext; | 248 | struct skb_ext; |
| 246 | 249 | ||
| 247 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) | ||
| 248 | struct nf_conntrack { | ||
| 249 | atomic_t use; | ||
| 250 | }; | ||
| 251 | #endif | ||
| 252 | |||
| 253 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) | 250 | #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) |
| 254 | struct nf_bridge_info { | 251 | struct nf_bridge_info { |
| 255 | enum { | 252 | enum { |
| @@ -914,7 +911,6 @@ static inline bool skb_pfmemalloc(const struct sk_buff *skb) · 658 unmodified lines | |||
| 914 | #define SKB_DST_NOREF 1UL | 911 | #define SKB_DST_NOREF 1UL |
| 915 | #define SKB_DST_PTRMASK ~(SKB_DST_NOREF) | 912 | #define SKB_DST_PTRMASK ~(SKB_DST_NOREF) |
| 916 | 913 | ||
| 917 | #define SKB_NFCT_PTRMASK ~(7UL) | ||
| 918 | /** | 914 | /** |
| 919 | * skb_dst - returns skb dst_entry | 915 | * skb_dst - returns skb dst_entry |
| 920 | * @skb: buffer | 916 | * @skb: buffer |
| @@ -4040,25 +4036,27 @@ static inline void skb_remcsum_process(struct sk_buff *skb, void *ptr, · 3119 unmodified lines | |||
| 4040 | static inline struct nf_conntrack *skb_nfct(const struct sk_buff *skb) | 4036 | static inline struct nf_conntrack *skb_nfct(const struct sk_buff *skb) |
| 4041 | { | 4037 | { |
| 4042 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) | 4038 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
| 4043 | return (void *)(skb->_nfct & SKB_NFCT_PTRMASK); | 4039 | return (void *)(skb->_nfct & NFCT_PTRMASK); |
| 4044 | #else | 4040 | #else |
| 4045 | return NULL; | 4041 | return NULL; |
| 4046 | #endif | 4042 | #endif |
| 4047 | } | 4043 | } |
| 4048 | 4044 | ||
| 4049 | #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE) | 4045 | static inline unsigned long skb_get_nfct(const struct sk_buff *skb) |
| 4050 | void nf_conntrack_destroy(struct nf_conntrack *nfct); | ||
| 4051 | static inline void nf_conntrack_put(struct nf_conntrack *nfct) | ||
| 4052 | { | 4046 | { |
| 4053 | if (nfct && atomic_dec_and_test(&nfct->use)) | 4047 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
| 4054 | nf_conntrack_destroy(nfct); | 4048 | return skb->_nfct; |
| 4049 | #else | ||
| 4050 | return 0UL; | ||
| 4051 | #endif | ||
| 4055 | } | 4052 | } |
| 4056 | static inline void nf_conntrack_get(struct nf_conntrack *nfct) | 4053 | |
| 4054 | static inline void skb_set_nfct(struct sk_buff *skb, unsigned long nfct) | ||
| 4057 | { | 4055 | { |
| 4058 | if (nfct) | 4056 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
| 4059 | atomic_inc(&nfct->use); | 4057 | skb->_nfct = nfct; |
| 4060 | } | ||
| 4061 | #endif | 4058 | #endif |
| 4059 | } | ||
| 4062 | 4060 | ||
| 4063 | #ifdef CONFIG_SKB_EXTENSIONS | 4061 | #ifdef CONFIG_SKB_EXTENSIONS |
| 4064 | enum skb_ext_id { | 4062 | enum skb_ext_id { |
| @@ -13,12 +13,10 @@ · 12 unmodified lines | |||
| 13 | #ifndef _NF_CONNTRACK_H | 13 | #ifndef _NF_CONNTRACK_H |
| 14 | #define _NF_CONNTRACK_H | 14 | #define _NF_CONNTRACK_H |
| 15 | 15 | ||
| 16 | #include <linux/netfilter/nf_conntrack_common.h> | ||
| 17 | |||
| 18 | #include <linux/bitops.h> | 16 | #include <linux/bitops.h> |
| 19 | #include <linux/compiler.h> | 17 | #include <linux/compiler.h> |
| 20 | #include <linux/atomic.h> | ||
| 21 | 18 | ||
| 19 | #include <linux/netfilter/nf_conntrack_common.h> | ||
| 22 | #include <linux/netfilter/nf_conntrack_tcp.h> | 20 | #include <linux/netfilter/nf_conntrack_tcp.h> |
| 23 | #include <linux/netfilter/nf_conntrack_dccp.h> | 21 | #include <linux/netfilter/nf_conntrack_dccp.h> |
| 24 | #include <linux/netfilter/nf_conntrack_sctp.h> | 22 | #include <linux/netfilter/nf_conntrack_sctp.h> |
| @@ -58,7 +56,6 @@ struct nf_conntrack_net { · 33 unmodified lines | |||
| 58 | #include <net/netfilter/ipv6/nf_conntrack_ipv6.h> | 56 | #include <net/netfilter/ipv6/nf_conntrack_ipv6.h> |
| 59 | 57 | ||
| 60 | struct nf_conn { | 58 | struct nf_conn { |
| 61 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) | ||
| 62 | /* Usage count in here is 1 for hash table, 1 per skb, | 59 | /* Usage count in here is 1 for hash table, 1 per skb, |
| 63 | * plus 1 for any connection(s) we are `master' for | 60 | * plus 1 for any connection(s) we are `master' for |
| 64 | * | 61 | * |
| @@ -68,7 +65,6 @@ struct nf_conn { · 3 unmodified lines | |||
| 68 | * beware nf_ct_get() is different and don't inc refcnt. | 65 | * beware nf_ct_get() is different and don't inc refcnt. |
| 69 | */ | 66 | */ |
| 70 | struct nf_conntrack ct_general; | 67 | struct nf_conntrack ct_general; |
| 71 | #endif | ||
| 72 | 68 | ||
| 73 | spinlock_t lock; | 69 | spinlock_t lock; |
| 74 | /* jiffies32 when this ct is considered dead */ | 70 | /* jiffies32 when this ct is considered dead */ |
| @@ -149,18 +145,14 @@ void nf_conntrack_alter_reply(struct nf_conn *ct, · 74 unmodified lines | |||
| 149 | int nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple, | 145 | int nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple, |
| 150 | const struct nf_conn *ignored_conntrack); | 146 | const struct nf_conn *ignored_conntrack); |
| 151 | 147 | ||
| 152 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) | ||
| 153 | |||
| 154 | #define NFCT_INFOMASK 7UL | ||
| 155 | #define NFCT_PTRMASK ~(NFCT_INFOMASK) | ||
| 156 | |||
| 157 | /* Return conntrack_info and tuple hash for given skb. */ | 148 | /* Return conntrack_info and tuple hash for given skb. */ |
| 158 | static inline struct nf_conn * | 149 | static inline struct nf_conn * |
| 159 | nf_ct_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo) | 150 | nf_ct_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo) |
| 160 | { | 151 | { |
| 161 | *ctinfo = skb->_nfct & NFCT_INFOMASK; | 152 | unsigned long nfct = skb_get_nfct(skb); |
| 162 | 153 | ||
| 163 | return (struct nf_conn *)(skb->_nfct & NFCT_PTRMASK); | 154 | *ctinfo = nfct & NFCT_INFOMASK; |
| 155 | return (struct nf_conn *)(nfct & NFCT_PTRMASK); | ||
| 164 | } | 156 | } |
| 165 | 157 | ||
| 166 | /* decrement reference count on a conntrack */ | 158 | /* decrement reference count on a conntrack */ |
| @@ -170,8 +162,6 @@ static inline void nf_ct_put(struct nf_conn *ct) · 3 unmodified lines | |||
| 170 | nf_conntrack_put(&ct->ct_general); | 162 | nf_conntrack_put(&ct->ct_general); |
| 171 | } | 163 | } |
| 172 | 164 | ||
| 173 | #endif | ||
| 174 | |||
| 175 | /* Protocol module loading */ | 165 | /* Protocol module loading */ |
| 176 | int nf_ct_l3proto_try_module_get(unsigned short l3proto); | 166 | int nf_ct_l3proto_try_module_get(unsigned short l3proto); |
| 177 | void nf_ct_l3proto_module_put(unsigned short l3proto); | 167 | void nf_ct_l3proto_module_put(unsigned short l3proto); |
| @@ -323,16 +313,12 @@ void nf_ct_tmpl_free(struct nf_conn *tmpl); · 145 unmodified lines | |||
| 323 | 313 | ||
| 324 | u32 nf_ct_get_id(const struct nf_conn *ct); | 314 | u32 nf_ct_get_id(const struct nf_conn *ct); |
| 325 | 315 | ||
| 326 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) | ||
| 327 | |||
| 328 | static inline void | 316 | static inline void |
| 329 | nf_ct_set(struct sk_buff *skb, struct nf_conn *ct, enum ip_conntrack_info info) | 317 | nf_ct_set(struct sk_buff *skb, struct nf_conn *ct, enum ip_conntrack_info info) |
| 330 | { | 318 | { |
| 331 | skb->_nfct = (unsigned long)ct | info; | 319 | skb_set_nfct(skb, (unsigned long)ct | info); |
| 332 | } | 320 | } |
| 333 | 321 | ||
| 334 | #endif | ||
| 335 | |||
| 336 | #define NF_CT_STAT_INC(net, count) __this_cpu_inc((net)->ct.stat->count) | 322 | #define NF_CT_STAT_INC(net, count) __this_cpu_inc((net)->ct.stat->count) |
| 337 | #define NF_CT_STAT_INC_ATOMIC(net, count) this_cpu_inc((net)->ct.stat->count) | 323 | #define NF_CT_STAT_INC_ATOMIC(net, count) this_cpu_inc((net)->ct.stat->count) |
| 338 | #define NF_CT_STAT_ADD_ATOMIC(net, count, v) this_cpu_add((net)->ct.stat->count, (v)) | 324 | #define NF_CT_STAT_ADD_ATOMIC(net, count, v) this_cpu_add((net)->ct.stat->count, (v)) |
| @@ -1167,7 +1167,6 @@ static int __init nf_conntrack_standalone_init(void) · 1166 unmodified lines | |||
| 1167 | if (ret < 0) | 1167 | if (ret < 0) |
| 1168 | goto out_start; | 1168 | goto out_start; |
| 1169 | 1169 | ||
| 1170 | BUILD_BUG_ON(SKB_NFCT_PTRMASK != NFCT_PTRMASK); | ||
| 1171 | BUILD_BUG_ON(NFCT_INFOMASK <= IP_CT_NUMBER); | 1170 | BUILD_BUG_ON(NFCT_INFOMASK <= IP_CT_NUMBER); |
| 1172 | 1171 | ||
| 1173 | #ifdef CONFIG_SYSCTL | 1172 | #ifdef CONFIG_SYSCTL |