Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 2 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 8 | * Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>: |
| 9 | * - Real stateful connection tracking |
| 10 | * - Modified state transitions table |
| 11 | * - Window scaling support added |
| 12 | * - SACK support added |
| 13 | * |
| 14 | * Willy Tarreau: |
| 15 | * - State table bugfixes |
| 16 | * - More robust state changes |
| 17 | * - Tuning timer parameters |
| 18 | * |
| 19 | * 27 Oct 2004: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp> |
| 20 | * - genelized Layer 3 protocol part. |
| 21 | * |
| 22 | * Derived from net/ipv4/netfilter/ip_conntrack_proto_tcp.c |
| 23 | * |
| 24 | * version 2.2 |
| 25 | */ |
| 26 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 27 | #include <linux/types.h> |
| 28 | #include <linux/sched.h> |
| 29 | #include <linux/timer.h> |
| 30 | #include <linux/netfilter.h> |
| 31 | #include <linux/module.h> |
| 32 | #include <linux/in.h> |
| 33 | #include <linux/tcp.h> |
| 34 | #include <linux/spinlock.h> |
| 35 | #include <linux/skbuff.h> |
| 36 | #include <linux/ipv6.h> |
| 37 | #include <net/ip6_checksum.h> |
| 38 | |
| 39 | #include <net/tcp.h> |
| 40 | |
| 41 | #include <linux/netfilter.h> |
| 42 | #include <linux/netfilter_ipv4.h> |
| 43 | #include <linux/netfilter_ipv6.h> |
| 44 | #include <net/netfilter/nf_conntrack.h> |
| 45 | #include <net/netfilter/nf_conntrack_protocol.h> |
| 46 | |
| 47 | #if 0 |
| 48 | #define DEBUGP printk |
| 49 | #define DEBUGP_VARS |
| 50 | #else |
| 51 | #define DEBUGP(format, args...) |
| 52 | #endif |
| 53 | |
| 54 | /* Protects conntrack->proto.tcp */ |
| 55 | static DEFINE_RWLOCK(tcp_lock); |
| 56 | |
| 57 | /* "Be conservative in what you do, |
| 58 | be liberal in what you accept from others." |
| 59 | If it's non-zero, we mark only out of window RST segments as INVALID. */ |
| 60 | int nf_ct_tcp_be_liberal = 0; |
| 61 | |
| 62 | /* When connection is picked up from the middle, how many packets are required |
| 63 | to pass in each direction when we assume we are in sync - if any side uses |
| 64 | window scaling, we lost the game. |
| 65 | If it is set to zero, we disable picking up already established |
| 66 | connections. */ |
| 67 | int nf_ct_tcp_loose = 3; |
| 68 | |
| 69 | /* Max number of the retransmitted packets without receiving an (acceptable) |
| 70 | ACK from the destination. If this number is reached, a shorter timer |
| 71 | will be started. */ |
| 72 | int nf_ct_tcp_max_retrans = 3; |
| 73 | |
| 74 | /* FIXME: Examine ipfilter's timeouts and conntrack transitions more |
| 75 | closely. They're more complex. --RR */ |
| 76 | |
| 77 | static const char *tcp_conntrack_names[] = { |
| 78 | "NONE", |
| 79 | "SYN_SENT", |
| 80 | "SYN_RECV", |
| 81 | "ESTABLISHED", |
| 82 | "FIN_WAIT", |
| 83 | "CLOSE_WAIT", |
| 84 | "LAST_ACK", |
| 85 | "TIME_WAIT", |
| 86 | "CLOSE", |
| 87 | "LISTEN" |
| 88 | }; |
| 89 | |
| 90 | #define SECS * HZ |
| 91 | #define MINS * 60 SECS |
| 92 | #define HOURS * 60 MINS |
| 93 | #define DAYS * 24 HOURS |
| 94 | |
Patrick McHardy | babbdb1 | 2006-01-09 17:48:09 -0800 | [diff] [blame] | 95 | unsigned int nf_ct_tcp_timeout_syn_sent = 2 MINS; |
| 96 | unsigned int nf_ct_tcp_timeout_syn_recv = 60 SECS; |
| 97 | unsigned int nf_ct_tcp_timeout_established = 5 DAYS; |
| 98 | unsigned int nf_ct_tcp_timeout_fin_wait = 2 MINS; |
| 99 | unsigned int nf_ct_tcp_timeout_close_wait = 60 SECS; |
| 100 | unsigned int nf_ct_tcp_timeout_last_ack = 30 SECS; |
| 101 | unsigned int nf_ct_tcp_timeout_time_wait = 2 MINS; |
| 102 | unsigned int nf_ct_tcp_timeout_close = 10 SECS; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 103 | |
| 104 | /* RFC1122 says the R2 limit should be at least 100 seconds. |
| 105 | Linux uses 15 packets as limit, which corresponds |
| 106 | to ~13-30min depending on RTO. */ |
Patrick McHardy | babbdb1 | 2006-01-09 17:48:09 -0800 | [diff] [blame] | 107 | unsigned int nf_ct_tcp_timeout_max_retrans = 5 MINS; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 108 | |
Patrick McHardy | babbdb1 | 2006-01-09 17:48:09 -0800 | [diff] [blame] | 109 | static unsigned int * tcp_timeouts[] |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 110 | = { NULL, /* TCP_CONNTRACK_NONE */ |
| 111 | &nf_ct_tcp_timeout_syn_sent, /* TCP_CONNTRACK_SYN_SENT, */ |
| 112 | &nf_ct_tcp_timeout_syn_recv, /* TCP_CONNTRACK_SYN_RECV, */ |
| 113 | &nf_ct_tcp_timeout_established, /* TCP_CONNTRACK_ESTABLISHED, */ |
| 114 | &nf_ct_tcp_timeout_fin_wait, /* TCP_CONNTRACK_FIN_WAIT, */ |
| 115 | &nf_ct_tcp_timeout_close_wait, /* TCP_CONNTRACK_CLOSE_WAIT, */ |
| 116 | &nf_ct_tcp_timeout_last_ack, /* TCP_CONNTRACK_LAST_ACK, */ |
| 117 | &nf_ct_tcp_timeout_time_wait, /* TCP_CONNTRACK_TIME_WAIT, */ |
| 118 | &nf_ct_tcp_timeout_close, /* TCP_CONNTRACK_CLOSE, */ |
| 119 | NULL, /* TCP_CONNTRACK_LISTEN */ |
| 120 | }; |
| 121 | |
| 122 | #define sNO TCP_CONNTRACK_NONE |
| 123 | #define sSS TCP_CONNTRACK_SYN_SENT |
| 124 | #define sSR TCP_CONNTRACK_SYN_RECV |
| 125 | #define sES TCP_CONNTRACK_ESTABLISHED |
| 126 | #define sFW TCP_CONNTRACK_FIN_WAIT |
| 127 | #define sCW TCP_CONNTRACK_CLOSE_WAIT |
| 128 | #define sLA TCP_CONNTRACK_LAST_ACK |
| 129 | #define sTW TCP_CONNTRACK_TIME_WAIT |
| 130 | #define sCL TCP_CONNTRACK_CLOSE |
| 131 | #define sLI TCP_CONNTRACK_LISTEN |
| 132 | #define sIV TCP_CONNTRACK_MAX |
| 133 | #define sIG TCP_CONNTRACK_IGNORE |
| 134 | |
| 135 | /* What TCP flags are set from RST/SYN/FIN/ACK. */ |
| 136 | enum tcp_bit_set { |
| 137 | TCP_SYN_SET, |
| 138 | TCP_SYNACK_SET, |
| 139 | TCP_FIN_SET, |
| 140 | TCP_ACK_SET, |
| 141 | TCP_RST_SET, |
| 142 | TCP_NONE_SET, |
| 143 | }; |
| 144 | |
| 145 | /* |
| 146 | * The TCP state transition table needs a few words... |
| 147 | * |
| 148 | * We are the man in the middle. All the packets go through us |
| 149 | * but might get lost in transit to the destination. |
| 150 | * It is assumed that the destinations can't receive segments |
| 151 | * we haven't seen. |
| 152 | * |
| 153 | * The checked segment is in window, but our windows are *not* |
| 154 | * equivalent with the ones of the sender/receiver. We always |
| 155 | * try to guess the state of the current sender. |
| 156 | * |
| 157 | * The meaning of the states are: |
| 158 | * |
| 159 | * NONE: initial state |
| 160 | * SYN_SENT: SYN-only packet seen |
| 161 | * SYN_RECV: SYN-ACK packet seen |
| 162 | * ESTABLISHED: ACK packet seen |
| 163 | * FIN_WAIT: FIN packet seen |
| 164 | * CLOSE_WAIT: ACK seen (after FIN) |
| 165 | * LAST_ACK: FIN seen (after FIN) |
| 166 | * TIME_WAIT: last ACK seen |
| 167 | * CLOSE: closed connection |
| 168 | * |
| 169 | * LISTEN state is not used. |
| 170 | * |
| 171 | * Packets marked as IGNORED (sIG): |
| 172 | * if they may be either invalid or valid |
| 173 | * and the receiver may send back a connection |
| 174 | * closing RST or a SYN/ACK. |
| 175 | * |
| 176 | * Packets marked as INVALID (sIV): |
| 177 | * if they are invalid |
| 178 | * or we do not support the request (simultaneous open) |
| 179 | */ |
| 180 | static enum tcp_conntrack tcp_conntracks[2][6][TCP_CONNTRACK_MAX] = { |
| 181 | { |
| 182 | /* ORIGINAL */ |
| 183 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */ |
| 184 | /*syn*/ { sSS, sSS, sIG, sIG, sIG, sIG, sIG, sSS, sSS, sIV }, |
| 185 | /* |
| 186 | * sNO -> sSS Initialize a new connection |
| 187 | * sSS -> sSS Retransmitted SYN |
| 188 | * sSR -> sIG Late retransmitted SYN? |
| 189 | * sES -> sIG Error: SYNs in window outside the SYN_SENT state |
| 190 | * are errors. Receiver will reply with RST |
| 191 | * and close the connection. |
| 192 | * Or we are not in sync and hold a dead connection. |
| 193 | * sFW -> sIG |
| 194 | * sCW -> sIG |
| 195 | * sLA -> sIG |
| 196 | * sTW -> sSS Reopened connection (RFC 1122). |
| 197 | * sCL -> sSS |
| 198 | */ |
| 199 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */ |
| 200 | /*synack*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }, |
| 201 | /* |
| 202 | * A SYN/ACK from the client is always invalid: |
| 203 | * - either it tries to set up a simultaneous open, which is |
| 204 | * not supported; |
| 205 | * - or the firewall has just been inserted between the two hosts |
| 206 | * during the session set-up. The SYN will be retransmitted |
| 207 | * by the true client (or it'll time out). |
| 208 | */ |
| 209 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */ |
| 210 | /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV }, |
| 211 | /* |
| 212 | * sNO -> sIV Too late and no reason to do anything... |
| 213 | * sSS -> sIV Client migth not send FIN in this state: |
| 214 | * we enforce waiting for a SYN/ACK reply first. |
| 215 | * sSR -> sFW Close started. |
| 216 | * sES -> sFW |
| 217 | * sFW -> sLA FIN seen in both directions, waiting for |
| 218 | * the last ACK. |
| 219 | * Migth be a retransmitted FIN as well... |
| 220 | * sCW -> sLA |
| 221 | * sLA -> sLA Retransmitted FIN. Remain in the same state. |
| 222 | * sTW -> sTW |
| 223 | * sCL -> sCL |
| 224 | */ |
| 225 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */ |
| 226 | /*ack*/ { sES, sIV, sES, sES, sCW, sCW, sTW, sTW, sCL, sIV }, |
| 227 | /* |
| 228 | * sNO -> sES Assumed. |
| 229 | * sSS -> sIV ACK is invalid: we haven't seen a SYN/ACK yet. |
| 230 | * sSR -> sES Established state is reached. |
| 231 | * sES -> sES :-) |
| 232 | * sFW -> sCW Normal close request answered by ACK. |
| 233 | * sCW -> sCW |
| 234 | * sLA -> sTW Last ACK detected. |
| 235 | * sTW -> sTW Retransmitted last ACK. Remain in the same state. |
| 236 | * sCL -> sCL |
| 237 | */ |
| 238 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */ |
| 239 | /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV }, |
| 240 | /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV } |
| 241 | }, |
| 242 | { |
| 243 | /* REPLY */ |
| 244 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */ |
| 245 | /*syn*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV }, |
| 246 | /* |
| 247 | * sNO -> sIV Never reached. |
| 248 | * sSS -> sIV Simultaneous open, not supported |
| 249 | * sSR -> sIV Simultaneous open, not supported. |
| 250 | * sES -> sIV Server may not initiate a connection. |
| 251 | * sFW -> sIV |
| 252 | * sCW -> sIV |
| 253 | * sLA -> sIV |
| 254 | * sTW -> sIV Reopened connection, but server may not do it. |
| 255 | * sCL -> sIV |
| 256 | */ |
| 257 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */ |
| 258 | /*synack*/ { sIV, sSR, sSR, sIG, sIG, sIG, sIG, sIG, sIG, sIV }, |
| 259 | /* |
| 260 | * sSS -> sSR Standard open. |
| 261 | * sSR -> sSR Retransmitted SYN/ACK. |
| 262 | * sES -> sIG Late retransmitted SYN/ACK? |
| 263 | * sFW -> sIG Might be SYN/ACK answering ignored SYN |
| 264 | * sCW -> sIG |
| 265 | * sLA -> sIG |
| 266 | * sTW -> sIG |
| 267 | * sCL -> sIG |
| 268 | */ |
| 269 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */ |
| 270 | /*fin*/ { sIV, sIV, sFW, sFW, sLA, sLA, sLA, sTW, sCL, sIV }, |
| 271 | /* |
| 272 | * sSS -> sIV Server might not send FIN in this state. |
| 273 | * sSR -> sFW Close started. |
| 274 | * sES -> sFW |
| 275 | * sFW -> sLA FIN seen in both directions. |
| 276 | * sCW -> sLA |
| 277 | * sLA -> sLA Retransmitted FIN. |
| 278 | * sTW -> sTW |
| 279 | * sCL -> sCL |
| 280 | */ |
| 281 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */ |
Jozsef Kadlecsik | 73f3060 | 2005-12-01 14:28:58 -0800 | [diff] [blame] | 282 | /*ack*/ { sIV, sIG, sSR, sES, sCW, sCW, sTW, sTW, sCL, sIV }, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 283 | /* |
Jozsef Kadlecsik | 73f3060 | 2005-12-01 14:28:58 -0800 | [diff] [blame] | 284 | * sSS -> sIG Might be a half-open connection. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 285 | * sSR -> sSR Might answer late resent SYN. |
| 286 | * sES -> sES :-) |
| 287 | * sFW -> sCW Normal close request answered by ACK. |
| 288 | * sCW -> sCW |
| 289 | * sLA -> sTW Last ACK detected. |
| 290 | * sTW -> sTW Retransmitted last ACK. |
| 291 | * sCL -> sCL |
| 292 | */ |
| 293 | /* sNO, sSS, sSR, sES, sFW, sCW, sLA, sTW, sCL, sLI */ |
| 294 | /*rst*/ { sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL, sIV }, |
| 295 | /*none*/ { sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV, sIV } |
| 296 | } |
| 297 | }; |
| 298 | |
| 299 | static int tcp_pkt_to_tuple(const struct sk_buff *skb, |
| 300 | unsigned int dataoff, |
| 301 | struct nf_conntrack_tuple *tuple) |
| 302 | { |
| 303 | struct tcphdr _hdr, *hp; |
| 304 | |
| 305 | /* Actually only need first 8 bytes. */ |
| 306 | hp = skb_header_pointer(skb, dataoff, 8, &_hdr); |
| 307 | if (hp == NULL) |
| 308 | return 0; |
| 309 | |
| 310 | tuple->src.u.tcp.port = hp->source; |
| 311 | tuple->dst.u.tcp.port = hp->dest; |
| 312 | |
| 313 | return 1; |
| 314 | } |
| 315 | |
| 316 | static int tcp_invert_tuple(struct nf_conntrack_tuple *tuple, |
| 317 | const struct nf_conntrack_tuple *orig) |
| 318 | { |
| 319 | tuple->src.u.tcp.port = orig->dst.u.tcp.port; |
| 320 | tuple->dst.u.tcp.port = orig->src.u.tcp.port; |
| 321 | return 1; |
| 322 | } |
| 323 | |
| 324 | /* Print out the per-protocol part of the tuple. */ |
| 325 | static int tcp_print_tuple(struct seq_file *s, |
| 326 | const struct nf_conntrack_tuple *tuple) |
| 327 | { |
| 328 | return seq_printf(s, "sport=%hu dport=%hu ", |
| 329 | ntohs(tuple->src.u.tcp.port), |
| 330 | ntohs(tuple->dst.u.tcp.port)); |
| 331 | } |
| 332 | |
| 333 | /* Print out the private part of the conntrack. */ |
| 334 | static int tcp_print_conntrack(struct seq_file *s, |
| 335 | const struct nf_conn *conntrack) |
| 336 | { |
| 337 | enum tcp_conntrack state; |
| 338 | |
| 339 | read_lock_bh(&tcp_lock); |
| 340 | state = conntrack->proto.tcp.state; |
| 341 | read_unlock_bh(&tcp_lock); |
| 342 | |
| 343 | return seq_printf(s, "%s ", tcp_conntrack_names[state]); |
| 344 | } |
| 345 | |
| 346 | static unsigned int get_conntrack_index(const struct tcphdr *tcph) |
| 347 | { |
| 348 | if (tcph->rst) return TCP_RST_SET; |
| 349 | else if (tcph->syn) return (tcph->ack ? TCP_SYNACK_SET : TCP_SYN_SET); |
| 350 | else if (tcph->fin) return TCP_FIN_SET; |
| 351 | else if (tcph->ack) return TCP_ACK_SET; |
| 352 | else return TCP_NONE_SET; |
| 353 | } |
| 354 | |
| 355 | /* TCP connection tracking based on 'Real Stateful TCP Packet Filtering |
| 356 | in IP Filter' by Guido van Rooij. |
| 357 | |
| 358 | http://www.nluug.nl/events/sane2000/papers.html |
| 359 | http://www.iae.nl/users/guido/papers/tcp_filtering.ps.gz |
| 360 | |
| 361 | The boundaries and the conditions are changed according to RFC793: |
| 362 | the packet must intersect the window (i.e. segments may be |
| 363 | after the right or before the left edge) and thus receivers may ACK |
| 364 | segments after the right edge of the window. |
| 365 | |
| 366 | td_maxend = max(sack + max(win,1)) seen in reply packets |
| 367 | td_maxwin = max(max(win, 1)) + (sack - ack) seen in sent packets |
| 368 | td_maxwin += seq + len - sender.td_maxend |
| 369 | if seq + len > sender.td_maxend |
| 370 | td_end = max(seq + len) seen in sent packets |
| 371 | |
| 372 | I. Upper bound for valid data: seq <= sender.td_maxend |
| 373 | II. Lower bound for valid data: seq + len >= sender.td_end - receiver.td_maxwin |
| 374 | III. Upper bound for valid ack: sack <= receiver.td_end |
| 375 | IV. Lower bound for valid ack: ack >= receiver.td_end - MAXACKWINDOW |
| 376 | |
| 377 | where sack is the highest right edge of sack block found in the packet. |
| 378 | |
| 379 | The upper bound limit for a valid ack is not ignored - |
| 380 | we doesn't have to deal with fragments. |
| 381 | */ |
| 382 | |
| 383 | static inline __u32 segment_seq_plus_len(__u32 seq, |
| 384 | size_t len, |
| 385 | unsigned int dataoff, |
| 386 | struct tcphdr *tcph) |
| 387 | { |
| 388 | /* XXX Should I use payload length field in IP/IPv6 header ? |
| 389 | * - YK */ |
| 390 | return (seq + len - dataoff - tcph->doff*4 |
| 391 | + (tcph->syn ? 1 : 0) + (tcph->fin ? 1 : 0)); |
| 392 | } |
| 393 | |
| 394 | /* Fixme: what about big packets? */ |
| 395 | #define MAXACKWINCONST 66000 |
| 396 | #define MAXACKWINDOW(sender) \ |
| 397 | ((sender)->td_maxwin > MAXACKWINCONST ? (sender)->td_maxwin \ |
| 398 | : MAXACKWINCONST) |
| 399 | |
| 400 | /* |
| 401 | * Simplified tcp_parse_options routine from tcp_input.c |
| 402 | */ |
| 403 | static void tcp_options(const struct sk_buff *skb, |
| 404 | unsigned int dataoff, |
| 405 | struct tcphdr *tcph, |
| 406 | struct ip_ct_tcp_state *state) |
| 407 | { |
| 408 | unsigned char buff[(15 * 4) - sizeof(struct tcphdr)]; |
| 409 | unsigned char *ptr; |
| 410 | int length = (tcph->doff*4) - sizeof(struct tcphdr); |
| 411 | |
| 412 | if (!length) |
| 413 | return; |
| 414 | |
| 415 | ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr), |
| 416 | length, buff); |
| 417 | BUG_ON(ptr == NULL); |
| 418 | |
| 419 | state->td_scale = |
| 420 | state->flags = 0; |
| 421 | |
| 422 | while (length > 0) { |
| 423 | int opcode=*ptr++; |
| 424 | int opsize; |
| 425 | |
| 426 | switch (opcode) { |
| 427 | case TCPOPT_EOL: |
| 428 | return; |
| 429 | case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */ |
| 430 | length--; |
| 431 | continue; |
| 432 | default: |
| 433 | opsize=*ptr++; |
| 434 | if (opsize < 2) /* "silly options" */ |
| 435 | return; |
| 436 | if (opsize > length) |
| 437 | break; /* don't parse partial options */ |
| 438 | |
| 439 | if (opcode == TCPOPT_SACK_PERM |
| 440 | && opsize == TCPOLEN_SACK_PERM) |
| 441 | state->flags |= IP_CT_TCP_FLAG_SACK_PERM; |
| 442 | else if (opcode == TCPOPT_WINDOW |
| 443 | && opsize == TCPOLEN_WINDOW) { |
| 444 | state->td_scale = *(u_int8_t *)ptr; |
| 445 | |
| 446 | if (state->td_scale > 14) { |
| 447 | /* See RFC1323 */ |
| 448 | state->td_scale = 14; |
| 449 | } |
| 450 | state->flags |= |
| 451 | IP_CT_TCP_FLAG_WINDOW_SCALE; |
| 452 | } |
| 453 | ptr += opsize - 2; |
| 454 | length -= opsize; |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | static void tcp_sack(const struct sk_buff *skb, unsigned int dataoff, |
| 460 | struct tcphdr *tcph, __u32 *sack) |
| 461 | { |
| 462 | unsigned char buff[(15 * 4) - sizeof(struct tcphdr)]; |
| 463 | unsigned char *ptr; |
| 464 | int length = (tcph->doff*4) - sizeof(struct tcphdr); |
| 465 | __u32 tmp; |
| 466 | |
| 467 | if (!length) |
| 468 | return; |
| 469 | |
| 470 | ptr = skb_header_pointer(skb, dataoff + sizeof(struct tcphdr), |
| 471 | length, buff); |
| 472 | BUG_ON(ptr == NULL); |
| 473 | |
| 474 | /* Fast path for timestamp-only option */ |
| 475 | if (length == TCPOLEN_TSTAMP_ALIGNED*4 |
| 476 | && *(__u32 *)ptr == |
| 477 | __constant_ntohl((TCPOPT_NOP << 24) |
| 478 | | (TCPOPT_NOP << 16) |
| 479 | | (TCPOPT_TIMESTAMP << 8) |
| 480 | | TCPOLEN_TIMESTAMP)) |
| 481 | return; |
| 482 | |
| 483 | while (length > 0) { |
| 484 | int opcode = *ptr++; |
| 485 | int opsize, i; |
| 486 | |
| 487 | switch (opcode) { |
| 488 | case TCPOPT_EOL: |
| 489 | return; |
| 490 | case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */ |
| 491 | length--; |
| 492 | continue; |
| 493 | default: |
| 494 | opsize = *ptr++; |
| 495 | if (opsize < 2) /* "silly options" */ |
| 496 | return; |
| 497 | if (opsize > length) |
| 498 | break; /* don't parse partial options */ |
| 499 | |
| 500 | if (opcode == TCPOPT_SACK |
| 501 | && opsize >= (TCPOLEN_SACK_BASE |
| 502 | + TCPOLEN_SACK_PERBLOCK) |
| 503 | && !((opsize - TCPOLEN_SACK_BASE) |
| 504 | % TCPOLEN_SACK_PERBLOCK)) { |
| 505 | for (i = 0; |
| 506 | i < (opsize - TCPOLEN_SACK_BASE); |
| 507 | i += TCPOLEN_SACK_PERBLOCK) { |
| 508 | memcpy(&tmp, (__u32 *)(ptr + i) + 1, |
| 509 | sizeof(__u32)); |
| 510 | tmp = ntohl(tmp); |
| 511 | |
| 512 | if (after(tmp, *sack)) |
| 513 | *sack = tmp; |
| 514 | } |
| 515 | return; |
| 516 | } |
| 517 | ptr += opsize - 2; |
| 518 | length -= opsize; |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | static int tcp_in_window(struct ip_ct_tcp *state, |
| 524 | enum ip_conntrack_dir dir, |
| 525 | unsigned int index, |
| 526 | const struct sk_buff *skb, |
| 527 | unsigned int dataoff, |
| 528 | struct tcphdr *tcph, |
| 529 | int pf) |
| 530 | { |
| 531 | struct ip_ct_tcp_state *sender = &state->seen[dir]; |
| 532 | struct ip_ct_tcp_state *receiver = &state->seen[!dir]; |
| 533 | __u32 seq, ack, sack, end, win, swin; |
| 534 | int res; |
| 535 | |
| 536 | /* |
| 537 | * Get the required data from the packet. |
| 538 | */ |
| 539 | seq = ntohl(tcph->seq); |
| 540 | ack = sack = ntohl(tcph->ack_seq); |
| 541 | win = ntohs(tcph->window); |
| 542 | end = segment_seq_plus_len(seq, skb->len, dataoff, tcph); |
| 543 | |
| 544 | if (receiver->flags & IP_CT_TCP_FLAG_SACK_PERM) |
| 545 | tcp_sack(skb, dataoff, tcph, &sack); |
| 546 | |
| 547 | DEBUGP("tcp_in_window: START\n"); |
| 548 | DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu " |
| 549 | "seq=%u ack=%u sack=%u win=%u end=%u\n", |
| 550 | NIPQUAD(iph->saddr), ntohs(tcph->source), |
| 551 | NIPQUAD(iph->daddr), ntohs(tcph->dest), |
| 552 | seq, ack, sack, win, end); |
| 553 | DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i " |
| 554 | "receiver end=%u maxend=%u maxwin=%u scale=%i\n", |
| 555 | sender->td_end, sender->td_maxend, sender->td_maxwin, |
| 556 | sender->td_scale, |
| 557 | receiver->td_end, receiver->td_maxend, receiver->td_maxwin, |
| 558 | receiver->td_scale); |
| 559 | |
| 560 | if (sender->td_end == 0) { |
| 561 | /* |
| 562 | * Initialize sender data. |
| 563 | */ |
| 564 | if (tcph->syn && tcph->ack) { |
| 565 | /* |
| 566 | * Outgoing SYN-ACK in reply to a SYN. |
| 567 | */ |
| 568 | sender->td_end = |
| 569 | sender->td_maxend = end; |
| 570 | sender->td_maxwin = (win == 0 ? 1 : win); |
| 571 | |
| 572 | tcp_options(skb, dataoff, tcph, sender); |
| 573 | /* |
| 574 | * RFC 1323: |
| 575 | * Both sides must send the Window Scale option |
| 576 | * to enable window scaling in either direction. |
| 577 | */ |
| 578 | if (!(sender->flags & IP_CT_TCP_FLAG_WINDOW_SCALE |
| 579 | && receiver->flags & IP_CT_TCP_FLAG_WINDOW_SCALE)) |
| 580 | sender->td_scale = |
| 581 | receiver->td_scale = 0; |
| 582 | } else { |
| 583 | /* |
| 584 | * We are in the middle of a connection, |
| 585 | * its history is lost for us. |
| 586 | * Let's try to use the data from the packet. |
| 587 | */ |
| 588 | sender->td_end = end; |
| 589 | sender->td_maxwin = (win == 0 ? 1 : win); |
| 590 | sender->td_maxend = end + sender->td_maxwin; |
| 591 | } |
| 592 | } else if (((state->state == TCP_CONNTRACK_SYN_SENT |
| 593 | && dir == IP_CT_DIR_ORIGINAL) |
| 594 | || (state->state == TCP_CONNTRACK_SYN_RECV |
| 595 | && dir == IP_CT_DIR_REPLY)) |
| 596 | && after(end, sender->td_end)) { |
| 597 | /* |
| 598 | * RFC 793: "if a TCP is reinitialized ... then it need |
| 599 | * not wait at all; it must only be sure to use sequence |
| 600 | * numbers larger than those recently used." |
| 601 | */ |
| 602 | sender->td_end = |
| 603 | sender->td_maxend = end; |
| 604 | sender->td_maxwin = (win == 0 ? 1 : win); |
| 605 | |
| 606 | tcp_options(skb, dataoff, tcph, sender); |
| 607 | } |
| 608 | |
| 609 | if (!(tcph->ack)) { |
| 610 | /* |
| 611 | * If there is no ACK, just pretend it was set and OK. |
| 612 | */ |
| 613 | ack = sack = receiver->td_end; |
| 614 | } else if (((tcp_flag_word(tcph) & (TCP_FLAG_ACK|TCP_FLAG_RST)) == |
| 615 | (TCP_FLAG_ACK|TCP_FLAG_RST)) |
| 616 | && (ack == 0)) { |
| 617 | /* |
| 618 | * Broken TCP stacks, that set ACK in RST packets as well |
| 619 | * with zero ack value. |
| 620 | */ |
| 621 | ack = sack = receiver->td_end; |
| 622 | } |
| 623 | |
| 624 | if (seq == end |
| 625 | && (!tcph->rst |
| 626 | || (seq == 0 && state->state == TCP_CONNTRACK_SYN_SENT))) |
| 627 | /* |
| 628 | * Packets contains no data: we assume it is valid |
| 629 | * and check the ack value only. |
| 630 | * However RST segments are always validated by their |
| 631 | * SEQ number, except when seq == 0 (reset sent answering |
| 632 | * SYN. |
| 633 | */ |
| 634 | seq = end = sender->td_end; |
| 635 | |
| 636 | DEBUGP("tcp_in_window: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu " |
| 637 | "seq=%u ack=%u sack =%u win=%u end=%u\n", |
| 638 | NIPQUAD(iph->saddr), ntohs(tcph->source), |
| 639 | NIPQUAD(iph->daddr), ntohs(tcph->dest), |
| 640 | seq, ack, sack, win, end); |
| 641 | DEBUGP("tcp_in_window: sender end=%u maxend=%u maxwin=%u scale=%i " |
| 642 | "receiver end=%u maxend=%u maxwin=%u scale=%i\n", |
| 643 | sender->td_end, sender->td_maxend, sender->td_maxwin, |
| 644 | sender->td_scale, |
| 645 | receiver->td_end, receiver->td_maxend, receiver->td_maxwin, |
| 646 | receiver->td_scale); |
| 647 | |
| 648 | DEBUGP("tcp_in_window: I=%i II=%i III=%i IV=%i\n", |
| 649 | before(seq, sender->td_maxend + 1), |
| 650 | after(end, sender->td_end - receiver->td_maxwin - 1), |
| 651 | before(sack, receiver->td_end + 1), |
| 652 | after(ack, receiver->td_end - MAXACKWINDOW(sender))); |
| 653 | |
| 654 | if (sender->loose || receiver->loose || |
| 655 | (before(seq, sender->td_maxend + 1) && |
| 656 | after(end, sender->td_end - receiver->td_maxwin - 1) && |
| 657 | before(sack, receiver->td_end + 1) && |
| 658 | after(ack, receiver->td_end - MAXACKWINDOW(sender)))) { |
| 659 | /* |
| 660 | * Take into account window scaling (RFC 1323). |
| 661 | */ |
| 662 | if (!tcph->syn) |
| 663 | win <<= sender->td_scale; |
| 664 | |
| 665 | /* |
| 666 | * Update sender data. |
| 667 | */ |
| 668 | swin = win + (sack - ack); |
| 669 | if (sender->td_maxwin < swin) |
| 670 | sender->td_maxwin = swin; |
| 671 | if (after(end, sender->td_end)) |
| 672 | sender->td_end = end; |
| 673 | /* |
| 674 | * Update receiver data. |
| 675 | */ |
| 676 | if (after(end, sender->td_maxend)) |
| 677 | receiver->td_maxwin += end - sender->td_maxend; |
| 678 | if (after(sack + win, receiver->td_maxend - 1)) { |
| 679 | receiver->td_maxend = sack + win; |
| 680 | if (win == 0) |
| 681 | receiver->td_maxend++; |
| 682 | } |
| 683 | |
| 684 | /* |
| 685 | * Check retransmissions. |
| 686 | */ |
| 687 | if (index == TCP_ACK_SET) { |
| 688 | if (state->last_dir == dir |
| 689 | && state->last_seq == seq |
| 690 | && state->last_ack == ack |
| 691 | && state->last_end == end) |
| 692 | state->retrans++; |
| 693 | else { |
| 694 | state->last_dir = dir; |
| 695 | state->last_seq = seq; |
| 696 | state->last_ack = ack; |
| 697 | state->last_end = end; |
| 698 | state->retrans = 0; |
| 699 | } |
| 700 | } |
| 701 | /* |
| 702 | * Close the window of disabled window tracking :-) |
| 703 | */ |
| 704 | if (sender->loose) |
| 705 | sender->loose--; |
| 706 | |
| 707 | res = 1; |
| 708 | } else { |
| 709 | if (LOG_INVALID(IPPROTO_TCP)) |
| 710 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
| 711 | "nf_ct_tcp: %s ", |
| 712 | before(seq, sender->td_maxend + 1) ? |
| 713 | after(end, sender->td_end - receiver->td_maxwin - 1) ? |
| 714 | before(sack, receiver->td_end + 1) ? |
| 715 | after(ack, receiver->td_end - MAXACKWINDOW(sender)) ? "BUG" |
| 716 | : "ACK is under the lower bound (possible overly delayed ACK)" |
| 717 | : "ACK is over the upper bound (ACKed data not seen yet)" |
| 718 | : "SEQ is under the lower bound (already ACKed data retransmitted)" |
| 719 | : "SEQ is over the upper bound (over the window of the receiver)"); |
| 720 | |
| 721 | res = nf_ct_tcp_be_liberal; |
| 722 | } |
| 723 | |
| 724 | DEBUGP("tcp_in_window: res=%i sender end=%u maxend=%u maxwin=%u " |
| 725 | "receiver end=%u maxend=%u maxwin=%u\n", |
| 726 | res, sender->td_end, sender->td_maxend, sender->td_maxwin, |
| 727 | receiver->td_end, receiver->td_maxend, receiver->td_maxwin); |
| 728 | |
| 729 | return res; |
| 730 | } |
| 731 | |
| 732 | #ifdef CONFIG_IP_NF_NAT_NEEDED |
| 733 | /* Update sender->td_end after NAT successfully mangled the packet */ |
| 734 | /* Caller must linearize skb at tcp header. */ |
| 735 | void nf_conntrack_tcp_update(struct sk_buff *skb, |
| 736 | unsigned int dataoff, |
| 737 | struct nf_conn *conntrack, |
| 738 | int dir) |
| 739 | { |
| 740 | struct tcphdr *tcph = (void *)skb->data + dataoff; |
| 741 | __u32 end; |
| 742 | #ifdef DEBUGP_VARS |
| 743 | struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[dir]; |
| 744 | struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[!dir]; |
| 745 | #endif |
| 746 | |
| 747 | end = segment_seq_plus_len(ntohl(tcph->seq), skb->len, dataoff, tcph); |
| 748 | |
| 749 | write_lock_bh(&tcp_lock); |
| 750 | /* |
| 751 | * We have to worry for the ack in the reply packet only... |
| 752 | */ |
| 753 | if (after(end, conntrack->proto.tcp.seen[dir].td_end)) |
| 754 | conntrack->proto.tcp.seen[dir].td_end = end; |
| 755 | conntrack->proto.tcp.last_end = end; |
| 756 | write_unlock_bh(&tcp_lock); |
| 757 | DEBUGP("tcp_update: sender end=%u maxend=%u maxwin=%u scale=%i " |
| 758 | "receiver end=%u maxend=%u maxwin=%u scale=%i\n", |
| 759 | sender->td_end, sender->td_maxend, sender->td_maxwin, |
| 760 | sender->td_scale, |
| 761 | receiver->td_end, receiver->td_maxend, receiver->td_maxwin, |
| 762 | receiver->td_scale); |
| 763 | } |
| 764 | |
| 765 | #endif |
| 766 | |
| 767 | #define TH_FIN 0x01 |
| 768 | #define TH_SYN 0x02 |
| 769 | #define TH_RST 0x04 |
| 770 | #define TH_PUSH 0x08 |
| 771 | #define TH_ACK 0x10 |
| 772 | #define TH_URG 0x20 |
| 773 | #define TH_ECE 0x40 |
| 774 | #define TH_CWR 0x80 |
| 775 | |
| 776 | /* table of valid flag combinations - ECE and CWR are always valid */ |
| 777 | static u8 tcp_valid_flags[(TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG) + 1] = |
| 778 | { |
| 779 | [TH_SYN] = 1, |
| 780 | [TH_SYN|TH_ACK] = 1, |
Vlad Drukker | a2d7222 | 2005-11-12 12:13:14 -0800 | [diff] [blame] | 781 | [TH_SYN|TH_PUSH] = 1, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 782 | [TH_SYN|TH_ACK|TH_PUSH] = 1, |
| 783 | [TH_RST] = 1, |
| 784 | [TH_RST|TH_ACK] = 1, |
| 785 | [TH_RST|TH_ACK|TH_PUSH] = 1, |
| 786 | [TH_FIN|TH_ACK] = 1, |
| 787 | [TH_ACK] = 1, |
| 788 | [TH_ACK|TH_PUSH] = 1, |
| 789 | [TH_ACK|TH_URG] = 1, |
| 790 | [TH_ACK|TH_URG|TH_PUSH] = 1, |
| 791 | [TH_FIN|TH_ACK|TH_PUSH] = 1, |
| 792 | [TH_FIN|TH_ACK|TH_URG] = 1, |
| 793 | [TH_FIN|TH_ACK|TH_URG|TH_PUSH] = 1, |
| 794 | }; |
| 795 | |
| 796 | /* Protect conntrack agaist broken packets. Code taken from ipt_unclean.c. */ |
| 797 | static int tcp_error(struct sk_buff *skb, |
| 798 | unsigned int dataoff, |
| 799 | enum ip_conntrack_info *ctinfo, |
| 800 | int pf, |
Patrick McHardy | 96f6bf8 | 2006-04-06 14:19:24 -0700 | [diff] [blame] | 801 | unsigned int hooknum) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 802 | { |
| 803 | struct tcphdr _tcph, *th; |
| 804 | unsigned int tcplen = skb->len - dataoff; |
| 805 | u_int8_t tcpflags; |
| 806 | |
| 807 | /* Smaller that minimal TCP header? */ |
| 808 | th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph); |
| 809 | if (th == NULL) { |
| 810 | if (LOG_INVALID(IPPROTO_TCP)) |
| 811 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
| 812 | "nf_ct_tcp: short packet "); |
| 813 | return -NF_ACCEPT; |
| 814 | } |
| 815 | |
| 816 | /* Not whole TCP header or malformed packet */ |
| 817 | if (th->doff*4 < sizeof(struct tcphdr) || tcplen < th->doff*4) { |
| 818 | if (LOG_INVALID(IPPROTO_TCP)) |
| 819 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
| 820 | "nf_ct_tcp: truncated/malformed packet "); |
| 821 | return -NF_ACCEPT; |
| 822 | } |
| 823 | |
| 824 | /* Checksum invalid? Ignore. |
| 825 | * We skip checking packets on the outgoing path |
| 826 | * because the semantic of CHECKSUM_HW is different there |
| 827 | * and moreover root might send raw packets. |
| 828 | */ |
| 829 | /* FIXME: Source route IP option packets --RR */ |
Patrick McHardy | 39a27a3 | 2006-05-29 18:23:54 -0700 | [diff] [blame] | 830 | if (nf_conntrack_checksum && |
| 831 | ((pf == PF_INET && hooknum == NF_IP_PRE_ROUTING) || |
| 832 | (pf == PF_INET6 && hooknum == NF_IP6_PRE_ROUTING)) && |
Patrick McHardy | 96f6bf8 | 2006-04-06 14:19:24 -0700 | [diff] [blame] | 833 | nf_checksum(skb, hooknum, dataoff, IPPROTO_TCP, pf)) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 834 | if (LOG_INVALID(IPPROTO_TCP)) |
| 835 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
| 836 | "nf_ct_tcp: bad TCP checksum "); |
| 837 | return -NF_ACCEPT; |
| 838 | } |
| 839 | |
| 840 | /* Check TCP flags. */ |
| 841 | tcpflags = (((u_int8_t *)th)[13] & ~(TH_ECE|TH_CWR)); |
| 842 | if (!tcp_valid_flags[tcpflags]) { |
| 843 | if (LOG_INVALID(IPPROTO_TCP)) |
| 844 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
| 845 | "nf_ct_tcp: invalid TCP flag combination "); |
| 846 | return -NF_ACCEPT; |
| 847 | } |
| 848 | |
| 849 | return NF_ACCEPT; |
| 850 | } |
| 851 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 852 | /* Returns verdict for packet, or -1 for invalid. */ |
| 853 | static int tcp_packet(struct nf_conn *conntrack, |
| 854 | const struct sk_buff *skb, |
| 855 | unsigned int dataoff, |
| 856 | enum ip_conntrack_info ctinfo, |
| 857 | int pf, |
| 858 | unsigned int hooknum) |
| 859 | { |
| 860 | enum tcp_conntrack new_state, old_state; |
| 861 | enum ip_conntrack_dir dir; |
| 862 | struct tcphdr *th, _tcph; |
| 863 | unsigned long timeout; |
| 864 | unsigned int index; |
| 865 | |
| 866 | th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph); |
| 867 | BUG_ON(th == NULL); |
| 868 | |
| 869 | write_lock_bh(&tcp_lock); |
| 870 | old_state = conntrack->proto.tcp.state; |
| 871 | dir = CTINFO2DIR(ctinfo); |
| 872 | index = get_conntrack_index(th); |
| 873 | new_state = tcp_conntracks[dir][index][old_state]; |
| 874 | |
| 875 | switch (new_state) { |
| 876 | case TCP_CONNTRACK_IGNORE: |
Jozsef Kadlecsik | 73f3060 | 2005-12-01 14:28:58 -0800 | [diff] [blame] | 877 | /* Ignored packets: |
| 878 | * |
| 879 | * a) SYN in ORIGINAL |
| 880 | * b) SYN/ACK in REPLY |
| 881 | * c) ACK in reply direction after initial SYN in original. |
| 882 | */ |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 883 | if (index == TCP_SYNACK_SET |
| 884 | && conntrack->proto.tcp.last_index == TCP_SYN_SET |
| 885 | && conntrack->proto.tcp.last_dir != dir |
| 886 | && ntohl(th->ack_seq) == |
| 887 | conntrack->proto.tcp.last_end) { |
| 888 | /* This SYN/ACK acknowledges a SYN that we earlier |
| 889 | * ignored as invalid. This means that the client and |
| 890 | * the server are both in sync, while the firewall is |
| 891 | * not. We kill this session and block the SYN/ACK so |
| 892 | * that the client cannot but retransmit its SYN and |
| 893 | * thus initiate a clean new session. |
| 894 | */ |
| 895 | write_unlock_bh(&tcp_lock); |
| 896 | if (LOG_INVALID(IPPROTO_TCP)) |
| 897 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
| 898 | "nf_ct_tcp: killing out of sync session "); |
| 899 | if (del_timer(&conntrack->timeout)) |
| 900 | conntrack->timeout.function((unsigned long) |
| 901 | conntrack); |
| 902 | return -NF_DROP; |
| 903 | } |
| 904 | conntrack->proto.tcp.last_index = index; |
| 905 | conntrack->proto.tcp.last_dir = dir; |
| 906 | conntrack->proto.tcp.last_seq = ntohl(th->seq); |
| 907 | conntrack->proto.tcp.last_end = |
| 908 | segment_seq_plus_len(ntohl(th->seq), skb->len, dataoff, th); |
| 909 | |
| 910 | write_unlock_bh(&tcp_lock); |
| 911 | if (LOG_INVALID(IPPROTO_TCP)) |
| 912 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
| 913 | "nf_ct_tcp: invalid packed ignored "); |
| 914 | return NF_ACCEPT; |
| 915 | case TCP_CONNTRACK_MAX: |
| 916 | /* Invalid packet */ |
| 917 | DEBUGP("nf_ct_tcp: Invalid dir=%i index=%u ostate=%u\n", |
| 918 | dir, get_conntrack_index(th), |
| 919 | old_state); |
| 920 | write_unlock_bh(&tcp_lock); |
| 921 | if (LOG_INVALID(IPPROTO_TCP)) |
| 922 | nf_log_packet(pf, 0, skb, NULL, NULL, NULL, |
| 923 | "nf_ct_tcp: invalid state "); |
| 924 | return -NF_ACCEPT; |
| 925 | case TCP_CONNTRACK_SYN_SENT: |
| 926 | if (old_state < TCP_CONNTRACK_TIME_WAIT) |
| 927 | break; |
| 928 | if ((conntrack->proto.tcp.seen[dir].flags & |
| 929 | IP_CT_TCP_FLAG_CLOSE_INIT) |
| 930 | || after(ntohl(th->seq), |
| 931 | conntrack->proto.tcp.seen[dir].td_end)) { |
| 932 | /* Attempt to reopen a closed connection. |
| 933 | * Delete this connection and look up again. */ |
| 934 | write_unlock_bh(&tcp_lock); |
| 935 | if (del_timer(&conntrack->timeout)) |
| 936 | conntrack->timeout.function((unsigned long) |
| 937 | conntrack); |
| 938 | return -NF_REPEAT; |
KOVACS Krisztian | 3746a2b | 2005-11-14 15:23:01 -0800 | [diff] [blame] | 939 | } else { |
| 940 | write_unlock_bh(&tcp_lock); |
| 941 | if (LOG_INVALID(IPPROTO_TCP)) |
| 942 | nf_log_packet(pf, 0, skb, NULL, NULL, |
| 943 | NULL, "nf_ct_tcp: invalid SYN"); |
| 944 | return -NF_ACCEPT; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 945 | } |
| 946 | case TCP_CONNTRACK_CLOSE: |
| 947 | if (index == TCP_RST_SET |
Jozsef Kadlecsik | 73f3060 | 2005-12-01 14:28:58 -0800 | [diff] [blame] | 948 | && ((test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status) |
| 949 | && conntrack->proto.tcp.last_index == TCP_SYN_SET) |
| 950 | || (!test_bit(IPS_ASSURED_BIT, &conntrack->status) |
| 951 | && conntrack->proto.tcp.last_index == TCP_ACK_SET)) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 952 | && ntohl(th->ack_seq) == conntrack->proto.tcp.last_end) { |
Adrian Bunk | 93b1fae | 2006-01-10 00:13:33 +0100 | [diff] [blame] | 953 | /* RST sent to invalid SYN or ACK we had let through |
Jozsef Kadlecsik | 73f3060 | 2005-12-01 14:28:58 -0800 | [diff] [blame] | 954 | * at a) and c) above: |
| 955 | * |
| 956 | * a) SYN was in window then |
| 957 | * c) we hold a half-open connection. |
| 958 | * |
| 959 | * Delete our connection entry. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 960 | * We skip window checking, because packet might ACK |
Jozsef Kadlecsik | 73f3060 | 2005-12-01 14:28:58 -0800 | [diff] [blame] | 961 | * segments we ignored. */ |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 962 | goto in_window; |
| 963 | } |
Adrian Bunk | 93b1fae | 2006-01-10 00:13:33 +0100 | [diff] [blame] | 964 | /* Just fall through */ |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 965 | default: |
| 966 | /* Keep compilers happy. */ |
| 967 | break; |
| 968 | } |
| 969 | |
| 970 | if (!tcp_in_window(&conntrack->proto.tcp, dir, index, |
| 971 | skb, dataoff, th, pf)) { |
| 972 | write_unlock_bh(&tcp_lock); |
| 973 | return -NF_ACCEPT; |
| 974 | } |
| 975 | in_window: |
| 976 | /* From now on we have got in-window packets */ |
| 977 | conntrack->proto.tcp.last_index = index; |
| 978 | |
| 979 | DEBUGP("tcp_conntracks: src=%u.%u.%u.%u:%hu dst=%u.%u.%u.%u:%hu " |
| 980 | "syn=%i ack=%i fin=%i rst=%i old=%i new=%i\n", |
| 981 | NIPQUAD(iph->saddr), ntohs(th->source), |
| 982 | NIPQUAD(iph->daddr), ntohs(th->dest), |
| 983 | (th->syn ? 1 : 0), (th->ack ? 1 : 0), |
| 984 | (th->fin ? 1 : 0), (th->rst ? 1 : 0), |
| 985 | old_state, new_state); |
| 986 | |
| 987 | conntrack->proto.tcp.state = new_state; |
| 988 | if (old_state != new_state |
| 989 | && (new_state == TCP_CONNTRACK_FIN_WAIT |
| 990 | || new_state == TCP_CONNTRACK_CLOSE)) |
| 991 | conntrack->proto.tcp.seen[dir].flags |= IP_CT_TCP_FLAG_CLOSE_INIT; |
| 992 | timeout = conntrack->proto.tcp.retrans >= nf_ct_tcp_max_retrans |
| 993 | && *tcp_timeouts[new_state] > nf_ct_tcp_timeout_max_retrans |
| 994 | ? nf_ct_tcp_timeout_max_retrans : *tcp_timeouts[new_state]; |
| 995 | write_unlock_bh(&tcp_lock); |
| 996 | |
| 997 | nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb); |
| 998 | if (new_state != old_state) |
| 999 | nf_conntrack_event_cache(IPCT_PROTOINFO, skb); |
| 1000 | |
| 1001 | if (!test_bit(IPS_SEEN_REPLY_BIT, &conntrack->status)) { |
| 1002 | /* If only reply is a RST, we can consider ourselves not to |
| 1003 | have an established connection: this is a fairly common |
| 1004 | problem case, so we can delete the conntrack |
| 1005 | immediately. --RR */ |
| 1006 | if (th->rst) { |
| 1007 | if (del_timer(&conntrack->timeout)) |
| 1008 | conntrack->timeout.function((unsigned long) |
| 1009 | conntrack); |
| 1010 | return NF_ACCEPT; |
| 1011 | } |
| 1012 | } else if (!test_bit(IPS_ASSURED_BIT, &conntrack->status) |
| 1013 | && (old_state == TCP_CONNTRACK_SYN_RECV |
| 1014 | || old_state == TCP_CONNTRACK_ESTABLISHED) |
| 1015 | && new_state == TCP_CONNTRACK_ESTABLISHED) { |
| 1016 | /* Set ASSURED if we see see valid ack in ESTABLISHED |
| 1017 | after SYN_RECV or a valid answer for a picked up |
| 1018 | connection. */ |
| 1019 | set_bit(IPS_ASSURED_BIT, &conntrack->status); |
| 1020 | nf_conntrack_event_cache(IPCT_STATUS, skb); |
| 1021 | } |
| 1022 | nf_ct_refresh_acct(conntrack, ctinfo, skb, timeout); |
| 1023 | |
| 1024 | return NF_ACCEPT; |
| 1025 | } |
| 1026 | |
| 1027 | /* Called when a new connection for this protocol found. */ |
| 1028 | static int tcp_new(struct nf_conn *conntrack, |
| 1029 | const struct sk_buff *skb, |
| 1030 | unsigned int dataoff) |
| 1031 | { |
| 1032 | enum tcp_conntrack new_state; |
| 1033 | struct tcphdr *th, _tcph; |
| 1034 | #ifdef DEBUGP_VARS |
| 1035 | struct ip_ct_tcp_state *sender = &conntrack->proto.tcp.seen[0]; |
| 1036 | struct ip_ct_tcp_state *receiver = &conntrack->proto.tcp.seen[1]; |
| 1037 | #endif |
| 1038 | |
| 1039 | th = skb_header_pointer(skb, dataoff, sizeof(_tcph), &_tcph); |
| 1040 | BUG_ON(th == NULL); |
| 1041 | |
| 1042 | /* Don't need lock here: this conntrack not in circulation yet */ |
| 1043 | new_state |
| 1044 | = tcp_conntracks[0][get_conntrack_index(th)] |
| 1045 | [TCP_CONNTRACK_NONE]; |
| 1046 | |
| 1047 | /* Invalid: delete conntrack */ |
| 1048 | if (new_state >= TCP_CONNTRACK_MAX) { |
| 1049 | DEBUGP("nf_ct_tcp: invalid new deleting.\n"); |
| 1050 | return 0; |
| 1051 | } |
| 1052 | |
| 1053 | if (new_state == TCP_CONNTRACK_SYN_SENT) { |
| 1054 | /* SYN packet */ |
| 1055 | conntrack->proto.tcp.seen[0].td_end = |
| 1056 | segment_seq_plus_len(ntohl(th->seq), skb->len, |
| 1057 | dataoff, th); |
| 1058 | conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window); |
| 1059 | if (conntrack->proto.tcp.seen[0].td_maxwin == 0) |
| 1060 | conntrack->proto.tcp.seen[0].td_maxwin = 1; |
| 1061 | conntrack->proto.tcp.seen[0].td_maxend = |
| 1062 | conntrack->proto.tcp.seen[0].td_end; |
| 1063 | |
| 1064 | tcp_options(skb, dataoff, th, &conntrack->proto.tcp.seen[0]); |
| 1065 | conntrack->proto.tcp.seen[1].flags = 0; |
| 1066 | conntrack->proto.tcp.seen[0].loose = |
| 1067 | conntrack->proto.tcp.seen[1].loose = 0; |
| 1068 | } else if (nf_ct_tcp_loose == 0) { |
| 1069 | /* Don't try to pick up connections. */ |
| 1070 | return 0; |
| 1071 | } else { |
| 1072 | /* |
| 1073 | * We are in the middle of a connection, |
| 1074 | * its history is lost for us. |
| 1075 | * Let's try to use the data from the packet. |
| 1076 | */ |
| 1077 | conntrack->proto.tcp.seen[0].td_end = |
| 1078 | segment_seq_plus_len(ntohl(th->seq), skb->len, |
| 1079 | dataoff, th); |
| 1080 | conntrack->proto.tcp.seen[0].td_maxwin = ntohs(th->window); |
| 1081 | if (conntrack->proto.tcp.seen[0].td_maxwin == 0) |
| 1082 | conntrack->proto.tcp.seen[0].td_maxwin = 1; |
| 1083 | conntrack->proto.tcp.seen[0].td_maxend = |
| 1084 | conntrack->proto.tcp.seen[0].td_end + |
| 1085 | conntrack->proto.tcp.seen[0].td_maxwin; |
| 1086 | conntrack->proto.tcp.seen[0].td_scale = 0; |
| 1087 | |
| 1088 | /* We assume SACK. Should we assume window scaling too? */ |
| 1089 | conntrack->proto.tcp.seen[0].flags = |
| 1090 | conntrack->proto.tcp.seen[1].flags = IP_CT_TCP_FLAG_SACK_PERM; |
| 1091 | conntrack->proto.tcp.seen[0].loose = |
| 1092 | conntrack->proto.tcp.seen[1].loose = nf_ct_tcp_loose; |
| 1093 | } |
| 1094 | |
| 1095 | conntrack->proto.tcp.seen[1].td_end = 0; |
| 1096 | conntrack->proto.tcp.seen[1].td_maxend = 0; |
| 1097 | conntrack->proto.tcp.seen[1].td_maxwin = 1; |
| 1098 | conntrack->proto.tcp.seen[1].td_scale = 0; |
| 1099 | |
| 1100 | /* tcp_packet will set them */ |
| 1101 | conntrack->proto.tcp.state = TCP_CONNTRACK_NONE; |
| 1102 | conntrack->proto.tcp.last_index = TCP_NONE_SET; |
| 1103 | |
| 1104 | DEBUGP("tcp_new: sender end=%u maxend=%u maxwin=%u scale=%i " |
| 1105 | "receiver end=%u maxend=%u maxwin=%u scale=%i\n", |
| 1106 | sender->td_end, sender->td_maxend, sender->td_maxwin, |
| 1107 | sender->td_scale, |
| 1108 | receiver->td_end, receiver->td_maxend, receiver->td_maxwin, |
| 1109 | receiver->td_scale); |
| 1110 | return 1; |
| 1111 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1112 | |
| 1113 | #if defined(CONFIG_NF_CT_NETLINK) || \ |
| 1114 | defined(CONFIG_NF_CT_NETLINK_MODULE) |
| 1115 | |
| 1116 | #include <linux/netfilter/nfnetlink.h> |
| 1117 | #include <linux/netfilter/nfnetlink_conntrack.h> |
| 1118 | |
| 1119 | static int tcp_to_nfattr(struct sk_buff *skb, struct nfattr *nfa, |
| 1120 | const struct nf_conn *ct) |
| 1121 | { |
| 1122 | struct nfattr *nest_parms; |
| 1123 | |
| 1124 | read_lock_bh(&tcp_lock); |
| 1125 | nest_parms = NFA_NEST(skb, CTA_PROTOINFO_TCP); |
| 1126 | NFA_PUT(skb, CTA_PROTOINFO_TCP_STATE, sizeof(u_int8_t), |
| 1127 | &ct->proto.tcp.state); |
| 1128 | read_unlock_bh(&tcp_lock); |
| 1129 | |
| 1130 | NFA_NEST_END(skb, nest_parms); |
| 1131 | |
| 1132 | return 0; |
| 1133 | |
| 1134 | nfattr_failure: |
| 1135 | read_unlock_bh(&tcp_lock); |
| 1136 | return -1; |
| 1137 | } |
| 1138 | |
| 1139 | static const size_t cta_min_tcp[CTA_PROTOINFO_TCP_MAX] = { |
| 1140 | [CTA_PROTOINFO_TCP_STATE-1] = sizeof(u_int8_t), |
| 1141 | }; |
| 1142 | |
| 1143 | static int nfattr_to_tcp(struct nfattr *cda[], struct nf_conn *ct) |
| 1144 | { |
| 1145 | struct nfattr *attr = cda[CTA_PROTOINFO_TCP-1]; |
| 1146 | struct nfattr *tb[CTA_PROTOINFO_TCP_MAX]; |
| 1147 | |
| 1148 | /* updates could not contain anything about the private |
| 1149 | * protocol info, in that case skip the parsing */ |
| 1150 | if (!attr) |
| 1151 | return 0; |
| 1152 | |
| 1153 | nfattr_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, attr); |
| 1154 | |
| 1155 | if (nfattr_bad_size(tb, CTA_PROTOINFO_TCP_MAX, cta_min_tcp)) |
| 1156 | return -EINVAL; |
| 1157 | |
| 1158 | if (!tb[CTA_PROTOINFO_TCP_STATE-1]) |
| 1159 | return -EINVAL; |
| 1160 | |
| 1161 | write_lock_bh(&tcp_lock); |
| 1162 | ct->proto.tcp.state = |
| 1163 | *(u_int8_t *)NFA_DATA(tb[CTA_PROTOINFO_TCP_STATE-1]); |
| 1164 | write_unlock_bh(&tcp_lock); |
| 1165 | |
| 1166 | return 0; |
| 1167 | } |
| 1168 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1169 | |
| 1170 | struct nf_conntrack_protocol nf_conntrack_protocol_tcp4 = |
| 1171 | { |
| 1172 | .l3proto = PF_INET, |
| 1173 | .proto = IPPROTO_TCP, |
| 1174 | .name = "tcp", |
| 1175 | .pkt_to_tuple = tcp_pkt_to_tuple, |
| 1176 | .invert_tuple = tcp_invert_tuple, |
| 1177 | .print_tuple = tcp_print_tuple, |
| 1178 | .print_conntrack = tcp_print_conntrack, |
| 1179 | .packet = tcp_packet, |
| 1180 | .new = tcp_new, |
Patrick McHardy | 96f6bf8 | 2006-04-06 14:19:24 -0700 | [diff] [blame] | 1181 | .error = tcp_error, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1182 | #if defined(CONFIG_NF_CT_NETLINK) || \ |
| 1183 | defined(CONFIG_NF_CT_NETLINK_MODULE) |
| 1184 | .to_nfattr = tcp_to_nfattr, |
| 1185 | .from_nfattr = nfattr_to_tcp, |
| 1186 | .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr, |
| 1187 | .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple, |
| 1188 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1189 | }; |
| 1190 | |
| 1191 | struct nf_conntrack_protocol nf_conntrack_protocol_tcp6 = |
| 1192 | { |
| 1193 | .l3proto = PF_INET6, |
| 1194 | .proto = IPPROTO_TCP, |
| 1195 | .name = "tcp", |
| 1196 | .pkt_to_tuple = tcp_pkt_to_tuple, |
| 1197 | .invert_tuple = tcp_invert_tuple, |
| 1198 | .print_tuple = tcp_print_tuple, |
| 1199 | .print_conntrack = tcp_print_conntrack, |
| 1200 | .packet = tcp_packet, |
| 1201 | .new = tcp_new, |
Patrick McHardy | 96f6bf8 | 2006-04-06 14:19:24 -0700 | [diff] [blame] | 1202 | .error = tcp_error, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 1203 | #if defined(CONFIG_NF_CT_NETLINK) || \ |
| 1204 | defined(CONFIG_NF_CT_NETLINK_MODULE) |
| 1205 | .to_nfattr = tcp_to_nfattr, |
| 1206 | .from_nfattr = nfattr_to_tcp, |
| 1207 | .tuple_to_nfattr = nf_ct_port_tuple_to_nfattr, |
| 1208 | .nfattr_to_tuple = nf_ct_port_nfattr_to_tuple, |
| 1209 | #endif |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1210 | }; |
| 1211 | |
| 1212 | EXPORT_SYMBOL(nf_conntrack_protocol_tcp4); |
| 1213 | EXPORT_SYMBOL(nf_conntrack_protocol_tcp6); |