tcp_ipv4.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498
  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. #define pr_fmt(fmt) "TCP: " fmt
  52. #include <linux/bottom_half.h>
  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 <linux/slab.h>
  62. #include <net/net_namespace.h>
  63. #include <net/icmp.h>
  64. #include <net/inet_hashtables.h>
  65. #include <net/tcp.h>
  66. #include <net/transp_v6.h>
  67. #include <net/ipv6.h>
  68. #include <net/inet_common.h>
  69. #include <net/timewait_sock.h>
  70. #include <net/xfrm.h>
  71. #include <net/secure_seq.h>
  72. #include <net/busy_poll.h>
  73. #include <linux/inet.h>
  74. #include <linux/ipv6.h>
  75. #include <linux/stddef.h>
  76. #include <linux/proc_fs.h>
  77. #include <linux/seq_file.h>
  78. #include <linux/inetdevice.h>
  79. #include <crypto/hash.h>
  80. #include <linux/scatterlist.h>
  81. #ifdef CONFIG_TCP_MD5SIG
  82. static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
  83. __be32 daddr, __be32 saddr, const struct tcphdr *th);
  84. #endif
  85. struct inet_hashinfo tcp_hashinfo;
  86. EXPORT_SYMBOL(tcp_hashinfo);
  87. static u32 tcp_v4_init_seq(const struct sk_buff *skb)
  88. {
  89. return secure_tcp_seq(ip_hdr(skb)->daddr,
  90. ip_hdr(skb)->saddr,
  91. tcp_hdr(skb)->dest,
  92. tcp_hdr(skb)->source);
  93. }
  94. static u32 tcp_v4_init_ts_off(const struct net *net, const struct sk_buff *skb)
  95. {
  96. return secure_tcp_ts_off(net, ip_hdr(skb)->daddr, ip_hdr(skb)->saddr);
  97. }
  98. int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
  99. {
  100. const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw);
  101. struct tcp_sock *tp = tcp_sk(sk);
  102. /* With PAWS, it is safe from the viewpoint
  103. of data integrity. Even without PAWS it is safe provided sequence
  104. spaces do not overlap i.e. at data rates <= 80Mbit/sec.
  105. Actually, the idea is close to VJ's one, only timestamp cache is
  106. held not per host, but per port pair and TW bucket is used as state
  107. holder.
  108. If TW bucket has been already destroyed we fall back to VJ's scheme
  109. and use initial timestamp retrieved from peer table.
  110. */
  111. if (tcptw->tw_ts_recent_stamp &&
  112. (!twp || (sock_net(sk)->ipv4.sysctl_tcp_tw_reuse &&
  113. get_seconds() - tcptw->tw_ts_recent_stamp > 1))) {
  114. tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2;
  115. if (tp->write_seq == 0)
  116. tp->write_seq = 1;
  117. tp->rx_opt.ts_recent = tcptw->tw_ts_recent;
  118. tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
  119. sock_hold(sktw);
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. EXPORT_SYMBOL_GPL(tcp_twsk_unique);
  125. /* This will initiate an outgoing connection. */
  126. int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  127. {
  128. struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
  129. struct inet_sock *inet = inet_sk(sk);
  130. struct tcp_sock *tp = tcp_sk(sk);
  131. __be16 orig_sport, orig_dport;
  132. __be32 daddr, nexthop;
  133. struct flowi4 *fl4;
  134. struct rtable *rt;
  135. int err;
  136. struct ip_options_rcu *inet_opt;
  137. struct inet_timewait_death_row *tcp_death_row = &sock_net(sk)->ipv4.tcp_death_row;
  138. if (addr_len < sizeof(struct sockaddr_in))
  139. return -EINVAL;
  140. if (usin->sin_family != AF_INET)
  141. return -EAFNOSUPPORT;
  142. nexthop = daddr = usin->sin_addr.s_addr;
  143. inet_opt = rcu_dereference_protected(inet->inet_opt,
  144. lockdep_sock_is_held(sk));
  145. if (inet_opt && inet_opt->opt.srr) {
  146. if (!daddr)
  147. return -EINVAL;
  148. nexthop = inet_opt->opt.faddr;
  149. }
  150. orig_sport = inet->inet_sport;
  151. orig_dport = usin->sin_port;
  152. fl4 = &inet->cork.fl.u.ip4;
  153. rt = ip_route_connect(fl4, nexthop, inet->inet_saddr,
  154. RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
  155. IPPROTO_TCP,
  156. orig_sport, orig_dport, sk);
  157. if (IS_ERR(rt)) {
  158. err = PTR_ERR(rt);
  159. if (err == -ENETUNREACH)
  160. IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
  161. return err;
  162. }
  163. if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
  164. ip_rt_put(rt);
  165. return -ENETUNREACH;
  166. }
  167. if (!inet_opt || !inet_opt->opt.srr)
  168. daddr = fl4->daddr;
  169. if (!inet->inet_saddr)
  170. inet->inet_saddr = fl4->saddr;
  171. sk_rcv_saddr_set(sk, inet->inet_saddr);
  172. if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) {
  173. /* Reset inherited state */
  174. tp->rx_opt.ts_recent = 0;
  175. tp->rx_opt.ts_recent_stamp = 0;
  176. if (likely(!tp->repair))
  177. tp->write_seq = 0;
  178. }
  179. inet->inet_dport = usin->sin_port;
  180. sk_daddr_set(sk, daddr);
  181. inet_csk(sk)->icsk_ext_hdr_len = 0;
  182. if (inet_opt)
  183. inet_csk(sk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
  184. tp->rx_opt.mss_clamp = TCP_MSS_DEFAULT;
  185. /* Socket identity is still unknown (sport may be zero).
  186. * However we set state to SYN-SENT and not releasing socket
  187. * lock select source port, enter ourselves into the hash tables and
  188. * complete initialization after this.
  189. */
  190. tcp_set_state(sk, TCP_SYN_SENT);
  191. err = inet_hash_connect(tcp_death_row, sk);
  192. if (err)
  193. goto failure;
  194. sk_set_txhash(sk);
  195. rt = ip_route_newports(fl4, rt, orig_sport, orig_dport,
  196. inet->inet_sport, inet->inet_dport, sk);
  197. if (IS_ERR(rt)) {
  198. err = PTR_ERR(rt);
  199. rt = NULL;
  200. goto failure;
  201. }
  202. /* OK, now commit destination to socket. */
  203. sk->sk_gso_type = SKB_GSO_TCPV4;
  204. sk_setup_caps(sk, &rt->dst);
  205. rt = NULL;
  206. if (likely(!tp->repair)) {
  207. if (!tp->write_seq)
  208. tp->write_seq = secure_tcp_seq(inet->inet_saddr,
  209. inet->inet_daddr,
  210. inet->inet_sport,
  211. usin->sin_port);
  212. tp->tsoffset = secure_tcp_ts_off(sock_net(sk),
  213. inet->inet_saddr,
  214. inet->inet_daddr);
  215. }
  216. inet->inet_id = tp->write_seq ^ jiffies;
  217. if (tcp_fastopen_defer_connect(sk, &err))
  218. return err;
  219. if (err)
  220. goto failure;
  221. err = tcp_connect(sk);
  222. if (err)
  223. goto failure;
  224. return 0;
  225. failure:
  226. /*
  227. * This unhashes the socket and releases the local port,
  228. * if necessary.
  229. */
  230. tcp_set_state(sk, TCP_CLOSE);
  231. ip_rt_put(rt);
  232. sk->sk_route_caps = 0;
  233. inet->inet_dport = 0;
  234. return err;
  235. }
  236. EXPORT_SYMBOL(tcp_v4_connect);
  237. /*
  238. * This routine reacts to ICMP_FRAG_NEEDED mtu indications as defined in RFC1191.
  239. * It can be called through tcp_release_cb() if socket was owned by user
  240. * at the time tcp_v4_err() was called to handle ICMP message.
  241. */
  242. void tcp_v4_mtu_reduced(struct sock *sk)
  243. {
  244. struct inet_sock *inet = inet_sk(sk);
  245. struct dst_entry *dst;
  246. u32 mtu;
  247. if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
  248. return;
  249. mtu = tcp_sk(sk)->mtu_info;
  250. dst = inet_csk_update_pmtu(sk, mtu);
  251. if (!dst)
  252. return;
  253. /* Something is about to be wrong... Remember soft error
  254. * for the case, if this connection will not able to recover.
  255. */
  256. if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
  257. sk->sk_err_soft = EMSGSIZE;
  258. mtu = dst_mtu(dst);
  259. if (inet->pmtudisc != IP_PMTUDISC_DONT &&
  260. ip_sk_accept_pmtu(sk) &&
  261. inet_csk(sk)->icsk_pmtu_cookie > mtu) {
  262. tcp_sync_mss(sk, mtu);
  263. /* Resend the TCP packet because it's
  264. * clear that the old packet has been
  265. * dropped. This is the new "fast" path mtu
  266. * discovery.
  267. */
  268. tcp_simple_retransmit(sk);
  269. } /* else let the usual retransmit timer handle it */
  270. }
  271. EXPORT_SYMBOL(tcp_v4_mtu_reduced);
  272. static void do_redirect(struct sk_buff *skb, struct sock *sk)
  273. {
  274. struct dst_entry *dst = __sk_dst_check(sk, 0);
  275. if (dst)
  276. dst->ops->redirect(dst, sk, skb);
  277. }
  278. /* handle ICMP messages on TCP_NEW_SYN_RECV request sockets */
  279. void tcp_req_err(struct sock *sk, u32 seq, bool abort)
  280. {
  281. struct request_sock *req = inet_reqsk(sk);
  282. struct net *net = sock_net(sk);
  283. /* ICMPs are not backlogged, hence we cannot get
  284. * an established socket here.
  285. */
  286. if (seq != tcp_rsk(req)->snt_isn) {
  287. __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
  288. } else if (abort) {
  289. /*
  290. * Still in SYN_RECV, just remove it silently.
  291. * There is no good way to pass the error to the newly
  292. * created socket, and POSIX does not want network
  293. * errors returned from accept().
  294. */
  295. inet_csk_reqsk_queue_drop(req->rsk_listener, req);
  296. tcp_listendrop(req->rsk_listener);
  297. }
  298. reqsk_put(req);
  299. }
  300. EXPORT_SYMBOL(tcp_req_err);
  301. /*
  302. * This routine is called by the ICMP module when it gets some
  303. * sort of error condition. If err < 0 then the socket should
  304. * be closed and the error returned to the user. If err > 0
  305. * it's just the icmp type << 8 | icmp code. After adjustment
  306. * header points to the first 8 bytes of the tcp header. We need
  307. * to find the appropriate port.
  308. *
  309. * The locking strategy used here is very "optimistic". When
  310. * someone else accesses the socket the ICMP is just dropped
  311. * and for some paths there is no check at all.
  312. * A more general error queue to queue errors for later handling
  313. * is probably better.
  314. *
  315. */
  316. void tcp_v4_err(struct sk_buff *icmp_skb, u32 info)
  317. {
  318. const struct iphdr *iph = (const struct iphdr *)icmp_skb->data;
  319. struct tcphdr *th = (struct tcphdr *)(icmp_skb->data + (iph->ihl << 2));
  320. struct inet_connection_sock *icsk;
  321. struct tcp_sock *tp;
  322. struct inet_sock *inet;
  323. const int type = icmp_hdr(icmp_skb)->type;
  324. const int code = icmp_hdr(icmp_skb)->code;
  325. struct sock *sk;
  326. struct sk_buff *skb;
  327. struct request_sock *fastopen;
  328. u32 seq, snd_una;
  329. s32 remaining;
  330. u32 delta_us;
  331. int err;
  332. struct net *net = dev_net(icmp_skb->dev);
  333. sk = __inet_lookup_established(net, &tcp_hashinfo, iph->daddr,
  334. th->dest, iph->saddr, ntohs(th->source),
  335. inet_iif(icmp_skb), 0);
  336. if (!sk) {
  337. __ICMP_INC_STATS(net, ICMP_MIB_INERRORS);
  338. return;
  339. }
  340. if (sk->sk_state == TCP_TIME_WAIT) {
  341. inet_twsk_put(inet_twsk(sk));
  342. return;
  343. }
  344. seq = ntohl(th->seq);
  345. if (sk->sk_state == TCP_NEW_SYN_RECV)
  346. return tcp_req_err(sk, seq,
  347. type == ICMP_PARAMETERPROB ||
  348. type == ICMP_TIME_EXCEEDED ||
  349. (type == ICMP_DEST_UNREACH &&
  350. (code == ICMP_NET_UNREACH ||
  351. code == ICMP_HOST_UNREACH)));
  352. bh_lock_sock(sk);
  353. /* If too many ICMPs get dropped on busy
  354. * servers this needs to be solved differently.
  355. * We do take care of PMTU discovery (RFC1191) special case :
  356. * we can receive locally generated ICMP messages while socket is held.
  357. */
  358. if (sock_owned_by_user(sk)) {
  359. if (!(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED))
  360. __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
  361. }
  362. if (sk->sk_state == TCP_CLOSE)
  363. goto out;
  364. if (unlikely(iph->ttl < inet_sk(sk)->min_ttl)) {
  365. __NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
  366. goto out;
  367. }
  368. icsk = inet_csk(sk);
  369. tp = tcp_sk(sk);
  370. /* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */
  371. fastopen = tp->fastopen_rsk;
  372. snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : tp->snd_una;
  373. if (sk->sk_state != TCP_LISTEN &&
  374. !between(seq, snd_una, tp->snd_nxt)) {
  375. __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
  376. goto out;
  377. }
  378. switch (type) {
  379. case ICMP_REDIRECT:
  380. if (!sock_owned_by_user(sk))
  381. do_redirect(icmp_skb, sk);
  382. goto out;
  383. case ICMP_SOURCE_QUENCH:
  384. /* Just silently ignore these. */
  385. goto out;
  386. case ICMP_PARAMETERPROB:
  387. err = EPROTO;
  388. break;
  389. case ICMP_DEST_UNREACH:
  390. if (code > NR_ICMP_UNREACH)
  391. goto out;
  392. if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
  393. /* We are not interested in TCP_LISTEN and open_requests
  394. * (SYN-ACKs send out by Linux are always <576bytes so
  395. * they should go through unfragmented).
  396. */
  397. if (sk->sk_state == TCP_LISTEN)
  398. goto out;
  399. tp->mtu_info = info;
  400. if (!sock_owned_by_user(sk)) {
  401. tcp_v4_mtu_reduced(sk);
  402. } else {
  403. if (!test_and_set_bit(TCP_MTU_REDUCED_DEFERRED, &sk->sk_tsq_flags))
  404. sock_hold(sk);
  405. }
  406. goto out;
  407. }
  408. err = icmp_err_convert[code].errno;
  409. /* check if icmp_skb allows revert of backoff
  410. * (see draft-zimmermann-tcp-lcd) */
  411. if (code != ICMP_NET_UNREACH && code != ICMP_HOST_UNREACH)
  412. break;
  413. if (seq != tp->snd_una || !icsk->icsk_retransmits ||
  414. !icsk->icsk_backoff || fastopen)
  415. break;
  416. if (sock_owned_by_user(sk))
  417. break;
  418. icsk->icsk_backoff--;
  419. icsk->icsk_rto = tp->srtt_us ? __tcp_set_rto(tp) :
  420. TCP_TIMEOUT_INIT;
  421. icsk->icsk_rto = inet_csk_rto_backoff(icsk, TCP_RTO_MAX);
  422. skb = tcp_write_queue_head(sk);
  423. BUG_ON(!skb);
  424. tcp_mstamp_refresh(tp);
  425. delta_us = (u32)(tp->tcp_mstamp - skb->skb_mstamp);
  426. remaining = icsk->icsk_rto -
  427. usecs_to_jiffies(delta_us);
  428. if (remaining > 0) {
  429. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  430. remaining, TCP_RTO_MAX);
  431. } else {
  432. /* RTO revert clocked out retransmission.
  433. * Will retransmit now */
  434. tcp_retransmit_timer(sk);
  435. }
  436. break;
  437. case ICMP_TIME_EXCEEDED:
  438. err = EHOSTUNREACH;
  439. break;
  440. default:
  441. goto out;
  442. }
  443. switch (sk->sk_state) {
  444. case TCP_SYN_SENT:
  445. case TCP_SYN_RECV:
  446. /* Only in fast or simultaneous open. If a fast open socket is
  447. * is already accepted it is treated as a connected one below.
  448. */
  449. if (fastopen && !fastopen->sk)
  450. break;
  451. if (!sock_owned_by_user(sk)) {
  452. sk->sk_err = err;
  453. sk->sk_error_report(sk);
  454. tcp_done(sk);
  455. } else {
  456. sk->sk_err_soft = err;
  457. }
  458. goto out;
  459. }
  460. /* If we've already connected we will keep trying
  461. * until we time out, or the user gives up.
  462. *
  463. * rfc1122 4.2.3.9 allows to consider as hard errors
  464. * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
  465. * but it is obsoleted by pmtu discovery).
  466. *
  467. * Note, that in modern internet, where routing is unreliable
  468. * and in each dark corner broken firewalls sit, sending random
  469. * errors ordered by their masters even this two messages finally lose
  470. * their original sense (even Linux sends invalid PORT_UNREACHs)
  471. *
  472. * Now we are in compliance with RFCs.
  473. * --ANK (980905)
  474. */
  475. inet = inet_sk(sk);
  476. if (!sock_owned_by_user(sk) && inet->recverr) {
  477. sk->sk_err = err;
  478. sk->sk_error_report(sk);
  479. } else { /* Only an error on timeout */
  480. sk->sk_err_soft = err;
  481. }
  482. out:
  483. bh_unlock_sock(sk);
  484. sock_put(sk);
  485. }
  486. void __tcp_v4_send_check(struct sk_buff *skb, __be32 saddr, __be32 daddr)
  487. {
  488. struct tcphdr *th = tcp_hdr(skb);
  489. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  490. th->check = ~tcp_v4_check(skb->len, saddr, daddr, 0);
  491. skb->csum_start = skb_transport_header(skb) - skb->head;
  492. skb->csum_offset = offsetof(struct tcphdr, check);
  493. } else {
  494. th->check = tcp_v4_check(skb->len, saddr, daddr,
  495. csum_partial(th,
  496. th->doff << 2,
  497. skb->csum));
  498. }
  499. }
  500. /* This routine computes an IPv4 TCP checksum. */
  501. void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb)
  502. {
  503. const struct inet_sock *inet = inet_sk(sk);
  504. __tcp_v4_send_check(skb, inet->inet_saddr, inet->inet_daddr);
  505. }
  506. EXPORT_SYMBOL(tcp_v4_send_check);
  507. /*
  508. * This routine will send an RST to the other tcp.
  509. *
  510. * Someone asks: why I NEVER use socket parameters (TOS, TTL etc.)
  511. * for reset.
  512. * Answer: if a packet caused RST, it is not for a socket
  513. * existing in our system, if it is matched to a socket,
  514. * it is just duplicate segment or bug in other side's TCP.
  515. * So that we build reply only basing on parameters
  516. * arrived with segment.
  517. * Exception: precedence violation. We do not implement it in any case.
  518. */
  519. static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb)
  520. {
  521. const struct tcphdr *th = tcp_hdr(skb);
  522. struct {
  523. struct tcphdr th;
  524. #ifdef CONFIG_TCP_MD5SIG
  525. __be32 opt[(TCPOLEN_MD5SIG_ALIGNED >> 2)];
  526. #endif
  527. } rep;
  528. struct ip_reply_arg arg;
  529. #ifdef CONFIG_TCP_MD5SIG
  530. struct tcp_md5sig_key *key = NULL;
  531. const __u8 *hash_location = NULL;
  532. unsigned char newhash[16];
  533. int genhash;
  534. struct sock *sk1 = NULL;
  535. #endif
  536. struct net *net;
  537. /* Never send a reset in response to a reset. */
  538. if (th->rst)
  539. return;
  540. /* If sk not NULL, it means we did a successful lookup and incoming
  541. * route had to be correct. prequeue might have dropped our dst.
  542. */
  543. if (!sk && skb_rtable(skb)->rt_type != RTN_LOCAL)
  544. return;
  545. /* Swap the send and the receive. */
  546. memset(&rep, 0, sizeof(rep));
  547. rep.th.dest = th->source;
  548. rep.th.source = th->dest;
  549. rep.th.doff = sizeof(struct tcphdr) / 4;
  550. rep.th.rst = 1;
  551. if (th->ack) {
  552. rep.th.seq = th->ack_seq;
  553. } else {
  554. rep.th.ack = 1;
  555. rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin +
  556. skb->len - (th->doff << 2));
  557. }
  558. memset(&arg, 0, sizeof(arg));
  559. arg.iov[0].iov_base = (unsigned char *)&rep;
  560. arg.iov[0].iov_len = sizeof(rep.th);
  561. net = sk ? sock_net(sk) : dev_net(skb_dst(skb)->dev);
  562. #ifdef CONFIG_TCP_MD5SIG
  563. rcu_read_lock();
  564. hash_location = tcp_parse_md5sig_option(th);
  565. if (sk && sk_fullsock(sk)) {
  566. key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)
  567. &ip_hdr(skb)->saddr, AF_INET);
  568. } else if (hash_location) {
  569. /*
  570. * active side is lost. Try to find listening socket through
  571. * source port, and then find md5 key through listening socket.
  572. * we are not loose security here:
  573. * Incoming packet is checked with md5 hash with finding key,
  574. * no RST generated if md5 hash doesn't match.
  575. */
  576. sk1 = __inet_lookup_listener(net, &tcp_hashinfo, NULL, 0,
  577. ip_hdr(skb)->saddr,
  578. th->source, ip_hdr(skb)->daddr,
  579. ntohs(th->source), inet_iif(skb),
  580. tcp_v4_sdif(skb));
  581. /* don't send rst if it can't find key */
  582. if (!sk1)
  583. goto out;
  584. key = tcp_md5_do_lookup(sk1, (union tcp_md5_addr *)
  585. &ip_hdr(skb)->saddr, AF_INET);
  586. if (!key)
  587. goto out;
  588. genhash = tcp_v4_md5_hash_skb(newhash, key, NULL, skb);
  589. if (genhash || memcmp(hash_location, newhash, 16) != 0)
  590. goto out;
  591. }
  592. if (key) {
  593. rep.opt[0] = htonl((TCPOPT_NOP << 24) |
  594. (TCPOPT_NOP << 16) |
  595. (TCPOPT_MD5SIG << 8) |
  596. TCPOLEN_MD5SIG);
  597. /* Update length and the length the header thinks exists */
  598. arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
  599. rep.th.doff = arg.iov[0].iov_len / 4;
  600. tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[1],
  601. key, ip_hdr(skb)->saddr,
  602. ip_hdr(skb)->daddr, &rep.th);
  603. }
  604. #endif
  605. arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
  606. ip_hdr(skb)->saddr, /* XXX */
  607. arg.iov[0].iov_len, IPPROTO_TCP, 0);
  608. arg.csumoffset = offsetof(struct tcphdr, check) / 2;
  609. arg.flags = (sk && inet_sk_transparent(sk)) ? IP_REPLY_ARG_NOSRCCHECK : 0;
  610. /* When socket is gone, all binding information is lost.
  611. * routing might fail in this case. No choice here, if we choose to force
  612. * input interface, we will misroute in case of asymmetric route.
  613. */
  614. if (sk)
  615. arg.bound_dev_if = sk->sk_bound_dev_if;
  616. BUILD_BUG_ON(offsetof(struct sock, sk_bound_dev_if) !=
  617. offsetof(struct inet_timewait_sock, tw_bound_dev_if));
  618. arg.tos = ip_hdr(skb)->tos;
  619. arg.uid = sock_net_uid(net, sk && sk_fullsock(sk) ? sk : NULL);
  620. local_bh_disable();
  621. ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
  622. skb, &TCP_SKB_CB(skb)->header.h4.opt,
  623. ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
  624. &arg, arg.iov[0].iov_len);
  625. __TCP_INC_STATS(net, TCP_MIB_OUTSEGS);
  626. __TCP_INC_STATS(net, TCP_MIB_OUTRSTS);
  627. local_bh_enable();
  628. #ifdef CONFIG_TCP_MD5SIG
  629. out:
  630. rcu_read_unlock();
  631. #endif
  632. }
  633. /* The code following below sending ACKs in SYN-RECV and TIME-WAIT states
  634. outside socket context is ugly, certainly. What can I do?
  635. */
  636. static void tcp_v4_send_ack(const struct sock *sk,
  637. struct sk_buff *skb, u32 seq, u32 ack,
  638. u32 win, u32 tsval, u32 tsecr, int oif,
  639. struct tcp_md5sig_key *key,
  640. int reply_flags, u8 tos)
  641. {
  642. const struct tcphdr *th = tcp_hdr(skb);
  643. struct {
  644. struct tcphdr th;
  645. __be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2)
  646. #ifdef CONFIG_TCP_MD5SIG
  647. + (TCPOLEN_MD5SIG_ALIGNED >> 2)
  648. #endif
  649. ];
  650. } rep;
  651. struct net *net = sock_net(sk);
  652. struct ip_reply_arg arg;
  653. memset(&rep.th, 0, sizeof(struct tcphdr));
  654. memset(&arg, 0, sizeof(arg));
  655. arg.iov[0].iov_base = (unsigned char *)&rep;
  656. arg.iov[0].iov_len = sizeof(rep.th);
  657. if (tsecr) {
  658. rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  659. (TCPOPT_TIMESTAMP << 8) |
  660. TCPOLEN_TIMESTAMP);
  661. rep.opt[1] = htonl(tsval);
  662. rep.opt[2] = htonl(tsecr);
  663. arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED;
  664. }
  665. /* Swap the send and the receive. */
  666. rep.th.dest = th->source;
  667. rep.th.source = th->dest;
  668. rep.th.doff = arg.iov[0].iov_len / 4;
  669. rep.th.seq = htonl(seq);
  670. rep.th.ack_seq = htonl(ack);
  671. rep.th.ack = 1;
  672. rep.th.window = htons(win);
  673. #ifdef CONFIG_TCP_MD5SIG
  674. if (key) {
  675. int offset = (tsecr) ? 3 : 0;
  676. rep.opt[offset++] = htonl((TCPOPT_NOP << 24) |
  677. (TCPOPT_NOP << 16) |
  678. (TCPOPT_MD5SIG << 8) |
  679. TCPOLEN_MD5SIG);
  680. arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
  681. rep.th.doff = arg.iov[0].iov_len/4;
  682. tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[offset],
  683. key, ip_hdr(skb)->saddr,
  684. ip_hdr(skb)->daddr, &rep.th);
  685. }
  686. #endif
  687. arg.flags = reply_flags;
  688. arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
  689. ip_hdr(skb)->saddr, /* XXX */
  690. arg.iov[0].iov_len, IPPROTO_TCP, 0);
  691. arg.csumoffset = offsetof(struct tcphdr, check) / 2;
  692. if (oif)
  693. arg.bound_dev_if = oif;
  694. arg.tos = tos;
  695. arg.uid = sock_net_uid(net, sk_fullsock(sk) ? sk : NULL);
  696. local_bh_disable();
  697. ip_send_unicast_reply(*this_cpu_ptr(net->ipv4.tcp_sk),
  698. skb, &TCP_SKB_CB(skb)->header.h4.opt,
  699. ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
  700. &arg, arg.iov[0].iov_len);
  701. __TCP_INC_STATS(net, TCP_MIB_OUTSEGS);
  702. local_bh_enable();
  703. }
  704. static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
  705. {
  706. struct inet_timewait_sock *tw = inet_twsk(sk);
  707. struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
  708. tcp_v4_send_ack(sk, skb,
  709. tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
  710. tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
  711. tcp_time_stamp_raw() + tcptw->tw_ts_offset,
  712. tcptw->tw_ts_recent,
  713. tw->tw_bound_dev_if,
  714. tcp_twsk_md5_key(tcptw),
  715. tw->tw_transparent ? IP_REPLY_ARG_NOSRCCHECK : 0,
  716. tw->tw_tos
  717. );
  718. inet_twsk_put(tw);
  719. }
  720. static void tcp_v4_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
  721. struct request_sock *req)
  722. {
  723. /* sk->sk_state == TCP_LISTEN -> for regular TCP_SYN_RECV
  724. * sk->sk_state == TCP_SYN_RECV -> for Fast Open.
  725. */
  726. u32 seq = (sk->sk_state == TCP_LISTEN) ? tcp_rsk(req)->snt_isn + 1 :
  727. tcp_sk(sk)->snd_nxt;
  728. /* RFC 7323 2.3
  729. * The window field (SEG.WND) of every outgoing segment, with the
  730. * exception of <SYN> segments, MUST be right-shifted by
  731. * Rcv.Wind.Shift bits:
  732. */
  733. tcp_v4_send_ack(sk, skb, seq,
  734. tcp_rsk(req)->rcv_nxt,
  735. req->rsk_rcv_wnd >> inet_rsk(req)->rcv_wscale,
  736. tcp_time_stamp_raw() + tcp_rsk(req)->ts_off,
  737. req->ts_recent,
  738. 0,
  739. tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&ip_hdr(skb)->daddr,
  740. AF_INET),
  741. inet_rsk(req)->no_srccheck ? IP_REPLY_ARG_NOSRCCHECK : 0,
  742. ip_hdr(skb)->tos);
  743. }
  744. /*
  745. * Send a SYN-ACK after having received a SYN.
  746. * This still operates on a request_sock only, not on a big
  747. * socket.
  748. */
  749. static int tcp_v4_send_synack(const struct sock *sk, struct dst_entry *dst,
  750. struct flowi *fl,
  751. struct request_sock *req,
  752. struct tcp_fastopen_cookie *foc,
  753. enum tcp_synack_type synack_type)
  754. {
  755. const struct inet_request_sock *ireq = inet_rsk(req);
  756. struct flowi4 fl4;
  757. int err = -1;
  758. struct sk_buff *skb;
  759. /* First, grab a route. */
  760. if (!dst && (dst = inet_csk_route_req(sk, &fl4, req)) == NULL)
  761. return -1;
  762. skb = tcp_make_synack(sk, dst, req, foc, synack_type);
  763. if (skb) {
  764. __tcp_v4_send_check(skb, ireq->ir_loc_addr, ireq->ir_rmt_addr);
  765. err = ip_build_and_send_pkt(skb, sk, ireq->ir_loc_addr,
  766. ireq->ir_rmt_addr,
  767. ireq->opt);
  768. err = net_xmit_eval(err);
  769. }
  770. return err;
  771. }
  772. /*
  773. * IPv4 request_sock destructor.
  774. */
  775. static void tcp_v4_reqsk_destructor(struct request_sock *req)
  776. {
  777. kfree(inet_rsk(req)->opt);
  778. }
  779. #ifdef CONFIG_TCP_MD5SIG
  780. /*
  781. * RFC2385 MD5 checksumming requires a mapping of
  782. * IP address->MD5 Key.
  783. * We need to maintain these in the sk structure.
  784. */
  785. /* Find the Key structure for an address. */
  786. struct tcp_md5sig_key *tcp_md5_do_lookup(const struct sock *sk,
  787. const union tcp_md5_addr *addr,
  788. int family)
  789. {
  790. const struct tcp_sock *tp = tcp_sk(sk);
  791. struct tcp_md5sig_key *key;
  792. const struct tcp_md5sig_info *md5sig;
  793. __be32 mask;
  794. struct tcp_md5sig_key *best_match = NULL;
  795. bool match;
  796. /* caller either holds rcu_read_lock() or socket lock */
  797. md5sig = rcu_dereference_check(tp->md5sig_info,
  798. lockdep_sock_is_held(sk));
  799. if (!md5sig)
  800. return NULL;
  801. hlist_for_each_entry_rcu(key, &md5sig->head, node) {
  802. if (key->family != family)
  803. continue;
  804. if (family == AF_INET) {
  805. mask = inet_make_mask(key->prefixlen);
  806. match = (key->addr.a4.s_addr & mask) ==
  807. (addr->a4.s_addr & mask);
  808. #if IS_ENABLED(CONFIG_IPV6)
  809. } else if (family == AF_INET6) {
  810. match = ipv6_prefix_equal(&key->addr.a6, &addr->a6,
  811. key->prefixlen);
  812. #endif
  813. } else {
  814. match = false;
  815. }
  816. if (match && (!best_match ||
  817. key->prefixlen > best_match->prefixlen))
  818. best_match = key;
  819. }
  820. return best_match;
  821. }
  822. EXPORT_SYMBOL(tcp_md5_do_lookup);
  823. static struct tcp_md5sig_key *tcp_md5_do_lookup_exact(const struct sock *sk,
  824. const union tcp_md5_addr *addr,
  825. int family, u8 prefixlen)
  826. {
  827. const struct tcp_sock *tp = tcp_sk(sk);
  828. struct tcp_md5sig_key *key;
  829. unsigned int size = sizeof(struct in_addr);
  830. const struct tcp_md5sig_info *md5sig;
  831. /* caller either holds rcu_read_lock() or socket lock */
  832. md5sig = rcu_dereference_check(tp->md5sig_info,
  833. lockdep_sock_is_held(sk));
  834. if (!md5sig)
  835. return NULL;
  836. #if IS_ENABLED(CONFIG_IPV6)
  837. if (family == AF_INET6)
  838. size = sizeof(struct in6_addr);
  839. #endif
  840. hlist_for_each_entry_rcu(key, &md5sig->head, node) {
  841. if (key->family != family)
  842. continue;
  843. if (!memcmp(&key->addr, addr, size) &&
  844. key->prefixlen == prefixlen)
  845. return key;
  846. }
  847. return NULL;
  848. }
  849. struct tcp_md5sig_key *tcp_v4_md5_lookup(const struct sock *sk,
  850. const struct sock *addr_sk)
  851. {
  852. const union tcp_md5_addr *addr;
  853. addr = (const union tcp_md5_addr *)&addr_sk->sk_daddr;
  854. return tcp_md5_do_lookup(sk, addr, AF_INET);
  855. }
  856. EXPORT_SYMBOL(tcp_v4_md5_lookup);
  857. /* This can be called on a newly created socket, from other files */
  858. int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
  859. int family, u8 prefixlen, const u8 *newkey, u8 newkeylen,
  860. gfp_t gfp)
  861. {
  862. /* Add Key to the list */
  863. struct tcp_md5sig_key *key;
  864. struct tcp_sock *tp = tcp_sk(sk);
  865. struct tcp_md5sig_info *md5sig;
  866. key = tcp_md5_do_lookup_exact(sk, addr, family, prefixlen);
  867. if (key) {
  868. /* Pre-existing entry - just update that one. */
  869. memcpy(key->key, newkey, newkeylen);
  870. key->keylen = newkeylen;
  871. return 0;
  872. }
  873. md5sig = rcu_dereference_protected(tp->md5sig_info,
  874. lockdep_sock_is_held(sk));
  875. if (!md5sig) {
  876. md5sig = kmalloc(sizeof(*md5sig), gfp);
  877. if (!md5sig)
  878. return -ENOMEM;
  879. sk_nocaps_add(sk, NETIF_F_GSO_MASK);
  880. INIT_HLIST_HEAD(&md5sig->head);
  881. rcu_assign_pointer(tp->md5sig_info, md5sig);
  882. }
  883. key = sock_kmalloc(sk, sizeof(*key), gfp);
  884. if (!key)
  885. return -ENOMEM;
  886. if (!tcp_alloc_md5sig_pool()) {
  887. sock_kfree_s(sk, key, sizeof(*key));
  888. return -ENOMEM;
  889. }
  890. memcpy(key->key, newkey, newkeylen);
  891. key->keylen = newkeylen;
  892. key->family = family;
  893. key->prefixlen = prefixlen;
  894. memcpy(&key->addr, addr,
  895. (family == AF_INET6) ? sizeof(struct in6_addr) :
  896. sizeof(struct in_addr));
  897. hlist_add_head_rcu(&key->node, &md5sig->head);
  898. return 0;
  899. }
  900. EXPORT_SYMBOL(tcp_md5_do_add);
  901. int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr, int family,
  902. u8 prefixlen)
  903. {
  904. struct tcp_md5sig_key *key;
  905. key = tcp_md5_do_lookup_exact(sk, addr, family, prefixlen);
  906. if (!key)
  907. return -ENOENT;
  908. hlist_del_rcu(&key->node);
  909. atomic_sub(sizeof(*key), &sk->sk_omem_alloc);
  910. kfree_rcu(key, rcu);
  911. return 0;
  912. }
  913. EXPORT_SYMBOL(tcp_md5_do_del);
  914. static void tcp_clear_md5_list(struct sock *sk)
  915. {
  916. struct tcp_sock *tp = tcp_sk(sk);
  917. struct tcp_md5sig_key *key;
  918. struct hlist_node *n;
  919. struct tcp_md5sig_info *md5sig;
  920. md5sig = rcu_dereference_protected(tp->md5sig_info, 1);
  921. hlist_for_each_entry_safe(key, n, &md5sig->head, node) {
  922. hlist_del_rcu(&key->node);
  923. atomic_sub(sizeof(*key), &sk->sk_omem_alloc);
  924. kfree_rcu(key, rcu);
  925. }
  926. }
  927. static int tcp_v4_parse_md5_keys(struct sock *sk, int optname,
  928. char __user *optval, int optlen)
  929. {
  930. struct tcp_md5sig cmd;
  931. struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.tcpm_addr;
  932. u8 prefixlen = 32;
  933. if (optlen < sizeof(cmd))
  934. return -EINVAL;
  935. if (copy_from_user(&cmd, optval, sizeof(cmd)))
  936. return -EFAULT;
  937. if (sin->sin_family != AF_INET)
  938. return -EINVAL;
  939. if (optname == TCP_MD5SIG_EXT &&
  940. cmd.tcpm_flags & TCP_MD5SIG_FLAG_PREFIX) {
  941. prefixlen = cmd.tcpm_prefixlen;
  942. if (prefixlen > 32)
  943. return -EINVAL;
  944. }
  945. if (!cmd.tcpm_keylen)
  946. return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin->sin_addr.s_addr,
  947. AF_INET, prefixlen);
  948. if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
  949. return -EINVAL;
  950. return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin->sin_addr.s_addr,
  951. AF_INET, prefixlen, cmd.tcpm_key, cmd.tcpm_keylen,
  952. GFP_KERNEL);
  953. }
  954. static int tcp_v4_md5_hash_headers(struct tcp_md5sig_pool *hp,
  955. __be32 daddr, __be32 saddr,
  956. const struct tcphdr *th, int nbytes)
  957. {
  958. struct tcp4_pseudohdr *bp;
  959. struct scatterlist sg;
  960. struct tcphdr *_th;
  961. bp = hp->scratch;
  962. bp->saddr = saddr;
  963. bp->daddr = daddr;
  964. bp->pad = 0;
  965. bp->protocol = IPPROTO_TCP;
  966. bp->len = cpu_to_be16(nbytes);
  967. _th = (struct tcphdr *)(bp + 1);
  968. memcpy(_th, th, sizeof(*th));
  969. _th->check = 0;
  970. sg_init_one(&sg, bp, sizeof(*bp) + sizeof(*th));
  971. ahash_request_set_crypt(hp->md5_req, &sg, NULL,
  972. sizeof(*bp) + sizeof(*th));
  973. return crypto_ahash_update(hp->md5_req);
  974. }
  975. static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
  976. __be32 daddr, __be32 saddr, const struct tcphdr *th)
  977. {
  978. struct tcp_md5sig_pool *hp;
  979. struct ahash_request *req;
  980. hp = tcp_get_md5sig_pool();
  981. if (!hp)
  982. goto clear_hash_noput;
  983. req = hp->md5_req;
  984. if (crypto_ahash_init(req))
  985. goto clear_hash;
  986. if (tcp_v4_md5_hash_headers(hp, daddr, saddr, th, th->doff << 2))
  987. goto clear_hash;
  988. if (tcp_md5_hash_key(hp, key))
  989. goto clear_hash;
  990. ahash_request_set_crypt(req, NULL, md5_hash, 0);
  991. if (crypto_ahash_final(req))
  992. goto clear_hash;
  993. tcp_put_md5sig_pool();
  994. return 0;
  995. clear_hash:
  996. tcp_put_md5sig_pool();
  997. clear_hash_noput:
  998. memset(md5_hash, 0, 16);
  999. return 1;
  1000. }
  1001. int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
  1002. const struct sock *sk,
  1003. const struct sk_buff *skb)
  1004. {
  1005. struct tcp_md5sig_pool *hp;
  1006. struct ahash_request *req;
  1007. const struct tcphdr *th = tcp_hdr(skb);
  1008. __be32 saddr, daddr;
  1009. if (sk) { /* valid for establish/request sockets */
  1010. saddr = sk->sk_rcv_saddr;
  1011. daddr = sk->sk_daddr;
  1012. } else {
  1013. const struct iphdr *iph = ip_hdr(skb);
  1014. saddr = iph->saddr;
  1015. daddr = iph->daddr;
  1016. }
  1017. hp = tcp_get_md5sig_pool();
  1018. if (!hp)
  1019. goto clear_hash_noput;
  1020. req = hp->md5_req;
  1021. if (crypto_ahash_init(req))
  1022. goto clear_hash;
  1023. if (tcp_v4_md5_hash_headers(hp, daddr, saddr, th, skb->len))
  1024. goto clear_hash;
  1025. if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2))
  1026. goto clear_hash;
  1027. if (tcp_md5_hash_key(hp, key))
  1028. goto clear_hash;
  1029. ahash_request_set_crypt(req, NULL, md5_hash, 0);
  1030. if (crypto_ahash_final(req))
  1031. goto clear_hash;
  1032. tcp_put_md5sig_pool();
  1033. return 0;
  1034. clear_hash:
  1035. tcp_put_md5sig_pool();
  1036. clear_hash_noput:
  1037. memset(md5_hash, 0, 16);
  1038. return 1;
  1039. }
  1040. EXPORT_SYMBOL(tcp_v4_md5_hash_skb);
  1041. #endif
  1042. /* Called with rcu_read_lock() */
  1043. static bool tcp_v4_inbound_md5_hash(const struct sock *sk,
  1044. const struct sk_buff *skb)
  1045. {
  1046. #ifdef CONFIG_TCP_MD5SIG
  1047. /*
  1048. * This gets called for each TCP segment that arrives
  1049. * so we want to be efficient.
  1050. * We have 3 drop cases:
  1051. * o No MD5 hash and one expected.
  1052. * o MD5 hash and we're not expecting one.
  1053. * o MD5 hash and its wrong.
  1054. */
  1055. const __u8 *hash_location = NULL;
  1056. struct tcp_md5sig_key *hash_expected;
  1057. const struct iphdr *iph = ip_hdr(skb);
  1058. const struct tcphdr *th = tcp_hdr(skb);
  1059. int genhash;
  1060. unsigned char newhash[16];
  1061. hash_expected = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&iph->saddr,
  1062. AF_INET);
  1063. hash_location = tcp_parse_md5sig_option(th);
  1064. /* We've parsed the options - do we have a hash? */
  1065. if (!hash_expected && !hash_location)
  1066. return false;
  1067. if (hash_expected && !hash_location) {
  1068. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND);
  1069. return true;
  1070. }
  1071. if (!hash_expected && hash_location) {
  1072. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED);
  1073. return true;
  1074. }
  1075. /* Okay, so this is hash_expected and hash_location -
  1076. * so we need to calculate the checksum.
  1077. */
  1078. genhash = tcp_v4_md5_hash_skb(newhash,
  1079. hash_expected,
  1080. NULL, skb);
  1081. if (genhash || memcmp(hash_location, newhash, 16) != 0) {
  1082. NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5FAILURE);
  1083. net_info_ratelimited("MD5 Hash failed for (%pI4, %d)->(%pI4, %d)%s\n",
  1084. &iph->saddr, ntohs(th->source),
  1085. &iph->daddr, ntohs(th->dest),
  1086. genhash ? " tcp_v4_calc_md5_hash failed"
  1087. : "");
  1088. return true;
  1089. }
  1090. return false;
  1091. #endif
  1092. return false;
  1093. }
  1094. static void tcp_v4_init_req(struct request_sock *req,
  1095. const struct sock *sk_listener,
  1096. struct sk_buff *skb)
  1097. {
  1098. struct inet_request_sock *ireq = inet_rsk(req);
  1099. sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
  1100. sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
  1101. ireq->opt = tcp_v4_save_options(sock_net(sk_listener), skb);
  1102. }
  1103. static struct dst_entry *tcp_v4_route_req(const struct sock *sk,
  1104. struct flowi *fl,
  1105. const struct request_sock *req)
  1106. {
  1107. return inet_csk_route_req(sk, &fl->u.ip4, req);
  1108. }
  1109. struct request_sock_ops tcp_request_sock_ops __read_mostly = {
  1110. .family = PF_INET,
  1111. .obj_size = sizeof(struct tcp_request_sock),
  1112. .rtx_syn_ack = tcp_rtx_synack,
  1113. .send_ack = tcp_v4_reqsk_send_ack,
  1114. .destructor = tcp_v4_reqsk_destructor,
  1115. .send_reset = tcp_v4_send_reset,
  1116. .syn_ack_timeout = tcp_syn_ack_timeout,
  1117. };
  1118. static const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
  1119. .mss_clamp = TCP_MSS_DEFAULT,
  1120. #ifdef CONFIG_TCP_MD5SIG
  1121. .req_md5_lookup = tcp_v4_md5_lookup,
  1122. .calc_md5_hash = tcp_v4_md5_hash_skb,
  1123. #endif
  1124. .init_req = tcp_v4_init_req,
  1125. #ifdef CONFIG_SYN_COOKIES
  1126. .cookie_init_seq = cookie_v4_init_sequence,
  1127. #endif
  1128. .route_req = tcp_v4_route_req,
  1129. .init_seq = tcp_v4_init_seq,
  1130. .init_ts_off = tcp_v4_init_ts_off,
  1131. .send_synack = tcp_v4_send_synack,
  1132. };
  1133. int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
  1134. {
  1135. /* Never answer to SYNs send to broadcast or multicast */
  1136. if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
  1137. goto drop;
  1138. return tcp_conn_request(&tcp_request_sock_ops,
  1139. &tcp_request_sock_ipv4_ops, sk, skb);
  1140. drop:
  1141. tcp_listendrop(sk);
  1142. return 0;
  1143. }
  1144. EXPORT_SYMBOL(tcp_v4_conn_request);
  1145. /*
  1146. * The three way handshake has completed - we got a valid synack -
  1147. * now create the new socket.
  1148. */
  1149. struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
  1150. struct request_sock *req,
  1151. struct dst_entry *dst,
  1152. struct request_sock *req_unhash,
  1153. bool *own_req)
  1154. {
  1155. struct inet_request_sock *ireq;
  1156. struct inet_sock *newinet;
  1157. struct tcp_sock *newtp;
  1158. struct sock *newsk;
  1159. #ifdef CONFIG_TCP_MD5SIG
  1160. struct tcp_md5sig_key *key;
  1161. #endif
  1162. struct ip_options_rcu *inet_opt;
  1163. if (sk_acceptq_is_full(sk))
  1164. goto exit_overflow;
  1165. newsk = tcp_create_openreq_child(sk, req, skb);
  1166. if (!newsk)
  1167. goto exit_nonewsk;
  1168. newsk->sk_gso_type = SKB_GSO_TCPV4;
  1169. inet_sk_rx_dst_set(newsk, skb);
  1170. newtp = tcp_sk(newsk);
  1171. newinet = inet_sk(newsk);
  1172. ireq = inet_rsk(req);
  1173. sk_daddr_set(newsk, ireq->ir_rmt_addr);
  1174. sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
  1175. newsk->sk_bound_dev_if = ireq->ir_iif;
  1176. newinet->inet_saddr = ireq->ir_loc_addr;
  1177. inet_opt = ireq->opt;
  1178. rcu_assign_pointer(newinet->inet_opt, inet_opt);
  1179. ireq->opt = NULL;
  1180. newinet->mc_index = inet_iif(skb);
  1181. newinet->mc_ttl = ip_hdr(skb)->ttl;
  1182. newinet->rcv_tos = ip_hdr(skb)->tos;
  1183. inet_csk(newsk)->icsk_ext_hdr_len = 0;
  1184. if (inet_opt)
  1185. inet_csk(newsk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
  1186. newinet->inet_id = newtp->write_seq ^ jiffies;
  1187. if (!dst) {
  1188. dst = inet_csk_route_child_sock(sk, newsk, req);
  1189. if (!dst)
  1190. goto put_and_exit;
  1191. } else {
  1192. /* syncookie case : see end of cookie_v4_check() */
  1193. }
  1194. sk_setup_caps(newsk, dst);
  1195. tcp_ca_openreq_child(newsk, dst);
  1196. tcp_sync_mss(newsk, dst_mtu(dst));
  1197. newtp->advmss = tcp_mss_clamp(tcp_sk(sk), dst_metric_advmss(dst));
  1198. tcp_initialize_rcv_mss(newsk);
  1199. #ifdef CONFIG_TCP_MD5SIG
  1200. /* Copy over the MD5 key from the original socket */
  1201. key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&newinet->inet_daddr,
  1202. AF_INET);
  1203. if (key) {
  1204. /*
  1205. * We're using one, so create a matching key
  1206. * on the newsk structure. If we fail to get
  1207. * memory, then we end up not copying the key
  1208. * across. Shucks.
  1209. */
  1210. tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newinet->inet_daddr,
  1211. AF_INET, 32, key->key, key->keylen, GFP_ATOMIC);
  1212. sk_nocaps_add(newsk, NETIF_F_GSO_MASK);
  1213. }
  1214. #endif
  1215. if (__inet_inherit_port(sk, newsk) < 0)
  1216. goto put_and_exit;
  1217. *own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash));
  1218. if (*own_req)
  1219. tcp_move_syn(newtp, req);
  1220. return newsk;
  1221. exit_overflow:
  1222. NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
  1223. exit_nonewsk:
  1224. dst_release(dst);
  1225. exit:
  1226. tcp_listendrop(sk);
  1227. return NULL;
  1228. put_and_exit:
  1229. inet_csk_prepare_forced_close(newsk);
  1230. tcp_done(newsk);
  1231. goto exit;
  1232. }
  1233. EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
  1234. static struct sock *tcp_v4_cookie_check(struct sock *sk, struct sk_buff *skb)
  1235. {
  1236. #ifdef CONFIG_SYN_COOKIES
  1237. const struct tcphdr *th = tcp_hdr(skb);
  1238. if (!th->syn)
  1239. sk = cookie_v4_check(sk, skb);
  1240. #endif
  1241. return sk;
  1242. }
  1243. /* The socket must have it's spinlock held when we get
  1244. * here, unless it is a TCP_LISTEN socket.
  1245. *
  1246. * We have a potential double-lock case here, so even when
  1247. * doing backlog processing we use the BH locking scheme.
  1248. * This is because we cannot sleep with the original spinlock
  1249. * held.
  1250. */
  1251. int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
  1252. {
  1253. struct sock *rsk;
  1254. if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
  1255. struct dst_entry *dst = sk->sk_rx_dst;
  1256. sock_rps_save_rxhash(sk, skb);
  1257. sk_mark_napi_id(sk, skb);
  1258. if (dst) {
  1259. if (inet_sk(sk)->rx_dst_ifindex != skb->skb_iif ||
  1260. !dst->ops->check(dst, 0)) {
  1261. dst_release(dst);
  1262. sk->sk_rx_dst = NULL;
  1263. }
  1264. }
  1265. tcp_rcv_established(sk, skb, tcp_hdr(skb));
  1266. return 0;
  1267. }
  1268. if (tcp_checksum_complete(skb))
  1269. goto csum_err;
  1270. if (sk->sk_state == TCP_LISTEN) {
  1271. struct sock *nsk = tcp_v4_cookie_check(sk, skb);
  1272. if (!nsk)
  1273. goto discard;
  1274. if (nsk != sk) {
  1275. if (tcp_child_process(sk, nsk, skb)) {
  1276. rsk = nsk;
  1277. goto reset;
  1278. }
  1279. return 0;
  1280. }
  1281. } else
  1282. sock_rps_save_rxhash(sk, skb);
  1283. if (tcp_rcv_state_process(sk, skb)) {
  1284. rsk = sk;
  1285. goto reset;
  1286. }
  1287. return 0;
  1288. reset:
  1289. tcp_v4_send_reset(rsk, skb);
  1290. discard:
  1291. kfree_skb(skb);
  1292. /* Be careful here. If this function gets more complicated and
  1293. * gcc suffers from register pressure on the x86, sk (in %ebx)
  1294. * might be destroyed here. This current version compiles correctly,
  1295. * but you have been warned.
  1296. */
  1297. return 0;
  1298. csum_err:
  1299. TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS);
  1300. TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
  1301. goto discard;
  1302. }
  1303. EXPORT_SYMBOL(tcp_v4_do_rcv);
  1304. int tcp_v4_early_demux(struct sk_buff *skb)
  1305. {
  1306. const struct iphdr *iph;
  1307. const struct tcphdr *th;
  1308. struct sock *sk;
  1309. if (skb->pkt_type != PACKET_HOST)
  1310. return 0;
  1311. if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct tcphdr)))
  1312. return 0;
  1313. iph = ip_hdr(skb);
  1314. th = tcp_hdr(skb);
  1315. if (th->doff < sizeof(struct tcphdr) / 4)
  1316. return 0;
  1317. sk = __inet_lookup_established(dev_net(skb->dev), &tcp_hashinfo,
  1318. iph->saddr, th->source,
  1319. iph->daddr, ntohs(th->dest),
  1320. skb->skb_iif, inet_sdif(skb));
  1321. if (sk) {
  1322. skb->sk = sk;
  1323. skb->destructor = sock_edemux;
  1324. if (sk_fullsock(sk)) {
  1325. struct dst_entry *dst = READ_ONCE(sk->sk_rx_dst);
  1326. if (dst)
  1327. dst = dst_check(dst, 0);
  1328. if (dst &&
  1329. inet_sk(sk)->rx_dst_ifindex == skb->skb_iif)
  1330. skb_dst_set_noref(skb, dst);
  1331. }
  1332. }
  1333. return 0;
  1334. }
  1335. bool tcp_add_backlog(struct sock *sk, struct sk_buff *skb)
  1336. {
  1337. u32 limit = sk->sk_rcvbuf + sk->sk_sndbuf;
  1338. /* Only socket owner can try to collapse/prune rx queues
  1339. * to reduce memory overhead, so add a little headroom here.
  1340. * Few sockets backlog are possibly concurrently non empty.
  1341. */
  1342. limit += 64*1024;
  1343. /* In case all data was pulled from skb frags (in __pskb_pull_tail()),
  1344. * we can fix skb->truesize to its real value to avoid future drops.
  1345. * This is valid because skb is not yet charged to the socket.
  1346. * It has been noticed pure SACK packets were sometimes dropped
  1347. * (if cooked by drivers without copybreak feature).
  1348. */
  1349. skb_condense(skb);
  1350. if (unlikely(sk_add_backlog(sk, skb, limit))) {
  1351. bh_unlock_sock(sk);
  1352. __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPBACKLOGDROP);
  1353. return true;
  1354. }
  1355. return false;
  1356. }
  1357. EXPORT_SYMBOL(tcp_add_backlog);
  1358. int tcp_filter(struct sock *sk, struct sk_buff *skb)
  1359. {
  1360. struct tcphdr *th = (struct tcphdr *)skb->data;
  1361. unsigned int eaten = skb->len;
  1362. int err;
  1363. err = sk_filter_trim_cap(sk, skb, th->doff * 4);
  1364. if (!err) {
  1365. eaten -= skb->len;
  1366. TCP_SKB_CB(skb)->end_seq -= eaten;
  1367. }
  1368. return err;
  1369. }
  1370. EXPORT_SYMBOL(tcp_filter);
  1371. /*
  1372. * From tcp_input.c
  1373. */
  1374. int tcp_v4_rcv(struct sk_buff *skb)
  1375. {
  1376. struct net *net = dev_net(skb->dev);
  1377. int sdif = inet_sdif(skb);
  1378. const struct iphdr *iph;
  1379. const struct tcphdr *th;
  1380. bool refcounted;
  1381. struct sock *sk;
  1382. int ret;
  1383. if (skb->pkt_type != PACKET_HOST)
  1384. goto discard_it;
  1385. /* Count it even if it's bad */
  1386. __TCP_INC_STATS(net, TCP_MIB_INSEGS);
  1387. if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
  1388. goto discard_it;
  1389. th = (const struct tcphdr *)skb->data;
  1390. if (unlikely(th->doff < sizeof(struct tcphdr) / 4))
  1391. goto bad_packet;
  1392. if (!pskb_may_pull(skb, th->doff * 4))
  1393. goto discard_it;
  1394. /* An explanation is required here, I think.
  1395. * Packet length and doff are validated by header prediction,
  1396. * provided case of th->doff==0 is eliminated.
  1397. * So, we defer the checks. */
  1398. if (skb_checksum_init(skb, IPPROTO_TCP, inet_compute_pseudo))
  1399. goto csum_error;
  1400. th = (const struct tcphdr *)skb->data;
  1401. iph = ip_hdr(skb);
  1402. /* This is tricky : We move IPCB at its correct location into TCP_SKB_CB()
  1403. * barrier() makes sure compiler wont play fool^Waliasing games.
  1404. */
  1405. memmove(&TCP_SKB_CB(skb)->header.h4, IPCB(skb),
  1406. sizeof(struct inet_skb_parm));
  1407. barrier();
  1408. TCP_SKB_CB(skb)->seq = ntohl(th->seq);
  1409. TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
  1410. skb->len - th->doff * 4);
  1411. TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
  1412. TCP_SKB_CB(skb)->tcp_flags = tcp_flag_byte(th);
  1413. TCP_SKB_CB(skb)->tcp_tw_isn = 0;
  1414. TCP_SKB_CB(skb)->ip_dsfield = ipv4_get_dsfield(iph);
  1415. TCP_SKB_CB(skb)->sacked = 0;
  1416. TCP_SKB_CB(skb)->has_rxtstamp =
  1417. skb->tstamp || skb_hwtstamps(skb)->hwtstamp;
  1418. lookup:
  1419. sk = __inet_lookup_skb(&tcp_hashinfo, skb, __tcp_hdrlen(th), th->source,
  1420. th->dest, sdif, &refcounted);
  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 (sk->sk_state == TCP_NEW_SYN_RECV) {
  1427. struct request_sock *req = inet_reqsk(sk);
  1428. struct sock *nsk;
  1429. sk = req->rsk_listener;
  1430. if (unlikely(tcp_v4_inbound_md5_hash(sk, skb))) {
  1431. sk_drops_add(sk, skb);
  1432. reqsk_put(req);
  1433. goto discard_it;
  1434. }
  1435. if (unlikely(sk->sk_state != TCP_LISTEN)) {
  1436. inet_csk_reqsk_queue_drop_and_put(sk, req);
  1437. goto lookup;
  1438. }
  1439. /* We own a reference on the listener, increase it again
  1440. * as we might lose it too soon.
  1441. */
  1442. sock_hold(sk);
  1443. refcounted = true;
  1444. nsk = NULL;
  1445. if (!tcp_filter(sk, skb))
  1446. nsk = tcp_check_req(sk, skb, req, false);
  1447. if (!nsk) {
  1448. reqsk_put(req);
  1449. goto discard_and_relse;
  1450. }
  1451. if (nsk == sk) {
  1452. reqsk_put(req);
  1453. } else if (tcp_child_process(sk, nsk, skb)) {
  1454. tcp_v4_send_reset(nsk, skb);
  1455. goto discard_and_relse;
  1456. } else {
  1457. sock_put(sk);
  1458. return 0;
  1459. }
  1460. }
  1461. if (unlikely(iph->ttl < inet_sk(sk)->min_ttl)) {
  1462. __NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
  1463. goto discard_and_relse;
  1464. }
  1465. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  1466. goto discard_and_relse;
  1467. if (tcp_v4_inbound_md5_hash(sk, skb))
  1468. goto discard_and_relse;
  1469. nf_reset(skb);
  1470. if (tcp_filter(sk, skb))
  1471. goto discard_and_relse;
  1472. th = (const struct tcphdr *)skb->data;
  1473. iph = ip_hdr(skb);
  1474. skb->dev = NULL;
  1475. if (sk->sk_state == TCP_LISTEN) {
  1476. ret = tcp_v4_do_rcv(sk, skb);
  1477. goto put_and_return;
  1478. }
  1479. sk_incoming_cpu_update(sk);
  1480. bh_lock_sock_nested(sk);
  1481. tcp_segs_in(tcp_sk(sk), skb);
  1482. ret = 0;
  1483. if (!sock_owned_by_user(sk)) {
  1484. ret = tcp_v4_do_rcv(sk, skb);
  1485. } else if (tcp_add_backlog(sk, skb)) {
  1486. goto discard_and_relse;
  1487. }
  1488. bh_unlock_sock(sk);
  1489. put_and_return:
  1490. if (refcounted)
  1491. sock_put(sk);
  1492. return ret;
  1493. no_tcp_socket:
  1494. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1495. goto discard_it;
  1496. if (tcp_checksum_complete(skb)) {
  1497. csum_error:
  1498. __TCP_INC_STATS(net, TCP_MIB_CSUMERRORS);
  1499. bad_packet:
  1500. __TCP_INC_STATS(net, TCP_MIB_INERRS);
  1501. } else {
  1502. tcp_v4_send_reset(NULL, skb);
  1503. }
  1504. discard_it:
  1505. /* Discard frame. */
  1506. kfree_skb(skb);
  1507. return 0;
  1508. discard_and_relse:
  1509. sk_drops_add(sk, skb);
  1510. if (refcounted)
  1511. sock_put(sk);
  1512. goto discard_it;
  1513. do_time_wait:
  1514. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  1515. inet_twsk_put(inet_twsk(sk));
  1516. goto discard_it;
  1517. }
  1518. if (tcp_checksum_complete(skb)) {
  1519. inet_twsk_put(inet_twsk(sk));
  1520. goto csum_error;
  1521. }
  1522. switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
  1523. case TCP_TW_SYN: {
  1524. struct sock *sk2 = inet_lookup_listener(dev_net(skb->dev),
  1525. &tcp_hashinfo, skb,
  1526. __tcp_hdrlen(th),
  1527. iph->saddr, th->source,
  1528. iph->daddr, th->dest,
  1529. inet_iif(skb),
  1530. sdif);
  1531. if (sk2) {
  1532. inet_twsk_deschedule_put(inet_twsk(sk));
  1533. sk = sk2;
  1534. refcounted = false;
  1535. goto process;
  1536. }
  1537. /* Fall through to ACK */
  1538. }
  1539. case TCP_TW_ACK:
  1540. tcp_v4_timewait_ack(sk, skb);
  1541. break;
  1542. case TCP_TW_RST:
  1543. tcp_v4_send_reset(sk, skb);
  1544. inet_twsk_deschedule_put(inet_twsk(sk));
  1545. goto discard_it;
  1546. case TCP_TW_SUCCESS:;
  1547. }
  1548. goto discard_it;
  1549. }
  1550. static struct timewait_sock_ops tcp_timewait_sock_ops = {
  1551. .twsk_obj_size = sizeof(struct tcp_timewait_sock),
  1552. .twsk_unique = tcp_twsk_unique,
  1553. .twsk_destructor= tcp_twsk_destructor,
  1554. };
  1555. void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb)
  1556. {
  1557. struct dst_entry *dst = skb_dst(skb);
  1558. if (dst && dst_hold_safe(dst)) {
  1559. sk->sk_rx_dst = dst;
  1560. inet_sk(sk)->rx_dst_ifindex = skb->skb_iif;
  1561. }
  1562. }
  1563. EXPORT_SYMBOL(inet_sk_rx_dst_set);
  1564. const struct inet_connection_sock_af_ops ipv4_specific = {
  1565. .queue_xmit = ip_queue_xmit,
  1566. .send_check = tcp_v4_send_check,
  1567. .rebuild_header = inet_sk_rebuild_header,
  1568. .sk_rx_dst_set = inet_sk_rx_dst_set,
  1569. .conn_request = tcp_v4_conn_request,
  1570. .syn_recv_sock = tcp_v4_syn_recv_sock,
  1571. .net_header_len = sizeof(struct iphdr),
  1572. .setsockopt = ip_setsockopt,
  1573. .getsockopt = ip_getsockopt,
  1574. .addr2sockaddr = inet_csk_addr2sockaddr,
  1575. .sockaddr_len = sizeof(struct sockaddr_in),
  1576. #ifdef CONFIG_COMPAT
  1577. .compat_setsockopt = compat_ip_setsockopt,
  1578. .compat_getsockopt = compat_ip_getsockopt,
  1579. #endif
  1580. .mtu_reduced = tcp_v4_mtu_reduced,
  1581. };
  1582. EXPORT_SYMBOL(ipv4_specific);
  1583. #ifdef CONFIG_TCP_MD5SIG
  1584. static const struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
  1585. .md5_lookup = tcp_v4_md5_lookup,
  1586. .calc_md5_hash = tcp_v4_md5_hash_skb,
  1587. .md5_parse = tcp_v4_parse_md5_keys,
  1588. };
  1589. #endif
  1590. /* NOTE: A lot of things set to zero explicitly by call to
  1591. * sk_alloc() so need not be done here.
  1592. */
  1593. static int tcp_v4_init_sock(struct sock *sk)
  1594. {
  1595. struct inet_connection_sock *icsk = inet_csk(sk);
  1596. tcp_init_sock(sk);
  1597. icsk->icsk_af_ops = &ipv4_specific;
  1598. #ifdef CONFIG_TCP_MD5SIG
  1599. tcp_sk(sk)->af_specific = &tcp_sock_ipv4_specific;
  1600. #endif
  1601. return 0;
  1602. }
  1603. void tcp_v4_destroy_sock(struct sock *sk)
  1604. {
  1605. struct tcp_sock *tp = tcp_sk(sk);
  1606. tcp_clear_xmit_timers(sk);
  1607. tcp_cleanup_congestion_control(sk);
  1608. tcp_cleanup_ulp(sk);
  1609. /* Cleanup up the write buffer. */
  1610. tcp_write_queue_purge(sk);
  1611. /* Check if we want to disable active TFO */
  1612. tcp_fastopen_active_disable_ofo_check(sk);
  1613. /* Cleans up our, hopefully empty, out_of_order_queue. */
  1614. skb_rbtree_purge(&tp->out_of_order_queue);
  1615. #ifdef CONFIG_TCP_MD5SIG
  1616. /* Clean up the MD5 key list, if any */
  1617. if (tp->md5sig_info) {
  1618. tcp_clear_md5_list(sk);
  1619. kfree_rcu(tp->md5sig_info, rcu);
  1620. tp->md5sig_info = NULL;
  1621. }
  1622. #endif
  1623. /* Clean up a referenced TCP bind bucket. */
  1624. if (inet_csk(sk)->icsk_bind_hash)
  1625. inet_put_port(sk);
  1626. BUG_ON(tp->fastopen_rsk);
  1627. /* If socket is aborted during connect operation */
  1628. tcp_free_fastopen_req(tp);
  1629. tcp_saved_syn_free(tp);
  1630. sk_sockets_allocated_dec(sk);
  1631. }
  1632. EXPORT_SYMBOL(tcp_v4_destroy_sock);
  1633. #ifdef CONFIG_PROC_FS
  1634. /* Proc filesystem TCP sock list dumping. */
  1635. /*
  1636. * Get next listener socket follow cur. If cur is NULL, get first socket
  1637. * starting from bucket given in st->bucket; when st->bucket is zero the
  1638. * very first socket in the hash table is returned.
  1639. */
  1640. static void *listening_get_next(struct seq_file *seq, void *cur)
  1641. {
  1642. struct tcp_iter_state *st = seq->private;
  1643. struct net *net = seq_file_net(seq);
  1644. struct inet_listen_hashbucket *ilb;
  1645. struct sock *sk = cur;
  1646. if (!sk) {
  1647. get_head:
  1648. ilb = &tcp_hashinfo.listening_hash[st->bucket];
  1649. spin_lock(&ilb->lock);
  1650. sk = sk_head(&ilb->head);
  1651. st->offset = 0;
  1652. goto get_sk;
  1653. }
  1654. ilb = &tcp_hashinfo.listening_hash[st->bucket];
  1655. ++st->num;
  1656. ++st->offset;
  1657. sk = sk_next(sk);
  1658. get_sk:
  1659. sk_for_each_from(sk) {
  1660. if (!net_eq(sock_net(sk), net))
  1661. continue;
  1662. if (sk->sk_family == st->family)
  1663. return sk;
  1664. }
  1665. spin_unlock(&ilb->lock);
  1666. st->offset = 0;
  1667. if (++st->bucket < INET_LHTABLE_SIZE)
  1668. goto get_head;
  1669. return NULL;
  1670. }
  1671. static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
  1672. {
  1673. struct tcp_iter_state *st = seq->private;
  1674. void *rc;
  1675. st->bucket = 0;
  1676. st->offset = 0;
  1677. rc = listening_get_next(seq, NULL);
  1678. while (rc && *pos) {
  1679. rc = listening_get_next(seq, rc);
  1680. --*pos;
  1681. }
  1682. return rc;
  1683. }
  1684. static inline bool empty_bucket(const struct tcp_iter_state *st)
  1685. {
  1686. return hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].chain);
  1687. }
  1688. /*
  1689. * Get first established socket starting from bucket given in st->bucket.
  1690. * If st->bucket is zero, the very first socket in the hash is returned.
  1691. */
  1692. static void *established_get_first(struct seq_file *seq)
  1693. {
  1694. struct tcp_iter_state *st = seq->private;
  1695. struct net *net = seq_file_net(seq);
  1696. void *rc = NULL;
  1697. st->offset = 0;
  1698. for (; st->bucket <= tcp_hashinfo.ehash_mask; ++st->bucket) {
  1699. struct sock *sk;
  1700. struct hlist_nulls_node *node;
  1701. spinlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket);
  1702. /* Lockless fast path for the common case of empty buckets */
  1703. if (empty_bucket(st))
  1704. continue;
  1705. spin_lock_bh(lock);
  1706. sk_nulls_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
  1707. if (sk->sk_family != st->family ||
  1708. !net_eq(sock_net(sk), net)) {
  1709. continue;
  1710. }
  1711. rc = sk;
  1712. goto out;
  1713. }
  1714. spin_unlock_bh(lock);
  1715. }
  1716. out:
  1717. return rc;
  1718. }
  1719. static void *established_get_next(struct seq_file *seq, void *cur)
  1720. {
  1721. struct sock *sk = cur;
  1722. struct hlist_nulls_node *node;
  1723. struct tcp_iter_state *st = seq->private;
  1724. struct net *net = seq_file_net(seq);
  1725. ++st->num;
  1726. ++st->offset;
  1727. sk = sk_nulls_next(sk);
  1728. sk_nulls_for_each_from(sk, node) {
  1729. if (sk->sk_family == st->family && net_eq(sock_net(sk), net))
  1730. return sk;
  1731. }
  1732. spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
  1733. ++st->bucket;
  1734. return established_get_first(seq);
  1735. }
  1736. static void *established_get_idx(struct seq_file *seq, loff_t pos)
  1737. {
  1738. struct tcp_iter_state *st = seq->private;
  1739. void *rc;
  1740. st->bucket = 0;
  1741. rc = established_get_first(seq);
  1742. while (rc && pos) {
  1743. rc = established_get_next(seq, rc);
  1744. --pos;
  1745. }
  1746. return rc;
  1747. }
  1748. static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
  1749. {
  1750. void *rc;
  1751. struct tcp_iter_state *st = seq->private;
  1752. st->state = TCP_SEQ_STATE_LISTENING;
  1753. rc = listening_get_idx(seq, &pos);
  1754. if (!rc) {
  1755. st->state = TCP_SEQ_STATE_ESTABLISHED;
  1756. rc = established_get_idx(seq, pos);
  1757. }
  1758. return rc;
  1759. }
  1760. static void *tcp_seek_last_pos(struct seq_file *seq)
  1761. {
  1762. struct tcp_iter_state *st = seq->private;
  1763. int offset = st->offset;
  1764. int orig_num = st->num;
  1765. void *rc = NULL;
  1766. switch (st->state) {
  1767. case TCP_SEQ_STATE_LISTENING:
  1768. if (st->bucket >= INET_LHTABLE_SIZE)
  1769. break;
  1770. st->state = TCP_SEQ_STATE_LISTENING;
  1771. rc = listening_get_next(seq, NULL);
  1772. while (offset-- && rc)
  1773. rc = listening_get_next(seq, rc);
  1774. if (rc)
  1775. break;
  1776. st->bucket = 0;
  1777. st->state = TCP_SEQ_STATE_ESTABLISHED;
  1778. /* Fallthrough */
  1779. case TCP_SEQ_STATE_ESTABLISHED:
  1780. if (st->bucket > tcp_hashinfo.ehash_mask)
  1781. break;
  1782. rc = established_get_first(seq);
  1783. while (offset-- && rc)
  1784. rc = established_get_next(seq, rc);
  1785. }
  1786. st->num = orig_num;
  1787. return rc;
  1788. }
  1789. static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
  1790. {
  1791. struct tcp_iter_state *st = seq->private;
  1792. void *rc;
  1793. if (*pos && *pos == st->last_pos) {
  1794. rc = tcp_seek_last_pos(seq);
  1795. if (rc)
  1796. goto out;
  1797. }
  1798. st->state = TCP_SEQ_STATE_LISTENING;
  1799. st->num = 0;
  1800. st->bucket = 0;
  1801. st->offset = 0;
  1802. rc = *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  1803. out:
  1804. st->last_pos = *pos;
  1805. return rc;
  1806. }
  1807. static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  1808. {
  1809. struct tcp_iter_state *st = seq->private;
  1810. void *rc = NULL;
  1811. if (v == SEQ_START_TOKEN) {
  1812. rc = tcp_get_idx(seq, 0);
  1813. goto out;
  1814. }
  1815. switch (st->state) {
  1816. case TCP_SEQ_STATE_LISTENING:
  1817. rc = listening_get_next(seq, v);
  1818. if (!rc) {
  1819. st->state = TCP_SEQ_STATE_ESTABLISHED;
  1820. st->bucket = 0;
  1821. st->offset = 0;
  1822. rc = established_get_first(seq);
  1823. }
  1824. break;
  1825. case TCP_SEQ_STATE_ESTABLISHED:
  1826. rc = established_get_next(seq, v);
  1827. break;
  1828. }
  1829. out:
  1830. ++*pos;
  1831. st->last_pos = *pos;
  1832. return rc;
  1833. }
  1834. static void tcp_seq_stop(struct seq_file *seq, void *v)
  1835. {
  1836. struct tcp_iter_state *st = seq->private;
  1837. switch (st->state) {
  1838. case TCP_SEQ_STATE_LISTENING:
  1839. if (v != SEQ_START_TOKEN)
  1840. spin_unlock(&tcp_hashinfo.listening_hash[st->bucket].lock);
  1841. break;
  1842. case TCP_SEQ_STATE_ESTABLISHED:
  1843. if (v)
  1844. spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
  1845. break;
  1846. }
  1847. }
  1848. int tcp_seq_open(struct inode *inode, struct file *file)
  1849. {
  1850. struct tcp_seq_afinfo *afinfo = PDE_DATA(inode);
  1851. struct tcp_iter_state *s;
  1852. int err;
  1853. err = seq_open_net(inode, file, &afinfo->seq_ops,
  1854. sizeof(struct tcp_iter_state));
  1855. if (err < 0)
  1856. return err;
  1857. s = ((struct seq_file *)file->private_data)->private;
  1858. s->family = afinfo->family;
  1859. s->last_pos = 0;
  1860. return 0;
  1861. }
  1862. EXPORT_SYMBOL(tcp_seq_open);
  1863. int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo)
  1864. {
  1865. int rc = 0;
  1866. struct proc_dir_entry *p;
  1867. afinfo->seq_ops.start = tcp_seq_start;
  1868. afinfo->seq_ops.next = tcp_seq_next;
  1869. afinfo->seq_ops.stop = tcp_seq_stop;
  1870. p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
  1871. afinfo->seq_fops, afinfo);
  1872. if (!p)
  1873. rc = -ENOMEM;
  1874. return rc;
  1875. }
  1876. EXPORT_SYMBOL(tcp_proc_register);
  1877. void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo)
  1878. {
  1879. remove_proc_entry(afinfo->name, net->proc_net);
  1880. }
  1881. EXPORT_SYMBOL(tcp_proc_unregister);
  1882. static void get_openreq4(const struct request_sock *req,
  1883. struct seq_file *f, int i)
  1884. {
  1885. const struct inet_request_sock *ireq = inet_rsk(req);
  1886. long delta = req->rsk_timer.expires - jiffies;
  1887. seq_printf(f, "%4d: %08X:%04X %08X:%04X"
  1888. " %02X %08X:%08X %02X:%08lX %08X %5u %8d %u %d %pK",
  1889. i,
  1890. ireq->ir_loc_addr,
  1891. ireq->ir_num,
  1892. ireq->ir_rmt_addr,
  1893. ntohs(ireq->ir_rmt_port),
  1894. TCP_SYN_RECV,
  1895. 0, 0, /* could print option size, but that is af dependent. */
  1896. 1, /* timers active (only the expire timer) */
  1897. jiffies_delta_to_clock_t(delta),
  1898. req->num_timeout,
  1899. from_kuid_munged(seq_user_ns(f),
  1900. sock_i_uid(req->rsk_listener)),
  1901. 0, /* non standard timer */
  1902. 0, /* open_requests have no inode */
  1903. 0,
  1904. req);
  1905. }
  1906. static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i)
  1907. {
  1908. int timer_active;
  1909. unsigned long timer_expires;
  1910. const struct tcp_sock *tp = tcp_sk(sk);
  1911. const struct inet_connection_sock *icsk = inet_csk(sk);
  1912. const struct inet_sock *inet = inet_sk(sk);
  1913. const struct fastopen_queue *fastopenq = &icsk->icsk_accept_queue.fastopenq;
  1914. __be32 dest = inet->inet_daddr;
  1915. __be32 src = inet->inet_rcv_saddr;
  1916. __u16 destp = ntohs(inet->inet_dport);
  1917. __u16 srcp = ntohs(inet->inet_sport);
  1918. int rx_queue;
  1919. int state;
  1920. if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
  1921. icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
  1922. icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
  1923. timer_active = 1;
  1924. timer_expires = icsk->icsk_timeout;
  1925. } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
  1926. timer_active = 4;
  1927. timer_expires = icsk->icsk_timeout;
  1928. } else if (timer_pending(&sk->sk_timer)) {
  1929. timer_active = 2;
  1930. timer_expires = sk->sk_timer.expires;
  1931. } else {
  1932. timer_active = 0;
  1933. timer_expires = jiffies;
  1934. }
  1935. state = sk_state_load(sk);
  1936. if (state == TCP_LISTEN)
  1937. rx_queue = sk->sk_ack_backlog;
  1938. else
  1939. /* Because we don't lock the socket,
  1940. * we might find a transient negative value.
  1941. */
  1942. rx_queue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0);
  1943. seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
  1944. "%08X %5u %8d %lu %d %pK %lu %lu %u %u %d",
  1945. i, src, srcp, dest, destp, state,
  1946. tp->write_seq - tp->snd_una,
  1947. rx_queue,
  1948. timer_active,
  1949. jiffies_delta_to_clock_t(timer_expires - jiffies),
  1950. icsk->icsk_retransmits,
  1951. from_kuid_munged(seq_user_ns(f), sock_i_uid(sk)),
  1952. icsk->icsk_probes_out,
  1953. sock_i_ino(sk),
  1954. refcount_read(&sk->sk_refcnt), sk,
  1955. jiffies_to_clock_t(icsk->icsk_rto),
  1956. jiffies_to_clock_t(icsk->icsk_ack.ato),
  1957. (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
  1958. tp->snd_cwnd,
  1959. state == TCP_LISTEN ?
  1960. fastopenq->max_qlen :
  1961. (tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh));
  1962. }
  1963. static void get_timewait4_sock(const struct inet_timewait_sock *tw,
  1964. struct seq_file *f, int i)
  1965. {
  1966. long delta = tw->tw_timer.expires - jiffies;
  1967. __be32 dest, src;
  1968. __u16 destp, srcp;
  1969. dest = tw->tw_daddr;
  1970. src = tw->tw_rcv_saddr;
  1971. destp = ntohs(tw->tw_dport);
  1972. srcp = ntohs(tw->tw_sport);
  1973. seq_printf(f, "%4d: %08X:%04X %08X:%04X"
  1974. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK",
  1975. i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
  1976. 3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0,
  1977. refcount_read(&tw->tw_refcnt), tw);
  1978. }
  1979. #define TMPSZ 150
  1980. static int tcp4_seq_show(struct seq_file *seq, void *v)
  1981. {
  1982. struct tcp_iter_state *st;
  1983. struct sock *sk = v;
  1984. seq_setwidth(seq, TMPSZ - 1);
  1985. if (v == SEQ_START_TOKEN) {
  1986. seq_puts(seq, " sl local_address rem_address st tx_queue "
  1987. "rx_queue tr tm->when retrnsmt uid timeout "
  1988. "inode");
  1989. goto out;
  1990. }
  1991. st = seq->private;
  1992. if (sk->sk_state == TCP_TIME_WAIT)
  1993. get_timewait4_sock(v, seq, st->num);
  1994. else if (sk->sk_state == TCP_NEW_SYN_RECV)
  1995. get_openreq4(v, seq, st->num);
  1996. else
  1997. get_tcp4_sock(v, seq, st->num);
  1998. out:
  1999. seq_pad(seq, '\n');
  2000. return 0;
  2001. }
  2002. static const struct file_operations tcp_afinfo_seq_fops = {
  2003. .owner = THIS_MODULE,
  2004. .open = tcp_seq_open,
  2005. .read = seq_read,
  2006. .llseek = seq_lseek,
  2007. .release = seq_release_net
  2008. };
  2009. static struct tcp_seq_afinfo tcp4_seq_afinfo = {
  2010. .name = "tcp",
  2011. .family = AF_INET,
  2012. .seq_fops = &tcp_afinfo_seq_fops,
  2013. .seq_ops = {
  2014. .show = tcp4_seq_show,
  2015. },
  2016. };
  2017. static int __net_init tcp4_proc_init_net(struct net *net)
  2018. {
  2019. return tcp_proc_register(net, &tcp4_seq_afinfo);
  2020. }
  2021. static void __net_exit tcp4_proc_exit_net(struct net *net)
  2022. {
  2023. tcp_proc_unregister(net, &tcp4_seq_afinfo);
  2024. }
  2025. static struct pernet_operations tcp4_net_ops = {
  2026. .init = tcp4_proc_init_net,
  2027. .exit = tcp4_proc_exit_net,
  2028. };
  2029. int __init tcp4_proc_init(void)
  2030. {
  2031. return register_pernet_subsys(&tcp4_net_ops);
  2032. }
  2033. void tcp4_proc_exit(void)
  2034. {
  2035. unregister_pernet_subsys(&tcp4_net_ops);
  2036. }
  2037. #endif /* CONFIG_PROC_FS */
  2038. struct proto tcp_prot = {
  2039. .name = "TCP",
  2040. .owner = THIS_MODULE,
  2041. .close = tcp_close,
  2042. .connect = tcp_v4_connect,
  2043. .disconnect = tcp_disconnect,
  2044. .accept = inet_csk_accept,
  2045. .ioctl = tcp_ioctl,
  2046. .init = tcp_v4_init_sock,
  2047. .destroy = tcp_v4_destroy_sock,
  2048. .shutdown = tcp_shutdown,
  2049. .setsockopt = tcp_setsockopt,
  2050. .getsockopt = tcp_getsockopt,
  2051. .keepalive = tcp_set_keepalive,
  2052. .recvmsg = tcp_recvmsg,
  2053. .sendmsg = tcp_sendmsg,
  2054. .sendpage = tcp_sendpage,
  2055. .backlog_rcv = tcp_v4_do_rcv,
  2056. .release_cb = tcp_release_cb,
  2057. .hash = inet_hash,
  2058. .unhash = inet_unhash,
  2059. .get_port = inet_csk_get_port,
  2060. .enter_memory_pressure = tcp_enter_memory_pressure,
  2061. .leave_memory_pressure = tcp_leave_memory_pressure,
  2062. .stream_memory_free = tcp_stream_memory_free,
  2063. .sockets_allocated = &tcp_sockets_allocated,
  2064. .orphan_count = &tcp_orphan_count,
  2065. .memory_allocated = &tcp_memory_allocated,
  2066. .memory_pressure = &tcp_memory_pressure,
  2067. .sysctl_mem = sysctl_tcp_mem,
  2068. .sysctl_wmem = sysctl_tcp_wmem,
  2069. .sysctl_rmem = sysctl_tcp_rmem,
  2070. .max_header = MAX_TCP_HEADER,
  2071. .obj_size = sizeof(struct tcp_sock),
  2072. .slab_flags = SLAB_TYPESAFE_BY_RCU,
  2073. .twsk_prot = &tcp_timewait_sock_ops,
  2074. .rsk_prot = &tcp_request_sock_ops,
  2075. .h.hashinfo = &tcp_hashinfo,
  2076. .no_autobind = true,
  2077. #ifdef CONFIG_COMPAT
  2078. .compat_setsockopt = compat_tcp_setsockopt,
  2079. .compat_getsockopt = compat_tcp_getsockopt,
  2080. #endif
  2081. .diag_destroy = tcp_abort,
  2082. };
  2083. EXPORT_SYMBOL(tcp_prot);
  2084. static void __net_exit tcp_sk_exit(struct net *net)
  2085. {
  2086. int cpu;
  2087. for_each_possible_cpu(cpu)
  2088. inet_ctl_sock_destroy(*per_cpu_ptr(net->ipv4.tcp_sk, cpu));
  2089. free_percpu(net->ipv4.tcp_sk);
  2090. }
  2091. static int __net_init tcp_sk_init(struct net *net)
  2092. {
  2093. int res, cpu, cnt;
  2094. net->ipv4.tcp_sk = alloc_percpu(struct sock *);
  2095. if (!net->ipv4.tcp_sk)
  2096. return -ENOMEM;
  2097. for_each_possible_cpu(cpu) {
  2098. struct sock *sk;
  2099. res = inet_ctl_sock_create(&sk, PF_INET, SOCK_RAW,
  2100. IPPROTO_TCP, net);
  2101. if (res)
  2102. goto fail;
  2103. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  2104. *per_cpu_ptr(net->ipv4.tcp_sk, cpu) = sk;
  2105. }
  2106. net->ipv4.sysctl_tcp_ecn = 2;
  2107. net->ipv4.sysctl_tcp_ecn_fallback = 1;
  2108. net->ipv4.sysctl_tcp_base_mss = TCP_BASE_MSS;
  2109. net->ipv4.sysctl_tcp_probe_threshold = TCP_PROBE_THRESHOLD;
  2110. net->ipv4.sysctl_tcp_probe_interval = TCP_PROBE_INTERVAL;
  2111. net->ipv4.sysctl_tcp_keepalive_time = TCP_KEEPALIVE_TIME;
  2112. net->ipv4.sysctl_tcp_keepalive_probes = TCP_KEEPALIVE_PROBES;
  2113. net->ipv4.sysctl_tcp_keepalive_intvl = TCP_KEEPALIVE_INTVL;
  2114. net->ipv4.sysctl_tcp_syn_retries = TCP_SYN_RETRIES;
  2115. net->ipv4.sysctl_tcp_synack_retries = TCP_SYNACK_RETRIES;
  2116. net->ipv4.sysctl_tcp_syncookies = 1;
  2117. net->ipv4.sysctl_tcp_reordering = TCP_FASTRETRANS_THRESH;
  2118. net->ipv4.sysctl_tcp_retries1 = TCP_RETR1;
  2119. net->ipv4.sysctl_tcp_retries2 = TCP_RETR2;
  2120. net->ipv4.sysctl_tcp_orphan_retries = 0;
  2121. net->ipv4.sysctl_tcp_fin_timeout = TCP_FIN_TIMEOUT;
  2122. net->ipv4.sysctl_tcp_notsent_lowat = UINT_MAX;
  2123. net->ipv4.sysctl_tcp_tw_reuse = 0;
  2124. cnt = tcp_hashinfo.ehash_mask + 1;
  2125. net->ipv4.tcp_death_row.sysctl_max_tw_buckets = (cnt + 1) / 2;
  2126. net->ipv4.tcp_death_row.hashinfo = &tcp_hashinfo;
  2127. net->ipv4.sysctl_max_syn_backlog = max(128, cnt / 256);
  2128. net->ipv4.sysctl_tcp_sack = 1;
  2129. net->ipv4.sysctl_tcp_window_scaling = 1;
  2130. net->ipv4.sysctl_tcp_timestamps = 1;
  2131. return 0;
  2132. fail:
  2133. tcp_sk_exit(net);
  2134. return res;
  2135. }
  2136. static void __net_exit tcp_sk_exit_batch(struct list_head *net_exit_list)
  2137. {
  2138. inet_twsk_purge(&tcp_hashinfo, AF_INET);
  2139. }
  2140. static struct pernet_operations __net_initdata tcp_sk_ops = {
  2141. .init = tcp_sk_init,
  2142. .exit = tcp_sk_exit,
  2143. .exit_batch = tcp_sk_exit_batch,
  2144. };
  2145. void __init tcp_v4_init(void)
  2146. {
  2147. if (register_pernet_subsys(&tcp_sk_ops))
  2148. panic("Failed to create the TCP control socket.\n");
  2149. }