Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Connection state tracking for netfilter. This is separated from, |
| 3 | * but required by, the (future) NAT layer; it can also be used by an iptables |
| 4 | * extension. |
| 5 | * |
| 6 | * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp> |
| 7 | * - generalize L3 protocol dependent part. |
| 8 | * |
| 9 | * Derived from include/linux/netfiter_ipv4/ip_conntrack.h |
| 10 | */ |
| 11 | |
| 12 | #ifndef _NF_CONNTRACK_H |
| 13 | #define _NF_CONNTRACK_H |
| 14 | |
| 15 | #include <linux/netfilter/nf_conntrack_common.h> |
| 16 | |
| 17 | #ifdef __KERNEL__ |
| 18 | #include <linux/config.h> |
| 19 | #include <linux/bitops.h> |
| 20 | #include <linux/compiler.h> |
| 21 | #include <asm/atomic.h> |
| 22 | |
| 23 | #include <linux/netfilter/nf_conntrack_tcp.h> |
| 24 | #include <linux/netfilter/nf_conntrack_sctp.h> |
| 25 | #include <net/netfilter/ipv4/nf_conntrack_icmp.h> |
| 26 | #include <net/netfilter/ipv6/nf_conntrack_icmpv6.h> |
| 27 | |
| 28 | #include <net/netfilter/nf_conntrack_tuple.h> |
| 29 | |
| 30 | /* per conntrack: protocol private data */ |
| 31 | union nf_conntrack_proto { |
| 32 | /* insert conntrack proto private data here */ |
| 33 | struct ip_ct_sctp sctp; |
| 34 | struct ip_ct_tcp tcp; |
| 35 | struct ip_ct_icmp icmp; |
| 36 | struct nf_ct_icmpv6 icmpv6; |
| 37 | }; |
| 38 | |
| 39 | union nf_conntrack_expect_proto { |
| 40 | /* insert expect proto private data here */ |
| 41 | }; |
| 42 | |
| 43 | /* Add protocol helper include file here */ |
| 44 | #include <linux/netfilter/nf_conntrack_ftp.h> |
| 45 | |
| 46 | /* per conntrack: application helper private data */ |
| 47 | union nf_conntrack_help { |
| 48 | /* insert conntrack helper private data (master) here */ |
| 49 | struct ip_ct_ftp_master ct_ftp_info; |
| 50 | }; |
| 51 | |
| 52 | #include <linux/types.h> |
| 53 | #include <linux/skbuff.h> |
| 54 | |
| 55 | #ifdef CONFIG_NETFILTER_DEBUG |
| 56 | #define NF_CT_ASSERT(x) \ |
| 57 | do { \ |
| 58 | if (!(x)) \ |
| 59 | /* Wooah! I'm tripping my conntrack in a frenzy of \ |
| 60 | netplay... */ \ |
| 61 | printk("NF_CT_ASSERT: %s:%i(%s)\n", \ |
| 62 | __FILE__, __LINE__, __FUNCTION__); \ |
| 63 | } while(0) |
| 64 | #else |
| 65 | #define NF_CT_ASSERT(x) |
| 66 | #endif |
| 67 | |
| 68 | struct nf_conntrack_helper; |
| 69 | |
| 70 | #include <net/netfilter/ipv4/nf_conntrack_ipv4.h> |
| 71 | struct nf_conn |
| 72 | { |
| 73 | /* Usage count in here is 1 for hash table/destruct timer, 1 per skb, |
| 74 | plus 1 for any connection(s) we are `master' for */ |
| 75 | struct nf_conntrack ct_general; |
| 76 | |
| 77 | /* XXX should I move this to the tail ? - Y.K */ |
| 78 | /* These are my tuples; original and reply */ |
| 79 | struct nf_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX]; |
| 80 | |
| 81 | /* Have we seen traffic both ways yet? (bitset) */ |
| 82 | unsigned long status; |
| 83 | |
| 84 | /* Timer function; drops refcnt when it goes off. */ |
| 85 | struct timer_list timeout; |
| 86 | |
| 87 | #ifdef CONFIG_NF_CT_ACCT |
| 88 | /* Accounting Information (same cache line as other written members) */ |
| 89 | struct ip_conntrack_counter counters[IP_CT_DIR_MAX]; |
| 90 | #endif |
| 91 | /* If we were expected by an expectation, this will be it */ |
| 92 | struct nf_conn *master; |
| 93 | |
| 94 | /* Current number of expected connections */ |
| 95 | unsigned int expecting; |
| 96 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame^] | 97 | /* Unique ID that identifies this conntrack*/ |
| 98 | unsigned int id; |
| 99 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 100 | /* Helper. if any */ |
| 101 | struct nf_conntrack_helper *helper; |
| 102 | |
| 103 | /* features - nat, helper, ... used by allocating system */ |
| 104 | u_int32_t features; |
| 105 | |
| 106 | /* Storage reserved for other modules: */ |
| 107 | |
| 108 | union nf_conntrack_proto proto; |
| 109 | |
| 110 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
| 111 | u_int32_t mark; |
| 112 | #endif |
| 113 | |
| 114 | /* These members are dynamically allocated. */ |
| 115 | |
| 116 | union nf_conntrack_help *help; |
| 117 | |
| 118 | /* Layer 3 dependent members. (ex: NAT) */ |
| 119 | union { |
| 120 | struct nf_conntrack_ipv4 *ipv4; |
| 121 | } l3proto; |
| 122 | void *data[0]; |
| 123 | }; |
| 124 | |
| 125 | struct nf_conntrack_expect |
| 126 | { |
| 127 | /* Internal linked list (global expectation list) */ |
| 128 | struct list_head list; |
| 129 | |
| 130 | /* We expect this tuple, with the following mask */ |
| 131 | struct nf_conntrack_tuple tuple, mask; |
| 132 | |
| 133 | /* Function to call after setup and insertion */ |
| 134 | void (*expectfn)(struct nf_conn *new, |
| 135 | struct nf_conntrack_expect *this); |
| 136 | |
| 137 | /* The conntrack of the master connection */ |
| 138 | struct nf_conn *master; |
| 139 | |
| 140 | /* Timer function; deletes the expectation. */ |
| 141 | struct timer_list timeout; |
| 142 | |
| 143 | /* Usage count. */ |
| 144 | atomic_t use; |
| 145 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame^] | 146 | /* Unique ID */ |
| 147 | unsigned int id; |
| 148 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 149 | /* Flags */ |
| 150 | unsigned int flags; |
| 151 | |
| 152 | #ifdef CONFIG_NF_NAT_NEEDED |
| 153 | /* This is the original per-proto part, used to map the |
| 154 | * expected connection the way the recipient expects. */ |
| 155 | union nf_conntrack_manip_proto saved_proto; |
| 156 | /* Direction relative to the master connection. */ |
| 157 | enum ip_conntrack_dir dir; |
| 158 | #endif |
| 159 | }; |
| 160 | |
| 161 | #define NF_CT_EXPECT_PERMANENT 0x1 |
| 162 | |
| 163 | static inline struct nf_conn * |
| 164 | nf_ct_tuplehash_to_ctrack(const struct nf_conntrack_tuple_hash *hash) |
| 165 | { |
| 166 | return container_of(hash, struct nf_conn, |
| 167 | tuplehash[hash->tuple.dst.dir]); |
| 168 | } |
| 169 | |
| 170 | /* get master conntrack via master expectation */ |
| 171 | #define master_ct(conntr) (conntr->master) |
| 172 | |
| 173 | /* Alter reply tuple (maybe alter helper). */ |
| 174 | extern void |
| 175 | nf_conntrack_alter_reply(struct nf_conn *conntrack, |
| 176 | const struct nf_conntrack_tuple *newreply); |
| 177 | |
| 178 | /* Is this tuple taken? (ignoring any belonging to the given |
| 179 | conntrack). */ |
| 180 | extern int |
| 181 | nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple, |
| 182 | const struct nf_conn *ignored_conntrack); |
| 183 | |
| 184 | /* Return conntrack_info and tuple hash for given skb. */ |
| 185 | static inline struct nf_conn * |
| 186 | nf_ct_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo) |
| 187 | { |
| 188 | *ctinfo = skb->nfctinfo; |
| 189 | return (struct nf_conn *)skb->nfct; |
| 190 | } |
| 191 | |
| 192 | /* decrement reference count on a conntrack */ |
| 193 | static inline void nf_ct_put(struct nf_conn *ct) |
| 194 | { |
| 195 | NF_CT_ASSERT(ct); |
| 196 | nf_conntrack_put(&ct->ct_general); |
| 197 | } |
| 198 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame^] | 199 | extern struct nf_conntrack_tuple_hash * |
| 200 | __nf_conntrack_find(const struct nf_conntrack_tuple *tuple, |
| 201 | const struct nf_conn *ignored_conntrack); |
| 202 | |
| 203 | extern void nf_conntrack_hash_insert(struct nf_conn *ct); |
| 204 | |
| 205 | extern struct nf_conntrack_expect * |
| 206 | __nf_conntrack_expect_find(const struct nf_conntrack_tuple *tuple); |
| 207 | |
| 208 | extern struct nf_conntrack_expect * |
| 209 | nf_conntrack_expect_find(const struct nf_conntrack_tuple *tuple); |
| 210 | |
| 211 | extern void nf_ct_unlink_expect(struct nf_conntrack_expect *exp); |
| 212 | |
| 213 | extern void nf_ct_remove_expectations(struct nf_conn *ct); |
| 214 | |
| 215 | extern void nf_conntrack_flush(void); |
| 216 | |
| 217 | extern struct nf_conntrack_helper * |
| 218 | nf_ct_helper_find_get( const struct nf_conntrack_tuple *tuple); |
| 219 | extern void nf_ct_helper_put(struct nf_conntrack_helper *helper); |
| 220 | |
| 221 | extern struct nf_conntrack_helper * |
| 222 | __nf_conntrack_helper_find_byname(const char *name); |
| 223 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 224 | /* call to create an explicit dependency on nf_conntrack. */ |
| 225 | extern void need_nf_conntrack(void); |
| 226 | |
| 227 | extern int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse, |
| 228 | const struct nf_conntrack_tuple *orig); |
| 229 | |
| 230 | extern void __nf_ct_refresh_acct(struct nf_conn *ct, |
| 231 | enum ip_conntrack_info ctinfo, |
| 232 | const struct sk_buff *skb, |
| 233 | unsigned long extra_jiffies, |
| 234 | int do_acct); |
| 235 | |
| 236 | /* Refresh conntrack for this many jiffies and do accounting */ |
| 237 | static inline void nf_ct_refresh_acct(struct nf_conn *ct, |
| 238 | enum ip_conntrack_info ctinfo, |
| 239 | const struct sk_buff *skb, |
| 240 | unsigned long extra_jiffies) |
| 241 | { |
| 242 | __nf_ct_refresh_acct(ct, ctinfo, skb, extra_jiffies, 1); |
| 243 | } |
| 244 | |
| 245 | /* Refresh conntrack for this many jiffies */ |
| 246 | static inline void nf_ct_refresh(struct nf_conn *ct, |
| 247 | const struct sk_buff *skb, |
| 248 | unsigned long extra_jiffies) |
| 249 | { |
| 250 | __nf_ct_refresh_acct(ct, 0, skb, extra_jiffies, 0); |
| 251 | } |
| 252 | |
| 253 | /* These are for NAT. Icky. */ |
| 254 | /* Update TCP window tracking data when NAT mangles the packet */ |
| 255 | extern void nf_conntrack_tcp_update(struct sk_buff *skb, |
| 256 | unsigned int dataoff, |
| 257 | struct nf_conn *conntrack, |
| 258 | int dir); |
| 259 | |
| 260 | /* Call me when a conntrack is destroyed. */ |
| 261 | extern void (*nf_conntrack_destroyed)(struct nf_conn *conntrack); |
| 262 | |
| 263 | /* Fake conntrack entry for untracked connections */ |
| 264 | extern struct nf_conn nf_conntrack_untracked; |
| 265 | |
| 266 | extern int nf_ct_no_defrag; |
| 267 | |
| 268 | /* Iterate over all conntracks: if iter returns true, it's deleted. */ |
| 269 | extern void |
| 270 | nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data); |
| 271 | extern void nf_conntrack_free(struct nf_conn *ct); |
| 272 | extern struct nf_conn * |
| 273 | nf_conntrack_alloc(const struct nf_conntrack_tuple *orig, |
| 274 | const struct nf_conntrack_tuple *repl); |
| 275 | |
| 276 | /* It's confirmed if it is, or has been in the hash table. */ |
| 277 | static inline int nf_ct_is_confirmed(struct nf_conn *ct) |
| 278 | { |
| 279 | return test_bit(IPS_CONFIRMED_BIT, &ct->status); |
| 280 | } |
| 281 | |
| 282 | static inline int nf_ct_is_dying(struct nf_conn *ct) |
| 283 | { |
| 284 | return test_bit(IPS_DYING_BIT, &ct->status); |
| 285 | } |
| 286 | |
| 287 | extern unsigned int nf_conntrack_htable_size; |
| 288 | |
| 289 | #define NF_CT_STAT_INC(count) (__get_cpu_var(nf_conntrack_stat).count++) |
| 290 | |
| 291 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
| 292 | #include <linux/notifier.h> |
| 293 | #include <linux/interrupt.h> |
| 294 | |
| 295 | struct nf_conntrack_ecache { |
| 296 | struct nf_conn *ct; |
| 297 | unsigned int events; |
| 298 | }; |
| 299 | DECLARE_PER_CPU(struct nf_conntrack_ecache, nf_conntrack_ecache); |
| 300 | |
| 301 | #define CONNTRACK_ECACHE(x) (__get_cpu_var(nf_conntrack_ecache).x) |
| 302 | |
| 303 | extern struct notifier_block *nf_conntrack_chain; |
| 304 | extern struct notifier_block *nf_conntrack_expect_chain; |
| 305 | |
| 306 | static inline int nf_conntrack_register_notifier(struct notifier_block *nb) |
| 307 | { |
| 308 | return notifier_chain_register(&nf_conntrack_chain, nb); |
| 309 | } |
| 310 | |
| 311 | static inline int nf_conntrack_unregister_notifier(struct notifier_block *nb) |
| 312 | { |
| 313 | return notifier_chain_unregister(&nf_conntrack_chain, nb); |
| 314 | } |
| 315 | |
| 316 | static inline int |
| 317 | nf_conntrack_expect_register_notifier(struct notifier_block *nb) |
| 318 | { |
| 319 | return notifier_chain_register(&nf_conntrack_expect_chain, nb); |
| 320 | } |
| 321 | |
| 322 | static inline int |
| 323 | nf_conntrack_expect_unregister_notifier(struct notifier_block *nb) |
| 324 | { |
| 325 | return notifier_chain_unregister(&nf_conntrack_expect_chain, nb); |
| 326 | } |
| 327 | |
| 328 | extern void nf_ct_deliver_cached_events(const struct nf_conn *ct); |
| 329 | extern void __nf_ct_event_cache_init(struct nf_conn *ct); |
| 330 | |
| 331 | static inline void |
| 332 | nf_conntrack_event_cache(enum ip_conntrack_events event, |
| 333 | const struct sk_buff *skb) |
| 334 | { |
| 335 | struct nf_conn *ct = (struct nf_conn *)skb->nfct; |
| 336 | struct nf_conntrack_ecache *ecache; |
| 337 | |
| 338 | local_bh_disable(); |
| 339 | ecache = &__get_cpu_var(nf_conntrack_ecache); |
| 340 | if (ct != ecache->ct) |
| 341 | __nf_ct_event_cache_init(ct); |
| 342 | ecache->events |= event; |
| 343 | local_bh_enable(); |
| 344 | } |
| 345 | |
| 346 | static inline void nf_conntrack_event(enum ip_conntrack_events event, |
| 347 | struct nf_conn *ct) |
| 348 | { |
| 349 | if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct)) |
| 350 | notifier_call_chain(&nf_conntrack_chain, event, ct); |
| 351 | } |
| 352 | |
| 353 | static inline void |
| 354 | nf_conntrack_expect_event(enum ip_conntrack_expect_events event, |
| 355 | struct nf_conntrack_expect *exp) |
| 356 | { |
| 357 | notifier_call_chain(&nf_conntrack_expect_chain, event, exp); |
| 358 | } |
| 359 | #else /* CONFIG_NF_CONNTRACK_EVENTS */ |
| 360 | static inline void nf_conntrack_event_cache(enum ip_conntrack_events event, |
| 361 | const struct sk_buff *skb) {} |
| 362 | static inline void nf_conntrack_event(enum ip_conntrack_events event, |
| 363 | struct nf_conn *ct) {} |
| 364 | static inline void nf_ct_deliver_cached_events(const struct nf_conn *ct) {} |
| 365 | static inline void |
| 366 | nf_conntrack_expect_event(enum ip_conntrack_expect_events event, |
| 367 | struct nf_conntrack_expect *exp) {} |
| 368 | #endif /* CONFIG_NF_CONNTRACK_EVENTS */ |
| 369 | |
| 370 | /* no helper, no nat */ |
| 371 | #define NF_CT_F_BASIC 0 |
| 372 | /* for helper */ |
| 373 | #define NF_CT_F_HELP 1 |
| 374 | /* for nat. */ |
| 375 | #define NF_CT_F_NAT 2 |
| 376 | #define NF_CT_F_NUM 4 |
| 377 | |
| 378 | extern int |
| 379 | nf_conntrack_register_cache(u_int32_t features, const char *name, size_t size, |
| 380 | int (*init_conntrack)(struct nf_conn *, u_int32_t)); |
| 381 | extern void |
| 382 | nf_conntrack_unregister_cache(u_int32_t features); |
| 383 | |
| 384 | #endif /* __KERNEL__ */ |
| 385 | #endif /* _NF_CONNTRACK_H */ |