tcp_ipv4.c 63 KB

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