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