tcp_ipv4.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Implementation of the Transmission Control Protocol(TCP).
  7. *
  8. * Version: $Id: tcp_ipv4.c,v 1.240 2002/02/01 22:01:04 davem Exp $
  9. *
  10. * IPv4 specific functions
  11. *
  12. *
  13. * code split from:
  14. * linux/ipv4/tcp.c
  15. * linux/ipv4/tcp_input.c
  16. * linux/ipv4/tcp_output.c
  17. *
  18. * See tcp.c for author information
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License
  22. * as published by the Free Software Foundation; either version
  23. * 2 of the License, or (at your option) any later version.
  24. */
  25. /*
  26. * Changes:
  27. * David S. Miller : New socket lookup architecture.
  28. * This code is dedicated to John Dyson.
  29. * David S. Miller : Change semantics of established hash,
  30. * half is devoted to TIME_WAIT sockets
  31. * and the rest go in the other half.
  32. * Andi Kleen : Add support for syncookies and fixed
  33. * some bugs: ip options weren't passed to
  34. * the TCP layer, missed a check for an
  35. * ACK bit.
  36. * Andi Kleen : Implemented fast path mtu discovery.
  37. * Fixed many serious bugs in the
  38. * request_sock handling and moved
  39. * most of it into the af independent code.
  40. * Added tail drop and some other bugfixes.
  41. * Added new listen semantics.
  42. * Mike McLagan : Routing by source
  43. * Juan Jose Ciarlante: ip_dynaddr bits
  44. * Andi Kleen: various fixes.
  45. * Vitaly E. Lavrov : Transparent proxy revived after year
  46. * coma.
  47. * Andi Kleen : Fix new listen.
  48. * Andi Kleen : Fix accept error reporting.
  49. * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
  50. * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
  51. * a single port at the same time.
  52. */
  53. #include <linux/types.h>
  54. #include <linux/fcntl.h>
  55. #include <linux/module.h>
  56. #include <linux/random.h>
  57. #include <linux/cache.h>
  58. #include <linux/jhash.h>
  59. #include <linux/init.h>
  60. #include <linux/times.h>
  61. #include <net/icmp.h>
  62. #include <net/inet_hashtables.h>
  63. #include <net/tcp.h>
  64. #include <net/transp_v6.h>
  65. #include <net/ipv6.h>
  66. #include <net/inet_common.h>
  67. #include <net/timewait_sock.h>
  68. #include <net/xfrm.h>
  69. #include <net/netdma.h>
  70. #include <linux/inet.h>
  71. #include <linux/ipv6.h>
  72. #include <linux/stddef.h>
  73. #include <linux/proc_fs.h>
  74. #include <linux/seq_file.h>
  75. int sysctl_tcp_tw_reuse __read_mostly;
  76. int sysctl_tcp_low_latency __read_mostly;
  77. /* Check TCP sequence numbers in ICMP packets. */
  78. #define ICMP_MIN_LENGTH 8
  79. /* Socket used for sending RSTs */
  80. static struct socket *tcp_socket;
  81. void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb);
  82. struct inet_hashinfo __cacheline_aligned tcp_hashinfo = {
  83. .lhash_lock = __RW_LOCK_UNLOCKED(tcp_hashinfo.lhash_lock),
  84. .lhash_users = ATOMIC_INIT(0),
  85. .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(tcp_hashinfo.lhash_wait),
  86. };
  87. static int tcp_v4_get_port(struct sock *sk, unsigned short snum)
  88. {
  89. return inet_csk_get_port(&tcp_hashinfo, sk, snum,
  90. inet_csk_bind_conflict);
  91. }
  92. static void tcp_v4_hash(struct sock *sk)
  93. {
  94. inet_hash(&tcp_hashinfo, sk);
  95. }
  96. void tcp_unhash(struct sock *sk)
  97. {
  98. inet_unhash(&tcp_hashinfo, sk);
  99. }
  100. static inline __u32 tcp_v4_init_sequence(struct sk_buff *skb)
  101. {
  102. return secure_tcp_sequence_number(skb->nh.iph->daddr,
  103. skb->nh.iph->saddr,
  104. skb->h.th->dest,
  105. skb->h.th->source);
  106. }
  107. int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
  108. {
  109. const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw);
  110. struct tcp_sock *tp = tcp_sk(sk);
  111. /* With PAWS, it is safe from the viewpoint
  112. of data integrity. Even without PAWS it is safe provided sequence
  113. spaces do not overlap i.e. at data rates <= 80Mbit/sec.
  114. Actually, the idea is close to VJ's one, only timestamp cache is
  115. held not per host, but per port pair and TW bucket is used as state
  116. holder.
  117. If TW bucket has been already destroyed we fall back to VJ's scheme
  118. and use initial timestamp retrieved from peer table.
  119. */
  120. if (tcptw->tw_ts_recent_stamp &&
  121. (twp == NULL || (sysctl_tcp_tw_reuse &&
  122. xtime.tv_sec - tcptw->tw_ts_recent_stamp > 1))) {
  123. tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2;
  124. if (tp->write_seq == 0)
  125. tp->write_seq = 1;
  126. tp->rx_opt.ts_recent = tcptw->tw_ts_recent;
  127. tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
  128. sock_hold(sktw);
  129. return 1;
  130. }
  131. return 0;
  132. }
  133. EXPORT_SYMBOL_GPL(tcp_twsk_unique);
  134. /* This will initiate an outgoing connection. */
  135. int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  136. {
  137. struct inet_sock *inet = inet_sk(sk);
  138. struct tcp_sock *tp = tcp_sk(sk);
  139. struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
  140. struct rtable *rt;
  141. __be32 daddr, nexthop;
  142. int tmp;
  143. int err;
  144. if (addr_len < sizeof(struct sockaddr_in))
  145. return -EINVAL;
  146. if (usin->sin_family != AF_INET)
  147. return -EAFNOSUPPORT;
  148. nexthop = daddr = usin->sin_addr.s_addr;
  149. if (inet->opt && inet->opt->srr) {
  150. if (!daddr)
  151. return -EINVAL;
  152. nexthop = inet->opt->faddr;
  153. }
  154. tmp = ip_route_connect(&rt, nexthop, inet->saddr,
  155. RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
  156. IPPROTO_TCP,
  157. inet->sport, usin->sin_port, sk);
  158. if (tmp < 0)
  159. return tmp;
  160. if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
  161. ip_rt_put(rt);
  162. return -ENETUNREACH;
  163. }
  164. if (!inet->opt || !inet->opt->srr)
  165. daddr = rt->rt_dst;
  166. if (!inet->saddr)
  167. inet->saddr = rt->rt_src;
  168. inet->rcv_saddr = inet->saddr;
  169. if (tp->rx_opt.ts_recent_stamp && inet->daddr != daddr) {
  170. /* Reset inherited state */
  171. tp->rx_opt.ts_recent = 0;
  172. tp->rx_opt.ts_recent_stamp = 0;
  173. tp->write_seq = 0;
  174. }
  175. if (tcp_death_row.sysctl_tw_recycle &&
  176. !tp->rx_opt.ts_recent_stamp && rt->rt_dst == daddr) {
  177. struct inet_peer *peer = rt_get_peer(rt);
  178. /* VJ's idea. We save last timestamp seen from
  179. * the destination in peer table, when entering state TIME-WAIT
  180. * and initialize rx_opt.ts_recent from it, when trying new connection.
  181. */
  182. if (peer && peer->tcp_ts_stamp + TCP_PAWS_MSL >= xtime.tv_sec) {
  183. tp->rx_opt.ts_recent_stamp = peer->tcp_ts_stamp;
  184. tp->rx_opt.ts_recent = peer->tcp_ts;
  185. }
  186. }
  187. inet->dport = usin->sin_port;
  188. inet->daddr = daddr;
  189. inet_csk(sk)->icsk_ext_hdr_len = 0;
  190. if (inet->opt)
  191. inet_csk(sk)->icsk_ext_hdr_len = inet->opt->optlen;
  192. tp->rx_opt.mss_clamp = 536;
  193. /* Socket identity is still unknown (sport may be zero).
  194. * However we set state to SYN-SENT and not releasing socket
  195. * lock select source port, enter ourselves into the hash tables and
  196. * complete initialization after this.
  197. */
  198. tcp_set_state(sk, TCP_SYN_SENT);
  199. err = inet_hash_connect(&tcp_death_row, sk);
  200. if (err)
  201. goto failure;
  202. err = ip_route_newports(&rt, IPPROTO_TCP, inet->sport, inet->dport, sk);
  203. if (err)
  204. goto failure;
  205. /* OK, now commit destination to socket. */
  206. sk->sk_gso_type = SKB_GSO_TCPV4;
  207. sk_setup_caps(sk, &rt->u.dst);
  208. if (!tp->write_seq)
  209. tp->write_seq = secure_tcp_sequence_number(inet->saddr,
  210. inet->daddr,
  211. inet->sport,
  212. usin->sin_port);
  213. inet->id = tp->write_seq ^ jiffies;
  214. err = tcp_connect(sk);
  215. rt = NULL;
  216. if (err)
  217. goto failure;
  218. return 0;
  219. failure:
  220. /* This unhashes the socket and releases the local port, if necessary. */
  221. tcp_set_state(sk, TCP_CLOSE);
  222. ip_rt_put(rt);
  223. sk->sk_route_caps = 0;
  224. inet->dport = 0;
  225. return err;
  226. }
  227. /*
  228. * This routine does path mtu discovery as defined in RFC1191.
  229. */
  230. static void do_pmtu_discovery(struct sock *sk, struct iphdr *iph, u32 mtu)
  231. {
  232. struct dst_entry *dst;
  233. struct inet_sock *inet = inet_sk(sk);
  234. /* We are not interested in TCP_LISTEN and open_requests (SYN-ACKs
  235. * send out by Linux are always <576bytes so they should go through
  236. * unfragmented).
  237. */
  238. if (sk->sk_state == TCP_LISTEN)
  239. return;
  240. /* We don't check in the destentry if pmtu discovery is forbidden
  241. * on this route. We just assume that no packet_to_big packets
  242. * are send back when pmtu discovery is not active.
  243. * There is a small race when the user changes this flag in the
  244. * route, but I think that's acceptable.
  245. */
  246. if ((dst = __sk_dst_check(sk, 0)) == NULL)
  247. return;
  248. dst->ops->update_pmtu(dst, mtu);
  249. /* Something is about to be wrong... Remember soft error
  250. * for the case, if this connection will not able to recover.
  251. */
  252. if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
  253. sk->sk_err_soft = EMSGSIZE;
  254. mtu = dst_mtu(dst);
  255. if (inet->pmtudisc != IP_PMTUDISC_DONT &&
  256. inet_csk(sk)->icsk_pmtu_cookie > mtu) {
  257. tcp_sync_mss(sk, mtu);
  258. /* Resend the TCP packet because it's
  259. * clear that the old packet has been
  260. * dropped. This is the new "fast" path mtu
  261. * discovery.
  262. */
  263. tcp_simple_retransmit(sk);
  264. } /* else let the usual retransmit timer handle it */
  265. }
  266. /*
  267. * This routine is called by the ICMP module when it gets some
  268. * sort of error condition. If err < 0 then the socket should
  269. * be closed and the error returned to the user. If err > 0
  270. * it's just the icmp type << 8 | icmp code. After adjustment
  271. * header points to the first 8 bytes of the tcp header. We need
  272. * to find the appropriate port.
  273. *
  274. * The locking strategy used here is very "optimistic". When
  275. * someone else accesses the socket the ICMP is just dropped
  276. * and for some paths there is no check at all.
  277. * A more general error queue to queue errors for later handling
  278. * is probably better.
  279. *
  280. */
  281. void tcp_v4_err(struct sk_buff *skb, u32 info)
  282. {
  283. struct iphdr *iph = (struct iphdr *)skb->data;
  284. struct tcphdr *th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
  285. struct tcp_sock *tp;
  286. struct inet_sock *inet;
  287. int type = skb->h.icmph->type;
  288. int code = skb->h.icmph->code;
  289. struct sock *sk;
  290. __u32 seq;
  291. int err;
  292. if (skb->len < (iph->ihl << 2) + 8) {
  293. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  294. return;
  295. }
  296. sk = inet_lookup(&tcp_hashinfo, iph->daddr, th->dest, iph->saddr,
  297. th->source, inet_iif(skb));
  298. if (!sk) {
  299. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  300. return;
  301. }
  302. if (sk->sk_state == TCP_TIME_WAIT) {
  303. inet_twsk_put(inet_twsk(sk));
  304. return;
  305. }
  306. bh_lock_sock(sk);
  307. /* If too many ICMPs get dropped on busy
  308. * servers this needs to be solved differently.
  309. */
  310. if (sock_owned_by_user(sk))
  311. NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
  312. if (sk->sk_state == TCP_CLOSE)
  313. goto out;
  314. tp = tcp_sk(sk);
  315. seq = ntohl(th->seq);
  316. if (sk->sk_state != TCP_LISTEN &&
  317. !between(seq, tp->snd_una, tp->snd_nxt)) {
  318. NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
  319. goto out;
  320. }
  321. switch (type) {
  322. case ICMP_SOURCE_QUENCH:
  323. /* Just silently ignore these. */
  324. goto out;
  325. case ICMP_PARAMETERPROB:
  326. err = EPROTO;
  327. break;
  328. case ICMP_DEST_UNREACH:
  329. if (code > NR_ICMP_UNREACH)
  330. goto out;
  331. if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
  332. if (!sock_owned_by_user(sk))
  333. do_pmtu_discovery(sk, iph, info);
  334. goto out;
  335. }
  336. err = icmp_err_convert[code].errno;
  337. break;
  338. case ICMP_TIME_EXCEEDED:
  339. err = EHOSTUNREACH;
  340. break;
  341. default:
  342. goto out;
  343. }
  344. switch (sk->sk_state) {
  345. struct request_sock *req, **prev;
  346. case TCP_LISTEN:
  347. if (sock_owned_by_user(sk))
  348. goto out;
  349. req = inet_csk_search_req(sk, &prev, th->dest,
  350. iph->daddr, iph->saddr);
  351. if (!req)
  352. goto out;
  353. /* ICMPs are not backlogged, hence we cannot get
  354. an established socket here.
  355. */
  356. BUG_TRAP(!req->sk);
  357. if (seq != tcp_rsk(req)->snt_isn) {
  358. NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
  359. goto out;
  360. }
  361. /*
  362. * Still in SYN_RECV, just remove it silently.
  363. * There is no good way to pass the error to the newly
  364. * created socket, and POSIX does not want network
  365. * errors returned from accept().
  366. */
  367. inet_csk_reqsk_queue_drop(sk, req, prev);
  368. goto out;
  369. case TCP_SYN_SENT:
  370. case TCP_SYN_RECV: /* Cannot happen.
  371. It can f.e. if SYNs crossed.
  372. */
  373. if (!sock_owned_by_user(sk)) {
  374. sk->sk_err = err;
  375. sk->sk_error_report(sk);
  376. tcp_done(sk);
  377. } else {
  378. sk->sk_err_soft = err;
  379. }
  380. goto out;
  381. }
  382. /* If we've already connected we will keep trying
  383. * until we time out, or the user gives up.
  384. *
  385. * rfc1122 4.2.3.9 allows to consider as hard errors
  386. * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
  387. * but it is obsoleted by pmtu discovery).
  388. *
  389. * Note, that in modern internet, where routing is unreliable
  390. * and in each dark corner broken firewalls sit, sending random
  391. * errors ordered by their masters even this two messages finally lose
  392. * their original sense (even Linux sends invalid PORT_UNREACHs)
  393. *
  394. * Now we are in compliance with RFCs.
  395. * --ANK (980905)
  396. */
  397. inet = inet_sk(sk);
  398. if (!sock_owned_by_user(sk) && inet->recverr) {
  399. sk->sk_err = err;
  400. sk->sk_error_report(sk);
  401. } else { /* Only an error on timeout */
  402. sk->sk_err_soft = err;
  403. }
  404. out:
  405. bh_unlock_sock(sk);
  406. sock_put(sk);
  407. }
  408. /* This routine computes an IPv4 TCP checksum. */
  409. void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
  410. {
  411. struct inet_sock *inet = inet_sk(sk);
  412. struct tcphdr *th = skb->h.th;
  413. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  414. th->check = ~tcp_v4_check(th, len, inet->saddr, inet->daddr, 0);
  415. skb->csum = offsetof(struct tcphdr, check);
  416. } else {
  417. th->check = tcp_v4_check(th, len, inet->saddr, inet->daddr,
  418. csum_partial((char *)th,
  419. th->doff << 2,
  420. skb->csum));
  421. }
  422. }
  423. int tcp_v4_gso_send_check(struct sk_buff *skb)
  424. {
  425. struct iphdr *iph;
  426. struct tcphdr *th;
  427. if (!pskb_may_pull(skb, sizeof(*th)))
  428. return -EINVAL;
  429. iph = skb->nh.iph;
  430. th = skb->h.th;
  431. th->check = 0;
  432. th->check = ~tcp_v4_check(th, skb->len, iph->saddr, iph->daddr, 0);
  433. skb->csum = offsetof(struct tcphdr, check);
  434. skb->ip_summed = CHECKSUM_PARTIAL;
  435. return 0;
  436. }
  437. /*
  438. * This routine will send an RST to the other tcp.
  439. *
  440. * Someone asks: why I NEVER use socket parameters (TOS, TTL etc.)
  441. * for reset.
  442. * Answer: if a packet caused RST, it is not for a socket
  443. * existing in our system, if it is matched to a socket,
  444. * it is just duplicate segment or bug in other side's TCP.
  445. * So that we build reply only basing on parameters
  446. * arrived with segment.
  447. * Exception: precedence violation. We do not implement it in any case.
  448. */
  449. static void tcp_v4_send_reset(struct sk_buff *skb)
  450. {
  451. struct tcphdr *th = skb->h.th;
  452. struct tcphdr rth;
  453. struct ip_reply_arg arg;
  454. /* Never send a reset in response to a reset. */
  455. if (th->rst)
  456. return;
  457. if (((struct rtable *)skb->dst)->rt_type != RTN_LOCAL)
  458. return;
  459. /* Swap the send and the receive. */
  460. memset(&rth, 0, sizeof(struct tcphdr));
  461. rth.dest = th->source;
  462. rth.source = th->dest;
  463. rth.doff = sizeof(struct tcphdr) / 4;
  464. rth.rst = 1;
  465. if (th->ack) {
  466. rth.seq = th->ack_seq;
  467. } else {
  468. rth.ack = 1;
  469. rth.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin +
  470. skb->len - (th->doff << 2));
  471. }
  472. memset(&arg, 0, sizeof arg);
  473. arg.iov[0].iov_base = (unsigned char *)&rth;
  474. arg.iov[0].iov_len = sizeof rth;
  475. arg.csum = csum_tcpudp_nofold(skb->nh.iph->daddr,
  476. skb->nh.iph->saddr, /*XXX*/
  477. sizeof(struct tcphdr), IPPROTO_TCP, 0);
  478. arg.csumoffset = offsetof(struct tcphdr, check) / 2;
  479. ip_send_reply(tcp_socket->sk, skb, &arg, sizeof rth);
  480. TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
  481. TCP_INC_STATS_BH(TCP_MIB_OUTRSTS);
  482. }
  483. /* The code following below sending ACKs in SYN-RECV and TIME-WAIT states
  484. outside socket context is ugly, certainly. What can I do?
  485. */
  486. static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
  487. u32 win, u32 ts)
  488. {
  489. struct tcphdr *th = skb->h.th;
  490. struct {
  491. struct tcphdr th;
  492. u32 tsopt[TCPOLEN_TSTAMP_ALIGNED >> 2];
  493. } rep;
  494. struct ip_reply_arg arg;
  495. memset(&rep.th, 0, sizeof(struct tcphdr));
  496. memset(&arg, 0, sizeof arg);
  497. arg.iov[0].iov_base = (unsigned char *)&rep;
  498. arg.iov[0].iov_len = sizeof(rep.th);
  499. if (ts) {
  500. rep.tsopt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  501. (TCPOPT_TIMESTAMP << 8) |
  502. TCPOLEN_TIMESTAMP);
  503. rep.tsopt[1] = htonl(tcp_time_stamp);
  504. rep.tsopt[2] = htonl(ts);
  505. arg.iov[0].iov_len = sizeof(rep);
  506. }
  507. /* Swap the send and the receive. */
  508. rep.th.dest = th->source;
  509. rep.th.source = th->dest;
  510. rep.th.doff = arg.iov[0].iov_len / 4;
  511. rep.th.seq = htonl(seq);
  512. rep.th.ack_seq = htonl(ack);
  513. rep.th.ack = 1;
  514. rep.th.window = htons(win);
  515. arg.csum = csum_tcpudp_nofold(skb->nh.iph->daddr,
  516. skb->nh.iph->saddr, /*XXX*/
  517. arg.iov[0].iov_len, IPPROTO_TCP, 0);
  518. arg.csumoffset = offsetof(struct tcphdr, check) / 2;
  519. ip_send_reply(tcp_socket->sk, skb, &arg, arg.iov[0].iov_len);
  520. TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
  521. }
  522. static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
  523. {
  524. struct inet_timewait_sock *tw = inet_twsk(sk);
  525. const struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
  526. tcp_v4_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
  527. tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale, tcptw->tw_ts_recent);
  528. inet_twsk_put(tw);
  529. }
  530. static void tcp_v4_reqsk_send_ack(struct sk_buff *skb, struct request_sock *req)
  531. {
  532. tcp_v4_send_ack(skb, tcp_rsk(req)->snt_isn + 1, tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd,
  533. req->ts_recent);
  534. }
  535. /*
  536. * Send a SYN-ACK after having received an ACK.
  537. * This still operates on a request_sock only, not on a big
  538. * socket.
  539. */
  540. static int tcp_v4_send_synack(struct sock *sk, struct request_sock *req,
  541. struct dst_entry *dst)
  542. {
  543. const struct inet_request_sock *ireq = inet_rsk(req);
  544. int err = -1;
  545. struct sk_buff * skb;
  546. /* First, grab a route. */
  547. if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
  548. goto out;
  549. skb = tcp_make_synack(sk, dst, req);
  550. if (skb) {
  551. struct tcphdr *th = skb->h.th;
  552. th->check = tcp_v4_check(th, skb->len,
  553. ireq->loc_addr,
  554. ireq->rmt_addr,
  555. csum_partial((char *)th, skb->len,
  556. skb->csum));
  557. err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
  558. ireq->rmt_addr,
  559. ireq->opt);
  560. if (err == NET_XMIT_CN)
  561. err = 0;
  562. }
  563. out:
  564. dst_release(dst);
  565. return err;
  566. }
  567. /*
  568. * IPv4 request_sock destructor.
  569. */
  570. static void tcp_v4_reqsk_destructor(struct request_sock *req)
  571. {
  572. kfree(inet_rsk(req)->opt);
  573. }
  574. #ifdef CONFIG_SYN_COOKIES
  575. static void syn_flood_warning(struct sk_buff *skb)
  576. {
  577. static unsigned long warntime;
  578. if (time_after(jiffies, (warntime + HZ * 60))) {
  579. warntime = jiffies;
  580. printk(KERN_INFO
  581. "possible SYN flooding on port %d. Sending cookies.\n",
  582. ntohs(skb->h.th->dest));
  583. }
  584. }
  585. #endif
  586. /*
  587. * Save and compile IPv4 options into the request_sock if needed.
  588. */
  589. static struct ip_options *tcp_v4_save_options(struct sock *sk,
  590. struct sk_buff *skb)
  591. {
  592. struct ip_options *opt = &(IPCB(skb)->opt);
  593. struct ip_options *dopt = NULL;
  594. if (opt && opt->optlen) {
  595. int opt_size = optlength(opt);
  596. dopt = kmalloc(opt_size, GFP_ATOMIC);
  597. if (dopt) {
  598. if (ip_options_echo(dopt, skb)) {
  599. kfree(dopt);
  600. dopt = NULL;
  601. }
  602. }
  603. }
  604. return dopt;
  605. }
  606. struct request_sock_ops tcp_request_sock_ops __read_mostly = {
  607. .family = PF_INET,
  608. .obj_size = sizeof(struct tcp_request_sock),
  609. .rtx_syn_ack = tcp_v4_send_synack,
  610. .send_ack = tcp_v4_reqsk_send_ack,
  611. .destructor = tcp_v4_reqsk_destructor,
  612. .send_reset = tcp_v4_send_reset,
  613. };
  614. static struct timewait_sock_ops tcp_timewait_sock_ops = {
  615. .twsk_obj_size = sizeof(struct tcp_timewait_sock),
  616. .twsk_unique = tcp_twsk_unique,
  617. };
  618. int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
  619. {
  620. struct inet_request_sock *ireq;
  621. struct tcp_options_received tmp_opt;
  622. struct request_sock *req;
  623. __be32 saddr = skb->nh.iph->saddr;
  624. __be32 daddr = skb->nh.iph->daddr;
  625. __u32 isn = TCP_SKB_CB(skb)->when;
  626. struct dst_entry *dst = NULL;
  627. #ifdef CONFIG_SYN_COOKIES
  628. int want_cookie = 0;
  629. #else
  630. #define want_cookie 0 /* Argh, why doesn't gcc optimize this :( */
  631. #endif
  632. /* Never answer to SYNs send to broadcast or multicast */
  633. if (((struct rtable *)skb->dst)->rt_flags &
  634. (RTCF_BROADCAST | RTCF_MULTICAST))
  635. goto drop;
  636. /* TW buckets are converted to open requests without
  637. * limitations, they conserve resources and peer is
  638. * evidently real one.
  639. */
  640. if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
  641. #ifdef CONFIG_SYN_COOKIES
  642. if (sysctl_tcp_syncookies) {
  643. want_cookie = 1;
  644. } else
  645. #endif
  646. goto drop;
  647. }
  648. /* Accept backlog is full. If we have already queued enough
  649. * of warm entries in syn queue, drop request. It is better than
  650. * clogging syn queue with openreqs with exponentially increasing
  651. * timeout.
  652. */
  653. if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
  654. goto drop;
  655. req = reqsk_alloc(&tcp_request_sock_ops);
  656. if (!req)
  657. goto drop;
  658. tcp_clear_options(&tmp_opt);
  659. tmp_opt.mss_clamp = 536;
  660. tmp_opt.user_mss = tcp_sk(sk)->rx_opt.user_mss;
  661. tcp_parse_options(skb, &tmp_opt, 0);
  662. if (want_cookie) {
  663. tcp_clear_options(&tmp_opt);
  664. tmp_opt.saw_tstamp = 0;
  665. }
  666. if (tmp_opt.saw_tstamp && !tmp_opt.rcv_tsval) {
  667. /* Some OSes (unknown ones, but I see them on web server, which
  668. * contains information interesting only for windows'
  669. * users) do not send their stamp in SYN. It is easy case.
  670. * We simply do not advertise TS support.
  671. */
  672. tmp_opt.saw_tstamp = 0;
  673. tmp_opt.tstamp_ok = 0;
  674. }
  675. tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
  676. tcp_openreq_init(req, &tmp_opt, skb);
  677. if (security_inet_conn_request(sk, skb, req))
  678. goto drop_and_free;
  679. ireq = inet_rsk(req);
  680. ireq->loc_addr = daddr;
  681. ireq->rmt_addr = saddr;
  682. ireq->opt = tcp_v4_save_options(sk, skb);
  683. if (!want_cookie)
  684. TCP_ECN_create_request(req, skb->h.th);
  685. if (want_cookie) {
  686. #ifdef CONFIG_SYN_COOKIES
  687. syn_flood_warning(skb);
  688. #endif
  689. isn = cookie_v4_init_sequence(sk, skb, &req->mss);
  690. } else if (!isn) {
  691. struct inet_peer *peer = NULL;
  692. /* VJ's idea. We save last timestamp seen
  693. * from the destination in peer table, when entering
  694. * state TIME-WAIT, and check against it before
  695. * accepting new connection request.
  696. *
  697. * If "isn" is not zero, this request hit alive
  698. * timewait bucket, so that all the necessary checks
  699. * are made in the function processing timewait state.
  700. */
  701. if (tmp_opt.saw_tstamp &&
  702. tcp_death_row.sysctl_tw_recycle &&
  703. (dst = inet_csk_route_req(sk, req)) != NULL &&
  704. (peer = rt_get_peer((struct rtable *)dst)) != NULL &&
  705. peer->v4daddr == saddr) {
  706. if (xtime.tv_sec < peer->tcp_ts_stamp + TCP_PAWS_MSL &&
  707. (s32)(peer->tcp_ts - req->ts_recent) >
  708. TCP_PAWS_WINDOW) {
  709. NET_INC_STATS_BH(LINUX_MIB_PAWSPASSIVEREJECTED);
  710. dst_release(dst);
  711. goto drop_and_free;
  712. }
  713. }
  714. /* Kill the following clause, if you dislike this way. */
  715. else if (!sysctl_tcp_syncookies &&
  716. (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
  717. (sysctl_max_syn_backlog >> 2)) &&
  718. (!peer || !peer->tcp_ts_stamp) &&
  719. (!dst || !dst_metric(dst, RTAX_RTT))) {
  720. /* Without syncookies last quarter of
  721. * backlog is filled with destinations,
  722. * proven to be alive.
  723. * It means that we continue to communicate
  724. * to destinations, already remembered
  725. * to the moment of synflood.
  726. */
  727. LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open "
  728. "request from %u.%u.%u.%u/%u\n",
  729. NIPQUAD(saddr),
  730. ntohs(skb->h.th->source));
  731. dst_release(dst);
  732. goto drop_and_free;
  733. }
  734. isn = tcp_v4_init_sequence(skb);
  735. }
  736. tcp_rsk(req)->snt_isn = isn;
  737. if (tcp_v4_send_synack(sk, req, dst))
  738. goto drop_and_free;
  739. if (want_cookie) {
  740. reqsk_free(req);
  741. } else {
  742. inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
  743. }
  744. return 0;
  745. drop_and_free:
  746. reqsk_free(req);
  747. drop:
  748. return 0;
  749. }
  750. /*
  751. * The three way handshake has completed - we got a valid synack -
  752. * now create the new socket.
  753. */
  754. struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
  755. struct request_sock *req,
  756. struct dst_entry *dst)
  757. {
  758. struct inet_request_sock *ireq;
  759. struct inet_sock *newinet;
  760. struct tcp_sock *newtp;
  761. struct sock *newsk;
  762. if (sk_acceptq_is_full(sk))
  763. goto exit_overflow;
  764. if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
  765. goto exit;
  766. newsk = tcp_create_openreq_child(sk, req, skb);
  767. if (!newsk)
  768. goto exit;
  769. newsk->sk_gso_type = SKB_GSO_TCPV4;
  770. sk_setup_caps(newsk, dst);
  771. newtp = tcp_sk(newsk);
  772. newinet = inet_sk(newsk);
  773. ireq = inet_rsk(req);
  774. newinet->daddr = ireq->rmt_addr;
  775. newinet->rcv_saddr = ireq->loc_addr;
  776. newinet->saddr = ireq->loc_addr;
  777. newinet->opt = ireq->opt;
  778. ireq->opt = NULL;
  779. newinet->mc_index = inet_iif(skb);
  780. newinet->mc_ttl = skb->nh.iph->ttl;
  781. inet_csk(newsk)->icsk_ext_hdr_len = 0;
  782. if (newinet->opt)
  783. inet_csk(newsk)->icsk_ext_hdr_len = newinet->opt->optlen;
  784. newinet->id = newtp->write_seq ^ jiffies;
  785. tcp_mtup_init(newsk);
  786. tcp_sync_mss(newsk, dst_mtu(dst));
  787. newtp->advmss = dst_metric(dst, RTAX_ADVMSS);
  788. tcp_initialize_rcv_mss(newsk);
  789. __inet_hash(&tcp_hashinfo, newsk, 0);
  790. __inet_inherit_port(&tcp_hashinfo, sk, newsk);
  791. return newsk;
  792. exit_overflow:
  793. NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
  794. exit:
  795. NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
  796. dst_release(dst);
  797. return NULL;
  798. }
  799. static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
  800. {
  801. struct tcphdr *th = skb->h.th;
  802. struct iphdr *iph = skb->nh.iph;
  803. struct sock *nsk;
  804. struct request_sock **prev;
  805. /* Find possible connection requests. */
  806. struct request_sock *req = inet_csk_search_req(sk, &prev, th->source,
  807. iph->saddr, iph->daddr);
  808. if (req)
  809. return tcp_check_req(sk, skb, req, prev);
  810. nsk = inet_lookup_established(&tcp_hashinfo, skb->nh.iph->saddr,
  811. th->source, skb->nh.iph->daddr,
  812. th->dest, inet_iif(skb));
  813. if (nsk) {
  814. if (nsk->sk_state != TCP_TIME_WAIT) {
  815. bh_lock_sock(nsk);
  816. return nsk;
  817. }
  818. inet_twsk_put(inet_twsk(nsk));
  819. return NULL;
  820. }
  821. #ifdef CONFIG_SYN_COOKIES
  822. if (!th->rst && !th->syn && th->ack)
  823. sk = cookie_v4_check(sk, skb, &(IPCB(skb)->opt));
  824. #endif
  825. return sk;
  826. }
  827. static int tcp_v4_checksum_init(struct sk_buff *skb)
  828. {
  829. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  830. if (!tcp_v4_check(skb->h.th, skb->len, skb->nh.iph->saddr,
  831. skb->nh.iph->daddr, skb->csum)) {
  832. skb->ip_summed = CHECKSUM_UNNECESSARY;
  833. return 0;
  834. }
  835. }
  836. skb->csum = csum_tcpudp_nofold(skb->nh.iph->saddr, skb->nh.iph->daddr,
  837. skb->len, IPPROTO_TCP, 0);
  838. if (skb->len <= 76) {
  839. return __skb_checksum_complete(skb);
  840. }
  841. return 0;
  842. }
  843. /* The socket must have it's spinlock held when we get
  844. * here.
  845. *
  846. * We have a potential double-lock case here, so even when
  847. * doing backlog processing we use the BH locking scheme.
  848. * This is because we cannot sleep with the original spinlock
  849. * held.
  850. */
  851. int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
  852. {
  853. if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
  854. TCP_CHECK_TIMER(sk);
  855. if (tcp_rcv_established(sk, skb, skb->h.th, skb->len))
  856. goto reset;
  857. TCP_CHECK_TIMER(sk);
  858. return 0;
  859. }
  860. if (skb->len < (skb->h.th->doff << 2) || tcp_checksum_complete(skb))
  861. goto csum_err;
  862. if (sk->sk_state == TCP_LISTEN) {
  863. struct sock *nsk = tcp_v4_hnd_req(sk, skb);
  864. if (!nsk)
  865. goto discard;
  866. if (nsk != sk) {
  867. if (tcp_child_process(sk, nsk, skb))
  868. goto reset;
  869. return 0;
  870. }
  871. }
  872. TCP_CHECK_TIMER(sk);
  873. if (tcp_rcv_state_process(sk, skb, skb->h.th, skb->len))
  874. goto reset;
  875. TCP_CHECK_TIMER(sk);
  876. return 0;
  877. reset:
  878. tcp_v4_send_reset(skb);
  879. discard:
  880. kfree_skb(skb);
  881. /* Be careful here. If this function gets more complicated and
  882. * gcc suffers from register pressure on the x86, sk (in %ebx)
  883. * might be destroyed here. This current version compiles correctly,
  884. * but you have been warned.
  885. */
  886. return 0;
  887. csum_err:
  888. TCP_INC_STATS_BH(TCP_MIB_INERRS);
  889. goto discard;
  890. }
  891. /*
  892. * From tcp_input.c
  893. */
  894. int tcp_v4_rcv(struct sk_buff *skb)
  895. {
  896. struct tcphdr *th;
  897. struct sock *sk;
  898. int ret;
  899. if (skb->pkt_type != PACKET_HOST)
  900. goto discard_it;
  901. /* Count it even if it's bad */
  902. TCP_INC_STATS_BH(TCP_MIB_INSEGS);
  903. if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
  904. goto discard_it;
  905. th = skb->h.th;
  906. if (th->doff < sizeof(struct tcphdr) / 4)
  907. goto bad_packet;
  908. if (!pskb_may_pull(skb, th->doff * 4))
  909. goto discard_it;
  910. /* An explanation is required here, I think.
  911. * Packet length and doff are validated by header prediction,
  912. * provided case of th->doff==0 is eliminated.
  913. * So, we defer the checks. */
  914. if ((skb->ip_summed != CHECKSUM_UNNECESSARY &&
  915. tcp_v4_checksum_init(skb)))
  916. goto bad_packet;
  917. th = skb->h.th;
  918. TCP_SKB_CB(skb)->seq = ntohl(th->seq);
  919. TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
  920. skb->len - th->doff * 4);
  921. TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
  922. TCP_SKB_CB(skb)->when = 0;
  923. TCP_SKB_CB(skb)->flags = skb->nh.iph->tos;
  924. TCP_SKB_CB(skb)->sacked = 0;
  925. sk = __inet_lookup(&tcp_hashinfo, skb->nh.iph->saddr, th->source,
  926. skb->nh.iph->daddr, th->dest,
  927. inet_iif(skb));
  928. if (!sk)
  929. goto no_tcp_socket;
  930. process:
  931. if (sk->sk_state == TCP_TIME_WAIT)
  932. goto do_time_wait;
  933. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  934. goto discard_and_relse;
  935. nf_reset(skb);
  936. if (sk_filter(sk, skb))
  937. goto discard_and_relse;
  938. skb->dev = NULL;
  939. bh_lock_sock_nested(sk);
  940. ret = 0;
  941. if (!sock_owned_by_user(sk)) {
  942. #ifdef CONFIG_NET_DMA
  943. struct tcp_sock *tp = tcp_sk(sk);
  944. if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
  945. tp->ucopy.dma_chan = get_softnet_dma();
  946. if (tp->ucopy.dma_chan)
  947. ret = tcp_v4_do_rcv(sk, skb);
  948. else
  949. #endif
  950. {
  951. if (!tcp_prequeue(sk, skb))
  952. ret = tcp_v4_do_rcv(sk, skb);
  953. }
  954. } else
  955. sk_add_backlog(sk, skb);
  956. bh_unlock_sock(sk);
  957. sock_put(sk);
  958. return ret;
  959. no_tcp_socket:
  960. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  961. goto discard_it;
  962. if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
  963. bad_packet:
  964. TCP_INC_STATS_BH(TCP_MIB_INERRS);
  965. } else {
  966. tcp_v4_send_reset(skb);
  967. }
  968. discard_it:
  969. /* Discard frame. */
  970. kfree_skb(skb);
  971. return 0;
  972. discard_and_relse:
  973. sock_put(sk);
  974. goto discard_it;
  975. do_time_wait:
  976. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  977. inet_twsk_put(inet_twsk(sk));
  978. goto discard_it;
  979. }
  980. if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
  981. TCP_INC_STATS_BH(TCP_MIB_INERRS);
  982. inet_twsk_put(inet_twsk(sk));
  983. goto discard_it;
  984. }
  985. switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
  986. case TCP_TW_SYN: {
  987. struct sock *sk2 = inet_lookup_listener(&tcp_hashinfo,
  988. skb->nh.iph->daddr,
  989. th->dest,
  990. inet_iif(skb));
  991. if (sk2) {
  992. inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row);
  993. inet_twsk_put(inet_twsk(sk));
  994. sk = sk2;
  995. goto process;
  996. }
  997. /* Fall through to ACK */
  998. }
  999. case TCP_TW_ACK:
  1000. tcp_v4_timewait_ack(sk, skb);
  1001. break;
  1002. case TCP_TW_RST:
  1003. goto no_tcp_socket;
  1004. case TCP_TW_SUCCESS:;
  1005. }
  1006. goto discard_it;
  1007. }
  1008. /* VJ's idea. Save last timestamp seen from this destination
  1009. * and hold it at least for normal timewait interval to use for duplicate
  1010. * segment detection in subsequent connections, before they enter synchronized
  1011. * state.
  1012. */
  1013. int tcp_v4_remember_stamp(struct sock *sk)
  1014. {
  1015. struct inet_sock *inet = inet_sk(sk);
  1016. struct tcp_sock *tp = tcp_sk(sk);
  1017. struct rtable *rt = (struct rtable *)__sk_dst_get(sk);
  1018. struct inet_peer *peer = NULL;
  1019. int release_it = 0;
  1020. if (!rt || rt->rt_dst != inet->daddr) {
  1021. peer = inet_getpeer(inet->daddr, 1);
  1022. release_it = 1;
  1023. } else {
  1024. if (!rt->peer)
  1025. rt_bind_peer(rt, 1);
  1026. peer = rt->peer;
  1027. }
  1028. if (peer) {
  1029. if ((s32)(peer->tcp_ts - tp->rx_opt.ts_recent) <= 0 ||
  1030. (peer->tcp_ts_stamp + TCP_PAWS_MSL < xtime.tv_sec &&
  1031. peer->tcp_ts_stamp <= tp->rx_opt.ts_recent_stamp)) {
  1032. peer->tcp_ts_stamp = tp->rx_opt.ts_recent_stamp;
  1033. peer->tcp_ts = tp->rx_opt.ts_recent;
  1034. }
  1035. if (release_it)
  1036. inet_putpeer(peer);
  1037. return 1;
  1038. }
  1039. return 0;
  1040. }
  1041. int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw)
  1042. {
  1043. struct inet_peer *peer = inet_getpeer(tw->tw_daddr, 1);
  1044. if (peer) {
  1045. const struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
  1046. if ((s32)(peer->tcp_ts - tcptw->tw_ts_recent) <= 0 ||
  1047. (peer->tcp_ts_stamp + TCP_PAWS_MSL < xtime.tv_sec &&
  1048. peer->tcp_ts_stamp <= tcptw->tw_ts_recent_stamp)) {
  1049. peer->tcp_ts_stamp = tcptw->tw_ts_recent_stamp;
  1050. peer->tcp_ts = tcptw->tw_ts_recent;
  1051. }
  1052. inet_putpeer(peer);
  1053. return 1;
  1054. }
  1055. return 0;
  1056. }
  1057. struct inet_connection_sock_af_ops ipv4_specific = {
  1058. .queue_xmit = ip_queue_xmit,
  1059. .send_check = tcp_v4_send_check,
  1060. .rebuild_header = inet_sk_rebuild_header,
  1061. .conn_request = tcp_v4_conn_request,
  1062. .syn_recv_sock = tcp_v4_syn_recv_sock,
  1063. .remember_stamp = tcp_v4_remember_stamp,
  1064. .net_header_len = sizeof(struct iphdr),
  1065. .setsockopt = ip_setsockopt,
  1066. .getsockopt = ip_getsockopt,
  1067. .addr2sockaddr = inet_csk_addr2sockaddr,
  1068. .sockaddr_len = sizeof(struct sockaddr_in),
  1069. #ifdef CONFIG_COMPAT
  1070. .compat_setsockopt = compat_ip_setsockopt,
  1071. .compat_getsockopt = compat_ip_getsockopt,
  1072. #endif
  1073. };
  1074. /* NOTE: A lot of things set to zero explicitly by call to
  1075. * sk_alloc() so need not be done here.
  1076. */
  1077. static int tcp_v4_init_sock(struct sock *sk)
  1078. {
  1079. struct inet_connection_sock *icsk = inet_csk(sk);
  1080. struct tcp_sock *tp = tcp_sk(sk);
  1081. skb_queue_head_init(&tp->out_of_order_queue);
  1082. tcp_init_xmit_timers(sk);
  1083. tcp_prequeue_init(tp);
  1084. icsk->icsk_rto = TCP_TIMEOUT_INIT;
  1085. tp->mdev = TCP_TIMEOUT_INIT;
  1086. /* So many TCP implementations out there (incorrectly) count the
  1087. * initial SYN frame in their delayed-ACK and congestion control
  1088. * algorithms that we must have the following bandaid to talk
  1089. * efficiently to them. -DaveM
  1090. */
  1091. tp->snd_cwnd = 2;
  1092. /* See draft-stevens-tcpca-spec-01 for discussion of the
  1093. * initialization of these values.
  1094. */
  1095. tp->snd_ssthresh = 0x7fffffff; /* Infinity */
  1096. tp->snd_cwnd_clamp = ~0;
  1097. tp->mss_cache = 536;
  1098. tp->reordering = sysctl_tcp_reordering;
  1099. icsk->icsk_ca_ops = &tcp_init_congestion_ops;
  1100. sk->sk_state = TCP_CLOSE;
  1101. sk->sk_write_space = sk_stream_write_space;
  1102. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  1103. icsk->icsk_af_ops = &ipv4_specific;
  1104. icsk->icsk_sync_mss = tcp_sync_mss;
  1105. sk->sk_sndbuf = sysctl_tcp_wmem[1];
  1106. sk->sk_rcvbuf = sysctl_tcp_rmem[1];
  1107. atomic_inc(&tcp_sockets_allocated);
  1108. return 0;
  1109. }
  1110. int tcp_v4_destroy_sock(struct sock *sk)
  1111. {
  1112. struct tcp_sock *tp = tcp_sk(sk);
  1113. tcp_clear_xmit_timers(sk);
  1114. tcp_cleanup_congestion_control(sk);
  1115. /* Cleanup up the write buffer. */
  1116. sk_stream_writequeue_purge(sk);
  1117. /* Cleans up our, hopefully empty, out_of_order_queue. */
  1118. __skb_queue_purge(&tp->out_of_order_queue);
  1119. #ifdef CONFIG_NET_DMA
  1120. /* Cleans up our sk_async_wait_queue */
  1121. __skb_queue_purge(&sk->sk_async_wait_queue);
  1122. #endif
  1123. /* Clean prequeue, it must be empty really */
  1124. __skb_queue_purge(&tp->ucopy.prequeue);
  1125. /* Clean up a referenced TCP bind bucket. */
  1126. if (inet_csk(sk)->icsk_bind_hash)
  1127. inet_put_port(&tcp_hashinfo, sk);
  1128. /*
  1129. * If sendmsg cached page exists, toss it.
  1130. */
  1131. if (sk->sk_sndmsg_page) {
  1132. __free_page(sk->sk_sndmsg_page);
  1133. sk->sk_sndmsg_page = NULL;
  1134. }
  1135. atomic_dec(&tcp_sockets_allocated);
  1136. return 0;
  1137. }
  1138. EXPORT_SYMBOL(tcp_v4_destroy_sock);
  1139. #ifdef CONFIG_PROC_FS
  1140. /* Proc filesystem TCP sock list dumping. */
  1141. static inline struct inet_timewait_sock *tw_head(struct hlist_head *head)
  1142. {
  1143. return hlist_empty(head) ? NULL :
  1144. list_entry(head->first, struct inet_timewait_sock, tw_node);
  1145. }
  1146. static inline struct inet_timewait_sock *tw_next(struct inet_timewait_sock *tw)
  1147. {
  1148. return tw->tw_node.next ?
  1149. hlist_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
  1150. }
  1151. static void *listening_get_next(struct seq_file *seq, void *cur)
  1152. {
  1153. struct inet_connection_sock *icsk;
  1154. struct hlist_node *node;
  1155. struct sock *sk = cur;
  1156. struct tcp_iter_state* st = seq->private;
  1157. if (!sk) {
  1158. st->bucket = 0;
  1159. sk = sk_head(&tcp_hashinfo.listening_hash[0]);
  1160. goto get_sk;
  1161. }
  1162. ++st->num;
  1163. if (st->state == TCP_SEQ_STATE_OPENREQ) {
  1164. struct request_sock *req = cur;
  1165. icsk = inet_csk(st->syn_wait_sk);
  1166. req = req->dl_next;
  1167. while (1) {
  1168. while (req) {
  1169. if (req->rsk_ops->family == st->family) {
  1170. cur = req;
  1171. goto out;
  1172. }
  1173. req = req->dl_next;
  1174. }
  1175. if (++st->sbucket >= icsk->icsk_accept_queue.listen_opt->nr_table_entries)
  1176. break;
  1177. get_req:
  1178. req = icsk->icsk_accept_queue.listen_opt->syn_table[st->sbucket];
  1179. }
  1180. sk = sk_next(st->syn_wait_sk);
  1181. st->state = TCP_SEQ_STATE_LISTENING;
  1182. read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1183. } else {
  1184. icsk = inet_csk(sk);
  1185. read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1186. if (reqsk_queue_len(&icsk->icsk_accept_queue))
  1187. goto start_req;
  1188. read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1189. sk = sk_next(sk);
  1190. }
  1191. get_sk:
  1192. sk_for_each_from(sk, node) {
  1193. if (sk->sk_family == st->family) {
  1194. cur = sk;
  1195. goto out;
  1196. }
  1197. icsk = inet_csk(sk);
  1198. read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1199. if (reqsk_queue_len(&icsk->icsk_accept_queue)) {
  1200. start_req:
  1201. st->uid = sock_i_uid(sk);
  1202. st->syn_wait_sk = sk;
  1203. st->state = TCP_SEQ_STATE_OPENREQ;
  1204. st->sbucket = 0;
  1205. goto get_req;
  1206. }
  1207. read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1208. }
  1209. if (++st->bucket < INET_LHTABLE_SIZE) {
  1210. sk = sk_head(&tcp_hashinfo.listening_hash[st->bucket]);
  1211. goto get_sk;
  1212. }
  1213. cur = NULL;
  1214. out:
  1215. return cur;
  1216. }
  1217. static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
  1218. {
  1219. void *rc = listening_get_next(seq, NULL);
  1220. while (rc && *pos) {
  1221. rc = listening_get_next(seq, rc);
  1222. --*pos;
  1223. }
  1224. return rc;
  1225. }
  1226. static void *established_get_first(struct seq_file *seq)
  1227. {
  1228. struct tcp_iter_state* st = seq->private;
  1229. void *rc = NULL;
  1230. for (st->bucket = 0; st->bucket < tcp_hashinfo.ehash_size; ++st->bucket) {
  1231. struct sock *sk;
  1232. struct hlist_node *node;
  1233. struct inet_timewait_sock *tw;
  1234. /* We can reschedule _before_ having picked the target: */
  1235. cond_resched_softirq();
  1236. read_lock(&tcp_hashinfo.ehash[st->bucket].lock);
  1237. sk_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
  1238. if (sk->sk_family != st->family) {
  1239. continue;
  1240. }
  1241. rc = sk;
  1242. goto out;
  1243. }
  1244. st->state = TCP_SEQ_STATE_TIME_WAIT;
  1245. inet_twsk_for_each(tw, node,
  1246. &tcp_hashinfo.ehash[st->bucket + tcp_hashinfo.ehash_size].chain) {
  1247. if (tw->tw_family != st->family) {
  1248. continue;
  1249. }
  1250. rc = tw;
  1251. goto out;
  1252. }
  1253. read_unlock(&tcp_hashinfo.ehash[st->bucket].lock);
  1254. st->state = TCP_SEQ_STATE_ESTABLISHED;
  1255. }
  1256. out:
  1257. return rc;
  1258. }
  1259. static void *established_get_next(struct seq_file *seq, void *cur)
  1260. {
  1261. struct sock *sk = cur;
  1262. struct inet_timewait_sock *tw;
  1263. struct hlist_node *node;
  1264. struct tcp_iter_state* st = seq->private;
  1265. ++st->num;
  1266. if (st->state == TCP_SEQ_STATE_TIME_WAIT) {
  1267. tw = cur;
  1268. tw = tw_next(tw);
  1269. get_tw:
  1270. while (tw && tw->tw_family != st->family) {
  1271. tw = tw_next(tw);
  1272. }
  1273. if (tw) {
  1274. cur = tw;
  1275. goto out;
  1276. }
  1277. read_unlock(&tcp_hashinfo.ehash[st->bucket].lock);
  1278. st->state = TCP_SEQ_STATE_ESTABLISHED;
  1279. /* We can reschedule between buckets: */
  1280. cond_resched_softirq();
  1281. if (++st->bucket < tcp_hashinfo.ehash_size) {
  1282. read_lock(&tcp_hashinfo.ehash[st->bucket].lock);
  1283. sk = sk_head(&tcp_hashinfo.ehash[st->bucket].chain);
  1284. } else {
  1285. cur = NULL;
  1286. goto out;
  1287. }
  1288. } else
  1289. sk = sk_next(sk);
  1290. sk_for_each_from(sk, node) {
  1291. if (sk->sk_family == st->family)
  1292. goto found;
  1293. }
  1294. st->state = TCP_SEQ_STATE_TIME_WAIT;
  1295. tw = tw_head(&tcp_hashinfo.ehash[st->bucket + tcp_hashinfo.ehash_size].chain);
  1296. goto get_tw;
  1297. found:
  1298. cur = sk;
  1299. out:
  1300. return cur;
  1301. }
  1302. static void *established_get_idx(struct seq_file *seq, loff_t pos)
  1303. {
  1304. void *rc = established_get_first(seq);
  1305. while (rc && pos) {
  1306. rc = established_get_next(seq, rc);
  1307. --pos;
  1308. }
  1309. return rc;
  1310. }
  1311. static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
  1312. {
  1313. void *rc;
  1314. struct tcp_iter_state* st = seq->private;
  1315. inet_listen_lock(&tcp_hashinfo);
  1316. st->state = TCP_SEQ_STATE_LISTENING;
  1317. rc = listening_get_idx(seq, &pos);
  1318. if (!rc) {
  1319. inet_listen_unlock(&tcp_hashinfo);
  1320. local_bh_disable();
  1321. st->state = TCP_SEQ_STATE_ESTABLISHED;
  1322. rc = established_get_idx(seq, pos);
  1323. }
  1324. return rc;
  1325. }
  1326. static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
  1327. {
  1328. struct tcp_iter_state* st = seq->private;
  1329. st->state = TCP_SEQ_STATE_LISTENING;
  1330. st->num = 0;
  1331. return *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1332. }
  1333. static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1334. {
  1335. void *rc = NULL;
  1336. struct tcp_iter_state* st;
  1337. if (v == SEQ_START_TOKEN) {
  1338. rc = tcp_get_idx(seq, 0);
  1339. goto out;
  1340. }
  1341. st = seq->private;
  1342. switch (st->state) {
  1343. case TCP_SEQ_STATE_OPENREQ:
  1344. case TCP_SEQ_STATE_LISTENING:
  1345. rc = listening_get_next(seq, v);
  1346. if (!rc) {
  1347. inet_listen_unlock(&tcp_hashinfo);
  1348. local_bh_disable();
  1349. st->state = TCP_SEQ_STATE_ESTABLISHED;
  1350. rc = established_get_first(seq);
  1351. }
  1352. break;
  1353. case TCP_SEQ_STATE_ESTABLISHED:
  1354. case TCP_SEQ_STATE_TIME_WAIT:
  1355. rc = established_get_next(seq, v);
  1356. break;
  1357. }
  1358. out:
  1359. ++*pos;
  1360. return rc;
  1361. }
  1362. static void tcp_seq_stop(struct seq_file *seq, void *v)
  1363. {
  1364. struct tcp_iter_state* st = seq->private;
  1365. switch (st->state) {
  1366. case TCP_SEQ_STATE_OPENREQ:
  1367. if (v) {
  1368. struct inet_connection_sock *icsk = inet_csk(st->syn_wait_sk);
  1369. read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1370. }
  1371. case TCP_SEQ_STATE_LISTENING:
  1372. if (v != SEQ_START_TOKEN)
  1373. inet_listen_unlock(&tcp_hashinfo);
  1374. break;
  1375. case TCP_SEQ_STATE_TIME_WAIT:
  1376. case TCP_SEQ_STATE_ESTABLISHED:
  1377. if (v)
  1378. read_unlock(&tcp_hashinfo.ehash[st->bucket].lock);
  1379. local_bh_enable();
  1380. break;
  1381. }
  1382. }
  1383. static int tcp_seq_open(struct inode *inode, struct file *file)
  1384. {
  1385. struct tcp_seq_afinfo *afinfo = PDE(inode)->data;
  1386. struct seq_file *seq;
  1387. struct tcp_iter_state *s;
  1388. int rc;
  1389. if (unlikely(afinfo == NULL))
  1390. return -EINVAL;
  1391. s = kzalloc(sizeof(*s), GFP_KERNEL);
  1392. if (!s)
  1393. return -ENOMEM;
  1394. s->family = afinfo->family;
  1395. s->seq_ops.start = tcp_seq_start;
  1396. s->seq_ops.next = tcp_seq_next;
  1397. s->seq_ops.show = afinfo->seq_show;
  1398. s->seq_ops.stop = tcp_seq_stop;
  1399. rc = seq_open(file, &s->seq_ops);
  1400. if (rc)
  1401. goto out_kfree;
  1402. seq = file->private_data;
  1403. seq->private = s;
  1404. out:
  1405. return rc;
  1406. out_kfree:
  1407. kfree(s);
  1408. goto out;
  1409. }
  1410. int tcp_proc_register(struct tcp_seq_afinfo *afinfo)
  1411. {
  1412. int rc = 0;
  1413. struct proc_dir_entry *p;
  1414. if (!afinfo)
  1415. return -EINVAL;
  1416. afinfo->seq_fops->owner = afinfo->owner;
  1417. afinfo->seq_fops->open = tcp_seq_open;
  1418. afinfo->seq_fops->read = seq_read;
  1419. afinfo->seq_fops->llseek = seq_lseek;
  1420. afinfo->seq_fops->release = seq_release_private;
  1421. p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
  1422. if (p)
  1423. p->data = afinfo;
  1424. else
  1425. rc = -ENOMEM;
  1426. return rc;
  1427. }
  1428. void tcp_proc_unregister(struct tcp_seq_afinfo *afinfo)
  1429. {
  1430. if (!afinfo)
  1431. return;
  1432. proc_net_remove(afinfo->name);
  1433. memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
  1434. }
  1435. static void get_openreq4(struct sock *sk, struct request_sock *req,
  1436. char *tmpbuf, int i, int uid)
  1437. {
  1438. const struct inet_request_sock *ireq = inet_rsk(req);
  1439. int ttd = req->expires - jiffies;
  1440. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
  1441. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %p",
  1442. i,
  1443. ireq->loc_addr,
  1444. ntohs(inet_sk(sk)->sport),
  1445. ireq->rmt_addr,
  1446. ntohs(ireq->rmt_port),
  1447. TCP_SYN_RECV,
  1448. 0, 0, /* could print option size, but that is af dependent. */
  1449. 1, /* timers active (only the expire timer) */
  1450. jiffies_to_clock_t(ttd),
  1451. req->retrans,
  1452. uid,
  1453. 0, /* non standard timer */
  1454. 0, /* open_requests have no inode */
  1455. atomic_read(&sk->sk_refcnt),
  1456. req);
  1457. }
  1458. static void get_tcp4_sock(struct sock *sp, char *tmpbuf, int i)
  1459. {
  1460. int timer_active;
  1461. unsigned long timer_expires;
  1462. struct tcp_sock *tp = tcp_sk(sp);
  1463. const struct inet_connection_sock *icsk = inet_csk(sp);
  1464. struct inet_sock *inet = inet_sk(sp);
  1465. unsigned int dest = inet->daddr;
  1466. unsigned int src = inet->rcv_saddr;
  1467. __u16 destp = ntohs(inet->dport);
  1468. __u16 srcp = ntohs(inet->sport);
  1469. if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
  1470. timer_active = 1;
  1471. timer_expires = icsk->icsk_timeout;
  1472. } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
  1473. timer_active = 4;
  1474. timer_expires = icsk->icsk_timeout;
  1475. } else if (timer_pending(&sp->sk_timer)) {
  1476. timer_active = 2;
  1477. timer_expires = sp->sk_timer.expires;
  1478. } else {
  1479. timer_active = 0;
  1480. timer_expires = jiffies;
  1481. }
  1482. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
  1483. "%08X %5d %8d %lu %d %p %u %u %u %u %d",
  1484. i, src, srcp, dest, destp, sp->sk_state,
  1485. tp->write_seq - tp->snd_una,
  1486. (sp->sk_state == TCP_LISTEN) ? sp->sk_ack_backlog : (tp->rcv_nxt - tp->copied_seq),
  1487. timer_active,
  1488. jiffies_to_clock_t(timer_expires - jiffies),
  1489. icsk->icsk_retransmits,
  1490. sock_i_uid(sp),
  1491. icsk->icsk_probes_out,
  1492. sock_i_ino(sp),
  1493. atomic_read(&sp->sk_refcnt), sp,
  1494. icsk->icsk_rto,
  1495. icsk->icsk_ack.ato,
  1496. (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
  1497. tp->snd_cwnd,
  1498. tp->snd_ssthresh >= 0xFFFF ? -1 : tp->snd_ssthresh);
  1499. }
  1500. static void get_timewait4_sock(struct inet_timewait_sock *tw, char *tmpbuf, int i)
  1501. {
  1502. __be32 dest, src;
  1503. __u16 destp, srcp;
  1504. int ttd = tw->tw_ttd - jiffies;
  1505. if (ttd < 0)
  1506. ttd = 0;
  1507. dest = tw->tw_daddr;
  1508. src = tw->tw_rcv_saddr;
  1509. destp = ntohs(tw->tw_dport);
  1510. srcp = ntohs(tw->tw_sport);
  1511. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
  1512. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p",
  1513. i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
  1514. 3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
  1515. atomic_read(&tw->tw_refcnt), tw);
  1516. }
  1517. #define TMPSZ 150
  1518. static int tcp4_seq_show(struct seq_file *seq, void *v)
  1519. {
  1520. struct tcp_iter_state* st;
  1521. char tmpbuf[TMPSZ + 1];
  1522. if (v == SEQ_START_TOKEN) {
  1523. seq_printf(seq, "%-*s\n", TMPSZ - 1,
  1524. " sl local_address rem_address st tx_queue "
  1525. "rx_queue tr tm->when retrnsmt uid timeout "
  1526. "inode");
  1527. goto out;
  1528. }
  1529. st = seq->private;
  1530. switch (st->state) {
  1531. case TCP_SEQ_STATE_LISTENING:
  1532. case TCP_SEQ_STATE_ESTABLISHED:
  1533. get_tcp4_sock(v, tmpbuf, st->num);
  1534. break;
  1535. case TCP_SEQ_STATE_OPENREQ:
  1536. get_openreq4(st->syn_wait_sk, v, tmpbuf, st->num, st->uid);
  1537. break;
  1538. case TCP_SEQ_STATE_TIME_WAIT:
  1539. get_timewait4_sock(v, tmpbuf, st->num);
  1540. break;
  1541. }
  1542. seq_printf(seq, "%-*s\n", TMPSZ - 1, tmpbuf);
  1543. out:
  1544. return 0;
  1545. }
  1546. static struct file_operations tcp4_seq_fops;
  1547. static struct tcp_seq_afinfo tcp4_seq_afinfo = {
  1548. .owner = THIS_MODULE,
  1549. .name = "tcp",
  1550. .family = AF_INET,
  1551. .seq_show = tcp4_seq_show,
  1552. .seq_fops = &tcp4_seq_fops,
  1553. };
  1554. int __init tcp4_proc_init(void)
  1555. {
  1556. return tcp_proc_register(&tcp4_seq_afinfo);
  1557. }
  1558. void tcp4_proc_exit(void)
  1559. {
  1560. tcp_proc_unregister(&tcp4_seq_afinfo);
  1561. }
  1562. #endif /* CONFIG_PROC_FS */
  1563. struct proto tcp_prot = {
  1564. .name = "TCP",
  1565. .owner = THIS_MODULE,
  1566. .close = tcp_close,
  1567. .connect = tcp_v4_connect,
  1568. .disconnect = tcp_disconnect,
  1569. .accept = inet_csk_accept,
  1570. .ioctl = tcp_ioctl,
  1571. .init = tcp_v4_init_sock,
  1572. .destroy = tcp_v4_destroy_sock,
  1573. .shutdown = tcp_shutdown,
  1574. .setsockopt = tcp_setsockopt,
  1575. .getsockopt = tcp_getsockopt,
  1576. .sendmsg = tcp_sendmsg,
  1577. .recvmsg = tcp_recvmsg,
  1578. .backlog_rcv = tcp_v4_do_rcv,
  1579. .hash = tcp_v4_hash,
  1580. .unhash = tcp_unhash,
  1581. .get_port = tcp_v4_get_port,
  1582. .enter_memory_pressure = tcp_enter_memory_pressure,
  1583. .sockets_allocated = &tcp_sockets_allocated,
  1584. .orphan_count = &tcp_orphan_count,
  1585. .memory_allocated = &tcp_memory_allocated,
  1586. .memory_pressure = &tcp_memory_pressure,
  1587. .sysctl_mem = sysctl_tcp_mem,
  1588. .sysctl_wmem = sysctl_tcp_wmem,
  1589. .sysctl_rmem = sysctl_tcp_rmem,
  1590. .max_header = MAX_TCP_HEADER,
  1591. .obj_size = sizeof(struct tcp_sock),
  1592. .twsk_prot = &tcp_timewait_sock_ops,
  1593. .rsk_prot = &tcp_request_sock_ops,
  1594. #ifdef CONFIG_COMPAT
  1595. .compat_setsockopt = compat_tcp_setsockopt,
  1596. .compat_getsockopt = compat_tcp_getsockopt,
  1597. #endif
  1598. };
  1599. void __init tcp_v4_init(struct net_proto_family *ops)
  1600. {
  1601. if (inet_csk_ctl_sock_create(&tcp_socket, PF_INET, SOCK_RAW, IPPROTO_TCP) < 0)
  1602. panic("Failed to create the TCP control socket.\n");
  1603. }
  1604. EXPORT_SYMBOL(ipv4_specific);
  1605. EXPORT_SYMBOL(tcp_hashinfo);
  1606. EXPORT_SYMBOL(tcp_prot);
  1607. EXPORT_SYMBOL(tcp_unhash);
  1608. EXPORT_SYMBOL(tcp_v4_conn_request);
  1609. EXPORT_SYMBOL(tcp_v4_connect);
  1610. EXPORT_SYMBOL(tcp_v4_do_rcv);
  1611. EXPORT_SYMBOL(tcp_v4_remember_stamp);
  1612. EXPORT_SYMBOL(tcp_v4_send_check);
  1613. EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
  1614. #ifdef CONFIG_PROC_FS
  1615. EXPORT_SYMBOL(tcp_proc_register);
  1616. EXPORT_SYMBOL(tcp_proc_unregister);
  1617. #endif
  1618. EXPORT_SYMBOL(sysctl_local_port_range);
  1619. EXPORT_SYMBOL(sysctl_tcp_low_latency);