1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Connection state tracking for netfilter. This is separated from,
4 * but required by, the (future) NAT layer; it can also be used by an iptables
7 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
8 * - generalize L3 protocol dependent part.
10 * Derived from include/linux/netfiter_ipv4/ip_conntrack.h
13 #ifndef _NF_CONNTRACK_H
14 #define _NF_CONNTRACK_H
16 #include <linux/netfilter/nf_conntrack_common.h>
18 #include <linux/bitops.h>
19 #include <linux/compiler.h>
20 #include <linux/atomic.h>
22 #include <linux/netfilter/nf_conntrack_tcp.h>
23 #include <linux/netfilter/nf_conntrack_dccp.h>
24 #include <linux/netfilter/nf_conntrack_sctp.h>
25 #include <linux/netfilter/nf_conntrack_proto_gre.h>
26 #include <net/netfilter/ipv6/nf_conntrack_icmpv6.h>
28 #include <net/netfilter/nf_conntrack_tuple.h>
31 unsigned long stream_ts;
34 /* per conntrack: protocol private data */
35 union nf_conntrack_proto {
36 /* insert conntrack proto private data here */
37 struct nf_ct_dccp dccp;
38 struct ip_ct_sctp sctp;
42 unsigned int tmpl_padto;
45 union nf_conntrack_expect_proto {
46 /* insert expect proto private data here */
49 struct nf_conntrack_net {
52 unsigned int users_bridge;
55 #include <linux/types.h>
56 #include <linux/skbuff.h>
58 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
59 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
62 /* Usage count in here is 1 for hash table, 1 per skb,
63 * plus 1 for any connection(s) we are `master' for
65 * Hint, SKB address this struct and refcnt via skb->_nfct and
66 * helpers nf_conntrack_get() and nf_conntrack_put().
67 * Helper nf_ct_put() equals nf_conntrack_put() by dec refcnt,
68 * beware nf_ct_get() is different and don't inc refcnt.
70 struct nf_conntrack ct_general;
73 /* jiffies32 when this ct is considered dead */
76 #ifdef CONFIG_NF_CONNTRACK_ZONES
77 struct nf_conntrack_zone zone;
79 /* XXX should I move this to the tail ? - Y.K */
80 /* These are my tuples; original and reply */
81 struct nf_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
83 /* Have we seen traffic both ways yet? (bitset) */
87 possible_net_t ct_net;
89 #if IS_ENABLED(CONFIG_NF_NAT)
90 struct hlist_node nat_bysource;
92 /* all members below initialized via memset */
93 u8 __nfct_init_offset[0];
95 /* If we were expected by an expectation, this will be it */
96 struct nf_conn *master;
98 #if defined(CONFIG_NF_CONNTRACK_MARK)
102 #ifdef CONFIG_NF_CONNTRACK_SECMARK
107 struct nf_ct_ext *ext;
109 /* Storage reserved for other modules, must be the last member */
110 union nf_conntrack_proto proto;
113 static inline struct nf_conn *
114 nf_ct_tuplehash_to_ctrack(const struct nf_conntrack_tuple_hash *hash)
116 return container_of(hash, struct nf_conn,
117 tuplehash[hash->tuple.dst.dir]);
120 static inline u_int16_t nf_ct_l3num(const struct nf_conn *ct)
122 return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
125 static inline u_int8_t nf_ct_protonum(const struct nf_conn *ct)
127 return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
130 #define nf_ct_tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
132 /* get master conntrack via master expectation */
133 #define master_ct(conntr) (conntr->master)
135 extern struct net init_net;
137 static inline struct net *nf_ct_net(const struct nf_conn *ct)
139 return read_pnet(&ct->ct_net);
142 /* Alter reply tuple (maybe alter helper). */
143 void nf_conntrack_alter_reply(struct nf_conn *ct,
144 const struct nf_conntrack_tuple *newreply);
146 /* Is this tuple taken? (ignoring any belonging to the given
148 int nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
149 const struct nf_conn *ignored_conntrack);
151 #define NFCT_INFOMASK 7UL
152 #define NFCT_PTRMASK ~(NFCT_INFOMASK)
154 /* Return conntrack_info and tuple hash for given skb. */
155 static inline struct nf_conn *
156 nf_ct_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
158 *ctinfo = skb->_nfct & NFCT_INFOMASK;
160 return (struct nf_conn *)(skb->_nfct & NFCT_PTRMASK);
163 /* decrement reference count on a conntrack */
164 static inline void nf_ct_put(struct nf_conn *ct)
167 nf_conntrack_put(&ct->ct_general);
170 /* Protocol module loading */
171 int nf_ct_l3proto_try_module_get(unsigned short l3proto);
172 void nf_ct_l3proto_module_put(unsigned short l3proto);
174 /* load module; enable/disable conntrack in this namespace */
175 int nf_ct_netns_get(struct net *net, u8 nfproto);
176 void nf_ct_netns_put(struct net *net, u8 nfproto);
179 * Allocate a hashtable of hlist_head (if nulls == 0),
180 * or hlist_nulls_head (if nulls == 1)
182 void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls);
184 int nf_conntrack_hash_check_insert(struct nf_conn *ct);
185 bool nf_ct_delete(struct nf_conn *ct, u32 pid, int report);
187 bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
188 u_int16_t l3num, struct net *net,
189 struct nf_conntrack_tuple *tuple);
191 void __nf_ct_refresh_acct(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
192 const struct sk_buff *skb,
193 u32 extra_jiffies, bool do_acct);
195 /* Refresh conntrack for this many jiffies and do accounting */
196 static inline void nf_ct_refresh_acct(struct nf_conn *ct,
197 enum ip_conntrack_info ctinfo,
198 const struct sk_buff *skb,
201 __nf_ct_refresh_acct(ct, ctinfo, skb, extra_jiffies, true);
204 /* Refresh conntrack for this many jiffies */
205 static inline void nf_ct_refresh(struct nf_conn *ct,
206 const struct sk_buff *skb,
209 __nf_ct_refresh_acct(ct, 0, skb, extra_jiffies, false);
212 /* kill conntrack and do accounting */
213 bool nf_ct_kill_acct(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
214 const struct sk_buff *skb);
216 /* kill conntrack without accounting */
217 static inline bool nf_ct_kill(struct nf_conn *ct)
219 return nf_ct_delete(ct, 0, 0);
222 /* Set all unconfirmed conntrack as dying */
223 void nf_ct_unconfirmed_destroy(struct net *);
225 /* Iterate over all conntracks: if iter returns true, it's deleted. */
226 void nf_ct_iterate_cleanup_net(struct net *net,
227 int (*iter)(struct nf_conn *i, void *data),
228 void *data, u32 portid, int report);
230 /* also set unconfirmed conntracks as dying. Only use in module exit path. */
231 void nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data),
234 struct nf_conntrack_zone;
236 void nf_conntrack_free(struct nf_conn *ct);
237 struct nf_conn *nf_conntrack_alloc(struct net *net,
238 const struct nf_conntrack_zone *zone,
239 const struct nf_conntrack_tuple *orig,
240 const struct nf_conntrack_tuple *repl,
243 static inline int nf_ct_is_template(const struct nf_conn *ct)
245 return test_bit(IPS_TEMPLATE_BIT, &ct->status);
248 /* It's confirmed if it is, or has been in the hash table. */
249 static inline int nf_ct_is_confirmed(const struct nf_conn *ct)
251 return test_bit(IPS_CONFIRMED_BIT, &ct->status);
254 static inline int nf_ct_is_dying(const struct nf_conn *ct)
256 return test_bit(IPS_DYING_BIT, &ct->status);
259 /* Packet is received from loopback */
260 static inline bool nf_is_loopback_packet(const struct sk_buff *skb)
262 return skb->dev && skb->skb_iif && skb->dev->flags & IFF_LOOPBACK;
265 #define nfct_time_stamp ((u32)(jiffies))
267 /* jiffies until ct expires, 0 if already expired */
268 static inline unsigned long nf_ct_expires(const struct nf_conn *ct)
270 s32 timeout = ct->timeout - nfct_time_stamp;
272 return timeout > 0 ? timeout : 0;
275 static inline bool nf_ct_is_expired(const struct nf_conn *ct)
277 return (__s32)(ct->timeout - nfct_time_stamp) <= 0;
280 /* use after obtaining a reference count */
281 static inline bool nf_ct_should_gc(const struct nf_conn *ct)
283 return nf_ct_is_expired(ct) && nf_ct_is_confirmed(ct) &&
289 int nf_conntrack_set_hashsize(const char *val, const struct kernel_param *kp);
290 int nf_conntrack_hash_resize(unsigned int hashsize);
292 extern struct hlist_nulls_head *nf_conntrack_hash;
293 extern unsigned int nf_conntrack_htable_size;
294 extern seqcount_t nf_conntrack_generation;
295 extern unsigned int nf_conntrack_max;
297 /* must be called with rcu read lock held */
299 nf_conntrack_get_ht(struct hlist_nulls_head **hash, unsigned int *hsize)
301 struct hlist_nulls_head *hptr;
302 unsigned int sequence, hsz;
305 sequence = read_seqcount_begin(&nf_conntrack_generation);
306 hsz = nf_conntrack_htable_size;
307 hptr = nf_conntrack_hash;
308 } while (read_seqcount_retry(&nf_conntrack_generation, sequence));
314 struct nf_conn *nf_ct_tmpl_alloc(struct net *net,
315 const struct nf_conntrack_zone *zone,
317 void nf_ct_tmpl_free(struct nf_conn *tmpl);
319 u32 nf_ct_get_id(const struct nf_conn *ct);
322 nf_ct_set(struct sk_buff *skb, struct nf_conn *ct, enum ip_conntrack_info info)
324 skb->_nfct = (unsigned long)ct | info;
327 #define NF_CT_STAT_INC(net, count) __this_cpu_inc((net)->ct.stat->count)
328 #define NF_CT_STAT_INC_ATOMIC(net, count) this_cpu_inc((net)->ct.stat->count)
329 #define NF_CT_STAT_ADD_ATOMIC(net, count, v) this_cpu_add((net)->ct.stat->count, (v))
331 #define MODULE_ALIAS_NFCT_HELPER(helper) \
332 MODULE_ALIAS("nfct-helper-" helper)
334 #endif /* _NF_CONNTRACK_H */