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 | |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 70 | /* nf_conn feature for connections that have a helper */ |
| 71 | struct nf_conn_help { |
| 72 | /* Helper. if any */ |
| 73 | struct nf_conntrack_helper *helper; |
| 74 | |
| 75 | union nf_conntrack_help help; |
| 76 | |
| 77 | /* Current number of expected connections */ |
| 78 | unsigned int expecting; |
| 79 | }; |
| 80 | |
| 81 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 82 | #include <net/netfilter/ipv4/nf_conntrack_ipv4.h> |
| 83 | struct nf_conn |
| 84 | { |
| 85 | /* Usage count in here is 1 for hash table/destruct timer, 1 per skb, |
| 86 | plus 1 for any connection(s) we are `master' for */ |
| 87 | struct nf_conntrack ct_general; |
| 88 | |
| 89 | /* XXX should I move this to the tail ? - Y.K */ |
| 90 | /* These are my tuples; original and reply */ |
| 91 | struct nf_conntrack_tuple_hash tuplehash[IP_CT_DIR_MAX]; |
| 92 | |
| 93 | /* Have we seen traffic both ways yet? (bitset) */ |
| 94 | unsigned long status; |
| 95 | |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 96 | /* If we were expected by an expectation, this will be it */ |
| 97 | struct nf_conn *master; |
| 98 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 99 | /* Timer function; drops refcnt when it goes off. */ |
| 100 | struct timer_list timeout; |
| 101 | |
| 102 | #ifdef CONFIG_NF_CT_ACCT |
| 103 | /* Accounting Information (same cache line as other written members) */ |
| 104 | struct ip_conntrack_counter counters[IP_CT_DIR_MAX]; |
| 105 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 106 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 107 | /* Unique ID that identifies this conntrack*/ |
| 108 | unsigned int id; |
| 109 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 110 | /* features - nat, helper, ... used by allocating system */ |
| 111 | u_int32_t features; |
| 112 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 113 | #if defined(CONFIG_NF_CONNTRACK_MARK) |
| 114 | u_int32_t mark; |
| 115 | #endif |
| 116 | |
James Morris | 7c9728c | 2006-06-09 00:31:46 -0700 | [diff] [blame^] | 117 | #ifdef CONFIG_NF_CONNTRACK_SECMARK |
| 118 | u_int32_t secmark; |
| 119 | #endif |
| 120 | |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 121 | /* Storage reserved for other modules: */ |
| 122 | union nf_conntrack_proto proto; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 123 | |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 124 | /* features dynamically at the end: helper, nat (both optional) */ |
| 125 | char data[0]; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | struct nf_conntrack_expect |
| 129 | { |
| 130 | /* Internal linked list (global expectation list) */ |
| 131 | struct list_head list; |
| 132 | |
| 133 | /* We expect this tuple, with the following mask */ |
| 134 | struct nf_conntrack_tuple tuple, mask; |
| 135 | |
| 136 | /* Function to call after setup and insertion */ |
| 137 | void (*expectfn)(struct nf_conn *new, |
| 138 | struct nf_conntrack_expect *this); |
| 139 | |
| 140 | /* The conntrack of the master connection */ |
| 141 | struct nf_conn *master; |
| 142 | |
| 143 | /* Timer function; deletes the expectation. */ |
| 144 | struct timer_list timeout; |
| 145 | |
| 146 | /* Usage count. */ |
| 147 | atomic_t use; |
| 148 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 149 | /* Unique ID */ |
| 150 | unsigned int id; |
| 151 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 152 | /* Flags */ |
| 153 | unsigned int flags; |
| 154 | |
| 155 | #ifdef CONFIG_NF_NAT_NEEDED |
| 156 | /* This is the original per-proto part, used to map the |
| 157 | * expected connection the way the recipient expects. */ |
| 158 | union nf_conntrack_manip_proto saved_proto; |
| 159 | /* Direction relative to the master connection. */ |
| 160 | enum ip_conntrack_dir dir; |
| 161 | #endif |
| 162 | }; |
| 163 | |
| 164 | #define NF_CT_EXPECT_PERMANENT 0x1 |
| 165 | |
| 166 | static inline struct nf_conn * |
| 167 | nf_ct_tuplehash_to_ctrack(const struct nf_conntrack_tuple_hash *hash) |
| 168 | { |
| 169 | return container_of(hash, struct nf_conn, |
| 170 | tuplehash[hash->tuple.dst.dir]); |
| 171 | } |
| 172 | |
| 173 | /* get master conntrack via master expectation */ |
| 174 | #define master_ct(conntr) (conntr->master) |
| 175 | |
| 176 | /* Alter reply tuple (maybe alter helper). */ |
| 177 | extern void |
| 178 | nf_conntrack_alter_reply(struct nf_conn *conntrack, |
| 179 | const struct nf_conntrack_tuple *newreply); |
| 180 | |
| 181 | /* Is this tuple taken? (ignoring any belonging to the given |
| 182 | conntrack). */ |
| 183 | extern int |
| 184 | nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple, |
| 185 | const struct nf_conn *ignored_conntrack); |
| 186 | |
| 187 | /* Return conntrack_info and tuple hash for given skb. */ |
| 188 | static inline struct nf_conn * |
| 189 | nf_ct_get(const struct sk_buff *skb, enum ip_conntrack_info *ctinfo) |
| 190 | { |
| 191 | *ctinfo = skb->nfctinfo; |
| 192 | return (struct nf_conn *)skb->nfct; |
| 193 | } |
| 194 | |
| 195 | /* decrement reference count on a conntrack */ |
| 196 | static inline void nf_ct_put(struct nf_conn *ct) |
| 197 | { |
| 198 | NF_CT_ASSERT(ct); |
| 199 | nf_conntrack_put(&ct->ct_general); |
| 200 | } |
| 201 | |
Pablo Neira Ayuso | b9f78f9 | 2006-03-22 13:56:08 -0800 | [diff] [blame] | 202 | /* Protocol module loading */ |
| 203 | extern int nf_ct_l3proto_try_module_get(unsigned short l3proto); |
| 204 | extern void nf_ct_l3proto_module_put(unsigned short l3proto); |
| 205 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 206 | extern struct nf_conntrack_tuple_hash * |
| 207 | __nf_conntrack_find(const struct nf_conntrack_tuple *tuple, |
| 208 | const struct nf_conn *ignored_conntrack); |
| 209 | |
| 210 | extern void nf_conntrack_hash_insert(struct nf_conn *ct); |
| 211 | |
| 212 | extern struct nf_conntrack_expect * |
| 213 | __nf_conntrack_expect_find(const struct nf_conntrack_tuple *tuple); |
| 214 | |
| 215 | extern struct nf_conntrack_expect * |
| 216 | nf_conntrack_expect_find(const struct nf_conntrack_tuple *tuple); |
| 217 | |
| 218 | extern void nf_ct_unlink_expect(struct nf_conntrack_expect *exp); |
| 219 | |
| 220 | extern void nf_ct_remove_expectations(struct nf_conn *ct); |
| 221 | |
| 222 | extern void nf_conntrack_flush(void); |
| 223 | |
| 224 | extern struct nf_conntrack_helper * |
| 225 | nf_ct_helper_find_get( const struct nf_conntrack_tuple *tuple); |
| 226 | extern void nf_ct_helper_put(struct nf_conntrack_helper *helper); |
| 227 | |
| 228 | extern struct nf_conntrack_helper * |
| 229 | __nf_conntrack_helper_find_byname(const char *name); |
| 230 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 231 | extern int nf_ct_invert_tuplepr(struct nf_conntrack_tuple *inverse, |
| 232 | const struct nf_conntrack_tuple *orig); |
| 233 | |
| 234 | extern void __nf_ct_refresh_acct(struct nf_conn *ct, |
| 235 | enum ip_conntrack_info ctinfo, |
| 236 | const struct sk_buff *skb, |
| 237 | unsigned long extra_jiffies, |
| 238 | int do_acct); |
| 239 | |
| 240 | /* Refresh conntrack for this many jiffies and do accounting */ |
| 241 | static inline void nf_ct_refresh_acct(struct nf_conn *ct, |
| 242 | enum ip_conntrack_info ctinfo, |
| 243 | const struct sk_buff *skb, |
| 244 | unsigned long extra_jiffies) |
| 245 | { |
| 246 | __nf_ct_refresh_acct(ct, ctinfo, skb, extra_jiffies, 1); |
| 247 | } |
| 248 | |
| 249 | /* Refresh conntrack for this many jiffies */ |
| 250 | static inline void nf_ct_refresh(struct nf_conn *ct, |
| 251 | const struct sk_buff *skb, |
| 252 | unsigned long extra_jiffies) |
| 253 | { |
| 254 | __nf_ct_refresh_acct(ct, 0, skb, extra_jiffies, 0); |
| 255 | } |
| 256 | |
| 257 | /* These are for NAT. Icky. */ |
| 258 | /* Update TCP window tracking data when NAT mangles the packet */ |
| 259 | extern void nf_conntrack_tcp_update(struct sk_buff *skb, |
| 260 | unsigned int dataoff, |
| 261 | struct nf_conn *conntrack, |
| 262 | int dir); |
| 263 | |
| 264 | /* Call me when a conntrack is destroyed. */ |
| 265 | extern void (*nf_conntrack_destroyed)(struct nf_conn *conntrack); |
| 266 | |
| 267 | /* Fake conntrack entry for untracked connections */ |
| 268 | extern struct nf_conn nf_conntrack_untracked; |
| 269 | |
| 270 | extern int nf_ct_no_defrag; |
| 271 | |
| 272 | /* Iterate over all conntracks: if iter returns true, it's deleted. */ |
| 273 | extern void |
| 274 | nf_ct_iterate_cleanup(int (*iter)(struct nf_conn *i, void *data), void *data); |
| 275 | extern void nf_conntrack_free(struct nf_conn *ct); |
| 276 | extern struct nf_conn * |
| 277 | nf_conntrack_alloc(const struct nf_conntrack_tuple *orig, |
| 278 | const struct nf_conntrack_tuple *repl); |
| 279 | |
| 280 | /* It's confirmed if it is, or has been in the hash table. */ |
| 281 | static inline int nf_ct_is_confirmed(struct nf_conn *ct) |
| 282 | { |
| 283 | return test_bit(IPS_CONFIRMED_BIT, &ct->status); |
| 284 | } |
| 285 | |
| 286 | static inline int nf_ct_is_dying(struct nf_conn *ct) |
| 287 | { |
| 288 | return test_bit(IPS_DYING_BIT, &ct->status); |
| 289 | } |
| 290 | |
| 291 | extern unsigned int nf_conntrack_htable_size; |
Patrick McHardy | 39a27a3 | 2006-05-29 18:23:54 -0700 | [diff] [blame] | 292 | extern int nf_conntrack_checksum; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 293 | |
| 294 | #define NF_CT_STAT_INC(count) (__get_cpu_var(nf_conntrack_stat).count++) |
| 295 | |
| 296 | #ifdef CONFIG_NF_CONNTRACK_EVENTS |
| 297 | #include <linux/notifier.h> |
| 298 | #include <linux/interrupt.h> |
| 299 | |
| 300 | struct nf_conntrack_ecache { |
| 301 | struct nf_conn *ct; |
| 302 | unsigned int events; |
| 303 | }; |
| 304 | DECLARE_PER_CPU(struct nf_conntrack_ecache, nf_conntrack_ecache); |
| 305 | |
| 306 | #define CONNTRACK_ECACHE(x) (__get_cpu_var(nf_conntrack_ecache).x) |
| 307 | |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 308 | extern struct atomic_notifier_head nf_conntrack_chain; |
| 309 | extern struct atomic_notifier_head nf_conntrack_expect_chain; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 310 | |
| 311 | static inline int nf_conntrack_register_notifier(struct notifier_block *nb) |
| 312 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 313 | return atomic_notifier_chain_register(&nf_conntrack_chain, nb); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static inline int nf_conntrack_unregister_notifier(struct notifier_block *nb) |
| 317 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 318 | return atomic_notifier_chain_unregister(&nf_conntrack_chain, nb); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static inline int |
| 322 | nf_conntrack_expect_register_notifier(struct notifier_block *nb) |
| 323 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 324 | return atomic_notifier_chain_register(&nf_conntrack_expect_chain, nb); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | static inline int |
| 328 | nf_conntrack_expect_unregister_notifier(struct notifier_block *nb) |
| 329 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 330 | return atomic_notifier_chain_unregister(&nf_conntrack_expect_chain, |
| 331 | nb); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | extern void nf_ct_deliver_cached_events(const struct nf_conn *ct); |
| 335 | extern void __nf_ct_event_cache_init(struct nf_conn *ct); |
| 336 | |
| 337 | static inline void |
| 338 | nf_conntrack_event_cache(enum ip_conntrack_events event, |
| 339 | const struct sk_buff *skb) |
| 340 | { |
| 341 | struct nf_conn *ct = (struct nf_conn *)skb->nfct; |
| 342 | struct nf_conntrack_ecache *ecache; |
| 343 | |
| 344 | local_bh_disable(); |
| 345 | ecache = &__get_cpu_var(nf_conntrack_ecache); |
| 346 | if (ct != ecache->ct) |
| 347 | __nf_ct_event_cache_init(ct); |
| 348 | ecache->events |= event; |
| 349 | local_bh_enable(); |
| 350 | } |
| 351 | |
| 352 | static inline void nf_conntrack_event(enum ip_conntrack_events event, |
| 353 | struct nf_conn *ct) |
| 354 | { |
| 355 | if (nf_ct_is_confirmed(ct) && !nf_ct_is_dying(ct)) |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 356 | atomic_notifier_call_chain(&nf_conntrack_chain, event, ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static inline void |
| 360 | nf_conntrack_expect_event(enum ip_conntrack_expect_events event, |
| 361 | struct nf_conntrack_expect *exp) |
| 362 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 363 | atomic_notifier_call_chain(&nf_conntrack_expect_chain, event, exp); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 364 | } |
| 365 | #else /* CONFIG_NF_CONNTRACK_EVENTS */ |
| 366 | static inline void nf_conntrack_event_cache(enum ip_conntrack_events event, |
| 367 | const struct sk_buff *skb) {} |
| 368 | static inline void nf_conntrack_event(enum ip_conntrack_events event, |
| 369 | struct nf_conn *ct) {} |
| 370 | static inline void nf_ct_deliver_cached_events(const struct nf_conn *ct) {} |
| 371 | static inline void |
| 372 | nf_conntrack_expect_event(enum ip_conntrack_expect_events event, |
| 373 | struct nf_conntrack_expect *exp) {} |
| 374 | #endif /* CONFIG_NF_CONNTRACK_EVENTS */ |
| 375 | |
| 376 | /* no helper, no nat */ |
| 377 | #define NF_CT_F_BASIC 0 |
| 378 | /* for helper */ |
| 379 | #define NF_CT_F_HELP 1 |
| 380 | /* for nat. */ |
| 381 | #define NF_CT_F_NAT 2 |
| 382 | #define NF_CT_F_NUM 4 |
| 383 | |
| 384 | extern int |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 385 | nf_conntrack_register_cache(u_int32_t features, const char *name, size_t size); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 386 | extern void |
| 387 | nf_conntrack_unregister_cache(u_int32_t features); |
| 388 | |
Harald Welte | dc808fe | 2006-03-20 17:56:32 -0800 | [diff] [blame] | 389 | /* valid combinations: |
| 390 | * basic: nf_conn, nf_conn .. nf_conn_help |
| 391 | * nat: nf_conn .. nf_conn_nat, nf_conn .. nf_conn_nat, nf_conn help |
| 392 | */ |
| 393 | static inline struct nf_conn_help *nfct_help(const struct nf_conn *ct) |
| 394 | { |
| 395 | unsigned int offset = sizeof(struct nf_conn); |
| 396 | |
| 397 | if (!(ct->features & NF_CT_F_HELP)) |
| 398 | return NULL; |
| 399 | |
| 400 | return (struct nf_conn_help *) ((void *)ct + offset); |
| 401 | } |
| 402 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 403 | #endif /* __KERNEL__ */ |
| 404 | #endif /* _NF_CONNTRACK_H */ |