tcp_ipv4.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478
  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.1.1.1 2007/06/12 07:27:14 eyryu 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. #include <linux/crypto.h>
  76. #include <linux/scatterlist.h>
  77. int sysctl_tcp_tw_reuse __read_mostly;
  78. int sysctl_tcp_low_latency __read_mostly;
  79. /* Check TCP sequence numbers in ICMP packets. */
  80. #define ICMP_MIN_LENGTH 8
  81. /* Socket used for sending RSTs */
  82. static struct socket *tcp_socket;
  83. void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb);
  84. #ifdef CONFIG_TCP_MD5SIG
  85. static struct tcp_md5sig_key *tcp_v4_md5_do_lookup(struct sock *sk,
  86. __be32 addr);
  87. static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
  88. __be32 saddr, __be32 daddr,
  89. struct tcphdr *th, int protocol,
  90. int tcplen);
  91. #endif
  92. struct inet_hashinfo __cacheline_aligned tcp_hashinfo = {
  93. .lhash_lock = __RW_LOCK_UNLOCKED(tcp_hashinfo.lhash_lock),
  94. .lhash_users = ATOMIC_INIT(0),
  95. .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(tcp_hashinfo.lhash_wait),
  96. };
  97. static int tcp_v4_get_port(struct sock *sk, unsigned short snum)
  98. {
  99. return inet_csk_get_port(&tcp_hashinfo, sk, snum,
  100. inet_csk_bind_conflict);
  101. }
  102. static void tcp_v4_hash(struct sock *sk)
  103. {
  104. inet_hash(&tcp_hashinfo, sk);
  105. }
  106. void tcp_unhash(struct sock *sk)
  107. {
  108. inet_unhash(&tcp_hashinfo, sk);
  109. }
  110. static inline __u32 tcp_v4_init_sequence(struct sk_buff *skb)
  111. {
  112. return secure_tcp_sequence_number(skb->nh.iph->daddr,
  113. skb->nh.iph->saddr,
  114. skb->h.th->dest,
  115. skb->h.th->source);
  116. }
  117. int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
  118. {
  119. const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw);
  120. struct tcp_sock *tp = tcp_sk(sk);
  121. /* With PAWS, it is safe from the viewpoint
  122. of data integrity. Even without PAWS it is safe provided sequence
  123. spaces do not overlap i.e. at data rates <= 80Mbit/sec.
  124. Actually, the idea is close to VJ's one, only timestamp cache is
  125. held not per host, but per port pair and TW bucket is used as state
  126. holder.
  127. If TW bucket has been already destroyed we fall back to VJ's scheme
  128. and use initial timestamp retrieved from peer table.
  129. */
  130. if (tcptw->tw_ts_recent_stamp &&
  131. (twp == NULL || (sysctl_tcp_tw_reuse &&
  132. xtime.tv_sec - tcptw->tw_ts_recent_stamp > 1))) {
  133. tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2;
  134. if (tp->write_seq == 0)
  135. tp->write_seq = 1;
  136. tp->rx_opt.ts_recent = tcptw->tw_ts_recent;
  137. tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
  138. sock_hold(sktw);
  139. return 1;
  140. }
  141. return 0;
  142. }
  143. EXPORT_SYMBOL_GPL(tcp_twsk_unique);
  144. /* This will initiate an outgoing connection. */
  145. int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  146. {
  147. struct inet_sock *inet = inet_sk(sk);
  148. struct tcp_sock *tp = tcp_sk(sk);
  149. struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
  150. struct rtable *rt;
  151. __be32 daddr, nexthop;
  152. int tmp;
  153. int err;
  154. if (addr_len < sizeof(struct sockaddr_in))
  155. return -EINVAL;
  156. if (usin->sin_family != AF_INET)
  157. return -EAFNOSUPPORT;
  158. nexthop = daddr = usin->sin_addr.s_addr;
  159. if (inet->opt && inet->opt->srr) {
  160. if (!daddr)
  161. return -EINVAL;
  162. nexthop = inet->opt->faddr;
  163. }
  164. tmp = ip_route_connect(&rt, nexthop, inet->saddr,
  165. RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
  166. IPPROTO_TCP,
  167. inet->sport, usin->sin_port, sk, 1);
  168. if (tmp < 0)
  169. return tmp;
  170. if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
  171. ip_rt_put(rt);
  172. return -ENETUNREACH;
  173. }
  174. if (!inet->opt || !inet->opt->srr)
  175. daddr = rt->rt_dst;
  176. if (!inet->saddr)
  177. inet->saddr = rt->rt_src;
  178. inet->rcv_saddr = inet->saddr;
  179. if (tp->rx_opt.ts_recent_stamp && inet->daddr != daddr) {
  180. /* Reset inherited state */
  181. tp->rx_opt.ts_recent = 0;
  182. tp->rx_opt.ts_recent_stamp = 0;
  183. tp->write_seq = 0;
  184. }
  185. if (tcp_death_row.sysctl_tw_recycle &&
  186. !tp->rx_opt.ts_recent_stamp && rt->rt_dst == daddr) {
  187. struct inet_peer *peer = rt_get_peer(rt);
  188. /*
  189. * VJ's idea. We save last timestamp seen from
  190. * the destination in peer table, when entering state
  191. * TIME-WAIT * and initialize rx_opt.ts_recent from it,
  192. * when trying new connection.
  193. */
  194. if (peer != NULL &&
  195. peer->tcp_ts_stamp + TCP_PAWS_MSL >= xtime.tv_sec) {
  196. tp->rx_opt.ts_recent_stamp = peer->tcp_ts_stamp;
  197. tp->rx_opt.ts_recent = peer->tcp_ts;
  198. }
  199. }
  200. inet->dport = usin->sin_port;
  201. inet->daddr = daddr;
  202. inet_csk(sk)->icsk_ext_hdr_len = 0;
  203. if (inet->opt)
  204. inet_csk(sk)->icsk_ext_hdr_len = inet->opt->optlen;
  205. tp->rx_opt.mss_clamp = 536;
  206. /* Socket identity is still unknown (sport may be zero).
  207. * However we set state to SYN-SENT and not releasing socket
  208. * lock select source port, enter ourselves into the hash tables and
  209. * complete initialization after this.
  210. */
  211. tcp_set_state(sk, TCP_SYN_SENT);
  212. err = inet_hash_connect(&tcp_death_row, sk);
  213. if (err)
  214. goto failure;
  215. err = ip_route_newports(&rt, IPPROTO_TCP,
  216. inet->sport, inet->dport, sk);
  217. if (err)
  218. goto failure;
  219. /* OK, now commit destination to socket. */
  220. sk->sk_gso_type = SKB_GSO_TCPV4;
  221. sk_setup_caps(sk, &rt->u.dst);
  222. if (!tp->write_seq)
  223. tp->write_seq = secure_tcp_sequence_number(inet->saddr,
  224. inet->daddr,
  225. inet->sport,
  226. usin->sin_port);
  227. inet->id = tp->write_seq ^ jiffies;
  228. err = tcp_connect(sk);
  229. rt = NULL;
  230. if (err)
  231. goto failure;
  232. return 0;
  233. failure:
  234. /*
  235. * This unhashes the socket and releases the local port,
  236. * if necessary.
  237. */
  238. tcp_set_state(sk, TCP_CLOSE);
  239. ip_rt_put(rt);
  240. sk->sk_route_caps = 0;
  241. inet->dport = 0;
  242. return err;
  243. }
  244. /*
  245. * This routine does path mtu discovery as defined in RFC1191.
  246. */
  247. static void do_pmtu_discovery(struct sock *sk, struct iphdr *iph, u32 mtu)
  248. {
  249. struct dst_entry *dst;
  250. struct inet_sock *inet = inet_sk(sk);
  251. /* We are not interested in TCP_LISTEN and open_requests (SYN-ACKs
  252. * send out by Linux are always <576bytes so they should go through
  253. * unfragmented).
  254. */
  255. if (sk->sk_state == TCP_LISTEN)
  256. return;
  257. /* We don't check in the destentry if pmtu discovery is forbidden
  258. * on this route. We just assume that no packet_to_big packets
  259. * are send back when pmtu discovery is not active.
  260. * There is a small race when the user changes this flag in the
  261. * route, but I think that's acceptable.
  262. */
  263. if ((dst = __sk_dst_check(sk, 0)) == NULL)
  264. return;
  265. dst->ops->update_pmtu(dst, mtu);
  266. /* Something is about to be wrong... Remember soft error
  267. * for the case, if this connection will not able to recover.
  268. */
  269. if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
  270. sk->sk_err_soft = EMSGSIZE;
  271. mtu = dst_mtu(dst);
  272. if (inet->pmtudisc != IP_PMTUDISC_DONT &&
  273. inet_csk(sk)->icsk_pmtu_cookie > mtu) {
  274. tcp_sync_mss(sk, mtu);
  275. /* Resend the TCP packet because it's
  276. * clear that the old packet has been
  277. * dropped. This is the new "fast" path mtu
  278. * discovery.
  279. */
  280. tcp_simple_retransmit(sk);
  281. } /* else let the usual retransmit timer handle it */
  282. }
  283. /*
  284. * This routine is called by the ICMP module when it gets some
  285. * sort of error condition. If err < 0 then the socket should
  286. * be closed and the error returned to the user. If err > 0
  287. * it's just the icmp type << 8 | icmp code. After adjustment
  288. * header points to the first 8 bytes of the tcp header. We need
  289. * to find the appropriate port.
  290. *
  291. * The locking strategy used here is very "optimistic". When
  292. * someone else accesses the socket the ICMP is just dropped
  293. * and for some paths there is no check at all.
  294. * A more general error queue to queue errors for later handling
  295. * is probably better.
  296. *
  297. */
  298. void tcp_v4_err(struct sk_buff *skb, u32 info)
  299. {
  300. struct iphdr *iph = (struct iphdr *)skb->data;
  301. struct tcphdr *th = (struct tcphdr *)(skb->data + (iph->ihl << 2));
  302. struct tcp_sock *tp;
  303. struct inet_sock *inet;
  304. int type = skb->h.icmph->type;
  305. int code = skb->h.icmph->code;
  306. struct sock *sk;
  307. __u32 seq;
  308. int err;
  309. if (skb->len < (iph->ihl << 2) + 8) {
  310. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  311. return;
  312. }
  313. sk = inet_lookup(&tcp_hashinfo, iph->daddr, th->dest, iph->saddr,
  314. th->source, inet_iif(skb));
  315. if (!sk) {
  316. ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
  317. return;
  318. }
  319. if (sk->sk_state == TCP_TIME_WAIT) {
  320. inet_twsk_put(inet_twsk(sk));
  321. return;
  322. }
  323. bh_lock_sock(sk);
  324. /* If too many ICMPs get dropped on busy
  325. * servers this needs to be solved differently.
  326. */
  327. if (sock_owned_by_user(sk))
  328. NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
  329. if (sk->sk_state == TCP_CLOSE)
  330. goto out;
  331. tp = tcp_sk(sk);
  332. seq = ntohl(th->seq);
  333. if (sk->sk_state != TCP_LISTEN &&
  334. !between(seq, tp->snd_una, tp->snd_nxt)) {
  335. NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
  336. goto out;
  337. }
  338. switch (type) {
  339. case ICMP_SOURCE_QUENCH:
  340. /* Just silently ignore these. */
  341. goto out;
  342. case ICMP_PARAMETERPROB:
  343. err = EPROTO;
  344. break;
  345. case ICMP_DEST_UNREACH:
  346. if (code > NR_ICMP_UNREACH)
  347. goto out;
  348. if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
  349. if (!sock_owned_by_user(sk))
  350. do_pmtu_discovery(sk, iph, info);
  351. goto out;
  352. }
  353. err = icmp_err_convert[code].errno;
  354. break;
  355. case ICMP_TIME_EXCEEDED:
  356. err = EHOSTUNREACH;
  357. break;
  358. default:
  359. goto out;
  360. }
  361. switch (sk->sk_state) {
  362. struct request_sock *req, **prev;
  363. case TCP_LISTEN:
  364. if (sock_owned_by_user(sk))
  365. goto out;
  366. req = inet_csk_search_req(sk, &prev, th->dest,
  367. iph->daddr, iph->saddr);
  368. if (!req)
  369. goto out;
  370. /* ICMPs are not backlogged, hence we cannot get
  371. an established socket here.
  372. */
  373. BUG_TRAP(!req->sk);
  374. if (seq != tcp_rsk(req)->snt_isn) {
  375. NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
  376. goto out;
  377. }
  378. /*
  379. * Still in SYN_RECV, just remove it silently.
  380. * There is no good way to pass the error to the newly
  381. * created socket, and POSIX does not want network
  382. * errors returned from accept().
  383. */
  384. inet_csk_reqsk_queue_drop(sk, req, prev);
  385. goto out;
  386. case TCP_SYN_SENT:
  387. case TCP_SYN_RECV: /* Cannot happen.
  388. It can f.e. if SYNs crossed.
  389. */
  390. if (!sock_owned_by_user(sk)) {
  391. sk->sk_err = err;
  392. sk->sk_error_report(sk);
  393. tcp_done(sk);
  394. } else {
  395. sk->sk_err_soft = err;
  396. }
  397. goto out;
  398. }
  399. /* If we've already connected we will keep trying
  400. * until we time out, or the user gives up.
  401. *
  402. * rfc1122 4.2.3.9 allows to consider as hard errors
  403. * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
  404. * but it is obsoleted by pmtu discovery).
  405. *
  406. * Note, that in modern internet, where routing is unreliable
  407. * and in each dark corner broken firewalls sit, sending random
  408. * errors ordered by their masters even this two messages finally lose
  409. * their original sense (even Linux sends invalid PORT_UNREACHs)
  410. *
  411. * Now we are in compliance with RFCs.
  412. * --ANK (980905)
  413. */
  414. inet = inet_sk(sk);
  415. if (!sock_owned_by_user(sk) && inet->recverr) {
  416. sk->sk_err = err;
  417. sk->sk_error_report(sk);
  418. } else { /* Only an error on timeout */
  419. sk->sk_err_soft = err;
  420. }
  421. out:
  422. bh_unlock_sock(sk);
  423. sock_put(sk);
  424. }
  425. /* This routine computes an IPv4 TCP checksum. */
  426. void tcp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
  427. {
  428. struct inet_sock *inet = inet_sk(sk);
  429. struct tcphdr *th = skb->h.th;
  430. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  431. th->check = ~tcp_v4_check(len, inet->saddr,
  432. inet->daddr, 0);
  433. skb->csum_offset = offsetof(struct tcphdr, check);
  434. } else {
  435. th->check = tcp_v4_check(len, inet->saddr, inet->daddr,
  436. csum_partial((char *)th,
  437. th->doff << 2,
  438. skb->csum));
  439. }
  440. }
  441. int tcp_v4_gso_send_check(struct sk_buff *skb)
  442. {
  443. struct iphdr *iph;
  444. struct tcphdr *th;
  445. if (!pskb_may_pull(skb, sizeof(*th)))
  446. return -EINVAL;
  447. iph = skb->nh.iph;
  448. th = skb->h.th;
  449. th->check = 0;
  450. th->check = ~tcp_v4_check(skb->len, iph->saddr, iph->daddr, 0);
  451. skb->csum_offset = offsetof(struct tcphdr, check);
  452. skb->ip_summed = CHECKSUM_PARTIAL;
  453. return 0;
  454. }
  455. /*
  456. * This routine will send an RST to the other tcp.
  457. *
  458. * Someone asks: why I NEVER use socket parameters (TOS, TTL etc.)
  459. * for reset.
  460. * Answer: if a packet caused RST, it is not for a socket
  461. * existing in our system, if it is matched to a socket,
  462. * it is just duplicate segment or bug in other side's TCP.
  463. * So that we build reply only basing on parameters
  464. * arrived with segment.
  465. * Exception: precedence violation. We do not implement it in any case.
  466. */
  467. static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb)
  468. {
  469. struct tcphdr *th = skb->h.th;
  470. struct {
  471. struct tcphdr th;
  472. #ifdef CONFIG_TCP_MD5SIG
  473. __be32 opt[(TCPOLEN_MD5SIG_ALIGNED >> 2)];
  474. #endif
  475. } rep;
  476. struct ip_reply_arg arg;
  477. #ifdef CONFIG_TCP_MD5SIG
  478. struct tcp_md5sig_key *key;
  479. #endif
  480. /* Never send a reset in response to a reset. */
  481. if (th->rst)
  482. return;
  483. if (((struct rtable *)skb->dst)->rt_type != RTN_LOCAL)
  484. return;
  485. /* Swap the send and the receive. */
  486. memset(&rep, 0, sizeof(rep));
  487. rep.th.dest = th->source;
  488. rep.th.source = th->dest;
  489. rep.th.doff = sizeof(struct tcphdr) / 4;
  490. rep.th.rst = 1;
  491. if (th->ack) {
  492. rep.th.seq = th->ack_seq;
  493. } else {
  494. rep.th.ack = 1;
  495. rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin +
  496. skb->len - (th->doff << 2));
  497. }
  498. memset(&arg, 0, sizeof(arg));
  499. arg.iov[0].iov_base = (unsigned char *)&rep;
  500. arg.iov[0].iov_len = sizeof(rep.th);
  501. #ifdef CONFIG_TCP_MD5SIG
  502. key = sk ? tcp_v4_md5_do_lookup(sk, skb->nh.iph->daddr) : NULL;
  503. if (key) {
  504. rep.opt[0] = htonl((TCPOPT_NOP << 24) |
  505. (TCPOPT_NOP << 16) |
  506. (TCPOPT_MD5SIG << 8) |
  507. TCPOLEN_MD5SIG);
  508. /* Update length and the length the header thinks exists */
  509. arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
  510. rep.th.doff = arg.iov[0].iov_len / 4;
  511. tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[1],
  512. key,
  513. skb->nh.iph->daddr,
  514. skb->nh.iph->saddr,
  515. &rep.th, IPPROTO_TCP,
  516. arg.iov[0].iov_len);
  517. }
  518. #endif
  519. arg.csum = csum_tcpudp_nofold(skb->nh.iph->daddr,
  520. skb->nh.iph->saddr, /* XXX */
  521. sizeof(struct tcphdr), IPPROTO_TCP, 0);
  522. arg.csumoffset = offsetof(struct tcphdr, check) / 2;
  523. ip_send_reply(tcp_socket->sk, skb, &arg, arg.iov[0].iov_len);
  524. TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
  525. TCP_INC_STATS_BH(TCP_MIB_OUTRSTS);
  526. }
  527. /* The code following below sending ACKs in SYN-RECV and TIME-WAIT states
  528. outside socket context is ugly, certainly. What can I do?
  529. */
  530. static void tcp_v4_send_ack(struct tcp_timewait_sock *twsk,
  531. struct sk_buff *skb, u32 seq, u32 ack,
  532. u32 win, u32 ts)
  533. {
  534. struct tcphdr *th = skb->h.th;
  535. struct {
  536. struct tcphdr th;
  537. __be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2)
  538. #ifdef CONFIG_TCP_MD5SIG
  539. + (TCPOLEN_MD5SIG_ALIGNED >> 2)
  540. #endif
  541. ];
  542. } rep;
  543. struct ip_reply_arg arg;
  544. #ifdef CONFIG_TCP_MD5SIG
  545. struct tcp_md5sig_key *key;
  546. struct tcp_md5sig_key tw_key;
  547. #endif
  548. memset(&rep.th, 0, sizeof(struct tcphdr));
  549. memset(&arg, 0, sizeof(arg));
  550. arg.iov[0].iov_base = (unsigned char *)&rep;
  551. arg.iov[0].iov_len = sizeof(rep.th);
  552. if (ts) {
  553. rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  554. (TCPOPT_TIMESTAMP << 8) |
  555. TCPOLEN_TIMESTAMP);
  556. rep.opt[1] = htonl(tcp_time_stamp);
  557. rep.opt[2] = htonl(ts);
  558. arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED;
  559. }
  560. /* Swap the send and the receive. */
  561. rep.th.dest = th->source;
  562. rep.th.source = th->dest;
  563. rep.th.doff = arg.iov[0].iov_len / 4;
  564. rep.th.seq = htonl(seq);
  565. rep.th.ack_seq = htonl(ack);
  566. rep.th.ack = 1;
  567. rep.th.window = htons(win);
  568. #ifdef CONFIG_TCP_MD5SIG
  569. /*
  570. * The SKB holds an imcoming packet, but may not have a valid ->sk
  571. * pointer. This is especially the case when we're dealing with a
  572. * TIME_WAIT ack, because the sk structure is long gone, and only
  573. * the tcp_timewait_sock remains. So the md5 key is stashed in that
  574. * structure, and we use it in preference. I believe that (twsk ||
  575. * skb->sk) holds true, but we program defensively.
  576. */
  577. if (!twsk && skb->sk) {
  578. key = tcp_v4_md5_do_lookup(skb->sk, skb->nh.iph->daddr);
  579. } else if (twsk && twsk->tw_md5_keylen) {
  580. tw_key.key = twsk->tw_md5_key;
  581. tw_key.keylen = twsk->tw_md5_keylen;
  582. key = &tw_key;
  583. } else
  584. key = NULL;
  585. if (key) {
  586. int offset = (ts) ? 3 : 0;
  587. rep.opt[offset++] = htonl((TCPOPT_NOP << 24) |
  588. (TCPOPT_NOP << 16) |
  589. (TCPOPT_MD5SIG << 8) |
  590. TCPOLEN_MD5SIG);
  591. arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
  592. rep.th.doff = arg.iov[0].iov_len/4;
  593. tcp_v4_do_calc_md5_hash((__u8 *)&rep.opt[offset],
  594. key,
  595. skb->nh.iph->daddr,
  596. skb->nh.iph->saddr,
  597. &rep.th, IPPROTO_TCP,
  598. arg.iov[0].iov_len);
  599. }
  600. #endif
  601. arg.csum = csum_tcpudp_nofold(skb->nh.iph->daddr,
  602. skb->nh.iph->saddr, /* XXX */
  603. arg.iov[0].iov_len, IPPROTO_TCP, 0);
  604. arg.csumoffset = offsetof(struct tcphdr, check) / 2;
  605. ip_send_reply(tcp_socket->sk, skb, &arg, arg.iov[0].iov_len);
  606. TCP_INC_STATS_BH(TCP_MIB_OUTSEGS);
  607. }
  608. static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
  609. {
  610. struct inet_timewait_sock *tw = inet_twsk(sk);
  611. struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
  612. tcp_v4_send_ack(tcptw, skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
  613. tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
  614. tcptw->tw_ts_recent);
  615. inet_twsk_put(tw);
  616. }
  617. static void tcp_v4_reqsk_send_ack(struct sk_buff *skb,
  618. struct request_sock *req)
  619. {
  620. tcp_v4_send_ack(NULL, skb, tcp_rsk(req)->snt_isn + 1,
  621. tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd,
  622. req->ts_recent);
  623. }
  624. /*
  625. * Send a SYN-ACK after having received an ACK.
  626. * This still operates on a request_sock only, not on a big
  627. * socket.
  628. */
  629. static int tcp_v4_send_synack(struct sock *sk, struct request_sock *req,
  630. struct dst_entry *dst)
  631. {
  632. const struct inet_request_sock *ireq = inet_rsk(req);
  633. int err = -1;
  634. struct sk_buff * skb;
  635. /* First, grab a route. */
  636. if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
  637. goto out;
  638. skb = tcp_make_synack(sk, dst, req);
  639. if (skb) {
  640. struct tcphdr *th = skb->h.th;
  641. th->check = tcp_v4_check(skb->len,
  642. ireq->loc_addr,
  643. ireq->rmt_addr,
  644. csum_partial((char *)th, skb->len,
  645. skb->csum));
  646. err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
  647. ireq->rmt_addr,
  648. ireq->opt);
  649. err = net_xmit_eval(err);
  650. }
  651. out:
  652. dst_release(dst);
  653. return err;
  654. }
  655. /*
  656. * IPv4 request_sock destructor.
  657. */
  658. static void tcp_v4_reqsk_destructor(struct request_sock *req)
  659. {
  660. kfree(inet_rsk(req)->opt);
  661. }
  662. #ifdef CONFIG_SYN_COOKIES
  663. static void syn_flood_warning(struct sk_buff *skb)
  664. {
  665. static unsigned long warntime;
  666. if (time_after(jiffies, (warntime + HZ * 60))) {
  667. warntime = jiffies;
  668. printk(KERN_INFO
  669. "possible SYN flooding on port %d. Sending cookies.\n",
  670. ntohs(skb->h.th->dest));
  671. }
  672. }
  673. #endif
  674. /*
  675. * Save and compile IPv4 options into the request_sock if needed.
  676. */
  677. static struct ip_options *tcp_v4_save_options(struct sock *sk,
  678. struct sk_buff *skb)
  679. {
  680. struct ip_options *opt = &(IPCB(skb)->opt);
  681. struct ip_options *dopt = NULL;
  682. if (opt && opt->optlen) {
  683. int opt_size = optlength(opt);
  684. dopt = kmalloc(opt_size, GFP_ATOMIC);
  685. if (dopt) {
  686. if (ip_options_echo(dopt, skb)) {
  687. kfree(dopt);
  688. dopt = NULL;
  689. }
  690. }
  691. }
  692. return dopt;
  693. }
  694. #ifdef CONFIG_TCP_MD5SIG
  695. /*
  696. * RFC2385 MD5 checksumming requires a mapping of
  697. * IP address->MD5 Key.
  698. * We need to maintain these in the sk structure.
  699. */
  700. /* Find the Key structure for an address. */
  701. static struct tcp_md5sig_key *
  702. tcp_v4_md5_do_lookup(struct sock *sk, __be32 addr)
  703. {
  704. struct tcp_sock *tp = tcp_sk(sk);
  705. int i;
  706. if (!tp->md5sig_info || !tp->md5sig_info->entries4)
  707. return NULL;
  708. for (i = 0; i < tp->md5sig_info->entries4; i++) {
  709. if (tp->md5sig_info->keys4[i].addr == addr)
  710. return (struct tcp_md5sig_key *)
  711. &tp->md5sig_info->keys4[i];
  712. }
  713. return NULL;
  714. }
  715. struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk,
  716. struct sock *addr_sk)
  717. {
  718. return tcp_v4_md5_do_lookup(sk, inet_sk(addr_sk)->daddr);
  719. }
  720. EXPORT_SYMBOL(tcp_v4_md5_lookup);
  721. static struct tcp_md5sig_key *tcp_v4_reqsk_md5_lookup(struct sock *sk,
  722. struct request_sock *req)
  723. {
  724. return tcp_v4_md5_do_lookup(sk, inet_rsk(req)->rmt_addr);
  725. }
  726. /* This can be called on a newly created socket, from other files */
  727. int tcp_v4_md5_do_add(struct sock *sk, __be32 addr,
  728. u8 *newkey, u8 newkeylen)
  729. {
  730. /* Add Key to the list */
  731. struct tcp4_md5sig_key *key;
  732. struct tcp_sock *tp = tcp_sk(sk);
  733. struct tcp4_md5sig_key *keys;
  734. key = (struct tcp4_md5sig_key *)tcp_v4_md5_do_lookup(sk, addr);
  735. if (key) {
  736. /* Pre-existing entry - just update that one. */
  737. kfree(key->key);
  738. key->key = newkey;
  739. key->keylen = newkeylen;
  740. } else {
  741. struct tcp_md5sig_info *md5sig;
  742. if (!tp->md5sig_info) {
  743. tp->md5sig_info = kzalloc(sizeof(*tp->md5sig_info),
  744. GFP_ATOMIC);
  745. if (!tp->md5sig_info) {
  746. kfree(newkey);
  747. return -ENOMEM;
  748. }
  749. }
  750. if (tcp_alloc_md5sig_pool() == NULL) {
  751. kfree(newkey);
  752. return -ENOMEM;
  753. }
  754. md5sig = tp->md5sig_info;
  755. if (md5sig->alloced4 == md5sig->entries4) {
  756. keys = kmalloc((sizeof(*keys) *
  757. (md5sig->entries4 + 1)), GFP_ATOMIC);
  758. if (!keys) {
  759. kfree(newkey);
  760. tcp_free_md5sig_pool();
  761. return -ENOMEM;
  762. }
  763. if (md5sig->entries4)
  764. memcpy(keys, md5sig->keys4,
  765. sizeof(*keys) * md5sig->entries4);
  766. /* Free old key list, and reference new one */
  767. if (md5sig->keys4)
  768. kfree(md5sig->keys4);
  769. md5sig->keys4 = keys;
  770. md5sig->alloced4++;
  771. }
  772. md5sig->entries4++;
  773. md5sig->keys4[md5sig->entries4 - 1].addr = addr;
  774. md5sig->keys4[md5sig->entries4 - 1].key = newkey;
  775. md5sig->keys4[md5sig->entries4 - 1].keylen = newkeylen;
  776. }
  777. return 0;
  778. }
  779. EXPORT_SYMBOL(tcp_v4_md5_do_add);
  780. static int tcp_v4_md5_add_func(struct sock *sk, struct sock *addr_sk,
  781. u8 *newkey, u8 newkeylen)
  782. {
  783. return tcp_v4_md5_do_add(sk, inet_sk(addr_sk)->daddr,
  784. newkey, newkeylen);
  785. }
  786. int tcp_v4_md5_do_del(struct sock *sk, __be32 addr)
  787. {
  788. struct tcp_sock *tp = tcp_sk(sk);
  789. int i;
  790. for (i = 0; i < tp->md5sig_info->entries4; i++) {
  791. if (tp->md5sig_info->keys4[i].addr == addr) {
  792. /* Free the key */
  793. kfree(tp->md5sig_info->keys4[i].key);
  794. tp->md5sig_info->entries4--;
  795. if (tp->md5sig_info->entries4 == 0) {
  796. kfree(tp->md5sig_info->keys4);
  797. tp->md5sig_info->keys4 = NULL;
  798. tp->md5sig_info->alloced4 = 0;
  799. } else if (tp->md5sig_info->entries4 != i) {
  800. /* Need to do some manipulation */
  801. memcpy(&tp->md5sig_info->keys4[i],
  802. &tp->md5sig_info->keys4[i+1],
  803. (tp->md5sig_info->entries4 - i) *
  804. sizeof(struct tcp4_md5sig_key));
  805. }
  806. tcp_free_md5sig_pool();
  807. return 0;
  808. }
  809. }
  810. return -ENOENT;
  811. }
  812. EXPORT_SYMBOL(tcp_v4_md5_do_del);
  813. static void tcp_v4_clear_md5_list(struct sock *sk)
  814. {
  815. struct tcp_sock *tp = tcp_sk(sk);
  816. /* Free each key, then the set of key keys,
  817. * the crypto element, and then decrement our
  818. * hold on the last resort crypto.
  819. */
  820. if (tp->md5sig_info->entries4) {
  821. int i;
  822. for (i = 0; i < tp->md5sig_info->entries4; i++)
  823. kfree(tp->md5sig_info->keys4[i].key);
  824. tp->md5sig_info->entries4 = 0;
  825. tcp_free_md5sig_pool();
  826. }
  827. if (tp->md5sig_info->keys4) {
  828. kfree(tp->md5sig_info->keys4);
  829. tp->md5sig_info->keys4 = NULL;
  830. tp->md5sig_info->alloced4 = 0;
  831. }
  832. }
  833. static int tcp_v4_parse_md5_keys(struct sock *sk, char __user *optval,
  834. int optlen)
  835. {
  836. struct tcp_md5sig cmd;
  837. struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.tcpm_addr;
  838. u8 *newkey;
  839. if (optlen < sizeof(cmd))
  840. return -EINVAL;
  841. if (copy_from_user(&cmd, optval, sizeof(cmd)))
  842. return -EFAULT;
  843. if (sin->sin_family != AF_INET)
  844. return -EINVAL;
  845. if (!cmd.tcpm_key || !cmd.tcpm_keylen) {
  846. if (!tcp_sk(sk)->md5sig_info)
  847. return -ENOENT;
  848. return tcp_v4_md5_do_del(sk, sin->sin_addr.s_addr);
  849. }
  850. if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
  851. return -EINVAL;
  852. if (!tcp_sk(sk)->md5sig_info) {
  853. struct tcp_sock *tp = tcp_sk(sk);
  854. struct tcp_md5sig_info *p = kzalloc(sizeof(*p), GFP_KERNEL);
  855. if (!p)
  856. return -EINVAL;
  857. tp->md5sig_info = p;
  858. }
  859. newkey = kmemdup(cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
  860. if (!newkey)
  861. return -ENOMEM;
  862. return tcp_v4_md5_do_add(sk, sin->sin_addr.s_addr,
  863. newkey, cmd.tcpm_keylen);
  864. }
  865. static int tcp_v4_do_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
  866. __be32 saddr, __be32 daddr,
  867. struct tcphdr *th, int protocol,
  868. int tcplen)
  869. {
  870. struct scatterlist sg[4];
  871. __u16 data_len;
  872. int block = 0;
  873. __sum16 old_checksum;
  874. struct tcp_md5sig_pool *hp;
  875. struct tcp4_pseudohdr *bp;
  876. struct hash_desc *desc;
  877. int err;
  878. unsigned int nbytes = 0;
  879. /*
  880. * Okay, so RFC2385 is turned on for this connection,
  881. * so we need to generate the MD5 hash for the packet now.
  882. */
  883. hp = tcp_get_md5sig_pool();
  884. if (!hp)
  885. goto clear_hash_noput;
  886. bp = &hp->md5_blk.ip4;
  887. desc = &hp->md5_desc;
  888. /*
  889. * 1. the TCP pseudo-header (in the order: source IP address,
  890. * destination IP address, zero-padded protocol number, and
  891. * segment length)
  892. */
  893. bp->saddr = saddr;
  894. bp->daddr = daddr;
  895. bp->pad = 0;
  896. bp->protocol = protocol;
  897. bp->len = htons(tcplen);
  898. sg_set_buf(&sg[block++], bp, sizeof(*bp));
  899. nbytes += sizeof(*bp);
  900. /* 2. the TCP header, excluding options, and assuming a
  901. * checksum of zero/
  902. */
  903. old_checksum = th->check;
  904. th->check = 0;
  905. sg_set_buf(&sg[block++], th, sizeof(struct tcphdr));
  906. nbytes += sizeof(struct tcphdr);
  907. /* 3. the TCP segment data (if any) */
  908. data_len = tcplen - (th->doff << 2);
  909. if (data_len > 0) {
  910. unsigned char *data = (unsigned char *)th + (th->doff << 2);
  911. sg_set_buf(&sg[block++], data, data_len);
  912. nbytes += data_len;
  913. }
  914. /* 4. an independently-specified key or password, known to both
  915. * TCPs and presumably connection-specific
  916. */
  917. sg_set_buf(&sg[block++], key->key, key->keylen);
  918. nbytes += key->keylen;
  919. /* Now store the Hash into the packet */
  920. err = crypto_hash_init(desc);
  921. if (err)
  922. goto clear_hash;
  923. err = crypto_hash_update(desc, sg, nbytes);
  924. if (err)
  925. goto clear_hash;
  926. err = crypto_hash_final(desc, md5_hash);
  927. if (err)
  928. goto clear_hash;
  929. /* Reset header, and free up the crypto */
  930. tcp_put_md5sig_pool();
  931. th->check = old_checksum;
  932. out:
  933. return 0;
  934. clear_hash:
  935. tcp_put_md5sig_pool();
  936. clear_hash_noput:
  937. memset(md5_hash, 0, 16);
  938. goto out;
  939. }
  940. int tcp_v4_calc_md5_hash(char *md5_hash, struct tcp_md5sig_key *key,
  941. struct sock *sk,
  942. struct dst_entry *dst,
  943. struct request_sock *req,
  944. struct tcphdr *th, int protocol,
  945. int tcplen)
  946. {
  947. __be32 saddr, daddr;
  948. if (sk) {
  949. saddr = inet_sk(sk)->saddr;
  950. daddr = inet_sk(sk)->daddr;
  951. } else {
  952. struct rtable *rt = (struct rtable *)dst;
  953. BUG_ON(!rt);
  954. saddr = rt->rt_src;
  955. daddr = rt->rt_dst;
  956. }
  957. return tcp_v4_do_calc_md5_hash(md5_hash, key,
  958. saddr, daddr,
  959. th, protocol, tcplen);
  960. }
  961. EXPORT_SYMBOL(tcp_v4_calc_md5_hash);
  962. static int tcp_v4_inbound_md5_hash(struct sock *sk, struct sk_buff *skb)
  963. {
  964. /*
  965. * This gets called for each TCP segment that arrives
  966. * so we want to be efficient.
  967. * We have 3 drop cases:
  968. * o No MD5 hash and one expected.
  969. * o MD5 hash and we're not expecting one.
  970. * o MD5 hash and its wrong.
  971. */
  972. __u8 *hash_location = NULL;
  973. struct tcp_md5sig_key *hash_expected;
  974. struct iphdr *iph = skb->nh.iph;
  975. struct tcphdr *th = skb->h.th;
  976. int length = (th->doff << 2) - sizeof(struct tcphdr);
  977. int genhash;
  978. unsigned char *ptr;
  979. unsigned char newhash[16];
  980. hash_expected = tcp_v4_md5_do_lookup(sk, iph->saddr);
  981. /*
  982. * If the TCP option length is less than the TCP_MD5SIG
  983. * option length, then we can shortcut
  984. */
  985. if (length < TCPOLEN_MD5SIG) {
  986. if (hash_expected)
  987. return 1;
  988. else
  989. return 0;
  990. }
  991. /* Okay, we can't shortcut - we have to grub through the options */
  992. ptr = (unsigned char *)(th + 1);
  993. while (length > 0) {
  994. int opcode = *ptr++;
  995. int opsize;
  996. switch (opcode) {
  997. case TCPOPT_EOL:
  998. goto done_opts;
  999. case TCPOPT_NOP:
  1000. length--;
  1001. continue;
  1002. default:
  1003. opsize = *ptr++;
  1004. if (opsize < 2)
  1005. goto done_opts;
  1006. if (opsize > length)
  1007. goto done_opts;
  1008. if (opcode == TCPOPT_MD5SIG) {
  1009. hash_location = ptr;
  1010. goto done_opts;
  1011. }
  1012. }
  1013. ptr += opsize-2;
  1014. length -= opsize;
  1015. }
  1016. done_opts:
  1017. /* We've parsed the options - do we have a hash? */
  1018. if (!hash_expected && !hash_location)
  1019. return 0;
  1020. if (hash_expected && !hash_location) {
  1021. LIMIT_NETDEBUG(KERN_INFO "MD5 Hash expected but NOT found "
  1022. "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
  1023. NIPQUAD(iph->saddr), ntohs(th->source),
  1024. NIPQUAD(iph->daddr), ntohs(th->dest));
  1025. return 1;
  1026. }
  1027. if (!hash_expected && hash_location) {
  1028. LIMIT_NETDEBUG(KERN_INFO "MD5 Hash NOT expected but found "
  1029. "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)\n",
  1030. NIPQUAD(iph->saddr), ntohs(th->source),
  1031. NIPQUAD(iph->daddr), ntohs(th->dest));
  1032. return 1;
  1033. }
  1034. /* Okay, so this is hash_expected and hash_location -
  1035. * so we need to calculate the checksum.
  1036. */
  1037. genhash = tcp_v4_do_calc_md5_hash(newhash,
  1038. hash_expected,
  1039. iph->saddr, iph->daddr,
  1040. th, sk->sk_protocol,
  1041. skb->len);
  1042. if (genhash || memcmp(hash_location, newhash, 16) != 0) {
  1043. if (net_ratelimit()) {
  1044. printk(KERN_INFO "MD5 Hash failed for "
  1045. "(" NIPQUAD_FMT ", %d)->(" NIPQUAD_FMT ", %d)%s\n",
  1046. NIPQUAD(iph->saddr), ntohs(th->source),
  1047. NIPQUAD(iph->daddr), ntohs(th->dest),
  1048. genhash ? " tcp_v4_calc_md5_hash failed" : "");
  1049. }
  1050. return 1;
  1051. }
  1052. return 0;
  1053. }
  1054. #endif
  1055. struct request_sock_ops tcp_request_sock_ops __read_mostly = {
  1056. .family = PF_INET,
  1057. .obj_size = sizeof(struct tcp_request_sock),
  1058. .rtx_syn_ack = tcp_v4_send_synack,
  1059. .send_ack = tcp_v4_reqsk_send_ack,
  1060. .destructor = tcp_v4_reqsk_destructor,
  1061. .send_reset = tcp_v4_send_reset,
  1062. };
  1063. #ifdef CONFIG_TCP_MD5SIG
  1064. static struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
  1065. .md5_lookup = tcp_v4_reqsk_md5_lookup,
  1066. };
  1067. #endif
  1068. static struct timewait_sock_ops tcp_timewait_sock_ops = {
  1069. .twsk_obj_size = sizeof(struct tcp_timewait_sock),
  1070. .twsk_unique = tcp_twsk_unique,
  1071. .twsk_destructor= tcp_twsk_destructor,
  1072. };
  1073. int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
  1074. {
  1075. struct inet_request_sock *ireq;
  1076. struct tcp_options_received tmp_opt;
  1077. struct request_sock *req;
  1078. __be32 saddr = skb->nh.iph->saddr;
  1079. __be32 daddr = skb->nh.iph->daddr;
  1080. __u32 isn = TCP_SKB_CB(skb)->when;
  1081. struct dst_entry *dst = NULL;
  1082. #ifdef CONFIG_SYN_COOKIES
  1083. int want_cookie = 0;
  1084. #else
  1085. #define want_cookie 0 /* Argh, why doesn't gcc optimize this :( */
  1086. #endif
  1087. /* Never answer to SYNs send to broadcast or multicast */
  1088. if (((struct rtable *)skb->dst)->rt_flags &
  1089. (RTCF_BROADCAST | RTCF_MULTICAST))
  1090. goto drop;
  1091. /* TW buckets are converted to open requests without
  1092. * limitations, they conserve resources and peer is
  1093. * evidently real one.
  1094. */
  1095. if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
  1096. #ifdef CONFIG_SYN_COOKIES
  1097. if (sysctl_tcp_syncookies) {
  1098. want_cookie = 1;
  1099. } else
  1100. #endif
  1101. goto drop;
  1102. }
  1103. /* Accept backlog is full. If we have already queued enough
  1104. * of warm entries in syn queue, drop request. It is better than
  1105. * clogging syn queue with openreqs with exponentially increasing
  1106. * timeout.
  1107. */
  1108. if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
  1109. goto drop;
  1110. req = reqsk_alloc(&tcp_request_sock_ops);
  1111. if (!req)
  1112. goto drop;
  1113. #ifdef CONFIG_TCP_MD5SIG
  1114. tcp_rsk(req)->af_specific = &tcp_request_sock_ipv4_ops;
  1115. #endif
  1116. tcp_clear_options(&tmp_opt);
  1117. tmp_opt.mss_clamp = 536;
  1118. tmp_opt.user_mss = tcp_sk(sk)->rx_opt.user_mss;
  1119. tcp_parse_options(skb, &tmp_opt, 0);
  1120. if (want_cookie) {
  1121. tcp_clear_options(&tmp_opt);
  1122. tmp_opt.saw_tstamp = 0;
  1123. }
  1124. if (tmp_opt.saw_tstamp && !tmp_opt.rcv_tsval) {
  1125. /* Some OSes (unknown ones, but I see them on web server, which
  1126. * contains information interesting only for windows'
  1127. * users) do not send their stamp in SYN. It is easy case.
  1128. * We simply do not advertise TS support.
  1129. */
  1130. tmp_opt.saw_tstamp = 0;
  1131. tmp_opt.tstamp_ok = 0;
  1132. }
  1133. tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
  1134. tcp_openreq_init(req, &tmp_opt, skb);
  1135. if (security_inet_conn_request(sk, skb, req))
  1136. goto drop_and_free;
  1137. ireq = inet_rsk(req);
  1138. ireq->loc_addr = daddr;
  1139. ireq->rmt_addr = saddr;
  1140. ireq->opt = tcp_v4_save_options(sk, skb);
  1141. if (!want_cookie)
  1142. TCP_ECN_create_request(req, skb->h.th);
  1143. if (want_cookie) {
  1144. #ifdef CONFIG_SYN_COOKIES
  1145. syn_flood_warning(skb);
  1146. #endif
  1147. isn = cookie_v4_init_sequence(sk, skb, &req->mss);
  1148. } else if (!isn) {
  1149. struct inet_peer *peer = NULL;
  1150. /* VJ's idea. We save last timestamp seen
  1151. * from the destination in peer table, when entering
  1152. * state TIME-WAIT, and check against it before
  1153. * accepting new connection request.
  1154. *
  1155. * If "isn" is not zero, this request hit alive
  1156. * timewait bucket, so that all the necessary checks
  1157. * are made in the function processing timewait state.
  1158. */
  1159. if (tmp_opt.saw_tstamp &&
  1160. tcp_death_row.sysctl_tw_recycle &&
  1161. (dst = inet_csk_route_req(sk, req)) != NULL &&
  1162. (peer = rt_get_peer((struct rtable *)dst)) != NULL &&
  1163. peer->v4daddr == saddr) {
  1164. if (xtime.tv_sec < peer->tcp_ts_stamp + TCP_PAWS_MSL &&
  1165. (s32)(peer->tcp_ts - req->ts_recent) >
  1166. TCP_PAWS_WINDOW) {
  1167. NET_INC_STATS_BH(LINUX_MIB_PAWSPASSIVEREJECTED);
  1168. dst_release(dst);
  1169. goto drop_and_free;
  1170. }
  1171. }
  1172. /* Kill the following clause, if you dislike this way. */
  1173. else if (!sysctl_tcp_syncookies &&
  1174. (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
  1175. (sysctl_max_syn_backlog >> 2)) &&
  1176. (!peer || !peer->tcp_ts_stamp) &&
  1177. (!dst || !dst_metric(dst, RTAX_RTT))) {
  1178. /* Without syncookies last quarter of
  1179. * backlog is filled with destinations,
  1180. * proven to be alive.
  1181. * It means that we continue to communicate
  1182. * to destinations, already remembered
  1183. * to the moment of synflood.
  1184. */
  1185. LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open "
  1186. "request from %u.%u.%u.%u/%u\n",
  1187. NIPQUAD(saddr),
  1188. ntohs(skb->h.th->source));
  1189. dst_release(dst);
  1190. goto drop_and_free;
  1191. }
  1192. isn = tcp_v4_init_sequence(skb);
  1193. }
  1194. tcp_rsk(req)->snt_isn = isn;
  1195. if (tcp_v4_send_synack(sk, req, dst))
  1196. goto drop_and_free;
  1197. if (want_cookie) {
  1198. reqsk_free(req);
  1199. } else {
  1200. inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
  1201. }
  1202. return 0;
  1203. drop_and_free:
  1204. reqsk_free(req);
  1205. drop:
  1206. return 0;
  1207. }
  1208. /*
  1209. * The three way handshake has completed - we got a valid synack -
  1210. * now create the new socket.
  1211. */
  1212. struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
  1213. struct request_sock *req,
  1214. struct dst_entry *dst)
  1215. {
  1216. struct inet_request_sock *ireq;
  1217. struct inet_sock *newinet;
  1218. struct tcp_sock *newtp;
  1219. struct sock *newsk;
  1220. #ifdef CONFIG_TCP_MD5SIG
  1221. struct tcp_md5sig_key *key;
  1222. #endif
  1223. if (sk_acceptq_is_full(sk))
  1224. goto exit_overflow;
  1225. if (!dst && (dst = inet_csk_route_req(sk, req)) == NULL)
  1226. goto exit;
  1227. newsk = tcp_create_openreq_child(sk, req, skb);
  1228. if (!newsk)
  1229. goto exit;
  1230. newsk->sk_gso_type = SKB_GSO_TCPV4;
  1231. sk_setup_caps(newsk, dst);
  1232. newtp = tcp_sk(newsk);
  1233. newinet = inet_sk(newsk);
  1234. ireq = inet_rsk(req);
  1235. newinet->daddr = ireq->rmt_addr;
  1236. newinet->rcv_saddr = ireq->loc_addr;
  1237. newinet->saddr = ireq->loc_addr;
  1238. newinet->opt = ireq->opt;
  1239. ireq->opt = NULL;
  1240. newinet->mc_index = inet_iif(skb);
  1241. newinet->mc_ttl = skb->nh.iph->ttl;
  1242. inet_csk(newsk)->icsk_ext_hdr_len = 0;
  1243. if (newinet->opt)
  1244. inet_csk(newsk)->icsk_ext_hdr_len = newinet->opt->optlen;
  1245. newinet->id = newtp->write_seq ^ jiffies;
  1246. tcp_mtup_init(newsk);
  1247. tcp_sync_mss(newsk, dst_mtu(dst));
  1248. newtp->advmss = dst_metric(dst, RTAX_ADVMSS);
  1249. tcp_initialize_rcv_mss(newsk);
  1250. #ifdef CONFIG_TCP_MD5SIG
  1251. /* Copy over the MD5 key from the original socket */
  1252. if ((key = tcp_v4_md5_do_lookup(sk, newinet->daddr)) != NULL) {
  1253. /*
  1254. * We're using one, so create a matching key
  1255. * on the newsk structure. If we fail to get
  1256. * memory, then we end up not copying the key
  1257. * across. Shucks.
  1258. */
  1259. char *newkey = kmemdup(key->key, key->keylen, GFP_ATOMIC);
  1260. if (newkey != NULL)
  1261. tcp_v4_md5_do_add(newsk, inet_sk(sk)->daddr,
  1262. newkey, key->keylen);
  1263. }
  1264. #endif
  1265. __inet_hash(&tcp_hashinfo, newsk, 0);
  1266. __inet_inherit_port(&tcp_hashinfo, sk, newsk);
  1267. return newsk;
  1268. exit_overflow:
  1269. NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
  1270. exit:
  1271. NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
  1272. dst_release(dst);
  1273. return NULL;
  1274. }
  1275. static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
  1276. {
  1277. struct tcphdr *th = skb->h.th;
  1278. struct iphdr *iph = skb->nh.iph;
  1279. struct sock *nsk;
  1280. struct request_sock **prev;
  1281. /* Find possible connection requests. */
  1282. struct request_sock *req = inet_csk_search_req(sk, &prev, th->source,
  1283. iph->saddr, iph->daddr);
  1284. if (req)
  1285. return tcp_check_req(sk, skb, req, prev);
  1286. nsk = inet_lookup_established(&tcp_hashinfo, skb->nh.iph->saddr,
  1287. th->source, skb->nh.iph->daddr,
  1288. th->dest, inet_iif(skb));
  1289. if (nsk) {
  1290. if (nsk->sk_state != TCP_TIME_WAIT) {
  1291. bh_lock_sock(nsk);
  1292. return nsk;
  1293. }
  1294. inet_twsk_put(inet_twsk(nsk));
  1295. return NULL;
  1296. }
  1297. #ifdef CONFIG_SYN_COOKIES
  1298. if (!th->rst && !th->syn && th->ack)
  1299. sk = cookie_v4_check(sk, skb, &(IPCB(skb)->opt));
  1300. #endif
  1301. return sk;
  1302. }
  1303. static __sum16 tcp_v4_checksum_init(struct sk_buff *skb)
  1304. {
  1305. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  1306. if (!tcp_v4_check(skb->len, skb->nh.iph->saddr,
  1307. skb->nh.iph->daddr, skb->csum)) {
  1308. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1309. return 0;
  1310. }
  1311. }
  1312. skb->csum = csum_tcpudp_nofold(skb->nh.iph->saddr, skb->nh.iph->daddr,
  1313. skb->len, IPPROTO_TCP, 0);
  1314. if (skb->len <= 76) {
  1315. return __skb_checksum_complete(skb);
  1316. }
  1317. return 0;
  1318. }
  1319. /* The socket must have it's spinlock held when we get
  1320. * here.
  1321. *
  1322. * We have a potential double-lock case here, so even when
  1323. * doing backlog processing we use the BH locking scheme.
  1324. * This is because we cannot sleep with the original spinlock
  1325. * held.
  1326. */
  1327. int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
  1328. {
  1329. struct sock *rsk;
  1330. #ifdef CONFIG_TCP_MD5SIG
  1331. /*
  1332. * We really want to reject the packet as early as possible
  1333. * if:
  1334. * o We're expecting an MD5'd packet and this is no MD5 tcp option
  1335. * o There is an MD5 option and we're not expecting one
  1336. */
  1337. if (tcp_v4_inbound_md5_hash(sk, skb))
  1338. goto discard;
  1339. #endif
  1340. if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
  1341. TCP_CHECK_TIMER(sk);
  1342. if (tcp_rcv_established(sk, skb, skb->h.th, skb->len)) {
  1343. rsk = sk;
  1344. goto reset;
  1345. }
  1346. TCP_CHECK_TIMER(sk);
  1347. return 0;
  1348. }
  1349. if (skb->len < (skb->h.th->doff << 2) || tcp_checksum_complete(skb))
  1350. goto csum_err;
  1351. if (sk->sk_state == TCP_LISTEN) {
  1352. struct sock *nsk = tcp_v4_hnd_req(sk, skb);
  1353. if (!nsk)
  1354. goto discard;
  1355. if (nsk != sk) {
  1356. if (tcp_child_process(sk, nsk, skb)) {
  1357. rsk = nsk;
  1358. goto reset;
  1359. }
  1360. return 0;
  1361. }
  1362. }
  1363. TCP_CHECK_TIMER(sk);
  1364. if (tcp_rcv_state_process(sk, skb, skb->h.th, skb->len)) {
  1365. rsk = sk;
  1366. goto reset;
  1367. }
  1368. TCP_CHECK_TIMER(sk);
  1369. return 0;
  1370. reset:
  1371. tcp_v4_send_reset(rsk, skb);
  1372. discard:
  1373. kfree_skb(skb);
  1374. /* Be careful here. If this function gets more complicated and
  1375. * gcc suffers from register pressure on the x86, sk (in %ebx)
  1376. * might be destroyed here. This current version compiles correctly,
  1377. * but you have been warned.
  1378. */
  1379. return 0;
  1380. csum_err:
  1381. TCP_INC_STATS_BH(TCP_MIB_INERRS);
  1382. goto discard;
  1383. }
  1384. /*
  1385. * From tcp_input.c
  1386. */
  1387. int tcp_v4_rcv(struct sk_buff *skb)
  1388. {
  1389. struct tcphdr *th;
  1390. struct sock *sk;
  1391. int ret;
  1392. if (skb->pkt_type != PACKET_HOST)
  1393. goto discard_it;
  1394. /* Count it even if it's bad */
  1395. TCP_INC_STATS_BH(TCP_MIB_INSEGS);
  1396. if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
  1397. goto discard_it;
  1398. th = skb->h.th;
  1399. if (th->doff < sizeof(struct tcphdr) / 4)
  1400. goto bad_packet;
  1401. if (!pskb_may_pull(skb, th->doff * 4))
  1402. goto discard_it;
  1403. /* An explanation is required here, I think.
  1404. * Packet length and doff are validated by header prediction,
  1405. * provided case of th->doff==0 is eliminated.
  1406. * So, we defer the checks. */
  1407. if ((skb->ip_summed != CHECKSUM_UNNECESSARY &&
  1408. tcp_v4_checksum_init(skb)))
  1409. goto bad_packet;
  1410. th = skb->h.th;
  1411. TCP_SKB_CB(skb)->seq = ntohl(th->seq);
  1412. TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
  1413. skb->len - th->doff * 4);
  1414. TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
  1415. TCP_SKB_CB(skb)->when = 0;
  1416. TCP_SKB_CB(skb)->flags = skb->nh.iph->tos;
  1417. TCP_SKB_CB(skb)->sacked = 0;
  1418. sk = __inet_lookup(&tcp_hashinfo, skb->nh.iph->saddr, th->source,
  1419. skb->nh.iph->daddr, th->dest,
  1420. inet_iif(skb));
  1421. if (!sk)
  1422. goto no_tcp_socket;
  1423. process:
  1424. if (sk->sk_state == TCP_TIME_WAIT)
  1425. goto do_time_wait;
  1426. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  1427. goto discard_and_relse;
  1428. nf_reset(skb);
  1429. if (sk_filter(sk, skb))
  1430. goto discard_and_relse;
  1431. skb->dev = NULL;
  1432. bh_lock_sock_nested(sk);
  1433. ret = 0;
  1434. if (!sock_owned_by_user(sk)) {
  1435. #ifdef CONFIG_NET_DMA
  1436. struct tcp_sock *tp = tcp_sk(sk);
  1437. if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
  1438. tp->ucopy.dma_chan = get_softnet_dma();
  1439. if (tp->ucopy.dma_chan)
  1440. ret = tcp_v4_do_rcv(sk, skb);
  1441. else
  1442. #endif
  1443. {
  1444. if (!tcp_prequeue(sk, skb))
  1445. ret = tcp_v4_do_rcv(sk, skb);
  1446. }
  1447. } else
  1448. sk_add_backlog(sk, skb);
  1449. bh_unlock_sock(sk);
  1450. sock_put(sk);
  1451. return ret;
  1452. no_tcp_socket:
  1453. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1454. goto discard_it;
  1455. if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
  1456. bad_packet:
  1457. TCP_INC_STATS_BH(TCP_MIB_INERRS);
  1458. } else {
  1459. tcp_v4_send_reset(NULL, skb);
  1460. }
  1461. discard_it:
  1462. /* Discard frame. */
  1463. kfree_skb(skb);
  1464. return 0;
  1465. discard_and_relse:
  1466. sock_put(sk);
  1467. goto discard_it;
  1468. do_time_wait:
  1469. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  1470. inet_twsk_put(inet_twsk(sk));
  1471. goto discard_it;
  1472. }
  1473. if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
  1474. TCP_INC_STATS_BH(TCP_MIB_INERRS);
  1475. inet_twsk_put(inet_twsk(sk));
  1476. goto discard_it;
  1477. }
  1478. switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
  1479. case TCP_TW_SYN: {
  1480. struct sock *sk2 = inet_lookup_listener(&tcp_hashinfo,
  1481. skb->nh.iph->daddr,
  1482. th->dest,
  1483. inet_iif(skb));
  1484. if (sk2) {
  1485. inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row);
  1486. inet_twsk_put(inet_twsk(sk));
  1487. sk = sk2;
  1488. goto process;
  1489. }
  1490. /* Fall through to ACK */
  1491. }
  1492. case TCP_TW_ACK:
  1493. tcp_v4_timewait_ack(sk, skb);
  1494. break;
  1495. case TCP_TW_RST:
  1496. goto no_tcp_socket;
  1497. case TCP_TW_SUCCESS:;
  1498. }
  1499. goto discard_it;
  1500. }
  1501. /* VJ's idea. Save last timestamp seen from this destination
  1502. * and hold it at least for normal timewait interval to use for duplicate
  1503. * segment detection in subsequent connections, before they enter synchronized
  1504. * state.
  1505. */
  1506. int tcp_v4_remember_stamp(struct sock *sk)
  1507. {
  1508. struct inet_sock *inet = inet_sk(sk);
  1509. struct tcp_sock *tp = tcp_sk(sk);
  1510. struct rtable *rt = (struct rtable *)__sk_dst_get(sk);
  1511. struct inet_peer *peer = NULL;
  1512. int release_it = 0;
  1513. if (!rt || rt->rt_dst != inet->daddr) {
  1514. peer = inet_getpeer(inet->daddr, 1);
  1515. release_it = 1;
  1516. } else {
  1517. if (!rt->peer)
  1518. rt_bind_peer(rt, 1);
  1519. peer = rt->peer;
  1520. }
  1521. if (peer) {
  1522. if ((s32)(peer->tcp_ts - tp->rx_opt.ts_recent) <= 0 ||
  1523. (peer->tcp_ts_stamp + TCP_PAWS_MSL < xtime.tv_sec &&
  1524. peer->tcp_ts_stamp <= tp->rx_opt.ts_recent_stamp)) {
  1525. peer->tcp_ts_stamp = tp->rx_opt.ts_recent_stamp;
  1526. peer->tcp_ts = tp->rx_opt.ts_recent;
  1527. }
  1528. if (release_it)
  1529. inet_putpeer(peer);
  1530. return 1;
  1531. }
  1532. return 0;
  1533. }
  1534. int tcp_v4_tw_remember_stamp(struct inet_timewait_sock *tw)
  1535. {
  1536. struct inet_peer *peer = inet_getpeer(tw->tw_daddr, 1);
  1537. if (peer) {
  1538. const struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
  1539. if ((s32)(peer->tcp_ts - tcptw->tw_ts_recent) <= 0 ||
  1540. (peer->tcp_ts_stamp + TCP_PAWS_MSL < xtime.tv_sec &&
  1541. peer->tcp_ts_stamp <= tcptw->tw_ts_recent_stamp)) {
  1542. peer->tcp_ts_stamp = tcptw->tw_ts_recent_stamp;
  1543. peer->tcp_ts = tcptw->tw_ts_recent;
  1544. }
  1545. inet_putpeer(peer);
  1546. return 1;
  1547. }
  1548. return 0;
  1549. }
  1550. struct inet_connection_sock_af_ops ipv4_specific = {
  1551. .queue_xmit = ip_queue_xmit,
  1552. .send_check = tcp_v4_send_check,
  1553. .rebuild_header = inet_sk_rebuild_header,
  1554. .conn_request = tcp_v4_conn_request,
  1555. .syn_recv_sock = tcp_v4_syn_recv_sock,
  1556. .remember_stamp = tcp_v4_remember_stamp,
  1557. .net_header_len = sizeof(struct iphdr),
  1558. .setsockopt = ip_setsockopt,
  1559. .getsockopt = ip_getsockopt,
  1560. .addr2sockaddr = inet_csk_addr2sockaddr,
  1561. .sockaddr_len = sizeof(struct sockaddr_in),
  1562. #ifdef CONFIG_COMPAT
  1563. .compat_setsockopt = compat_ip_setsockopt,
  1564. .compat_getsockopt = compat_ip_getsockopt,
  1565. #endif
  1566. };
  1567. #ifdef CONFIG_TCP_MD5SIG
  1568. static struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
  1569. .md5_lookup = tcp_v4_md5_lookup,
  1570. .calc_md5_hash = tcp_v4_calc_md5_hash,
  1571. .md5_add = tcp_v4_md5_add_func,
  1572. .md5_parse = tcp_v4_parse_md5_keys,
  1573. };
  1574. #endif
  1575. /* NOTE: A lot of things set to zero explicitly by call to
  1576. * sk_alloc() so need not be done here.
  1577. */
  1578. static int tcp_v4_init_sock(struct sock *sk)
  1579. {
  1580. struct inet_connection_sock *icsk = inet_csk(sk);
  1581. struct tcp_sock *tp = tcp_sk(sk);
  1582. skb_queue_head_init(&tp->out_of_order_queue);
  1583. tcp_init_xmit_timers(sk);
  1584. tcp_prequeue_init(tp);
  1585. icsk->icsk_rto = TCP_TIMEOUT_INIT;
  1586. tp->mdev = TCP_TIMEOUT_INIT;
  1587. /* So many TCP implementations out there (incorrectly) count the
  1588. * initial SYN frame in their delayed-ACK and congestion control
  1589. * algorithms that we must have the following bandaid to talk
  1590. * efficiently to them. -DaveM
  1591. */
  1592. tp->snd_cwnd = 2;
  1593. /* See draft-stevens-tcpca-spec-01 for discussion of the
  1594. * initialization of these values.
  1595. */
  1596. tp->snd_ssthresh = 0x7fffffff; /* Infinity */
  1597. tp->snd_cwnd_clamp = ~0;
  1598. tp->mss_cache = 536;
  1599. tp->reordering = sysctl_tcp_reordering;
  1600. icsk->icsk_ca_ops = &tcp_init_congestion_ops;
  1601. sk->sk_state = TCP_CLOSE;
  1602. sk->sk_write_space = sk_stream_write_space;
  1603. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  1604. icsk->icsk_af_ops = &ipv4_specific;
  1605. icsk->icsk_sync_mss = tcp_sync_mss;
  1606. #ifdef CONFIG_TCP_MD5SIG
  1607. tp->af_specific = &tcp_sock_ipv4_specific;
  1608. #endif
  1609. sk->sk_sndbuf = sysctl_tcp_wmem[1];
  1610. sk->sk_rcvbuf = sysctl_tcp_rmem[1];
  1611. atomic_inc(&tcp_sockets_allocated);
  1612. return 0;
  1613. }
  1614. int tcp_v4_destroy_sock(struct sock *sk)
  1615. {
  1616. struct tcp_sock *tp = tcp_sk(sk);
  1617. tcp_clear_xmit_timers(sk);
  1618. tcp_cleanup_congestion_control(sk);
  1619. /* Cleanup up the write buffer. */
  1620. sk_stream_writequeue_purge(sk);
  1621. /* Cleans up our, hopefully empty, out_of_order_queue. */
  1622. __skb_queue_purge(&tp->out_of_order_queue);
  1623. #ifdef CONFIG_TCP_MD5SIG
  1624. /* Clean up the MD5 key list, if any */
  1625. if (tp->md5sig_info) {
  1626. tcp_v4_clear_md5_list(sk);
  1627. kfree(tp->md5sig_info);
  1628. tp->md5sig_info = NULL;
  1629. }
  1630. #endif
  1631. #ifdef CONFIG_NET_DMA
  1632. /* Cleans up our sk_async_wait_queue */
  1633. __skb_queue_purge(&sk->sk_async_wait_queue);
  1634. #endif
  1635. /* Clean prequeue, it must be empty really */
  1636. __skb_queue_purge(&tp->ucopy.prequeue);
  1637. /* Clean up a referenced TCP bind bucket. */
  1638. if (inet_csk(sk)->icsk_bind_hash)
  1639. inet_put_port(&tcp_hashinfo, sk);
  1640. /*
  1641. * If sendmsg cached page exists, toss it.
  1642. */
  1643. if (sk->sk_sndmsg_page) {
  1644. __free_page(sk->sk_sndmsg_page);
  1645. sk->sk_sndmsg_page = NULL;
  1646. }
  1647. atomic_dec(&tcp_sockets_allocated);
  1648. return 0;
  1649. }
  1650. EXPORT_SYMBOL(tcp_v4_destroy_sock);
  1651. #ifdef CONFIG_PROC_FS
  1652. /* Proc filesystem TCP sock list dumping. */
  1653. static inline struct inet_timewait_sock *tw_head(struct hlist_head *head)
  1654. {
  1655. return hlist_empty(head) ? NULL :
  1656. list_entry(head->first, struct inet_timewait_sock, tw_node);
  1657. }
  1658. static inline struct inet_timewait_sock *tw_next(struct inet_timewait_sock *tw)
  1659. {
  1660. return tw->tw_node.next ?
  1661. hlist_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
  1662. }
  1663. static void *listening_get_next(struct seq_file *seq, void *cur)
  1664. {
  1665. struct inet_connection_sock *icsk;
  1666. struct hlist_node *node;
  1667. struct sock *sk = cur;
  1668. struct tcp_iter_state* st = seq->private;
  1669. if (!sk) {
  1670. st->bucket = 0;
  1671. sk = sk_head(&tcp_hashinfo.listening_hash[0]);
  1672. goto get_sk;
  1673. }
  1674. ++st->num;
  1675. if (st->state == TCP_SEQ_STATE_OPENREQ) {
  1676. struct request_sock *req = cur;
  1677. icsk = inet_csk(st->syn_wait_sk);
  1678. req = req->dl_next;
  1679. while (1) {
  1680. while (req) {
  1681. if (req->rsk_ops->family == st->family) {
  1682. cur = req;
  1683. goto out;
  1684. }
  1685. req = req->dl_next;
  1686. }
  1687. if (++st->sbucket >= icsk->icsk_accept_queue.listen_opt->nr_table_entries)
  1688. break;
  1689. get_req:
  1690. req = icsk->icsk_accept_queue.listen_opt->syn_table[st->sbucket];
  1691. }
  1692. sk = sk_next(st->syn_wait_sk);
  1693. st->state = TCP_SEQ_STATE_LISTENING;
  1694. read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1695. } else {
  1696. icsk = inet_csk(sk);
  1697. read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1698. if (reqsk_queue_len(&icsk->icsk_accept_queue))
  1699. goto start_req;
  1700. read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1701. sk = sk_next(sk);
  1702. }
  1703. get_sk:
  1704. sk_for_each_from(sk, node) {
  1705. if (sk->sk_family == st->family) {
  1706. cur = sk;
  1707. goto out;
  1708. }
  1709. icsk = inet_csk(sk);
  1710. read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1711. if (reqsk_queue_len(&icsk->icsk_accept_queue)) {
  1712. start_req:
  1713. st->uid = sock_i_uid(sk);
  1714. st->syn_wait_sk = sk;
  1715. st->state = TCP_SEQ_STATE_OPENREQ;
  1716. st->sbucket = 0;
  1717. goto get_req;
  1718. }
  1719. read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1720. }
  1721. if (++st->bucket < INET_LHTABLE_SIZE) {
  1722. sk = sk_head(&tcp_hashinfo.listening_hash[st->bucket]);
  1723. goto get_sk;
  1724. }
  1725. cur = NULL;
  1726. out:
  1727. return cur;
  1728. }
  1729. static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
  1730. {
  1731. void *rc = listening_get_next(seq, NULL);
  1732. while (rc && *pos) {
  1733. rc = listening_get_next(seq, rc);
  1734. --*pos;
  1735. }
  1736. return rc;
  1737. }
  1738. static void *established_get_first(struct seq_file *seq)
  1739. {
  1740. struct tcp_iter_state* st = seq->private;
  1741. void *rc = NULL;
  1742. for (st->bucket = 0; st->bucket < tcp_hashinfo.ehash_size; ++st->bucket) {
  1743. struct sock *sk;
  1744. struct hlist_node *node;
  1745. struct inet_timewait_sock *tw;
  1746. /* We can reschedule _before_ having picked the target: */
  1747. cond_resched_softirq();
  1748. read_lock(&tcp_hashinfo.ehash[st->bucket].lock);
  1749. sk_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
  1750. if (sk->sk_family != st->family) {
  1751. continue;
  1752. }
  1753. rc = sk;
  1754. goto out;
  1755. }
  1756. st->state = TCP_SEQ_STATE_TIME_WAIT;
  1757. inet_twsk_for_each(tw, node,
  1758. &tcp_hashinfo.ehash[st->bucket].twchain) {
  1759. if (tw->tw_family != st->family) {
  1760. continue;
  1761. }
  1762. rc = tw;
  1763. goto out;
  1764. }
  1765. read_unlock(&tcp_hashinfo.ehash[st->bucket].lock);
  1766. st->state = TCP_SEQ_STATE_ESTABLISHED;
  1767. }
  1768. out:
  1769. return rc;
  1770. }
  1771. static void *established_get_next(struct seq_file *seq, void *cur)
  1772. {
  1773. struct sock *sk = cur;
  1774. struct inet_timewait_sock *tw;
  1775. struct hlist_node *node;
  1776. struct tcp_iter_state* st = seq->private;
  1777. ++st->num;
  1778. if (st->state == TCP_SEQ_STATE_TIME_WAIT) {
  1779. tw = cur;
  1780. tw = tw_next(tw);
  1781. get_tw:
  1782. while (tw && tw->tw_family != st->family) {
  1783. tw = tw_next(tw);
  1784. }
  1785. if (tw) {
  1786. cur = tw;
  1787. goto out;
  1788. }
  1789. read_unlock(&tcp_hashinfo.ehash[st->bucket].lock);
  1790. st->state = TCP_SEQ_STATE_ESTABLISHED;
  1791. /* We can reschedule between buckets: */
  1792. cond_resched_softirq();
  1793. if (++st->bucket < tcp_hashinfo.ehash_size) {
  1794. read_lock(&tcp_hashinfo.ehash[st->bucket].lock);
  1795. sk = sk_head(&tcp_hashinfo.ehash[st->bucket].chain);
  1796. } else {
  1797. cur = NULL;
  1798. goto out;
  1799. }
  1800. } else
  1801. sk = sk_next(sk);
  1802. sk_for_each_from(sk, node) {
  1803. if (sk->sk_family == st->family)
  1804. goto found;
  1805. }
  1806. st->state = TCP_SEQ_STATE_TIME_WAIT;
  1807. tw = tw_head(&tcp_hashinfo.ehash[st->bucket].twchain);
  1808. goto get_tw;
  1809. found:
  1810. cur = sk;
  1811. out:
  1812. return cur;
  1813. }
  1814. static void *established_get_idx(struct seq_file *seq, loff_t pos)
  1815. {
  1816. void *rc = established_get_first(seq);
  1817. while (rc && pos) {
  1818. rc = established_get_next(seq, rc);
  1819. --pos;
  1820. }
  1821. return rc;
  1822. }
  1823. static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
  1824. {
  1825. void *rc;
  1826. struct tcp_iter_state* st = seq->private;
  1827. inet_listen_lock(&tcp_hashinfo);
  1828. st->state = TCP_SEQ_STATE_LISTENING;
  1829. rc = listening_get_idx(seq, &pos);
  1830. if (!rc) {
  1831. inet_listen_unlock(&tcp_hashinfo);
  1832. local_bh_disable();
  1833. st->state = TCP_SEQ_STATE_ESTABLISHED;
  1834. rc = established_get_idx(seq, pos);
  1835. }
  1836. return rc;
  1837. }
  1838. static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
  1839. {
  1840. struct tcp_iter_state* st = seq->private;
  1841. st->state = TCP_SEQ_STATE_LISTENING;
  1842. st->num = 0;
  1843. return *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1844. }
  1845. static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1846. {
  1847. void *rc = NULL;
  1848. struct tcp_iter_state* st;
  1849. if (v == SEQ_START_TOKEN) {
  1850. rc = tcp_get_idx(seq, 0);
  1851. goto out;
  1852. }
  1853. st = seq->private;
  1854. switch (st->state) {
  1855. case TCP_SEQ_STATE_OPENREQ:
  1856. case TCP_SEQ_STATE_LISTENING:
  1857. rc = listening_get_next(seq, v);
  1858. if (!rc) {
  1859. inet_listen_unlock(&tcp_hashinfo);
  1860. local_bh_disable();
  1861. st->state = TCP_SEQ_STATE_ESTABLISHED;
  1862. rc = established_get_first(seq);
  1863. }
  1864. break;
  1865. case TCP_SEQ_STATE_ESTABLISHED:
  1866. case TCP_SEQ_STATE_TIME_WAIT:
  1867. rc = established_get_next(seq, v);
  1868. break;
  1869. }
  1870. out:
  1871. ++*pos;
  1872. return rc;
  1873. }
  1874. static void tcp_seq_stop(struct seq_file *seq, void *v)
  1875. {
  1876. struct tcp_iter_state* st = seq->private;
  1877. switch (st->state) {
  1878. case TCP_SEQ_STATE_OPENREQ:
  1879. if (v) {
  1880. struct inet_connection_sock *icsk = inet_csk(st->syn_wait_sk);
  1881. read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1882. }
  1883. case TCP_SEQ_STATE_LISTENING:
  1884. if (v != SEQ_START_TOKEN)
  1885. inet_listen_unlock(&tcp_hashinfo);
  1886. break;
  1887. case TCP_SEQ_STATE_TIME_WAIT:
  1888. case TCP_SEQ_STATE_ESTABLISHED:
  1889. if (v)
  1890. read_unlock(&tcp_hashinfo.ehash[st->bucket].lock);
  1891. local_bh_enable();
  1892. break;
  1893. }
  1894. }
  1895. static int tcp_seq_open(struct inode *inode, struct file *file)
  1896. {
  1897. struct tcp_seq_afinfo *afinfo = PDE(inode)->data;
  1898. struct seq_file *seq;
  1899. struct tcp_iter_state *s;
  1900. int rc;
  1901. if (unlikely(afinfo == NULL))
  1902. return -EINVAL;
  1903. s = kzalloc(sizeof(*s), GFP_KERNEL);
  1904. if (!s)
  1905. return -ENOMEM;
  1906. s->family = afinfo->family;
  1907. s->seq_ops.start = tcp_seq_start;
  1908. s->seq_ops.next = tcp_seq_next;
  1909. s->seq_ops.show = afinfo->seq_show;
  1910. s->seq_ops.stop = tcp_seq_stop;
  1911. rc = seq_open(file, &s->seq_ops);
  1912. if (rc)
  1913. goto out_kfree;
  1914. seq = file->private_data;
  1915. seq->private = s;
  1916. out:
  1917. return rc;
  1918. out_kfree:
  1919. kfree(s);
  1920. goto out;
  1921. }
  1922. int tcp_proc_register(struct tcp_seq_afinfo *afinfo)
  1923. {
  1924. int rc = 0;
  1925. struct proc_dir_entry *p;
  1926. if (!afinfo)
  1927. return -EINVAL;
  1928. afinfo->seq_fops->owner = afinfo->owner;
  1929. afinfo->seq_fops->open = tcp_seq_open;
  1930. afinfo->seq_fops->read = seq_read;
  1931. afinfo->seq_fops->llseek = seq_lseek;
  1932. afinfo->seq_fops->release = seq_release_private;
  1933. p = proc_net_fops_create(afinfo->name, S_IRUGO, afinfo->seq_fops);
  1934. if (p)
  1935. p->data = afinfo;
  1936. else
  1937. rc = -ENOMEM;
  1938. return rc;
  1939. }
  1940. void tcp_proc_unregister(struct tcp_seq_afinfo *afinfo)
  1941. {
  1942. if (!afinfo)
  1943. return;
  1944. proc_net_remove(afinfo->name);
  1945. memset(afinfo->seq_fops, 0, sizeof(*afinfo->seq_fops));
  1946. }
  1947. static void get_openreq4(struct sock *sk, struct request_sock *req,
  1948. char *tmpbuf, int i, int uid)
  1949. {
  1950. const struct inet_request_sock *ireq = inet_rsk(req);
  1951. int ttd = req->expires - jiffies;
  1952. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
  1953. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %p",
  1954. i,
  1955. ireq->loc_addr,
  1956. ntohs(inet_sk(sk)->sport),
  1957. ireq->rmt_addr,
  1958. ntohs(ireq->rmt_port),
  1959. TCP_SYN_RECV,
  1960. 0, 0, /* could print option size, but that is af dependent. */
  1961. 1, /* timers active (only the expire timer) */
  1962. jiffies_to_clock_t(ttd),
  1963. req->retrans,
  1964. uid,
  1965. 0, /* non standard timer */
  1966. 0, /* open_requests have no inode */
  1967. atomic_read(&sk->sk_refcnt),
  1968. req);
  1969. }
  1970. static void get_tcp4_sock(struct sock *sp, char *tmpbuf, int i)
  1971. {
  1972. int timer_active;
  1973. unsigned long timer_expires;
  1974. struct tcp_sock *tp = tcp_sk(sp);
  1975. const struct inet_connection_sock *icsk = inet_csk(sp);
  1976. struct inet_sock *inet = inet_sk(sp);
  1977. __be32 dest = inet->daddr;
  1978. __be32 src = inet->rcv_saddr;
  1979. __u16 destp = ntohs(inet->dport);
  1980. __u16 srcp = ntohs(inet->sport);
  1981. if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
  1982. timer_active = 1;
  1983. timer_expires = icsk->icsk_timeout;
  1984. } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
  1985. timer_active = 4;
  1986. timer_expires = icsk->icsk_timeout;
  1987. } else if (timer_pending(&sp->sk_timer)) {
  1988. timer_active = 2;
  1989. timer_expires = sp->sk_timer.expires;
  1990. } else {
  1991. timer_active = 0;
  1992. timer_expires = jiffies;
  1993. }
  1994. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
  1995. "%08X %5d %8d %lu %d %p %u %u %u %u %d",
  1996. i, src, srcp, dest, destp, sp->sk_state,
  1997. tp->write_seq - tp->snd_una,
  1998. sp->sk_state == TCP_LISTEN ? sp->sk_ack_backlog :
  1999. (tp->rcv_nxt - tp->copied_seq),
  2000. timer_active,
  2001. jiffies_to_clock_t(timer_expires - jiffies),
  2002. icsk->icsk_retransmits,
  2003. sock_i_uid(sp),
  2004. icsk->icsk_probes_out,
  2005. sock_i_ino(sp),
  2006. atomic_read(&sp->sk_refcnt), sp,
  2007. icsk->icsk_rto,
  2008. icsk->icsk_ack.ato,
  2009. (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
  2010. tp->snd_cwnd,
  2011. tp->snd_ssthresh >= 0xFFFF ? -1 : tp->snd_ssthresh);
  2012. }
  2013. static void get_timewait4_sock(struct inet_timewait_sock *tw,
  2014. char *tmpbuf, int i)
  2015. {
  2016. __be32 dest, src;
  2017. __u16 destp, srcp;
  2018. int ttd = tw->tw_ttd - jiffies;
  2019. if (ttd < 0)
  2020. ttd = 0;
  2021. dest = tw->tw_daddr;
  2022. src = tw->tw_rcv_saddr;
  2023. destp = ntohs(tw->tw_dport);
  2024. srcp = ntohs(tw->tw_sport);
  2025. sprintf(tmpbuf, "%4d: %08X:%04X %08X:%04X"
  2026. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p",
  2027. i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
  2028. 3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
  2029. atomic_read(&tw->tw_refcnt), tw);
  2030. }
  2031. #define TMPSZ 150
  2032. static int tcp4_seq_show(struct seq_file *seq, void *v)
  2033. {
  2034. struct tcp_iter_state* st;
  2035. char tmpbuf[TMPSZ + 1];
  2036. if (v == SEQ_START_TOKEN) {
  2037. seq_printf(seq, "%-*s\n", TMPSZ - 1,
  2038. " sl local_address rem_address st tx_queue "
  2039. "rx_queue tr tm->when retrnsmt uid timeout "
  2040. "inode");
  2041. goto out;
  2042. }
  2043. st = seq->private;
  2044. switch (st->state) {
  2045. case TCP_SEQ_STATE_LISTENING:
  2046. case TCP_SEQ_STATE_ESTABLISHED:
  2047. get_tcp4_sock(v, tmpbuf, st->num);
  2048. break;
  2049. case TCP_SEQ_STATE_OPENREQ:
  2050. get_openreq4(st->syn_wait_sk, v, tmpbuf, st->num, st->uid);
  2051. break;
  2052. case TCP_SEQ_STATE_TIME_WAIT:
  2053. get_timewait4_sock(v, tmpbuf, st->num);
  2054. break;
  2055. }
  2056. seq_printf(seq, "%-*s\n", TMPSZ - 1, tmpbuf);
  2057. out:
  2058. return 0;
  2059. }
  2060. static struct file_operations tcp4_seq_fops;
  2061. static struct tcp_seq_afinfo tcp4_seq_afinfo = {
  2062. .owner = THIS_MODULE,
  2063. .name = "tcp",
  2064. .family = AF_INET,
  2065. .seq_show = tcp4_seq_show,
  2066. .seq_fops = &tcp4_seq_fops,
  2067. };
  2068. int __init tcp4_proc_init(void)
  2069. {
  2070. return tcp_proc_register(&tcp4_seq_afinfo);
  2071. }
  2072. void tcp4_proc_exit(void)
  2073. {
  2074. tcp_proc_unregister(&tcp4_seq_afinfo);
  2075. }
  2076. #endif /* CONFIG_PROC_FS */
  2077. struct proto tcp_prot = {
  2078. .name = "TCP",
  2079. .owner = THIS_MODULE,
  2080. .close = tcp_close,
  2081. .connect = tcp_v4_connect,
  2082. .disconnect = tcp_disconnect,
  2083. .accept = inet_csk_accept,
  2084. .ioctl = tcp_ioctl,
  2085. .init = tcp_v4_init_sock,
  2086. .destroy = tcp_v4_destroy_sock,
  2087. .shutdown = tcp_shutdown,
  2088. .setsockopt = tcp_setsockopt,
  2089. .getsockopt = tcp_getsockopt,
  2090. .sendmsg = tcp_sendmsg,
  2091. .recvmsg = tcp_recvmsg,
  2092. .backlog_rcv = tcp_v4_do_rcv,
  2093. .hash = tcp_v4_hash,
  2094. .unhash = tcp_unhash,
  2095. .get_port = tcp_v4_get_port,
  2096. .enter_memory_pressure = tcp_enter_memory_pressure,
  2097. .sockets_allocated = &tcp_sockets_allocated,
  2098. .orphan_count = &tcp_orphan_count,
  2099. .memory_allocated = &tcp_memory_allocated,
  2100. .memory_pressure = &tcp_memory_pressure,
  2101. .sysctl_mem = sysctl_tcp_mem,
  2102. .sysctl_wmem = sysctl_tcp_wmem,
  2103. .sysctl_rmem = sysctl_tcp_rmem,
  2104. .max_header = MAX_TCP_HEADER,
  2105. .obj_size = sizeof(struct tcp_sock),
  2106. .twsk_prot = &tcp_timewait_sock_ops,
  2107. .rsk_prot = &tcp_request_sock_ops,
  2108. #ifdef CONFIG_COMPAT
  2109. .compat_setsockopt = compat_tcp_setsockopt,
  2110. .compat_getsockopt = compat_tcp_getsockopt,
  2111. #endif
  2112. };
  2113. void __init tcp_v4_init(struct net_proto_family *ops)
  2114. {
  2115. if (inet_csk_ctl_sock_create(&tcp_socket, PF_INET, SOCK_RAW,
  2116. IPPROTO_TCP) < 0)
  2117. panic("Failed to create the TCP control socket.\n");
  2118. }
  2119. EXPORT_SYMBOL(ipv4_specific);
  2120. EXPORT_SYMBOL(tcp_hashinfo);
  2121. EXPORT_SYMBOL(tcp_prot);
  2122. EXPORT_SYMBOL(tcp_unhash);
  2123. EXPORT_SYMBOL(tcp_v4_conn_request);
  2124. EXPORT_SYMBOL(tcp_v4_connect);
  2125. EXPORT_SYMBOL(tcp_v4_do_rcv);
  2126. EXPORT_SYMBOL(tcp_v4_remember_stamp);
  2127. EXPORT_SYMBOL(tcp_v4_send_check);
  2128. EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
  2129. #ifdef CONFIG_PROC_FS
  2130. EXPORT_SYMBOL(tcp_proc_register);
  2131. EXPORT_SYMBOL(tcp_proc_unregister);
  2132. #endif
  2133. EXPORT_SYMBOL(sysctl_local_port_range);
  2134. EXPORT_SYMBOL(sysctl_tcp_low_latency);