tcp_ipv4.c 65 KB

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