ip_conntrack_proto_tcp.c 35 KB

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