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