nf_conntrack_proto_tcp.c 42 KB

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