Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * Connection state tracking for netfilter.  This is separated from,
0004  * but required by, the (future) NAT layer; it can also be used by an iptables
0005  * extension.
0006  *
0007  * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
0008  *  - generalize L3 protocol dependent part.
0009  *
0010  * Derived from include/linux/netfiter_ipv4/ip_conntrack.h
0011  */
0012 
0013 #ifndef _NF_CONNTRACK_H
0014 #define _NF_CONNTRACK_H
0015 
0016 #include <linux/bitops.h>
0017 #include <linux/compiler.h>
0018 
0019 #include <linux/netfilter/nf_conntrack_common.h>
0020 #include <linux/netfilter/nf_conntrack_tcp.h>
0021 #include <linux/netfilter/nf_conntrack_dccp.h>
0022 #include <linux/netfilter/nf_conntrack_sctp.h>
0023 #include <linux/netfilter/nf_conntrack_proto_gre.h>
0024 
0025 #include <net/netfilter/nf_conntrack_tuple.h>
0026 
0027 struct nf_ct_udp {
0028     unsigned long   stream_ts;
0029 };
0030 
0031 /* per conntrack: protocol private data */
0032 union nf_conntrack_proto {
0033     /* insert conntrack proto private data here */
0034     struct nf_ct_dccp dccp;
0035     struct ip_ct_sctp sctp;
0036     struct ip_ct_tcp tcp;
0037     struct nf_ct_udp udp;
0038     struct nf_ct_gre gre;
0039     unsigned int tmpl_padto;
0040 };
0041 
0042 union nf_conntrack_expect_proto {
0043     /* insert expect proto private data here */
0044 };
0045 
0046 struct nf_conntrack_net_ecache {
0047     struct delayed_work dwork;
0048     spinlock_t dying_lock;
0049     struct hlist_nulls_head dying_list;
0050 };
0051 
0052 struct nf_conntrack_net {
0053     /* only used when new connection is allocated: */
0054     atomic_t count;
0055     unsigned int expect_count;
0056 
0057     /* only used from work queues, configuration plane, and so on: */
0058     unsigned int users4;
0059     unsigned int users6;
0060     unsigned int users_bridge;
0061 #ifdef CONFIG_SYSCTL
0062     struct ctl_table_header *sysctl_header;
0063 #endif
0064 #ifdef CONFIG_NF_CONNTRACK_EVENTS
0065     struct nf_conntrack_net_ecache ecache;
0066 #endif
0067 };
0068 
0069 #include <linux/types.h>
0070 #include <linux/skbuff.h>
0071 
0072 #include <net/netfilter/ipv4/nf_conntrack_ipv4.h>
0073 #include <net/netfilter/ipv6/nf_conntrack_ipv6.h>
0074 
0075 struct nf_conn {
0076     /* Usage count in here is 1 for hash table, 1 per skb,
0077      * plus 1 for any connection(s) we are `master' for
0078      *
0079      * Hint, SKB address this struct and refcnt via skb->_nfct and
0080      * helpers nf_conntrack_get() and nf_conntrack_put().
0081      * Helper nf_ct_put() equals nf_conntrack_put() by dec refcnt,
0082      * except that the latter uses internal indirection and does not
0083      * result in a conntrack module dependency.
0084      * beware nf_ct_get() is different and don't inc refcnt.
0085      */
0086     struct nf_conntrack ct_general;
0087 
0088     spinlock_t  lock;
0089     /* jiffies32 when this ct is considered dead */
0090     u32 timeout;
0091 
0092 #ifdef CONFIG_NF_CONNTRACK_ZONES
0093     struct nf_conntrack_zone zone;
0094 #endif
0095     /* XXX should I move this to the tail ? - Y.K */
0096     /* These are my tuples; original and reply */
0097     struct nf_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX];
0098 
0099     /* Have we seen traffic both ways yet? (bitset) */
0100     unsigned long status;
0101 
0102     possible_net_t ct_net;
0103 
0104 #if IS_ENABLED(CONFIG_NF_NAT)
0105     struct hlist_node   nat_bysource;
0106 #endif
0107     /* all members below initialized via memset */
0108     struct { } __nfct_init_offset;
0109 
0110     /* If we were expected by an expectation, this will be it */
0111     struct nf_conn *master;
0112 
0113 #if defined(CONFIG_NF_CONNTRACK_MARK)
0114     u_int32_t mark;
0115 #endif
0116 
0117 #ifdef CONFIG_NF_CONNTRACK_SECMARK
0118     u_int32_t secmark;
0119 #endif
0120 
0121     /* Extensions */
0122     struct nf_ct_ext *ext;
0123 
0124     /* Storage reserved for other modules, must be the last member */
0125     union nf_conntrack_proto proto;
0126 };
0127 
0128 static inline struct nf_conn *
0129 nf_ct_tuplehash_to_ctrack(const struct nf_conntrack_tuple_hash *hash)
0130 {
0131     return container_of(hash, struct nf_conn,
0132                 tuplehash[hash->tuple.dst.dir]);
0133 }
0134 
0135 static inline u_int16_t nf_ct_l3num(const struct nf_conn *ct)
0136 {
0137     return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
0138 }
0139 
0140 static inline u_int8_t nf_ct_protonum(const struct nf_conn *ct)
0141 {
0142     return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
0143 }
0144 
0145 #define nf_ct_tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
0146 
0147 /* get master conntrack via master expectation */
0148 #define master_ct(conntr) (conntr->master)
0149 
0150 extern struct net init_net;
0151 
0152 static inline struct net *nf_ct_net(const struct nf_conn *ct)
0153 {
0154     return read_pnet(&ct->ct_net);
0155 }
0156 
0157 /* Alter reply tuple (maybe alter helper). */
0158 void nf_conntrack_alter_reply(struct nf_conn *ct,
0159                   const struct nf_conntrack_tuple *newreply);
0160 
0161 /* Is this tuple taken? (ignoring any belonging to the given
0162    conntrack). */
0163 int nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
0164                  const struct nf_conn *ignored_conntrack);
0165 
0166 /* Return conntrack_info and tuple hash for given skb. */
0167 static inline struct nf_conn *
0168 nf_ct_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo)
0169 {
0170     unsigned long nfct = skb_get_nfct(skb);
0171 
0172     *ctinfo = nfct & NFCT_INFOMASK;
0173     return (struct nf_conn *)(nfct & NFCT_PTRMASK);
0174 }
0175 
0176 void nf_ct_destroy(struct nf_conntrack *nfct);
0177 
0178 /* decrement reference count on a conntrack */
0179 static inline void nf_ct_put(struct nf_conn *ct)
0180 {
0181     if (ct && refcount_dec_and_test(&ct->ct_general.use))
0182         nf_ct_destroy(&ct->ct_general);
0183 }
0184 
0185 /* Protocol module loading */
0186 int nf_ct_l3proto_try_module_get(unsigned short l3proto);
0187 void nf_ct_l3proto_module_put(unsigned short l3proto);
0188 
0189 /* load module; enable/disable conntrack in this namespace */
0190 int nf_ct_netns_get(struct net *net, u8 nfproto);
0191 void nf_ct_netns_put(struct net *net, u8 nfproto);
0192 
0193 /*
0194  * Allocate a hashtable of hlist_head (if nulls == 0),
0195  * or hlist_nulls_head (if nulls == 1)
0196  */
0197 void *nf_ct_alloc_hashtable(unsigned int *sizep, int nulls);
0198 
0199 int nf_conntrack_hash_check_insert(struct nf_conn *ct);
0200 bool nf_ct_delete(struct nf_conn *ct, u32 pid, int report);
0201 
0202 bool nf_ct_get_tuplepr(const struct sk_buff *skb, unsigned int nhoff,
0203                u_int16_t l3num, struct net *net,
0204                struct nf_conntrack_tuple *tuple);
0205 
0206 void __nf_ct_refresh_acct(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
0207               const struct sk_buff *skb,
0208               u32 extra_jiffies, bool do_acct);
0209 
0210 /* Refresh conntrack for this many jiffies and do accounting */
0211 static inline void nf_ct_refresh_acct(struct nf_conn *ct,
0212                       enum ip_conntrack_info ctinfo,
0213                       const struct sk_buff *skb,
0214                       u32 extra_jiffies)
0215 {
0216     __nf_ct_refresh_acct(ct, ctinfo, skb, extra_jiffies, true);
0217 }
0218 
0219 /* Refresh conntrack for this many jiffies */
0220 static inline void nf_ct_refresh(struct nf_conn *ct,
0221                  const struct sk_buff *skb,
0222                  u32 extra_jiffies)
0223 {
0224     __nf_ct_refresh_acct(ct, 0, skb, extra_jiffies, false);
0225 }
0226 
0227 /* kill conntrack and do accounting */
0228 bool nf_ct_kill_acct(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
0229              const struct sk_buff *skb);
0230 
0231 /* kill conntrack without accounting */
0232 static inline bool nf_ct_kill(struct nf_conn *ct)
0233 {
0234     return nf_ct_delete(ct, 0, 0);
0235 }
0236 
0237 struct nf_ct_iter_data {
0238     struct net *net;
0239     void *data;
0240     u32 portid;
0241     int report;
0242 };
0243 
0244 /* Iterate over all conntracks: if iter returns true, it's deleted. */
0245 void nf_ct_iterate_cleanup_net(int (*iter)(struct nf_conn *i, void *data),
0246                    const struct nf_ct_iter_data *iter_data);
0247 
0248 /* also set unconfirmed conntracks as dying. Only use in module exit path. */
0249 void nf_ct_iterate_destroy(int (*iter)(struct nf_conn *i, void *data),
0250                void *data);
0251 
0252 struct nf_conntrack_zone;
0253 
0254 void nf_conntrack_free(struct nf_conn *ct);
0255 struct nf_conn *nf_conntrack_alloc(struct net *net,
0256                    const struct nf_conntrack_zone *zone,
0257                    const struct nf_conntrack_tuple *orig,
0258                    const struct nf_conntrack_tuple *repl,
0259                    gfp_t gfp);
0260 
0261 static inline int nf_ct_is_template(const struct nf_conn *ct)
0262 {
0263     return test_bit(IPS_TEMPLATE_BIT, &ct->status);
0264 }
0265 
0266 /* It's confirmed if it is, or has been in the hash table. */
0267 static inline int nf_ct_is_confirmed(const struct nf_conn *ct)
0268 {
0269     return test_bit(IPS_CONFIRMED_BIT, &ct->status);
0270 }
0271 
0272 static inline int nf_ct_is_dying(const struct nf_conn *ct)
0273 {
0274     return test_bit(IPS_DYING_BIT, &ct->status);
0275 }
0276 
0277 /* Packet is received from loopback */
0278 static inline bool nf_is_loopback_packet(const struct sk_buff *skb)
0279 {
0280     return skb->dev && skb->skb_iif && skb->dev->flags & IFF_LOOPBACK;
0281 }
0282 
0283 #define nfct_time_stamp ((u32)(jiffies))
0284 
0285 /* jiffies until ct expires, 0 if already expired */
0286 static inline unsigned long nf_ct_expires(const struct nf_conn *ct)
0287 {
0288     s32 timeout = READ_ONCE(ct->timeout) - nfct_time_stamp;
0289 
0290     return max(timeout, 0);
0291 }
0292 
0293 static inline bool nf_ct_is_expired(const struct nf_conn *ct)
0294 {
0295     return (__s32)(READ_ONCE(ct->timeout) - nfct_time_stamp) <= 0;
0296 }
0297 
0298 /* use after obtaining a reference count */
0299 static inline bool nf_ct_should_gc(const struct nf_conn *ct)
0300 {
0301     return nf_ct_is_expired(ct) && nf_ct_is_confirmed(ct) &&
0302            !nf_ct_is_dying(ct);
0303 }
0304 
0305 #define NF_CT_DAY   (86400 * HZ)
0306 
0307 /* Set an arbitrary timeout large enough not to ever expire, this save
0308  * us a check for the IPS_OFFLOAD_BIT from the packet path via
0309  * nf_ct_is_expired().
0310  */
0311 static inline void nf_ct_offload_timeout(struct nf_conn *ct)
0312 {
0313     if (nf_ct_expires(ct) < NF_CT_DAY / 2)
0314         WRITE_ONCE(ct->timeout, nfct_time_stamp + NF_CT_DAY);
0315 }
0316 
0317 struct kernel_param;
0318 
0319 int nf_conntrack_set_hashsize(const char *val, const struct kernel_param *kp);
0320 int nf_conntrack_hash_resize(unsigned int hashsize);
0321 
0322 extern struct hlist_nulls_head *nf_conntrack_hash;
0323 extern unsigned int nf_conntrack_htable_size;
0324 extern seqcount_spinlock_t nf_conntrack_generation;
0325 extern unsigned int nf_conntrack_max;
0326 
0327 /* must be called with rcu read lock held */
0328 static inline void
0329 nf_conntrack_get_ht(struct hlist_nulls_head **hash, unsigned int *hsize)
0330 {
0331     struct hlist_nulls_head *hptr;
0332     unsigned int sequence, hsz;
0333 
0334     do {
0335         sequence = read_seqcount_begin(&nf_conntrack_generation);
0336         hsz = nf_conntrack_htable_size;
0337         hptr = nf_conntrack_hash;
0338     } while (read_seqcount_retry(&nf_conntrack_generation, sequence));
0339 
0340     *hash = hptr;
0341     *hsize = hsz;
0342 }
0343 
0344 struct nf_conn *nf_ct_tmpl_alloc(struct net *net,
0345                  const struct nf_conntrack_zone *zone,
0346                  gfp_t flags);
0347 void nf_ct_tmpl_free(struct nf_conn *tmpl);
0348 
0349 u32 nf_ct_get_id(const struct nf_conn *ct);
0350 u32 nf_conntrack_count(const struct net *net);
0351 
0352 static inline void
0353 nf_ct_set(struct sk_buff *skb, struct nf_conn *ct, enum ip_conntrack_info info)
0354 {
0355     skb_set_nfct(skb, (unsigned long)ct | info);
0356 }
0357 
0358 extern unsigned int nf_conntrack_net_id;
0359 
0360 static inline struct nf_conntrack_net *nf_ct_pernet(const struct net *net)
0361 {
0362     return net_generic(net, nf_conntrack_net_id);
0363 }
0364 
0365 #define NF_CT_STAT_INC(net, count)    __this_cpu_inc((net)->ct.stat->count)
0366 #define NF_CT_STAT_INC_ATOMIC(net, count) this_cpu_inc((net)->ct.stat->count)
0367 #define NF_CT_STAT_ADD_ATOMIC(net, count, v) this_cpu_add((net)->ct.stat->count, (v))
0368 
0369 #define MODULE_ALIAS_NFCT_HELPER(helper) \
0370         MODULE_ALIAS("nfct-helper-" helper)
0371 
0372 #endif /* _NF_CONNTRACK_H */