Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _IP_CONNTRACK_H |
| 2 | #define _IP_CONNTRACK_H |
| 3 | /* Connection state tracking for netfilter. This is separated from, |
| 4 | but required by, the NAT layer; it can also be used by an iptables |
| 5 | extension. */ |
| 6 | enum ip_conntrack_info |
| 7 | { |
| 8 | /* Part of an established connection (either direction). */ |
| 9 | IP_CT_ESTABLISHED, |
| 10 | |
| 11 | /* Like NEW, but related to an existing connection, or ICMP error |
| 12 | (in either direction). */ |
| 13 | IP_CT_RELATED, |
| 14 | |
| 15 | /* Started a new connection to track (only |
| 16 | IP_CT_DIR_ORIGINAL); may be a retransmission. */ |
| 17 | IP_CT_NEW, |
| 18 | |
| 19 | /* >= this indicates reply direction */ |
| 20 | IP_CT_IS_REPLY, |
| 21 | |
| 22 | /* Number of distinct IP_CT types (no NEW in reply dirn). */ |
| 23 | IP_CT_NUMBER = IP_CT_IS_REPLY * 2 - 1 |
| 24 | }; |
| 25 | |
| 26 | /* Bitset representing status of connection. */ |
| 27 | enum ip_conntrack_status { |
| 28 | /* It's an expected connection: bit 0 set. This bit never changed */ |
| 29 | IPS_EXPECTED_BIT = 0, |
| 30 | IPS_EXPECTED = (1 << IPS_EXPECTED_BIT), |
| 31 | |
| 32 | /* We've seen packets both ways: bit 1 set. Can be set, not unset. */ |
| 33 | IPS_SEEN_REPLY_BIT = 1, |
| 34 | IPS_SEEN_REPLY = (1 << IPS_SEEN_REPLY_BIT), |
| 35 | |
| 36 | /* Conntrack should never be early-expired. */ |
| 37 | IPS_ASSURED_BIT = 2, |
| 38 | IPS_ASSURED = (1 << IPS_ASSURED_BIT), |
| 39 | |
| 40 | /* Connection is confirmed: originating packet has left box */ |
| 41 | IPS_CONFIRMED_BIT = 3, |
| 42 | IPS_CONFIRMED = (1 << IPS_CONFIRMED_BIT), |
| 43 | |
| 44 | /* Connection needs src nat in orig dir. This bit never changed. */ |
| 45 | IPS_SRC_NAT_BIT = 4, |
| 46 | IPS_SRC_NAT = (1 << IPS_SRC_NAT_BIT), |
| 47 | |
| 48 | /* Connection needs dst nat in orig dir. This bit never changed. */ |
| 49 | IPS_DST_NAT_BIT = 5, |
| 50 | IPS_DST_NAT = (1 << IPS_DST_NAT_BIT), |
| 51 | |
| 52 | /* Both together. */ |
| 53 | IPS_NAT_MASK = (IPS_DST_NAT | IPS_SRC_NAT), |
| 54 | |
| 55 | /* Connection needs TCP sequence adjusted. */ |
| 56 | IPS_SEQ_ADJUST_BIT = 6, |
| 57 | IPS_SEQ_ADJUST = (1 << IPS_SEQ_ADJUST_BIT), |
| 58 | |
| 59 | /* NAT initialization bits. */ |
| 60 | IPS_SRC_NAT_DONE_BIT = 7, |
| 61 | IPS_SRC_NAT_DONE = (1 << IPS_SRC_NAT_DONE_BIT), |
| 62 | |
| 63 | IPS_DST_NAT_DONE_BIT = 8, |
| 64 | IPS_DST_NAT_DONE = (1 << IPS_DST_NAT_DONE_BIT), |
| 65 | |
| 66 | /* Both together */ |
| 67 | IPS_NAT_DONE_MASK = (IPS_DST_NAT_DONE | IPS_SRC_NAT_DONE), |
| 68 | }; |
| 69 | |
| 70 | #ifdef __KERNEL__ |
| 71 | #include <linux/config.h> |
| 72 | #include <linux/netfilter_ipv4/ip_conntrack_tuple.h> |
| 73 | #include <linux/bitops.h> |
| 74 | #include <linux/compiler.h> |
| 75 | #include <asm/atomic.h> |
| 76 | |
| 77 | #include <linux/netfilter_ipv4/ip_conntrack_tcp.h> |
| 78 | #include <linux/netfilter_ipv4/ip_conntrack_icmp.h> |
| 79 | #include <linux/netfilter_ipv4/ip_conntrack_sctp.h> |
| 80 | |
| 81 | /* per conntrack: protocol private data */ |
| 82 | union ip_conntrack_proto { |
| 83 | /* insert conntrack proto private data here */ |
| 84 | struct ip_ct_sctp sctp; |
| 85 | struct ip_ct_tcp tcp; |
| 86 | struct ip_ct_icmp icmp; |
| 87 | }; |
| 88 | |
| 89 | union ip_conntrack_expect_proto { |
| 90 | /* insert expect proto private data here */ |
| 91 | }; |
| 92 | |
| 93 | /* Add protocol helper include file here */ |
| 94 | #include <linux/netfilter_ipv4/ip_conntrack_amanda.h> |
| 95 | #include <linux/netfilter_ipv4/ip_conntrack_ftp.h> |
| 96 | #include <linux/netfilter_ipv4/ip_conntrack_irc.h> |
| 97 | |
| 98 | /* per conntrack: application helper private data */ |
| 99 | union ip_conntrack_help { |
| 100 | /* insert conntrack helper private data (master) here */ |
| 101 | struct ip_ct_ftp_master ct_ftp_info; |
| 102 | struct ip_ct_irc_master ct_irc_info; |
| 103 | }; |
| 104 | |
| 105 | #ifdef CONFIG_IP_NF_NAT_NEEDED |
| 106 | #include <linux/netfilter_ipv4/ip_nat.h> |
| 107 | #endif |
| 108 | |
| 109 | #include <linux/types.h> |
| 110 | #include <linux/skbuff.h> |
| 111 | |
| 112 | #ifdef CONFIG_NETFILTER_DEBUG |
| 113 | #define IP_NF_ASSERT(x) \ |
| 114 | do { \ |
| 115 | if (!(x)) \ |
| 116 | /* Wooah! I'm tripping my conntrack in a frenzy of \ |
| 117 | netplay... */ \ |
| 118 | printk("NF_IP_ASSERT: %s:%i(%s)\n", \ |
| 119 | __FILE__, __LINE__, __FUNCTION__); \ |
| 120 | } while(0) |
| 121 | #else |
| 122 | #define IP_NF_ASSERT(x) |
| 123 | #endif |
| 124 | |
| 125 | struct ip_conntrack_counter |
| 126 | { |
| 127 | u_int64_t packets; |
| 128 | u_int64_t bytes; |
| 129 | }; |
| 130 | |
| 131 | struct ip_conntrack_helper; |
| 132 | |
| 133 | struct ip_conntrack |
| 134 | { |
| 135 | /* Usage count in here is 1 for hash table/destruct timer, 1 per skb, |
| 136 | plus 1 for any connection(s) we are `master' for */ |
| 137 | struct nf_conntrack ct_general; |
| 138 | |
| 139 | /* Have we seen traffic both ways yet? (bitset) */ |
| 140 | unsigned long status; |
| 141 | |
| 142 | /* Timer function; drops refcnt when it goes off. */ |
| 143 | struct timer_list timeout; |
| 144 | |
| 145 | #ifdef CONFIG_IP_NF_CT_ACCT |
| 146 | /* Accounting Information (same cache line as other written members) */ |
| 147 | struct ip_conntrack_counter counters[IP_CT_DIR_MAX]; |
| 148 | #endif |
| 149 | /* If we were expected by an expectation, this will be it */ |
| 150 | struct ip_conntrack *master; |
| 151 | |
| 152 | /* Current number of expected connections */ |
| 153 | unsigned int expecting; |
| 154 | |
| 155 | /* Helper, if any. */ |
| 156 | struct ip_conntrack_helper *helper; |
| 157 | |
| 158 | /* Storage reserved for other modules: */ |
| 159 | union ip_conntrack_proto proto; |
| 160 | |
| 161 | union ip_conntrack_help help; |
| 162 | |
| 163 | #ifdef CONFIG_IP_NF_NAT_NEEDED |
| 164 | struct { |
| 165 | struct ip_nat_info info; |
| 166 | #if defined(CONFIG_IP_NF_TARGET_MASQUERADE) || \ |
| 167 | defined(CONFIG_IP_NF_TARGET_MASQUERADE_MODULE) |
| 168 | int masq_index; |
| 169 | #endif |
| 170 | } nat; |
| 171 | #endif /* CONFIG_IP_NF_NAT_NEEDED */ |
| 172 | |
| 173 | #if defined(CONFIG_IP_NF_CONNTRACK_MARK) |
| 174 | unsigned long mark; |
| 175 | #endif |
| 176 | |
| 177 | /* Traversed often, so hopefully in different cacheline to top */ |
| 178 | /* These are my tuples; original and reply */ |
| 179 | struct ip_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX]; |
| 180 | }; |
| 181 | |
| 182 | struct ip_conntrack_expect |
| 183 | { |
| 184 | /* Internal linked list (global expectation list) */ |
| 185 | struct list_head list; |
| 186 | |
| 187 | /* We expect this tuple, with the following mask */ |
| 188 | struct ip_conntrack_tuple tuple, mask; |
| 189 | |
| 190 | /* Function to call after setup and insertion */ |
| 191 | void (*expectfn)(struct ip_conntrack *new, |
| 192 | struct ip_conntrack_expect *this); |
| 193 | |
| 194 | /* The conntrack of the master connection */ |
| 195 | struct ip_conntrack *master; |
| 196 | |
| 197 | /* Timer function; deletes the expectation. */ |
| 198 | struct timer_list timeout; |
| 199 | |
Rusty Russell | 4acdbdb | 2005-07-21 13:14:46 -0700 | [diff] [blame^] | 200 | /* Usage count. */ |
| 201 | atomic_t use; |
| 202 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | #ifdef CONFIG_IP_NF_NAT_NEEDED |
| 204 | /* This is the original per-proto part, used to map the |
| 205 | * expected connection the way the recipient expects. */ |
| 206 | union ip_conntrack_manip_proto saved_proto; |
| 207 | /* Direction relative to the master connection. */ |
| 208 | enum ip_conntrack_dir dir; |
| 209 | #endif |
| 210 | }; |
| 211 | |
| 212 | static inline struct ip_conntrack * |
| 213 | tuplehash_to_ctrack(const struct ip_conntrack_tuple_hash *hash) |
| 214 | { |
| 215 | return container_of(hash, struct ip_conntrack, |
| 216 | tuplehash[hash->tuple.dst.dir]); |
| 217 | } |
| 218 | |
| 219 | /* get master conntrack via master expectation */ |
| 220 | #define master_ct(conntr) (conntr->master) |
| 221 | |
| 222 | /* Alter reply tuple (maybe alter helper). */ |
| 223 | extern void |
| 224 | ip_conntrack_alter_reply(struct ip_conntrack *conntrack, |
| 225 | const struct ip_conntrack_tuple *newreply); |
| 226 | |
| 227 | /* Is this tuple taken? (ignoring any belonging to the given |
| 228 | conntrack). */ |
| 229 | extern int |
| 230 | ip_conntrack_tuple_taken(const struct ip_conntrack_tuple *tuple, |
| 231 | const struct ip_conntrack *ignored_conntrack); |
| 232 | |
| 233 | /* Return conntrack_info and tuple hash for given skb. */ |
| 234 | static inline struct ip_conntrack * |
| 235 | ip_conntrack_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo) |
| 236 | { |
| 237 | *ctinfo = skb->nfctinfo; |
| 238 | return (struct ip_conntrack *)skb->nfct; |
| 239 | } |
| 240 | |
| 241 | /* decrement reference count on a conntrack */ |
| 242 | extern inline void ip_conntrack_put(struct ip_conntrack *ct); |
| 243 | |
| 244 | /* call to create an explicit dependency on ip_conntrack. */ |
| 245 | extern void need_ip_conntrack(void); |
| 246 | |
| 247 | extern int invert_tuplepr(struct ip_conntrack_tuple *inverse, |
| 248 | const struct ip_conntrack_tuple *orig); |
| 249 | |
| 250 | /* Refresh conntrack for this many jiffies */ |
| 251 | extern void ip_ct_refresh_acct(struct ip_conntrack *ct, |
| 252 | enum ip_conntrack_info ctinfo, |
| 253 | const struct sk_buff *skb, |
| 254 | unsigned long extra_jiffies); |
| 255 | |
| 256 | /* These are for NAT. Icky. */ |
| 257 | /* Update TCP window tracking data when NAT mangles the packet */ |
| 258 | extern void ip_conntrack_tcp_update(struct sk_buff *skb, |
| 259 | struct ip_conntrack *conntrack, |
| 260 | enum ip_conntrack_dir dir); |
| 261 | |
| 262 | /* Call me when a conntrack is destroyed. */ |
| 263 | extern void (*ip_conntrack_destroyed)(struct ip_conntrack *conntrack); |
| 264 | |
| 265 | /* Fake conntrack entry for untracked connections */ |
| 266 | extern struct ip_conntrack ip_conntrack_untracked; |
| 267 | |
| 268 | /* Returns new sk_buff, or NULL */ |
| 269 | struct sk_buff * |
| 270 | ip_ct_gather_frags(struct sk_buff *skb, u_int32_t user); |
| 271 | |
| 272 | /* Iterate over all conntracks: if iter returns true, it's deleted. */ |
| 273 | extern void |
| 274 | ip_ct_iterate_cleanup(int (*iter)(struct ip_conntrack *i, void *data), |
| 275 | void *data); |
| 276 | |
| 277 | /* It's confirmed if it is, or has been in the hash table. */ |
| 278 | static inline int is_confirmed(struct ip_conntrack *ct) |
| 279 | { |
| 280 | return test_bit(IPS_CONFIRMED_BIT, &ct->status); |
| 281 | } |
| 282 | |
| 283 | extern unsigned int ip_conntrack_htable_size; |
| 284 | |
| 285 | struct ip_conntrack_stat |
| 286 | { |
| 287 | unsigned int searched; |
| 288 | unsigned int found; |
| 289 | unsigned int new; |
| 290 | unsigned int invalid; |
| 291 | unsigned int ignore; |
| 292 | unsigned int delete; |
| 293 | unsigned int delete_list; |
| 294 | unsigned int insert; |
| 295 | unsigned int insert_failed; |
| 296 | unsigned int drop; |
| 297 | unsigned int early_drop; |
| 298 | unsigned int error; |
| 299 | unsigned int expect_new; |
| 300 | unsigned int expect_create; |
| 301 | unsigned int expect_delete; |
| 302 | }; |
| 303 | |
| 304 | #define CONNTRACK_STAT_INC(count) (__get_cpu_var(ip_conntrack_stat).count++) |
| 305 | |
| 306 | #ifdef CONFIG_IP_NF_NAT_NEEDED |
| 307 | static inline int ip_nat_initialized(struct ip_conntrack *conntrack, |
| 308 | enum ip_nat_manip_type manip) |
| 309 | { |
| 310 | if (manip == IP_NAT_MANIP_SRC) |
| 311 | return test_bit(IPS_SRC_NAT_DONE_BIT, &conntrack->status); |
| 312 | return test_bit(IPS_DST_NAT_DONE_BIT, &conntrack->status); |
| 313 | } |
| 314 | #endif /* CONFIG_IP_NF_NAT_NEEDED */ |
| 315 | |
| 316 | #endif /* __KERNEL__ */ |
| 317 | #endif /* _IP_CONNTRACK_H */ |