tcp_ipv4.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917
  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/netdma.h>
  72. #include <net/secure_seq.h>
  73. #include <net/tcp_memcontrol.h>
  74. #include <linux/inet.h>
  75. #include <linux/ipv6.h>
  76. #include <linux/stddef.h>
  77. #include <linux/proc_fs.h>
  78. #include <linux/seq_file.h>
  79. #include <linux/crypto.h>
  80. #include <linux/scatterlist.h>
  81. int sysctl_tcp_tw_reuse __read_mostly;
  82. int sysctl_tcp_low_latency __read_mostly;
  83. EXPORT_SYMBOL(sysctl_tcp_low_latency);
  84. #ifdef CONFIG_TCP_MD5SIG
  85. static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
  86. __be32 daddr, __be32 saddr, const struct tcphdr *th);
  87. #endif
  88. struct inet_hashinfo tcp_hashinfo;
  89. EXPORT_SYMBOL(tcp_hashinfo);
  90. static inline __u32 tcp_v4_init_sequence(const struct sk_buff *skb)
  91. {
  92. return secure_tcp_sequence_number(ip_hdr(skb)->daddr,
  93. ip_hdr(skb)->saddr,
  94. tcp_hdr(skb)->dest,
  95. tcp_hdr(skb)->source);
  96. }
  97. int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
  98. {
  99. const struct tcp_timewait_sock *tcptw = tcp_twsk(sktw);
  100. struct tcp_sock *tp = tcp_sk(sk);
  101. /* With PAWS, it is safe from the viewpoint
  102. of data integrity. Even without PAWS it is safe provided sequence
  103. spaces do not overlap i.e. at data rates <= 80Mbit/sec.
  104. Actually, the idea is close to VJ's one, only timestamp cache is
  105. held not per host, but per port pair and TW bucket is used as state
  106. holder.
  107. If TW bucket has been already destroyed we fall back to VJ's scheme
  108. and use initial timestamp retrieved from peer table.
  109. */
  110. if (tcptw->tw_ts_recent_stamp &&
  111. (twp == NULL || (sysctl_tcp_tw_reuse &&
  112. get_seconds() - tcptw->tw_ts_recent_stamp > 1))) {
  113. tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2;
  114. if (tp->write_seq == 0)
  115. tp->write_seq = 1;
  116. tp->rx_opt.ts_recent = tcptw->tw_ts_recent;
  117. tp->rx_opt.ts_recent_stamp = tcptw->tw_ts_recent_stamp;
  118. sock_hold(sktw);
  119. return 1;
  120. }
  121. return 0;
  122. }
  123. EXPORT_SYMBOL_GPL(tcp_twsk_unique);
  124. /* This will initiate an outgoing connection. */
  125. int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  126. {
  127. struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
  128. struct inet_sock *inet = inet_sk(sk);
  129. struct tcp_sock *tp = tcp_sk(sk);
  130. __be16 orig_sport, orig_dport;
  131. __be32 daddr, nexthop;
  132. struct flowi4 *fl4;
  133. struct rtable *rt;
  134. int err;
  135. struct ip_options_rcu *inet_opt;
  136. if (addr_len < sizeof(struct sockaddr_in))
  137. return -EINVAL;
  138. if (usin->sin_family != AF_INET)
  139. return -EAFNOSUPPORT;
  140. nexthop = daddr = usin->sin_addr.s_addr;
  141. inet_opt = rcu_dereference_protected(inet->inet_opt,
  142. sock_owned_by_user(sk));
  143. if (inet_opt && inet_opt->opt.srr) {
  144. if (!daddr)
  145. return -EINVAL;
  146. nexthop = inet_opt->opt.faddr;
  147. }
  148. orig_sport = inet->inet_sport;
  149. orig_dport = usin->sin_port;
  150. fl4 = &inet->cork.fl.u.ip4;
  151. rt = ip_route_connect(fl4, nexthop, inet->inet_saddr,
  152. RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
  153. IPPROTO_TCP,
  154. orig_sport, orig_dport, sk, true);
  155. if (IS_ERR(rt)) {
  156. err = PTR_ERR(rt);
  157. if (err == -ENETUNREACH)
  158. IP_INC_STATS_BH(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
  159. return err;
  160. }
  161. if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
  162. ip_rt_put(rt);
  163. return -ENETUNREACH;
  164. }
  165. if (!inet_opt || !inet_opt->opt.srr)
  166. daddr = fl4->daddr;
  167. if (!inet->inet_saddr)
  168. inet->inet_saddr = fl4->saddr;
  169. inet->inet_rcv_saddr = inet->inet_saddr;
  170. if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) {
  171. /* Reset inherited state */
  172. tp->rx_opt.ts_recent = 0;
  173. tp->rx_opt.ts_recent_stamp = 0;
  174. if (likely(!tp->repair))
  175. tp->write_seq = 0;
  176. }
  177. if (tcp_death_row.sysctl_tw_recycle &&
  178. !tp->rx_opt.ts_recent_stamp && fl4->daddr == daddr)
  179. tcp_fetch_timewait_stamp(sk, &rt->dst);
  180. inet->inet_dport = usin->sin_port;
  181. inet->inet_daddr = daddr;
  182. inet_csk(sk)->icsk_ext_hdr_len = 0;
  183. if (inet_opt)
  184. inet_csk(sk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
  185. tp->rx_opt.mss_clamp = TCP_MSS_DEFAULT;
  186. /* Socket identity is still unknown (sport may be zero).
  187. * However we set state to SYN-SENT and not releasing socket
  188. * lock select source port, enter ourselves into the hash tables and
  189. * complete initialization after this.
  190. */
  191. tcp_set_state(sk, TCP_SYN_SENT);
  192. err = inet_hash_connect(&tcp_death_row, sk);
  193. if (err)
  194. goto failure;
  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. if (!tp->write_seq && likely(!tp->repair))
  206. tp->write_seq = secure_tcp_sequence_number(inet->inet_saddr,
  207. inet->inet_daddr,
  208. inet->inet_sport,
  209. usin->sin_port);
  210. inet->inet_id = tp->write_seq ^ jiffies;
  211. err = tcp_connect(sk);
  212. rt = NULL;
  213. if (err)
  214. goto failure;
  215. return 0;
  216. failure:
  217. /*
  218. * This unhashes the socket and releases the local port,
  219. * if necessary.
  220. */
  221. tcp_set_state(sk, TCP_CLOSE);
  222. ip_rt_put(rt);
  223. sk->sk_route_caps = 0;
  224. inet->inet_dport = 0;
  225. return err;
  226. }
  227. EXPORT_SYMBOL(tcp_v4_connect);
  228. /*
  229. * This routine reacts to ICMP_FRAG_NEEDED mtu indications as defined in RFC1191.
  230. * It can be called through tcp_release_cb() if socket was owned by user
  231. * at the time tcp_v4_err() was called to handle ICMP message.
  232. */
  233. static void tcp_v4_mtu_reduced(struct sock *sk)
  234. {
  235. struct dst_entry *dst;
  236. struct inet_sock *inet = inet_sk(sk);
  237. u32 mtu = tcp_sk(sk)->mtu_info;
  238. /* We are not interested in TCP_LISTEN and open_requests (SYN-ACKs
  239. * send out by Linux are always <576bytes so they should go through
  240. * unfragmented).
  241. */
  242. if (sk->sk_state == TCP_LISTEN)
  243. return;
  244. dst = inet_csk_update_pmtu(sk, mtu);
  245. if (!dst)
  246. return;
  247. /* Something is about to be wrong... Remember soft error
  248. * for the case, if this connection will not able to recover.
  249. */
  250. if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
  251. sk->sk_err_soft = EMSGSIZE;
  252. mtu = dst_mtu(dst);
  253. if (inet->pmtudisc != IP_PMTUDISC_DONT &&
  254. inet_csk(sk)->icsk_pmtu_cookie > mtu) {
  255. tcp_sync_mss(sk, mtu);
  256. /* Resend the TCP packet because it's
  257. * clear that the old packet has been
  258. * dropped. This is the new "fast" path mtu
  259. * discovery.
  260. */
  261. tcp_simple_retransmit(sk);
  262. } /* else let the usual retransmit timer handle it */
  263. }
  264. static void do_redirect(struct sk_buff *skb, struct sock *sk)
  265. {
  266. struct dst_entry *dst = __sk_dst_check(sk, 0);
  267. if (dst)
  268. dst->ops->redirect(dst, sk, skb);
  269. }
  270. /*
  271. * This routine is called by the ICMP module when it gets some
  272. * sort of error condition. If err < 0 then the socket should
  273. * be closed and the error returned to the user. If err > 0
  274. * it's just the icmp type << 8 | icmp code. After adjustment
  275. * header points to the first 8 bytes of the tcp header. We need
  276. * to find the appropriate port.
  277. *
  278. * The locking strategy used here is very "optimistic". When
  279. * someone else accesses the socket the ICMP is just dropped
  280. * and for some paths there is no check at all.
  281. * A more general error queue to queue errors for later handling
  282. * is probably better.
  283. *
  284. */
  285. void tcp_v4_err(struct sk_buff *icmp_skb, u32 info)
  286. {
  287. const struct iphdr *iph = (const struct iphdr *)icmp_skb->data;
  288. struct tcphdr *th = (struct tcphdr *)(icmp_skb->data + (iph->ihl << 2));
  289. struct inet_connection_sock *icsk;
  290. struct tcp_sock *tp;
  291. struct inet_sock *inet;
  292. const int type = icmp_hdr(icmp_skb)->type;
  293. const int code = icmp_hdr(icmp_skb)->code;
  294. struct sock *sk;
  295. struct sk_buff *skb;
  296. struct request_sock *req;
  297. __u32 seq;
  298. __u32 remaining;
  299. int err;
  300. struct net *net = dev_net(icmp_skb->dev);
  301. if (icmp_skb->len < (iph->ihl << 2) + 8) {
  302. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  303. return;
  304. }
  305. sk = inet_lookup(net, &tcp_hashinfo, iph->daddr, th->dest,
  306. iph->saddr, th->source, inet_iif(icmp_skb));
  307. if (!sk) {
  308. ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
  309. return;
  310. }
  311. if (sk->sk_state == TCP_TIME_WAIT) {
  312. inet_twsk_put(inet_twsk(sk));
  313. return;
  314. }
  315. bh_lock_sock(sk);
  316. /* If too many ICMPs get dropped on busy
  317. * servers this needs to be solved differently.
  318. * We do take care of PMTU discovery (RFC1191) special case :
  319. * we can receive locally generated ICMP messages while socket is held.
  320. */
  321. if (sock_owned_by_user(sk)) {
  322. if (!(type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED))
  323. NET_INC_STATS_BH(net, LINUX_MIB_LOCKDROPPEDICMPS);
  324. }
  325. if (sk->sk_state == TCP_CLOSE)
  326. goto out;
  327. if (unlikely(iph->ttl < inet_sk(sk)->min_ttl)) {
  328. NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
  329. goto out;
  330. }
  331. icsk = inet_csk(sk);
  332. tp = tcp_sk(sk);
  333. req = tp->fastopen_rsk;
  334. seq = ntohl(th->seq);
  335. if (sk->sk_state != TCP_LISTEN &&
  336. !between(seq, tp->snd_una, tp->snd_nxt) &&
  337. (req == NULL || seq != tcp_rsk(req)->snt_isn)) {
  338. /* For a Fast Open socket, allow seq to be snt_isn. */
  339. NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
  340. goto out;
  341. }
  342. switch (type) {
  343. case ICMP_REDIRECT:
  344. do_redirect(icmp_skb, sk);
  345. goto out;
  346. case ICMP_SOURCE_QUENCH:
  347. /* Just silently ignore these. */
  348. goto out;
  349. case ICMP_PARAMETERPROB:
  350. err = EPROTO;
  351. break;
  352. case ICMP_DEST_UNREACH:
  353. if (code > NR_ICMP_UNREACH)
  354. goto out;
  355. if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
  356. tp->mtu_info = info;
  357. if (!sock_owned_by_user(sk)) {
  358. tcp_v4_mtu_reduced(sk);
  359. } else {
  360. if (!test_and_set_bit(TCP_MTU_REDUCED_DEFERRED, &tp->tsq_flags))
  361. sock_hold(sk);
  362. }
  363. goto out;
  364. }
  365. err = icmp_err_convert[code].errno;
  366. /* check if icmp_skb allows revert of backoff
  367. * (see draft-zimmermann-tcp-lcd) */
  368. if (code != ICMP_NET_UNREACH && code != ICMP_HOST_UNREACH)
  369. break;
  370. if (seq != tp->snd_una || !icsk->icsk_retransmits ||
  371. !icsk->icsk_backoff)
  372. break;
  373. /* XXX (TFO) - revisit the following logic for TFO */
  374. if (sock_owned_by_user(sk))
  375. break;
  376. icsk->icsk_backoff--;
  377. inet_csk(sk)->icsk_rto = (tp->srtt ? __tcp_set_rto(tp) :
  378. TCP_TIMEOUT_INIT) << icsk->icsk_backoff;
  379. tcp_bound_rto(sk);
  380. skb = tcp_write_queue_head(sk);
  381. BUG_ON(!skb);
  382. remaining = icsk->icsk_rto - min(icsk->icsk_rto,
  383. tcp_time_stamp - TCP_SKB_CB(skb)->when);
  384. if (remaining) {
  385. inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS,
  386. remaining, TCP_RTO_MAX);
  387. } else {
  388. /* RTO revert clocked out retransmission.
  389. * Will retransmit now */
  390. tcp_retransmit_timer(sk);
  391. }
  392. break;
  393. case ICMP_TIME_EXCEEDED:
  394. err = EHOSTUNREACH;
  395. break;
  396. default:
  397. goto out;
  398. }
  399. /* XXX (TFO) - if it's a TFO socket and has been accepted, rather
  400. * than following the TCP_SYN_RECV case and closing the socket,
  401. * we ignore the ICMP error and keep trying like a fully established
  402. * socket. Is this the right thing to do?
  403. */
  404. if (req && req->sk == NULL)
  405. goto out;
  406. switch (sk->sk_state) {
  407. struct request_sock *req, **prev;
  408. case TCP_LISTEN:
  409. if (sock_owned_by_user(sk))
  410. goto out;
  411. req = inet_csk_search_req(sk, &prev, th->dest,
  412. iph->daddr, iph->saddr);
  413. if (!req)
  414. goto out;
  415. /* ICMPs are not backlogged, hence we cannot get
  416. an established socket here.
  417. */
  418. WARN_ON(req->sk);
  419. if (seq != tcp_rsk(req)->snt_isn) {
  420. NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
  421. goto out;
  422. }
  423. /*
  424. * Still in SYN_RECV, just remove it silently.
  425. * There is no good way to pass the error to the newly
  426. * created socket, and POSIX does not want network
  427. * errors returned from accept().
  428. */
  429. inet_csk_reqsk_queue_drop(sk, req, prev);
  430. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
  431. goto out;
  432. case TCP_SYN_SENT:
  433. case TCP_SYN_RECV: /* Cannot happen.
  434. It can f.e. if SYNs crossed,
  435. or Fast Open.
  436. */
  437. if (!sock_owned_by_user(sk)) {
  438. sk->sk_err = err;
  439. sk->sk_error_report(sk);
  440. tcp_done(sk);
  441. } else {
  442. sk->sk_err_soft = err;
  443. }
  444. goto out;
  445. }
  446. /* If we've already connected we will keep trying
  447. * until we time out, or the user gives up.
  448. *
  449. * rfc1122 4.2.3.9 allows to consider as hard errors
  450. * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
  451. * but it is obsoleted by pmtu discovery).
  452. *
  453. * Note, that in modern internet, where routing is unreliable
  454. * and in each dark corner broken firewalls sit, sending random
  455. * errors ordered by their masters even this two messages finally lose
  456. * their original sense (even Linux sends invalid PORT_UNREACHs)
  457. *
  458. * Now we are in compliance with RFCs.
  459. * --ANK (980905)
  460. */
  461. inet = inet_sk(sk);
  462. if (!sock_owned_by_user(sk) && inet->recverr) {
  463. sk->sk_err = err;
  464. sk->sk_error_report(sk);
  465. } else { /* Only an error on timeout */
  466. sk->sk_err_soft = err;
  467. }
  468. out:
  469. bh_unlock_sock(sk);
  470. sock_put(sk);
  471. }
  472. static void __tcp_v4_send_check(struct sk_buff *skb,
  473. __be32 saddr, __be32 daddr)
  474. {
  475. struct tcphdr *th = tcp_hdr(skb);
  476. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  477. th->check = ~tcp_v4_check(skb->len, saddr, daddr, 0);
  478. skb->csum_start = skb_transport_header(skb) - skb->head;
  479. skb->csum_offset = offsetof(struct tcphdr, check);
  480. } else {
  481. th->check = tcp_v4_check(skb->len, saddr, daddr,
  482. csum_partial(th,
  483. th->doff << 2,
  484. skb->csum));
  485. }
  486. }
  487. /* This routine computes an IPv4 TCP checksum. */
  488. void tcp_v4_send_check(struct sock *sk, struct sk_buff *skb)
  489. {
  490. const struct inet_sock *inet = inet_sk(sk);
  491. __tcp_v4_send_check(skb, inet->inet_saddr, inet->inet_daddr);
  492. }
  493. EXPORT_SYMBOL(tcp_v4_send_check);
  494. int tcp_v4_gso_send_check(struct sk_buff *skb)
  495. {
  496. const struct iphdr *iph;
  497. struct tcphdr *th;
  498. if (!pskb_may_pull(skb, sizeof(*th)))
  499. return -EINVAL;
  500. iph = ip_hdr(skb);
  501. th = tcp_hdr(skb);
  502. th->check = 0;
  503. skb->ip_summed = CHECKSUM_PARTIAL;
  504. __tcp_v4_send_check(skb, iph->saddr, iph->daddr);
  505. return 0;
  506. }
  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(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;
  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 (skb_rtable(skb)->rt_type != RTN_LOCAL)
  541. return;
  542. /* Swap the send and the receive. */
  543. memset(&rep, 0, sizeof(rep));
  544. rep.th.dest = th->source;
  545. rep.th.source = th->dest;
  546. rep.th.doff = sizeof(struct tcphdr) / 4;
  547. rep.th.rst = 1;
  548. if (th->ack) {
  549. rep.th.seq = th->ack_seq;
  550. } else {
  551. rep.th.ack = 1;
  552. rep.th.ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin +
  553. skb->len - (th->doff << 2));
  554. }
  555. memset(&arg, 0, sizeof(arg));
  556. arg.iov[0].iov_base = (unsigned char *)&rep;
  557. arg.iov[0].iov_len = sizeof(rep.th);
  558. #ifdef CONFIG_TCP_MD5SIG
  559. hash_location = tcp_parse_md5sig_option(th);
  560. if (!sk && hash_location) {
  561. /*
  562. * active side is lost. Try to find listening socket through
  563. * source port, and then find md5 key through listening socket.
  564. * we are not loose security here:
  565. * Incoming packet is checked with md5 hash with finding key,
  566. * no RST generated if md5 hash doesn't match.
  567. */
  568. sk1 = __inet_lookup_listener(dev_net(skb_dst(skb)->dev),
  569. &tcp_hashinfo, ip_hdr(skb)->daddr,
  570. ntohs(th->source), inet_iif(skb));
  571. /* don't send rst if it can't find key */
  572. if (!sk1)
  573. return;
  574. rcu_read_lock();
  575. key = tcp_md5_do_lookup(sk1, (union tcp_md5_addr *)
  576. &ip_hdr(skb)->saddr, AF_INET);
  577. if (!key)
  578. goto release_sk1;
  579. genhash = tcp_v4_md5_hash_skb(newhash, key, NULL, NULL, skb);
  580. if (genhash || memcmp(hash_location, newhash, 16) != 0)
  581. goto release_sk1;
  582. } else {
  583. key = sk ? tcp_md5_do_lookup(sk, (union tcp_md5_addr *)
  584. &ip_hdr(skb)->saddr,
  585. AF_INET) : NULL;
  586. }
  587. if (key) {
  588. rep.opt[0] = htonl((TCPOPT_NOP << 24) |
  589. (TCPOPT_NOP << 16) |
  590. (TCPOPT_MD5SIG << 8) |
  591. TCPOLEN_MD5SIG);
  592. /* Update length and the length the header thinks exists */
  593. arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
  594. rep.th.doff = arg.iov[0].iov_len / 4;
  595. tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[1],
  596. key, ip_hdr(skb)->saddr,
  597. ip_hdr(skb)->daddr, &rep.th);
  598. }
  599. #endif
  600. arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
  601. ip_hdr(skb)->saddr, /* XXX */
  602. arg.iov[0].iov_len, IPPROTO_TCP, 0);
  603. arg.csumoffset = offsetof(struct tcphdr, check) / 2;
  604. arg.flags = (sk && inet_sk(sk)->transparent) ? IP_REPLY_ARG_NOSRCCHECK : 0;
  605. /* When socket is gone, all binding information is lost.
  606. * routing might fail in this case. No choice here, if we choose to force
  607. * input interface, we will misroute in case of asymmetric route.
  608. */
  609. if (sk)
  610. arg.bound_dev_if = sk->sk_bound_dev_if;
  611. net = dev_net(skb_dst(skb)->dev);
  612. arg.tos = ip_hdr(skb)->tos;
  613. ip_send_unicast_reply(net, skb, ip_hdr(skb)->saddr,
  614. ip_hdr(skb)->daddr, &arg, arg.iov[0].iov_len);
  615. TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
  616. TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS);
  617. #ifdef CONFIG_TCP_MD5SIG
  618. release_sk1:
  619. if (sk1) {
  620. rcu_read_unlock();
  621. sock_put(sk1);
  622. }
  623. #endif
  624. }
  625. /* The code following below sending ACKs in SYN-RECV and TIME-WAIT states
  626. outside socket context is ugly, certainly. What can I do?
  627. */
  628. static void tcp_v4_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
  629. u32 win, u32 ts, int oif,
  630. struct tcp_md5sig_key *key,
  631. int reply_flags, u8 tos)
  632. {
  633. const struct tcphdr *th = tcp_hdr(skb);
  634. struct {
  635. struct tcphdr th;
  636. __be32 opt[(TCPOLEN_TSTAMP_ALIGNED >> 2)
  637. #ifdef CONFIG_TCP_MD5SIG
  638. + (TCPOLEN_MD5SIG_ALIGNED >> 2)
  639. #endif
  640. ];
  641. } rep;
  642. struct ip_reply_arg arg;
  643. struct net *net = dev_net(skb_dst(skb)->dev);
  644. memset(&rep.th, 0, sizeof(struct tcphdr));
  645. memset(&arg, 0, sizeof(arg));
  646. arg.iov[0].iov_base = (unsigned char *)&rep;
  647. arg.iov[0].iov_len = sizeof(rep.th);
  648. if (ts) {
  649. rep.opt[0] = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  650. (TCPOPT_TIMESTAMP << 8) |
  651. TCPOLEN_TIMESTAMP);
  652. rep.opt[1] = htonl(tcp_time_stamp);
  653. rep.opt[2] = htonl(ts);
  654. arg.iov[0].iov_len += TCPOLEN_TSTAMP_ALIGNED;
  655. }
  656. /* Swap the send and the receive. */
  657. rep.th.dest = th->source;
  658. rep.th.source = th->dest;
  659. rep.th.doff = arg.iov[0].iov_len / 4;
  660. rep.th.seq = htonl(seq);
  661. rep.th.ack_seq = htonl(ack);
  662. rep.th.ack = 1;
  663. rep.th.window = htons(win);
  664. #ifdef CONFIG_TCP_MD5SIG
  665. if (key) {
  666. int offset = (ts) ? 3 : 0;
  667. rep.opt[offset++] = htonl((TCPOPT_NOP << 24) |
  668. (TCPOPT_NOP << 16) |
  669. (TCPOPT_MD5SIG << 8) |
  670. TCPOLEN_MD5SIG);
  671. arg.iov[0].iov_len += TCPOLEN_MD5SIG_ALIGNED;
  672. rep.th.doff = arg.iov[0].iov_len/4;
  673. tcp_v4_md5_hash_hdr((__u8 *) &rep.opt[offset],
  674. key, ip_hdr(skb)->saddr,
  675. ip_hdr(skb)->daddr, &rep.th);
  676. }
  677. #endif
  678. arg.flags = reply_flags;
  679. arg.csum = csum_tcpudp_nofold(ip_hdr(skb)->daddr,
  680. ip_hdr(skb)->saddr, /* XXX */
  681. arg.iov[0].iov_len, IPPROTO_TCP, 0);
  682. arg.csumoffset = offsetof(struct tcphdr, check) / 2;
  683. if (oif)
  684. arg.bound_dev_if = oif;
  685. arg.tos = tos;
  686. ip_send_unicast_reply(net, skb, ip_hdr(skb)->saddr,
  687. ip_hdr(skb)->daddr, &arg, arg.iov[0].iov_len);
  688. TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
  689. }
  690. static void tcp_v4_timewait_ack(struct sock *sk, struct sk_buff *skb)
  691. {
  692. struct inet_timewait_sock *tw = inet_twsk(sk);
  693. struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
  694. tcp_v4_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
  695. tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
  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(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. tcp_v4_send_ack(skb, (sk->sk_state == TCP_LISTEN) ?
  711. tcp_rsk(req)->snt_isn + 1 : tcp_sk(sk)->snd_nxt,
  712. tcp_rsk(req)->rcv_nxt, req->rcv_wnd,
  713. req->ts_recent,
  714. 0,
  715. tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&ip_hdr(skb)->daddr,
  716. AF_INET),
  717. inet_rsk(req)->no_srccheck ? IP_REPLY_ARG_NOSRCCHECK : 0,
  718. ip_hdr(skb)->tos);
  719. }
  720. /*
  721. * Send a SYN-ACK after having received a SYN.
  722. * This still operates on a request_sock only, not on a big
  723. * socket.
  724. */
  725. static int tcp_v4_send_synack(struct sock *sk, struct dst_entry *dst,
  726. struct request_sock *req,
  727. struct request_values *rvp,
  728. u16 queue_mapping,
  729. bool nocache)
  730. {
  731. const struct inet_request_sock *ireq = inet_rsk(req);
  732. struct flowi4 fl4;
  733. int err = -1;
  734. struct sk_buff * skb;
  735. /* First, grab a route. */
  736. if (!dst && (dst = inet_csk_route_req(sk, &fl4, req)) == NULL)
  737. return -1;
  738. skb = tcp_make_synack(sk, dst, req, rvp, NULL);
  739. if (skb) {
  740. __tcp_v4_send_check(skb, ireq->loc_addr, ireq->rmt_addr);
  741. skb_set_queue_mapping(skb, queue_mapping);
  742. err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
  743. ireq->rmt_addr,
  744. ireq->opt);
  745. err = net_xmit_eval(err);
  746. if (!tcp_rsk(req)->snt_synack && !err)
  747. tcp_rsk(req)->snt_synack = tcp_time_stamp;
  748. }
  749. return err;
  750. }
  751. static int tcp_v4_rtx_synack(struct sock *sk, struct request_sock *req,
  752. struct request_values *rvp)
  753. {
  754. int res = tcp_v4_send_synack(sk, NULL, req, rvp, 0, false);
  755. if (!res)
  756. TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_RETRANSSEGS);
  757. return res;
  758. }
  759. /*
  760. * IPv4 request_sock destructor.
  761. */
  762. static void tcp_v4_reqsk_destructor(struct request_sock *req)
  763. {
  764. kfree(inet_rsk(req)->opt);
  765. }
  766. /*
  767. * Return true if a syncookie should be sent
  768. */
  769. bool tcp_syn_flood_action(struct sock *sk,
  770. const struct sk_buff *skb,
  771. const char *proto)
  772. {
  773. const char *msg = "Dropping request";
  774. bool want_cookie = false;
  775. struct listen_sock *lopt;
  776. #ifdef CONFIG_SYN_COOKIES
  777. if (sysctl_tcp_syncookies) {
  778. msg = "Sending cookies";
  779. want_cookie = true;
  780. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPREQQFULLDOCOOKIES);
  781. } else
  782. #endif
  783. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPREQQFULLDROP);
  784. lopt = inet_csk(sk)->icsk_accept_queue.listen_opt;
  785. if (!lopt->synflood_warned) {
  786. lopt->synflood_warned = 1;
  787. pr_info("%s: Possible SYN flooding on port %d. %s. Check SNMP counters.\n",
  788. proto, ntohs(tcp_hdr(skb)->dest), msg);
  789. }
  790. return want_cookie;
  791. }
  792. EXPORT_SYMBOL(tcp_syn_flood_action);
  793. /*
  794. * Save and compile IPv4 options into the request_sock if needed.
  795. */
  796. static struct ip_options_rcu *tcp_v4_save_options(struct sk_buff *skb)
  797. {
  798. const struct ip_options *opt = &(IPCB(skb)->opt);
  799. struct ip_options_rcu *dopt = NULL;
  800. if (opt && opt->optlen) {
  801. int opt_size = sizeof(*dopt) + opt->optlen;
  802. dopt = kmalloc(opt_size, GFP_ATOMIC);
  803. if (dopt) {
  804. if (ip_options_echo(&dopt->opt, skb)) {
  805. kfree(dopt);
  806. dopt = NULL;
  807. }
  808. }
  809. }
  810. return dopt;
  811. }
  812. #ifdef CONFIG_TCP_MD5SIG
  813. /*
  814. * RFC2385 MD5 checksumming requires a mapping of
  815. * IP address->MD5 Key.
  816. * We need to maintain these in the sk structure.
  817. */
  818. /* Find the Key structure for an address. */
  819. struct tcp_md5sig_key *tcp_md5_do_lookup(struct sock *sk,
  820. const union tcp_md5_addr *addr,
  821. int family)
  822. {
  823. struct tcp_sock *tp = tcp_sk(sk);
  824. struct tcp_md5sig_key *key;
  825. struct hlist_node *pos;
  826. unsigned int size = sizeof(struct in_addr);
  827. struct tcp_md5sig_info *md5sig;
  828. /* caller either holds rcu_read_lock() or socket lock */
  829. md5sig = rcu_dereference_check(tp->md5sig_info,
  830. sock_owned_by_user(sk) ||
  831. lockdep_is_held(&sk->sk_lock.slock));
  832. if (!md5sig)
  833. return NULL;
  834. #if IS_ENABLED(CONFIG_IPV6)
  835. if (family == AF_INET6)
  836. size = sizeof(struct in6_addr);
  837. #endif
  838. hlist_for_each_entry_rcu(key, pos, &md5sig->head, node) {
  839. if (key->family != family)
  840. continue;
  841. if (!memcmp(&key->addr, addr, size))
  842. return key;
  843. }
  844. return NULL;
  845. }
  846. EXPORT_SYMBOL(tcp_md5_do_lookup);
  847. struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk,
  848. struct sock *addr_sk)
  849. {
  850. union tcp_md5_addr *addr;
  851. addr = (union tcp_md5_addr *)&inet_sk(addr_sk)->inet_daddr;
  852. return tcp_md5_do_lookup(sk, addr, AF_INET);
  853. }
  854. EXPORT_SYMBOL(tcp_v4_md5_lookup);
  855. static struct tcp_md5sig_key *tcp_v4_reqsk_md5_lookup(struct sock *sk,
  856. struct request_sock *req)
  857. {
  858. union tcp_md5_addr *addr;
  859. addr = (union tcp_md5_addr *)&inet_rsk(req)->rmt_addr;
  860. return tcp_md5_do_lookup(sk, addr, AF_INET);
  861. }
  862. /* This can be called on a newly created socket, from other files */
  863. int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
  864. int family, const u8 *newkey, u8 newkeylen, gfp_t gfp)
  865. {
  866. /* Add Key to the list */
  867. struct tcp_md5sig_key *key;
  868. struct tcp_sock *tp = tcp_sk(sk);
  869. struct tcp_md5sig_info *md5sig;
  870. key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&addr, AF_INET);
  871. if (key) {
  872. /* Pre-existing entry - just update that one. */
  873. memcpy(key->key, newkey, newkeylen);
  874. key->keylen = newkeylen;
  875. return 0;
  876. }
  877. md5sig = rcu_dereference_protected(tp->md5sig_info,
  878. sock_owned_by_user(sk));
  879. if (!md5sig) {
  880. md5sig = kmalloc(sizeof(*md5sig), gfp);
  881. if (!md5sig)
  882. return -ENOMEM;
  883. sk_nocaps_add(sk, NETIF_F_GSO_MASK);
  884. INIT_HLIST_HEAD(&md5sig->head);
  885. rcu_assign_pointer(tp->md5sig_info, md5sig);
  886. }
  887. key = sock_kmalloc(sk, sizeof(*key), gfp);
  888. if (!key)
  889. return -ENOMEM;
  890. if (hlist_empty(&md5sig->head) && !tcp_alloc_md5sig_pool(sk)) {
  891. sock_kfree_s(sk, key, sizeof(*key));
  892. return -ENOMEM;
  893. }
  894. memcpy(key->key, newkey, newkeylen);
  895. key->keylen = newkeylen;
  896. key->family = family;
  897. memcpy(&key->addr, addr,
  898. (family == AF_INET6) ? sizeof(struct in6_addr) :
  899. sizeof(struct in_addr));
  900. hlist_add_head_rcu(&key->node, &md5sig->head);
  901. return 0;
  902. }
  903. EXPORT_SYMBOL(tcp_md5_do_add);
  904. int tcp_md5_do_del(struct sock *sk, const union tcp_md5_addr *addr, int family)
  905. {
  906. struct tcp_sock *tp = tcp_sk(sk);
  907. struct tcp_md5sig_key *key;
  908. struct tcp_md5sig_info *md5sig;
  909. key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&addr, AF_INET);
  910. if (!key)
  911. return -ENOENT;
  912. hlist_del_rcu(&key->node);
  913. atomic_sub(sizeof(*key), &sk->sk_omem_alloc);
  914. kfree_rcu(key, rcu);
  915. md5sig = rcu_dereference_protected(tp->md5sig_info,
  916. sock_owned_by_user(sk));
  917. if (hlist_empty(&md5sig->head))
  918. tcp_free_md5sig_pool();
  919. return 0;
  920. }
  921. EXPORT_SYMBOL(tcp_md5_do_del);
  922. static void tcp_clear_md5_list(struct sock *sk)
  923. {
  924. struct tcp_sock *tp = tcp_sk(sk);
  925. struct tcp_md5sig_key *key;
  926. struct hlist_node *pos, *n;
  927. struct tcp_md5sig_info *md5sig;
  928. md5sig = rcu_dereference_protected(tp->md5sig_info, 1);
  929. if (!hlist_empty(&md5sig->head))
  930. tcp_free_md5sig_pool();
  931. hlist_for_each_entry_safe(key, pos, n, &md5sig->head, node) {
  932. hlist_del_rcu(&key->node);
  933. atomic_sub(sizeof(*key), &sk->sk_omem_alloc);
  934. kfree_rcu(key, rcu);
  935. }
  936. }
  937. static int tcp_v4_parse_md5_keys(struct sock *sk, char __user *optval,
  938. int optlen)
  939. {
  940. struct tcp_md5sig cmd;
  941. struct sockaddr_in *sin = (struct sockaddr_in *)&cmd.tcpm_addr;
  942. if (optlen < sizeof(cmd))
  943. return -EINVAL;
  944. if (copy_from_user(&cmd, optval, sizeof(cmd)))
  945. return -EFAULT;
  946. if (sin->sin_family != AF_INET)
  947. return -EINVAL;
  948. if (!cmd.tcpm_key || !cmd.tcpm_keylen)
  949. return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin->sin_addr.s_addr,
  950. AF_INET);
  951. if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
  952. return -EINVAL;
  953. return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin->sin_addr.s_addr,
  954. AF_INET, cmd.tcpm_key, cmd.tcpm_keylen,
  955. GFP_KERNEL);
  956. }
  957. static int tcp_v4_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp,
  958. __be32 daddr, __be32 saddr, int nbytes)
  959. {
  960. struct tcp4_pseudohdr *bp;
  961. struct scatterlist sg;
  962. bp = &hp->md5_blk.ip4;
  963. /*
  964. * 1. the TCP pseudo-header (in the order: source IP address,
  965. * destination IP address, zero-padded protocol number, and
  966. * segment length)
  967. */
  968. bp->saddr = saddr;
  969. bp->daddr = daddr;
  970. bp->pad = 0;
  971. bp->protocol = IPPROTO_TCP;
  972. bp->len = cpu_to_be16(nbytes);
  973. sg_init_one(&sg, bp, sizeof(*bp));
  974. return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp));
  975. }
  976. static int tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
  977. __be32 daddr, __be32 saddr, const struct tcphdr *th)
  978. {
  979. struct tcp_md5sig_pool *hp;
  980. struct hash_desc *desc;
  981. hp = tcp_get_md5sig_pool();
  982. if (!hp)
  983. goto clear_hash_noput;
  984. desc = &hp->md5_desc;
  985. if (crypto_hash_init(desc))
  986. goto clear_hash;
  987. if (tcp_v4_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2))
  988. goto clear_hash;
  989. if (tcp_md5_hash_header(hp, th))
  990. goto clear_hash;
  991. if (tcp_md5_hash_key(hp, key))
  992. goto clear_hash;
  993. if (crypto_hash_final(desc, md5_hash))
  994. goto clear_hash;
  995. tcp_put_md5sig_pool();
  996. return 0;
  997. clear_hash:
  998. tcp_put_md5sig_pool();
  999. clear_hash_noput:
  1000. memset(md5_hash, 0, 16);
  1001. return 1;
  1002. }
  1003. int tcp_v4_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
  1004. const struct sock *sk, const struct request_sock *req,
  1005. const struct sk_buff *skb)
  1006. {
  1007. struct tcp_md5sig_pool *hp;
  1008. struct hash_desc *desc;
  1009. const struct tcphdr *th = tcp_hdr(skb);
  1010. __be32 saddr, daddr;
  1011. if (sk) {
  1012. saddr = inet_sk(sk)->inet_saddr;
  1013. daddr = inet_sk(sk)->inet_daddr;
  1014. } else if (req) {
  1015. saddr = inet_rsk(req)->loc_addr;
  1016. daddr = inet_rsk(req)->rmt_addr;
  1017. } else {
  1018. const struct iphdr *iph = ip_hdr(skb);
  1019. saddr = iph->saddr;
  1020. daddr = iph->daddr;
  1021. }
  1022. hp = tcp_get_md5sig_pool();
  1023. if (!hp)
  1024. goto clear_hash_noput;
  1025. desc = &hp->md5_desc;
  1026. if (crypto_hash_init(desc))
  1027. goto clear_hash;
  1028. if (tcp_v4_md5_hash_pseudoheader(hp, daddr, saddr, skb->len))
  1029. goto clear_hash;
  1030. if (tcp_md5_hash_header(hp, th))
  1031. goto clear_hash;
  1032. if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2))
  1033. goto clear_hash;
  1034. if (tcp_md5_hash_key(hp, key))
  1035. goto clear_hash;
  1036. if (crypto_hash_final(desc, md5_hash))
  1037. goto clear_hash;
  1038. tcp_put_md5sig_pool();
  1039. return 0;
  1040. clear_hash:
  1041. tcp_put_md5sig_pool();
  1042. clear_hash_noput:
  1043. memset(md5_hash, 0, 16);
  1044. return 1;
  1045. }
  1046. EXPORT_SYMBOL(tcp_v4_md5_hash_skb);
  1047. static bool tcp_v4_inbound_md5_hash(struct sock *sk, const struct sk_buff *skb)
  1048. {
  1049. /*
  1050. * This gets called for each TCP segment that arrives
  1051. * so we want to be efficient.
  1052. * We have 3 drop cases:
  1053. * o No MD5 hash and one expected.
  1054. * o MD5 hash and we're not expecting one.
  1055. * o MD5 hash and its wrong.
  1056. */
  1057. const __u8 *hash_location = NULL;
  1058. struct tcp_md5sig_key *hash_expected;
  1059. const struct iphdr *iph = ip_hdr(skb);
  1060. const struct tcphdr *th = tcp_hdr(skb);
  1061. int genhash;
  1062. unsigned char newhash[16];
  1063. hash_expected = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&iph->saddr,
  1064. AF_INET);
  1065. hash_location = tcp_parse_md5sig_option(th);
  1066. /* We've parsed the options - do we have a hash? */
  1067. if (!hash_expected && !hash_location)
  1068. return false;
  1069. if (hash_expected && !hash_location) {
  1070. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND);
  1071. return true;
  1072. }
  1073. if (!hash_expected && hash_location) {
  1074. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED);
  1075. return true;
  1076. }
  1077. /* Okay, so this is hash_expected and hash_location -
  1078. * so we need to calculate the checksum.
  1079. */
  1080. genhash = tcp_v4_md5_hash_skb(newhash,
  1081. hash_expected,
  1082. NULL, NULL, skb);
  1083. if (genhash || memcmp(hash_location, newhash, 16) != 0) {
  1084. net_info_ratelimited("MD5 Hash failed for (%pI4, %d)->(%pI4, %d)%s\n",
  1085. &iph->saddr, ntohs(th->source),
  1086. &iph->daddr, ntohs(th->dest),
  1087. genhash ? " tcp_v4_calc_md5_hash failed"
  1088. : "");
  1089. return true;
  1090. }
  1091. return false;
  1092. }
  1093. #endif
  1094. struct request_sock_ops tcp_request_sock_ops __read_mostly = {
  1095. .family = PF_INET,
  1096. .obj_size = sizeof(struct tcp_request_sock),
  1097. .rtx_syn_ack = tcp_v4_rtx_synack,
  1098. .send_ack = tcp_v4_reqsk_send_ack,
  1099. .destructor = tcp_v4_reqsk_destructor,
  1100. .send_reset = tcp_v4_send_reset,
  1101. .syn_ack_timeout = tcp_syn_ack_timeout,
  1102. };
  1103. #ifdef CONFIG_TCP_MD5SIG
  1104. static const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
  1105. .md5_lookup = tcp_v4_reqsk_md5_lookup,
  1106. .calc_md5_hash = tcp_v4_md5_hash_skb,
  1107. };
  1108. #endif
  1109. static bool tcp_fastopen_check(struct sock *sk, struct sk_buff *skb,
  1110. struct request_sock *req,
  1111. struct tcp_fastopen_cookie *foc,
  1112. struct tcp_fastopen_cookie *valid_foc)
  1113. {
  1114. bool skip_cookie = false;
  1115. struct fastopen_queue *fastopenq;
  1116. if (likely(!fastopen_cookie_present(foc))) {
  1117. /* See include/net/tcp.h for the meaning of these knobs */
  1118. if ((sysctl_tcp_fastopen & TFO_SERVER_ALWAYS) ||
  1119. ((sysctl_tcp_fastopen & TFO_SERVER_COOKIE_NOT_REQD) &&
  1120. (TCP_SKB_CB(skb)->end_seq != TCP_SKB_CB(skb)->seq + 1)))
  1121. skip_cookie = true; /* no cookie to validate */
  1122. else
  1123. return false;
  1124. }
  1125. fastopenq = inet_csk(sk)->icsk_accept_queue.fastopenq;
  1126. /* A FO option is present; bump the counter. */
  1127. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVE);
  1128. /* Make sure the listener has enabled fastopen, and we don't
  1129. * exceed the max # of pending TFO requests allowed before trying
  1130. * to validating the cookie in order to avoid burning CPU cycles
  1131. * unnecessarily.
  1132. *
  1133. * XXX (TFO) - The implication of checking the max_qlen before
  1134. * processing a cookie request is that clients can't differentiate
  1135. * between qlen overflow causing Fast Open to be disabled
  1136. * temporarily vs a server not supporting Fast Open at all.
  1137. */
  1138. if ((sysctl_tcp_fastopen & TFO_SERVER_ENABLE) == 0 ||
  1139. fastopenq == NULL || fastopenq->max_qlen == 0)
  1140. return false;
  1141. if (fastopenq->qlen >= fastopenq->max_qlen) {
  1142. struct request_sock *req1;
  1143. spin_lock(&fastopenq->lock);
  1144. req1 = fastopenq->rskq_rst_head;
  1145. if ((req1 == NULL) || time_after(req1->expires, jiffies)) {
  1146. spin_unlock(&fastopenq->lock);
  1147. NET_INC_STATS_BH(sock_net(sk),
  1148. LINUX_MIB_TCPFASTOPENLISTENOVERFLOW);
  1149. /* Avoid bumping LINUX_MIB_TCPFASTOPENPASSIVEFAIL*/
  1150. foc->len = -1;
  1151. return false;
  1152. }
  1153. fastopenq->rskq_rst_head = req1->dl_next;
  1154. fastopenq->qlen--;
  1155. spin_unlock(&fastopenq->lock);
  1156. reqsk_free(req1);
  1157. }
  1158. if (skip_cookie) {
  1159. tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
  1160. return true;
  1161. }
  1162. if (foc->len == TCP_FASTOPEN_COOKIE_SIZE) {
  1163. if ((sysctl_tcp_fastopen & TFO_SERVER_COOKIE_NOT_CHKED) == 0) {
  1164. tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr, valid_foc);
  1165. if ((valid_foc->len != TCP_FASTOPEN_COOKIE_SIZE) ||
  1166. memcmp(&foc->val[0], &valid_foc->val[0],
  1167. TCP_FASTOPEN_COOKIE_SIZE) != 0)
  1168. return false;
  1169. valid_foc->len = -1;
  1170. }
  1171. /* Acknowledge the data received from the peer. */
  1172. tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
  1173. return true;
  1174. } else if (foc->len == 0) { /* Client requesting a cookie */
  1175. tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr, valid_foc);
  1176. NET_INC_STATS_BH(sock_net(sk),
  1177. LINUX_MIB_TCPFASTOPENCOOKIEREQD);
  1178. } else {
  1179. /* Client sent a cookie with wrong size. Treat it
  1180. * the same as invalid and return a valid one.
  1181. */
  1182. tcp_fastopen_cookie_gen(ip_hdr(skb)->saddr, valid_foc);
  1183. }
  1184. return false;
  1185. }
  1186. static int tcp_v4_conn_req_fastopen(struct sock *sk,
  1187. struct sk_buff *skb,
  1188. struct sk_buff *skb_synack,
  1189. struct request_sock *req,
  1190. struct request_values *rvp)
  1191. {
  1192. struct tcp_sock *tp = tcp_sk(sk);
  1193. struct request_sock_queue *queue = &inet_csk(sk)->icsk_accept_queue;
  1194. const struct inet_request_sock *ireq = inet_rsk(req);
  1195. struct sock *child;
  1196. int err;
  1197. req->num_retrans = 0;
  1198. req->num_timeout = 0;
  1199. req->sk = NULL;
  1200. child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL);
  1201. if (child == NULL) {
  1202. NET_INC_STATS_BH(sock_net(sk),
  1203. LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
  1204. kfree_skb(skb_synack);
  1205. return -1;
  1206. }
  1207. err = ip_build_and_send_pkt(skb_synack, sk, ireq->loc_addr,
  1208. ireq->rmt_addr, ireq->opt);
  1209. err = net_xmit_eval(err);
  1210. if (!err)
  1211. tcp_rsk(req)->snt_synack = tcp_time_stamp;
  1212. /* XXX (TFO) - is it ok to ignore error and continue? */
  1213. spin_lock(&queue->fastopenq->lock);
  1214. queue->fastopenq->qlen++;
  1215. spin_unlock(&queue->fastopenq->lock);
  1216. /* Initialize the child socket. Have to fix some values to take
  1217. * into account the child is a Fast Open socket and is created
  1218. * only out of the bits carried in the SYN packet.
  1219. */
  1220. tp = tcp_sk(child);
  1221. tp->fastopen_rsk = req;
  1222. /* Do a hold on the listner sk so that if the listener is being
  1223. * closed, the child that has been accepted can live on and still
  1224. * access listen_lock.
  1225. */
  1226. sock_hold(sk);
  1227. tcp_rsk(req)->listener = sk;
  1228. /* RFC1323: The window in SYN & SYN/ACK segments is never
  1229. * scaled. So correct it appropriately.
  1230. */
  1231. tp->snd_wnd = ntohs(tcp_hdr(skb)->window);
  1232. /* Activate the retrans timer so that SYNACK can be retransmitted.
  1233. * The request socket is not added to the SYN table of the parent
  1234. * because it's been added to the accept queue directly.
  1235. */
  1236. inet_csk_reset_xmit_timer(child, ICSK_TIME_RETRANS,
  1237. TCP_TIMEOUT_INIT, TCP_RTO_MAX);
  1238. /* Add the child socket directly into the accept queue */
  1239. inet_csk_reqsk_queue_add(sk, req, child);
  1240. /* Now finish processing the fastopen child socket. */
  1241. inet_csk(child)->icsk_af_ops->rebuild_header(child);
  1242. tcp_init_congestion_control(child);
  1243. tcp_mtup_init(child);
  1244. tcp_init_buffer_space(child);
  1245. tcp_init_metrics(child);
  1246. /* Queue the data carried in the SYN packet. We need to first
  1247. * bump skb's refcnt because the caller will attempt to free it.
  1248. *
  1249. * XXX (TFO) - we honor a zero-payload TFO request for now.
  1250. * (Any reason not to?)
  1251. */
  1252. if (TCP_SKB_CB(skb)->end_seq == TCP_SKB_CB(skb)->seq + 1) {
  1253. /* Don't queue the skb if there is no payload in SYN.
  1254. * XXX (TFO) - How about SYN+FIN?
  1255. */
  1256. tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
  1257. } else {
  1258. skb = skb_get(skb);
  1259. skb_dst_drop(skb);
  1260. __skb_pull(skb, tcp_hdr(skb)->doff * 4);
  1261. skb_set_owner_r(skb, child);
  1262. __skb_queue_tail(&child->sk_receive_queue, skb);
  1263. tp->rcv_nxt = TCP_SKB_CB(skb)->end_seq;
  1264. tp->syn_data_acked = 1;
  1265. }
  1266. sk->sk_data_ready(sk, 0);
  1267. bh_unlock_sock(child);
  1268. sock_put(child);
  1269. WARN_ON(req->sk == NULL);
  1270. return 0;
  1271. }
  1272. int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
  1273. {
  1274. struct tcp_extend_values tmp_ext;
  1275. struct tcp_options_received tmp_opt;
  1276. const u8 *hash_location;
  1277. struct request_sock *req;
  1278. struct inet_request_sock *ireq;
  1279. struct tcp_sock *tp = tcp_sk(sk);
  1280. struct dst_entry *dst = NULL;
  1281. __be32 saddr = ip_hdr(skb)->saddr;
  1282. __be32 daddr = ip_hdr(skb)->daddr;
  1283. __u32 isn = TCP_SKB_CB(skb)->when;
  1284. bool want_cookie = false;
  1285. struct flowi4 fl4;
  1286. struct tcp_fastopen_cookie foc = { .len = -1 };
  1287. struct tcp_fastopen_cookie valid_foc = { .len = -1 };
  1288. struct sk_buff *skb_synack;
  1289. int do_fastopen;
  1290. /* Never answer to SYNs send to broadcast or multicast */
  1291. if (skb_rtable(skb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
  1292. goto drop;
  1293. /* TW buckets are converted to open requests without
  1294. * limitations, they conserve resources and peer is
  1295. * evidently real one.
  1296. */
  1297. if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
  1298. want_cookie = tcp_syn_flood_action(sk, skb, "TCP");
  1299. if (!want_cookie)
  1300. goto drop;
  1301. }
  1302. /* Accept backlog is full. If we have already queued enough
  1303. * of warm entries in syn queue, drop request. It is better than
  1304. * clogging syn queue with openreqs with exponentially increasing
  1305. * timeout.
  1306. */
  1307. if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1) {
  1308. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
  1309. goto drop;
  1310. }
  1311. req = inet_reqsk_alloc(&tcp_request_sock_ops);
  1312. if (!req)
  1313. goto drop;
  1314. #ifdef CONFIG_TCP_MD5SIG
  1315. tcp_rsk(req)->af_specific = &tcp_request_sock_ipv4_ops;
  1316. #endif
  1317. tcp_clear_options(&tmp_opt);
  1318. tmp_opt.mss_clamp = TCP_MSS_DEFAULT;
  1319. tmp_opt.user_mss = tp->rx_opt.user_mss;
  1320. tcp_parse_options(skb, &tmp_opt, &hash_location, 0,
  1321. want_cookie ? NULL : &foc);
  1322. if (tmp_opt.cookie_plus > 0 &&
  1323. tmp_opt.saw_tstamp &&
  1324. !tp->rx_opt.cookie_out_never &&
  1325. (sysctl_tcp_cookie_size > 0 ||
  1326. (tp->cookie_values != NULL &&
  1327. tp->cookie_values->cookie_desired > 0))) {
  1328. u8 *c;
  1329. u32 *mess = &tmp_ext.cookie_bakery[COOKIE_DIGEST_WORDS];
  1330. int l = tmp_opt.cookie_plus - TCPOLEN_COOKIE_BASE;
  1331. if (tcp_cookie_generator(&tmp_ext.cookie_bakery[0]) != 0)
  1332. goto drop_and_release;
  1333. /* Secret recipe starts with IP addresses */
  1334. *mess++ ^= (__force u32)daddr;
  1335. *mess++ ^= (__force u32)saddr;
  1336. /* plus variable length Initiator Cookie */
  1337. c = (u8 *)mess;
  1338. while (l-- > 0)
  1339. *c++ ^= *hash_location++;
  1340. want_cookie = false; /* not our kind of cookie */
  1341. tmp_ext.cookie_out_never = 0; /* false */
  1342. tmp_ext.cookie_plus = tmp_opt.cookie_plus;
  1343. } else if (!tp->rx_opt.cookie_in_always) {
  1344. /* redundant indications, but ensure initialization. */
  1345. tmp_ext.cookie_out_never = 1; /* true */
  1346. tmp_ext.cookie_plus = 0;
  1347. } else {
  1348. goto drop_and_release;
  1349. }
  1350. tmp_ext.cookie_in_always = tp->rx_opt.cookie_in_always;
  1351. if (want_cookie && !tmp_opt.saw_tstamp)
  1352. tcp_clear_options(&tmp_opt);
  1353. tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
  1354. tcp_openreq_init(req, &tmp_opt, skb);
  1355. ireq = inet_rsk(req);
  1356. ireq->loc_addr = daddr;
  1357. ireq->rmt_addr = saddr;
  1358. ireq->no_srccheck = inet_sk(sk)->transparent;
  1359. ireq->opt = tcp_v4_save_options(skb);
  1360. if (security_inet_conn_request(sk, skb, req))
  1361. goto drop_and_free;
  1362. if (!want_cookie || tmp_opt.tstamp_ok)
  1363. TCP_ECN_create_request(req, skb);
  1364. if (want_cookie) {
  1365. isn = cookie_v4_init_sequence(sk, skb, &req->mss);
  1366. req->cookie_ts = tmp_opt.tstamp_ok;
  1367. } else if (!isn) {
  1368. /* VJ's idea. We save last timestamp seen
  1369. * from the destination in peer table, when entering
  1370. * state TIME-WAIT, and check against it before
  1371. * accepting new connection request.
  1372. *
  1373. * If "isn" is not zero, this request hit alive
  1374. * timewait bucket, so that all the necessary checks
  1375. * are made in the function processing timewait state.
  1376. */
  1377. if (tmp_opt.saw_tstamp &&
  1378. tcp_death_row.sysctl_tw_recycle &&
  1379. (dst = inet_csk_route_req(sk, &fl4, req)) != NULL &&
  1380. fl4.daddr == saddr) {
  1381. if (!tcp_peer_is_proven(req, dst, true)) {
  1382. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED);
  1383. goto drop_and_release;
  1384. }
  1385. }
  1386. /* Kill the following clause, if you dislike this way. */
  1387. else if (!sysctl_tcp_syncookies &&
  1388. (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
  1389. (sysctl_max_syn_backlog >> 2)) &&
  1390. !tcp_peer_is_proven(req, dst, false)) {
  1391. /* Without syncookies last quarter of
  1392. * backlog is filled with destinations,
  1393. * proven to be alive.
  1394. * It means that we continue to communicate
  1395. * to destinations, already remembered
  1396. * to the moment of synflood.
  1397. */
  1398. LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("drop open request from %pI4/%u\n"),
  1399. &saddr, ntohs(tcp_hdr(skb)->source));
  1400. goto drop_and_release;
  1401. }
  1402. isn = tcp_v4_init_sequence(skb);
  1403. }
  1404. tcp_rsk(req)->snt_isn = isn;
  1405. if (dst == NULL) {
  1406. dst = inet_csk_route_req(sk, &fl4, req);
  1407. if (dst == NULL)
  1408. goto drop_and_free;
  1409. }
  1410. do_fastopen = tcp_fastopen_check(sk, skb, req, &foc, &valid_foc);
  1411. /* We don't call tcp_v4_send_synack() directly because we need
  1412. * to make sure a child socket can be created successfully before
  1413. * sending back synack!
  1414. *
  1415. * XXX (TFO) - Ideally one would simply call tcp_v4_send_synack()
  1416. * (or better yet, call tcp_send_synack() in the child context
  1417. * directly, but will have to fix bunch of other code first)
  1418. * after syn_recv_sock() except one will need to first fix the
  1419. * latter to remove its dependency on the current implementation
  1420. * of tcp_v4_send_synack()->tcp_select_initial_window().
  1421. */
  1422. skb_synack = tcp_make_synack(sk, dst, req,
  1423. (struct request_values *)&tmp_ext,
  1424. fastopen_cookie_present(&valid_foc) ? &valid_foc : NULL);
  1425. if (skb_synack) {
  1426. __tcp_v4_send_check(skb_synack, ireq->loc_addr, ireq->rmt_addr);
  1427. skb_set_queue_mapping(skb_synack, skb_get_queue_mapping(skb));
  1428. } else
  1429. goto drop_and_free;
  1430. if (likely(!do_fastopen)) {
  1431. int err;
  1432. err = ip_build_and_send_pkt(skb_synack, sk, ireq->loc_addr,
  1433. ireq->rmt_addr, ireq->opt);
  1434. err = net_xmit_eval(err);
  1435. if (err || want_cookie)
  1436. goto drop_and_free;
  1437. tcp_rsk(req)->snt_synack = tcp_time_stamp;
  1438. tcp_rsk(req)->listener = NULL;
  1439. /* Add the request_sock to the SYN table */
  1440. inet_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
  1441. if (fastopen_cookie_present(&foc) && foc.len != 0)
  1442. NET_INC_STATS_BH(sock_net(sk),
  1443. LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
  1444. } else if (tcp_v4_conn_req_fastopen(sk, skb, skb_synack, req,
  1445. (struct request_values *)&tmp_ext))
  1446. goto drop_and_free;
  1447. return 0;
  1448. drop_and_release:
  1449. dst_release(dst);
  1450. drop_and_free:
  1451. reqsk_free(req);
  1452. drop:
  1453. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
  1454. return 0;
  1455. }
  1456. EXPORT_SYMBOL(tcp_v4_conn_request);
  1457. /*
  1458. * The three way handshake has completed - we got a valid synack -
  1459. * now create the new socket.
  1460. */
  1461. struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
  1462. struct request_sock *req,
  1463. struct dst_entry *dst)
  1464. {
  1465. struct inet_request_sock *ireq;
  1466. struct inet_sock *newinet;
  1467. struct tcp_sock *newtp;
  1468. struct sock *newsk;
  1469. #ifdef CONFIG_TCP_MD5SIG
  1470. struct tcp_md5sig_key *key;
  1471. #endif
  1472. struct ip_options_rcu *inet_opt;
  1473. if (sk_acceptq_is_full(sk))
  1474. goto exit_overflow;
  1475. newsk = tcp_create_openreq_child(sk, req, skb);
  1476. if (!newsk)
  1477. goto exit_nonewsk;
  1478. newsk->sk_gso_type = SKB_GSO_TCPV4;
  1479. inet_sk_rx_dst_set(newsk, skb);
  1480. newtp = tcp_sk(newsk);
  1481. newinet = inet_sk(newsk);
  1482. ireq = inet_rsk(req);
  1483. newinet->inet_daddr = ireq->rmt_addr;
  1484. newinet->inet_rcv_saddr = ireq->loc_addr;
  1485. newinet->inet_saddr = ireq->loc_addr;
  1486. inet_opt = ireq->opt;
  1487. rcu_assign_pointer(newinet->inet_opt, inet_opt);
  1488. ireq->opt = NULL;
  1489. newinet->mc_index = inet_iif(skb);
  1490. newinet->mc_ttl = ip_hdr(skb)->ttl;
  1491. newinet->rcv_tos = ip_hdr(skb)->tos;
  1492. inet_csk(newsk)->icsk_ext_hdr_len = 0;
  1493. if (inet_opt)
  1494. inet_csk(newsk)->icsk_ext_hdr_len = inet_opt->opt.optlen;
  1495. newinet->inet_id = newtp->write_seq ^ jiffies;
  1496. if (!dst) {
  1497. dst = inet_csk_route_child_sock(sk, newsk, req);
  1498. if (!dst)
  1499. goto put_and_exit;
  1500. } else {
  1501. /* syncookie case : see end of cookie_v4_check() */
  1502. }
  1503. sk_setup_caps(newsk, dst);
  1504. tcp_mtup_init(newsk);
  1505. tcp_sync_mss(newsk, dst_mtu(dst));
  1506. newtp->advmss = dst_metric_advmss(dst);
  1507. if (tcp_sk(sk)->rx_opt.user_mss &&
  1508. tcp_sk(sk)->rx_opt.user_mss < newtp->advmss)
  1509. newtp->advmss = tcp_sk(sk)->rx_opt.user_mss;
  1510. tcp_initialize_rcv_mss(newsk);
  1511. tcp_synack_rtt_meas(newsk, req);
  1512. newtp->total_retrans = req->num_retrans;
  1513. #ifdef CONFIG_TCP_MD5SIG
  1514. /* Copy over the MD5 key from the original socket */
  1515. key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&newinet->inet_daddr,
  1516. AF_INET);
  1517. if (key != NULL) {
  1518. /*
  1519. * We're using one, so create a matching key
  1520. * on the newsk structure. If we fail to get
  1521. * memory, then we end up not copying the key
  1522. * across. Shucks.
  1523. */
  1524. tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newinet->inet_daddr,
  1525. AF_INET, key->key, key->keylen, GFP_ATOMIC);
  1526. sk_nocaps_add(newsk, NETIF_F_GSO_MASK);
  1527. }
  1528. #endif
  1529. if (__inet_inherit_port(sk, newsk) < 0)
  1530. goto put_and_exit;
  1531. __inet_hash_nolisten(newsk, NULL);
  1532. return newsk;
  1533. exit_overflow:
  1534. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
  1535. exit_nonewsk:
  1536. dst_release(dst);
  1537. exit:
  1538. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
  1539. return NULL;
  1540. put_and_exit:
  1541. inet_csk_prepare_forced_close(newsk);
  1542. tcp_done(newsk);
  1543. goto exit;
  1544. }
  1545. EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
  1546. static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
  1547. {
  1548. struct tcphdr *th = tcp_hdr(skb);
  1549. const struct iphdr *iph = ip_hdr(skb);
  1550. struct sock *nsk;
  1551. struct request_sock **prev;
  1552. /* Find possible connection requests. */
  1553. struct request_sock *req = inet_csk_search_req(sk, &prev, th->source,
  1554. iph->saddr, iph->daddr);
  1555. if (req)
  1556. return tcp_check_req(sk, skb, req, prev, false);
  1557. nsk = inet_lookup_established(sock_net(sk), &tcp_hashinfo, iph->saddr,
  1558. th->source, iph->daddr, th->dest, inet_iif(skb));
  1559. if (nsk) {
  1560. if (nsk->sk_state != TCP_TIME_WAIT) {
  1561. bh_lock_sock(nsk);
  1562. return nsk;
  1563. }
  1564. inet_twsk_put(inet_twsk(nsk));
  1565. return NULL;
  1566. }
  1567. #ifdef CONFIG_SYN_COOKIES
  1568. if (!th->syn)
  1569. sk = cookie_v4_check(sk, skb, &(IPCB(skb)->opt));
  1570. #endif
  1571. return sk;
  1572. }
  1573. static __sum16 tcp_v4_checksum_init(struct sk_buff *skb)
  1574. {
  1575. const struct iphdr *iph = ip_hdr(skb);
  1576. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  1577. if (!tcp_v4_check(skb->len, iph->saddr,
  1578. iph->daddr, skb->csum)) {
  1579. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1580. return 0;
  1581. }
  1582. }
  1583. skb->csum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  1584. skb->len, IPPROTO_TCP, 0);
  1585. if (skb->len <= 76) {
  1586. return __skb_checksum_complete(skb);
  1587. }
  1588. return 0;
  1589. }
  1590. /* The socket must have it's spinlock held when we get
  1591. * here.
  1592. *
  1593. * We have a potential double-lock case here, so even when
  1594. * doing backlog processing we use the BH locking scheme.
  1595. * This is because we cannot sleep with the original spinlock
  1596. * held.
  1597. */
  1598. int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
  1599. {
  1600. struct sock *rsk;
  1601. #ifdef CONFIG_TCP_MD5SIG
  1602. /*
  1603. * We really want to reject the packet as early as possible
  1604. * if:
  1605. * o We're expecting an MD5'd packet and this is no MD5 tcp option
  1606. * o There is an MD5 option and we're not expecting one
  1607. */
  1608. if (tcp_v4_inbound_md5_hash(sk, skb))
  1609. goto discard;
  1610. #endif
  1611. if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
  1612. struct dst_entry *dst = sk->sk_rx_dst;
  1613. sock_rps_save_rxhash(sk, skb);
  1614. if (dst) {
  1615. if (inet_sk(sk)->rx_dst_ifindex != skb->skb_iif ||
  1616. dst->ops->check(dst, 0) == NULL) {
  1617. dst_release(dst);
  1618. sk->sk_rx_dst = NULL;
  1619. }
  1620. }
  1621. if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len)) {
  1622. rsk = sk;
  1623. goto reset;
  1624. }
  1625. return 0;
  1626. }
  1627. if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
  1628. goto csum_err;
  1629. if (sk->sk_state == TCP_LISTEN) {
  1630. struct sock *nsk = tcp_v4_hnd_req(sk, skb);
  1631. if (!nsk)
  1632. goto discard;
  1633. if (nsk != sk) {
  1634. sock_rps_save_rxhash(nsk, skb);
  1635. if (tcp_child_process(sk, nsk, skb)) {
  1636. rsk = nsk;
  1637. goto reset;
  1638. }
  1639. return 0;
  1640. }
  1641. } else
  1642. sock_rps_save_rxhash(sk, skb);
  1643. if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len)) {
  1644. rsk = sk;
  1645. goto reset;
  1646. }
  1647. return 0;
  1648. reset:
  1649. tcp_v4_send_reset(rsk, skb);
  1650. discard:
  1651. kfree_skb(skb);
  1652. /* Be careful here. If this function gets more complicated and
  1653. * gcc suffers from register pressure on the x86, sk (in %ebx)
  1654. * might be destroyed here. This current version compiles correctly,
  1655. * but you have been warned.
  1656. */
  1657. return 0;
  1658. csum_err:
  1659. TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
  1660. goto discard;
  1661. }
  1662. EXPORT_SYMBOL(tcp_v4_do_rcv);
  1663. void tcp_v4_early_demux(struct sk_buff *skb)
  1664. {
  1665. const struct iphdr *iph;
  1666. const struct tcphdr *th;
  1667. struct sock *sk;
  1668. if (skb->pkt_type != PACKET_HOST)
  1669. return;
  1670. if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct tcphdr)))
  1671. return;
  1672. iph = ip_hdr(skb);
  1673. th = tcp_hdr(skb);
  1674. if (th->doff < sizeof(struct tcphdr) / 4)
  1675. return;
  1676. sk = __inet_lookup_established(dev_net(skb->dev), &tcp_hashinfo,
  1677. iph->saddr, th->source,
  1678. iph->daddr, ntohs(th->dest),
  1679. skb->skb_iif);
  1680. if (sk) {
  1681. skb->sk = sk;
  1682. skb->destructor = sock_edemux;
  1683. if (sk->sk_state != TCP_TIME_WAIT) {
  1684. struct dst_entry *dst = sk->sk_rx_dst;
  1685. if (dst)
  1686. dst = dst_check(dst, 0);
  1687. if (dst &&
  1688. inet_sk(sk)->rx_dst_ifindex == skb->skb_iif)
  1689. skb_dst_set_noref(skb, dst);
  1690. }
  1691. }
  1692. }
  1693. /*
  1694. * From tcp_input.c
  1695. */
  1696. int tcp_v4_rcv(struct sk_buff *skb)
  1697. {
  1698. const struct iphdr *iph;
  1699. const struct tcphdr *th;
  1700. struct sock *sk;
  1701. int ret;
  1702. struct net *net = dev_net(skb->dev);
  1703. if (skb->pkt_type != PACKET_HOST)
  1704. goto discard_it;
  1705. /* Count it even if it's bad */
  1706. TCP_INC_STATS_BH(net, TCP_MIB_INSEGS);
  1707. if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
  1708. goto discard_it;
  1709. th = tcp_hdr(skb);
  1710. if (th->doff < sizeof(struct tcphdr) / 4)
  1711. goto bad_packet;
  1712. if (!pskb_may_pull(skb, th->doff * 4))
  1713. goto discard_it;
  1714. /* An explanation is required here, I think.
  1715. * Packet length and doff are validated by header prediction,
  1716. * provided case of th->doff==0 is eliminated.
  1717. * So, we defer the checks. */
  1718. if (!skb_csum_unnecessary(skb) && tcp_v4_checksum_init(skb))
  1719. goto bad_packet;
  1720. th = tcp_hdr(skb);
  1721. iph = ip_hdr(skb);
  1722. TCP_SKB_CB(skb)->seq = ntohl(th->seq);
  1723. TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
  1724. skb->len - th->doff * 4);
  1725. TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
  1726. TCP_SKB_CB(skb)->when = 0;
  1727. TCP_SKB_CB(skb)->ip_dsfield = ipv4_get_dsfield(iph);
  1728. TCP_SKB_CB(skb)->sacked = 0;
  1729. sk = __inet_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
  1730. if (!sk)
  1731. goto no_tcp_socket;
  1732. process:
  1733. if (sk->sk_state == TCP_TIME_WAIT)
  1734. goto do_time_wait;
  1735. if (unlikely(iph->ttl < inet_sk(sk)->min_ttl)) {
  1736. NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
  1737. goto discard_and_relse;
  1738. }
  1739. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb))
  1740. goto discard_and_relse;
  1741. nf_reset(skb);
  1742. if (sk_filter(sk, skb))
  1743. goto discard_and_relse;
  1744. skb->dev = NULL;
  1745. bh_lock_sock_nested(sk);
  1746. ret = 0;
  1747. if (!sock_owned_by_user(sk)) {
  1748. #ifdef CONFIG_NET_DMA
  1749. struct tcp_sock *tp = tcp_sk(sk);
  1750. if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
  1751. tp->ucopy.dma_chan = net_dma_find_channel();
  1752. if (tp->ucopy.dma_chan)
  1753. ret = tcp_v4_do_rcv(sk, skb);
  1754. else
  1755. #endif
  1756. {
  1757. if (!tcp_prequeue(sk, skb))
  1758. ret = tcp_v4_do_rcv(sk, skb);
  1759. }
  1760. } else if (unlikely(sk_add_backlog(sk, skb,
  1761. sk->sk_rcvbuf + sk->sk_sndbuf))) {
  1762. bh_unlock_sock(sk);
  1763. NET_INC_STATS_BH(net, LINUX_MIB_TCPBACKLOGDROP);
  1764. goto discard_and_relse;
  1765. }
  1766. bh_unlock_sock(sk);
  1767. sock_put(sk);
  1768. return ret;
  1769. no_tcp_socket:
  1770. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
  1771. goto discard_it;
  1772. if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
  1773. bad_packet:
  1774. TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
  1775. } else {
  1776. tcp_v4_send_reset(NULL, skb);
  1777. }
  1778. discard_it:
  1779. /* Discard frame. */
  1780. kfree_skb(skb);
  1781. return 0;
  1782. discard_and_relse:
  1783. sock_put(sk);
  1784. goto discard_it;
  1785. do_time_wait:
  1786. if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  1787. inet_twsk_put(inet_twsk(sk));
  1788. goto discard_it;
  1789. }
  1790. if (skb->len < (th->doff << 2) || tcp_checksum_complete(skb)) {
  1791. TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
  1792. inet_twsk_put(inet_twsk(sk));
  1793. goto discard_it;
  1794. }
  1795. switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
  1796. case TCP_TW_SYN: {
  1797. struct sock *sk2 = inet_lookup_listener(dev_net(skb->dev),
  1798. &tcp_hashinfo,
  1799. iph->daddr, th->dest,
  1800. inet_iif(skb));
  1801. if (sk2) {
  1802. inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row);
  1803. inet_twsk_put(inet_twsk(sk));
  1804. sk = sk2;
  1805. goto process;
  1806. }
  1807. /* Fall through to ACK */
  1808. }
  1809. case TCP_TW_ACK:
  1810. tcp_v4_timewait_ack(sk, skb);
  1811. break;
  1812. case TCP_TW_RST:
  1813. goto no_tcp_socket;
  1814. case TCP_TW_SUCCESS:;
  1815. }
  1816. goto discard_it;
  1817. }
  1818. static struct timewait_sock_ops tcp_timewait_sock_ops = {
  1819. .twsk_obj_size = sizeof(struct tcp_timewait_sock),
  1820. .twsk_unique = tcp_twsk_unique,
  1821. .twsk_destructor= tcp_twsk_destructor,
  1822. };
  1823. void inet_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb)
  1824. {
  1825. struct dst_entry *dst = skb_dst(skb);
  1826. dst_hold(dst);
  1827. sk->sk_rx_dst = dst;
  1828. inet_sk(sk)->rx_dst_ifindex = skb->skb_iif;
  1829. }
  1830. EXPORT_SYMBOL(inet_sk_rx_dst_set);
  1831. const struct inet_connection_sock_af_ops ipv4_specific = {
  1832. .queue_xmit = ip_queue_xmit,
  1833. .send_check = tcp_v4_send_check,
  1834. .rebuild_header = inet_sk_rebuild_header,
  1835. .sk_rx_dst_set = inet_sk_rx_dst_set,
  1836. .conn_request = tcp_v4_conn_request,
  1837. .syn_recv_sock = tcp_v4_syn_recv_sock,
  1838. .net_header_len = sizeof(struct iphdr),
  1839. .setsockopt = ip_setsockopt,
  1840. .getsockopt = ip_getsockopt,
  1841. .addr2sockaddr = inet_csk_addr2sockaddr,
  1842. .sockaddr_len = sizeof(struct sockaddr_in),
  1843. .bind_conflict = inet_csk_bind_conflict,
  1844. #ifdef CONFIG_COMPAT
  1845. .compat_setsockopt = compat_ip_setsockopt,
  1846. .compat_getsockopt = compat_ip_getsockopt,
  1847. #endif
  1848. };
  1849. EXPORT_SYMBOL(ipv4_specific);
  1850. #ifdef CONFIG_TCP_MD5SIG
  1851. static const struct tcp_sock_af_ops tcp_sock_ipv4_specific = {
  1852. .md5_lookup = tcp_v4_md5_lookup,
  1853. .calc_md5_hash = tcp_v4_md5_hash_skb,
  1854. .md5_parse = tcp_v4_parse_md5_keys,
  1855. };
  1856. #endif
  1857. /* NOTE: A lot of things set to zero explicitly by call to
  1858. * sk_alloc() so need not be done here.
  1859. */
  1860. static int tcp_v4_init_sock(struct sock *sk)
  1861. {
  1862. struct inet_connection_sock *icsk = inet_csk(sk);
  1863. tcp_init_sock(sk);
  1864. icsk->icsk_af_ops = &ipv4_specific;
  1865. #ifdef CONFIG_TCP_MD5SIG
  1866. tcp_sk(sk)->af_specific = &tcp_sock_ipv4_specific;
  1867. #endif
  1868. return 0;
  1869. }
  1870. void tcp_v4_destroy_sock(struct sock *sk)
  1871. {
  1872. struct tcp_sock *tp = tcp_sk(sk);
  1873. tcp_clear_xmit_timers(sk);
  1874. tcp_cleanup_congestion_control(sk);
  1875. /* Cleanup up the write buffer. */
  1876. tcp_write_queue_purge(sk);
  1877. /* Cleans up our, hopefully empty, out_of_order_queue. */
  1878. __skb_queue_purge(&tp->out_of_order_queue);
  1879. #ifdef CONFIG_TCP_MD5SIG
  1880. /* Clean up the MD5 key list, if any */
  1881. if (tp->md5sig_info) {
  1882. tcp_clear_md5_list(sk);
  1883. kfree_rcu(tp->md5sig_info, rcu);
  1884. tp->md5sig_info = NULL;
  1885. }
  1886. #endif
  1887. #ifdef CONFIG_NET_DMA
  1888. /* Cleans up our sk_async_wait_queue */
  1889. __skb_queue_purge(&sk->sk_async_wait_queue);
  1890. #endif
  1891. /* Clean prequeue, it must be empty really */
  1892. __skb_queue_purge(&tp->ucopy.prequeue);
  1893. /* Clean up a referenced TCP bind bucket. */
  1894. if (inet_csk(sk)->icsk_bind_hash)
  1895. inet_put_port(sk);
  1896. /* TCP Cookie Transactions */
  1897. if (tp->cookie_values != NULL) {
  1898. kref_put(&tp->cookie_values->kref,
  1899. tcp_cookie_values_release);
  1900. tp->cookie_values = NULL;
  1901. }
  1902. BUG_ON(tp->fastopen_rsk != NULL);
  1903. /* If socket is aborted during connect operation */
  1904. tcp_free_fastopen_req(tp);
  1905. sk_sockets_allocated_dec(sk);
  1906. sock_release_memcg(sk);
  1907. }
  1908. EXPORT_SYMBOL(tcp_v4_destroy_sock);
  1909. #ifdef CONFIG_PROC_FS
  1910. /* Proc filesystem TCP sock list dumping. */
  1911. static inline struct inet_timewait_sock *tw_head(struct hlist_nulls_head *head)
  1912. {
  1913. return hlist_nulls_empty(head) ? NULL :
  1914. list_entry(head->first, struct inet_timewait_sock, tw_node);
  1915. }
  1916. static inline struct inet_timewait_sock *tw_next(struct inet_timewait_sock *tw)
  1917. {
  1918. return !is_a_nulls(tw->tw_node.next) ?
  1919. hlist_nulls_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
  1920. }
  1921. /*
  1922. * Get next listener socket follow cur. If cur is NULL, get first socket
  1923. * starting from bucket given in st->bucket; when st->bucket is zero the
  1924. * very first socket in the hash table is returned.
  1925. */
  1926. static void *listening_get_next(struct seq_file *seq, void *cur)
  1927. {
  1928. struct inet_connection_sock *icsk;
  1929. struct hlist_nulls_node *node;
  1930. struct sock *sk = cur;
  1931. struct inet_listen_hashbucket *ilb;
  1932. struct tcp_iter_state *st = seq->private;
  1933. struct net *net = seq_file_net(seq);
  1934. if (!sk) {
  1935. ilb = &tcp_hashinfo.listening_hash[st->bucket];
  1936. spin_lock_bh(&ilb->lock);
  1937. sk = sk_nulls_head(&ilb->head);
  1938. st->offset = 0;
  1939. goto get_sk;
  1940. }
  1941. ilb = &tcp_hashinfo.listening_hash[st->bucket];
  1942. ++st->num;
  1943. ++st->offset;
  1944. if (st->state == TCP_SEQ_STATE_OPENREQ) {
  1945. struct request_sock *req = cur;
  1946. icsk = inet_csk(st->syn_wait_sk);
  1947. req = req->dl_next;
  1948. while (1) {
  1949. while (req) {
  1950. if (req->rsk_ops->family == st->family) {
  1951. cur = req;
  1952. goto out;
  1953. }
  1954. req = req->dl_next;
  1955. }
  1956. if (++st->sbucket >= icsk->icsk_accept_queue.listen_opt->nr_table_entries)
  1957. break;
  1958. get_req:
  1959. req = icsk->icsk_accept_queue.listen_opt->syn_table[st->sbucket];
  1960. }
  1961. sk = sk_nulls_next(st->syn_wait_sk);
  1962. st->state = TCP_SEQ_STATE_LISTENING;
  1963. read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1964. } else {
  1965. icsk = inet_csk(sk);
  1966. read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1967. if (reqsk_queue_len(&icsk->icsk_accept_queue))
  1968. goto start_req;
  1969. read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1970. sk = sk_nulls_next(sk);
  1971. }
  1972. get_sk:
  1973. sk_nulls_for_each_from(sk, node) {
  1974. if (!net_eq(sock_net(sk), net))
  1975. continue;
  1976. if (sk->sk_family == st->family) {
  1977. cur = sk;
  1978. goto out;
  1979. }
  1980. icsk = inet_csk(sk);
  1981. read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1982. if (reqsk_queue_len(&icsk->icsk_accept_queue)) {
  1983. start_req:
  1984. st->uid = sock_i_uid(sk);
  1985. st->syn_wait_sk = sk;
  1986. st->state = TCP_SEQ_STATE_OPENREQ;
  1987. st->sbucket = 0;
  1988. goto get_req;
  1989. }
  1990. read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  1991. }
  1992. spin_unlock_bh(&ilb->lock);
  1993. st->offset = 0;
  1994. if (++st->bucket < INET_LHTABLE_SIZE) {
  1995. ilb = &tcp_hashinfo.listening_hash[st->bucket];
  1996. spin_lock_bh(&ilb->lock);
  1997. sk = sk_nulls_head(&ilb->head);
  1998. goto get_sk;
  1999. }
  2000. cur = NULL;
  2001. out:
  2002. return cur;
  2003. }
  2004. static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
  2005. {
  2006. struct tcp_iter_state *st = seq->private;
  2007. void *rc;
  2008. st->bucket = 0;
  2009. st->offset = 0;
  2010. rc = listening_get_next(seq, NULL);
  2011. while (rc && *pos) {
  2012. rc = listening_get_next(seq, rc);
  2013. --*pos;
  2014. }
  2015. return rc;
  2016. }
  2017. static inline bool empty_bucket(struct tcp_iter_state *st)
  2018. {
  2019. return hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].chain) &&
  2020. hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].twchain);
  2021. }
  2022. /*
  2023. * Get first established socket starting from bucket given in st->bucket.
  2024. * If st->bucket is zero, the very first socket in the hash is returned.
  2025. */
  2026. static void *established_get_first(struct seq_file *seq)
  2027. {
  2028. struct tcp_iter_state *st = seq->private;
  2029. struct net *net = seq_file_net(seq);
  2030. void *rc = NULL;
  2031. st->offset = 0;
  2032. for (; st->bucket <= tcp_hashinfo.ehash_mask; ++st->bucket) {
  2033. struct sock *sk;
  2034. struct hlist_nulls_node *node;
  2035. struct inet_timewait_sock *tw;
  2036. spinlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket);
  2037. /* Lockless fast path for the common case of empty buckets */
  2038. if (empty_bucket(st))
  2039. continue;
  2040. spin_lock_bh(lock);
  2041. sk_nulls_for_each(sk, node, &tcp_hashinfo.ehash[st->bucket].chain) {
  2042. if (sk->sk_family != st->family ||
  2043. !net_eq(sock_net(sk), net)) {
  2044. continue;
  2045. }
  2046. rc = sk;
  2047. goto out;
  2048. }
  2049. st->state = TCP_SEQ_STATE_TIME_WAIT;
  2050. inet_twsk_for_each(tw, node,
  2051. &tcp_hashinfo.ehash[st->bucket].twchain) {
  2052. if (tw->tw_family != st->family ||
  2053. !net_eq(twsk_net(tw), net)) {
  2054. continue;
  2055. }
  2056. rc = tw;
  2057. goto out;
  2058. }
  2059. spin_unlock_bh(lock);
  2060. st->state = TCP_SEQ_STATE_ESTABLISHED;
  2061. }
  2062. out:
  2063. return rc;
  2064. }
  2065. static void *established_get_next(struct seq_file *seq, void *cur)
  2066. {
  2067. struct sock *sk = cur;
  2068. struct inet_timewait_sock *tw;
  2069. struct hlist_nulls_node *node;
  2070. struct tcp_iter_state *st = seq->private;
  2071. struct net *net = seq_file_net(seq);
  2072. ++st->num;
  2073. ++st->offset;
  2074. if (st->state == TCP_SEQ_STATE_TIME_WAIT) {
  2075. tw = cur;
  2076. tw = tw_next(tw);
  2077. get_tw:
  2078. while (tw && (tw->tw_family != st->family || !net_eq(twsk_net(tw), net))) {
  2079. tw = tw_next(tw);
  2080. }
  2081. if (tw) {
  2082. cur = tw;
  2083. goto out;
  2084. }
  2085. spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
  2086. st->state = TCP_SEQ_STATE_ESTABLISHED;
  2087. /* Look for next non empty bucket */
  2088. st->offset = 0;
  2089. while (++st->bucket <= tcp_hashinfo.ehash_mask &&
  2090. empty_bucket(st))
  2091. ;
  2092. if (st->bucket > tcp_hashinfo.ehash_mask)
  2093. return NULL;
  2094. spin_lock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
  2095. sk = sk_nulls_head(&tcp_hashinfo.ehash[st->bucket].chain);
  2096. } else
  2097. sk = sk_nulls_next(sk);
  2098. sk_nulls_for_each_from(sk, node) {
  2099. if (sk->sk_family == st->family && net_eq(sock_net(sk), net))
  2100. goto found;
  2101. }
  2102. st->state = TCP_SEQ_STATE_TIME_WAIT;
  2103. tw = tw_head(&tcp_hashinfo.ehash[st->bucket].twchain);
  2104. goto get_tw;
  2105. found:
  2106. cur = sk;
  2107. out:
  2108. return cur;
  2109. }
  2110. static void *established_get_idx(struct seq_file *seq, loff_t pos)
  2111. {
  2112. struct tcp_iter_state *st = seq->private;
  2113. void *rc;
  2114. st->bucket = 0;
  2115. rc = established_get_first(seq);
  2116. while (rc && pos) {
  2117. rc = established_get_next(seq, rc);
  2118. --pos;
  2119. }
  2120. return rc;
  2121. }
  2122. static void *tcp_get_idx(struct seq_file *seq, loff_t pos)
  2123. {
  2124. void *rc;
  2125. struct tcp_iter_state *st = seq->private;
  2126. st->state = TCP_SEQ_STATE_LISTENING;
  2127. rc = listening_get_idx(seq, &pos);
  2128. if (!rc) {
  2129. st->state = TCP_SEQ_STATE_ESTABLISHED;
  2130. rc = established_get_idx(seq, pos);
  2131. }
  2132. return rc;
  2133. }
  2134. static void *tcp_seek_last_pos(struct seq_file *seq)
  2135. {
  2136. struct tcp_iter_state *st = seq->private;
  2137. int offset = st->offset;
  2138. int orig_num = st->num;
  2139. void *rc = NULL;
  2140. switch (st->state) {
  2141. case TCP_SEQ_STATE_OPENREQ:
  2142. case TCP_SEQ_STATE_LISTENING:
  2143. if (st->bucket >= INET_LHTABLE_SIZE)
  2144. break;
  2145. st->state = TCP_SEQ_STATE_LISTENING;
  2146. rc = listening_get_next(seq, NULL);
  2147. while (offset-- && rc)
  2148. rc = listening_get_next(seq, rc);
  2149. if (rc)
  2150. break;
  2151. st->bucket = 0;
  2152. /* Fallthrough */
  2153. case TCP_SEQ_STATE_ESTABLISHED:
  2154. case TCP_SEQ_STATE_TIME_WAIT:
  2155. st->state = TCP_SEQ_STATE_ESTABLISHED;
  2156. if (st->bucket > tcp_hashinfo.ehash_mask)
  2157. break;
  2158. rc = established_get_first(seq);
  2159. while (offset-- && rc)
  2160. rc = established_get_next(seq, rc);
  2161. }
  2162. st->num = orig_num;
  2163. return rc;
  2164. }
  2165. static void *tcp_seq_start(struct seq_file *seq, loff_t *pos)
  2166. {
  2167. struct tcp_iter_state *st = seq->private;
  2168. void *rc;
  2169. if (*pos && *pos == st->last_pos) {
  2170. rc = tcp_seek_last_pos(seq);
  2171. if (rc)
  2172. goto out;
  2173. }
  2174. st->state = TCP_SEQ_STATE_LISTENING;
  2175. st->num = 0;
  2176. st->bucket = 0;
  2177. st->offset = 0;
  2178. rc = *pos ? tcp_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  2179. out:
  2180. st->last_pos = *pos;
  2181. return rc;
  2182. }
  2183. static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  2184. {
  2185. struct tcp_iter_state *st = seq->private;
  2186. void *rc = NULL;
  2187. if (v == SEQ_START_TOKEN) {
  2188. rc = tcp_get_idx(seq, 0);
  2189. goto out;
  2190. }
  2191. switch (st->state) {
  2192. case TCP_SEQ_STATE_OPENREQ:
  2193. case TCP_SEQ_STATE_LISTENING:
  2194. rc = listening_get_next(seq, v);
  2195. if (!rc) {
  2196. st->state = TCP_SEQ_STATE_ESTABLISHED;
  2197. st->bucket = 0;
  2198. st->offset = 0;
  2199. rc = established_get_first(seq);
  2200. }
  2201. break;
  2202. case TCP_SEQ_STATE_ESTABLISHED:
  2203. case TCP_SEQ_STATE_TIME_WAIT:
  2204. rc = established_get_next(seq, v);
  2205. break;
  2206. }
  2207. out:
  2208. ++*pos;
  2209. st->last_pos = *pos;
  2210. return rc;
  2211. }
  2212. static void tcp_seq_stop(struct seq_file *seq, void *v)
  2213. {
  2214. struct tcp_iter_state *st = seq->private;
  2215. switch (st->state) {
  2216. case TCP_SEQ_STATE_OPENREQ:
  2217. if (v) {
  2218. struct inet_connection_sock *icsk = inet_csk(st->syn_wait_sk);
  2219. read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
  2220. }
  2221. case TCP_SEQ_STATE_LISTENING:
  2222. if (v != SEQ_START_TOKEN)
  2223. spin_unlock_bh(&tcp_hashinfo.listening_hash[st->bucket].lock);
  2224. break;
  2225. case TCP_SEQ_STATE_TIME_WAIT:
  2226. case TCP_SEQ_STATE_ESTABLISHED:
  2227. if (v)
  2228. spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
  2229. break;
  2230. }
  2231. }
  2232. int tcp_seq_open(struct inode *inode, struct file *file)
  2233. {
  2234. struct tcp_seq_afinfo *afinfo = PDE(inode)->data;
  2235. struct tcp_iter_state *s;
  2236. int err;
  2237. err = seq_open_net(inode, file, &afinfo->seq_ops,
  2238. sizeof(struct tcp_iter_state));
  2239. if (err < 0)
  2240. return err;
  2241. s = ((struct seq_file *)file->private_data)->private;
  2242. s->family = afinfo->family;
  2243. s->last_pos = 0;
  2244. return 0;
  2245. }
  2246. EXPORT_SYMBOL(tcp_seq_open);
  2247. int tcp_proc_register(struct net *net, struct tcp_seq_afinfo *afinfo)
  2248. {
  2249. int rc = 0;
  2250. struct proc_dir_entry *p;
  2251. afinfo->seq_ops.start = tcp_seq_start;
  2252. afinfo->seq_ops.next = tcp_seq_next;
  2253. afinfo->seq_ops.stop = tcp_seq_stop;
  2254. p = proc_create_data(afinfo->name, S_IRUGO, net->proc_net,
  2255. afinfo->seq_fops, afinfo);
  2256. if (!p)
  2257. rc = -ENOMEM;
  2258. return rc;
  2259. }
  2260. EXPORT_SYMBOL(tcp_proc_register);
  2261. void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo)
  2262. {
  2263. proc_net_remove(net, afinfo->name);
  2264. }
  2265. EXPORT_SYMBOL(tcp_proc_unregister);
  2266. static void get_openreq4(const struct sock *sk, const struct request_sock *req,
  2267. struct seq_file *f, int i, kuid_t uid, int *len)
  2268. {
  2269. const struct inet_request_sock *ireq = inet_rsk(req);
  2270. long delta = req->expires - jiffies;
  2271. seq_printf(f, "%4d: %08X:%04X %08X:%04X"
  2272. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %u %d %pK%n",
  2273. i,
  2274. ireq->loc_addr,
  2275. ntohs(inet_sk(sk)->inet_sport),
  2276. ireq->rmt_addr,
  2277. ntohs(ireq->rmt_port),
  2278. TCP_SYN_RECV,
  2279. 0, 0, /* could print option size, but that is af dependent. */
  2280. 1, /* timers active (only the expire timer) */
  2281. jiffies_delta_to_clock_t(delta),
  2282. req->num_timeout,
  2283. from_kuid_munged(seq_user_ns(f), uid),
  2284. 0, /* non standard timer */
  2285. 0, /* open_requests have no inode */
  2286. atomic_read(&sk->sk_refcnt),
  2287. req,
  2288. len);
  2289. }
  2290. static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len)
  2291. {
  2292. int timer_active;
  2293. unsigned long timer_expires;
  2294. const struct tcp_sock *tp = tcp_sk(sk);
  2295. const struct inet_connection_sock *icsk = inet_csk(sk);
  2296. const struct inet_sock *inet = inet_sk(sk);
  2297. struct fastopen_queue *fastopenq = icsk->icsk_accept_queue.fastopenq;
  2298. __be32 dest = inet->inet_daddr;
  2299. __be32 src = inet->inet_rcv_saddr;
  2300. __u16 destp = ntohs(inet->inet_dport);
  2301. __u16 srcp = ntohs(inet->inet_sport);
  2302. int rx_queue;
  2303. if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
  2304. timer_active = 1;
  2305. timer_expires = icsk->icsk_timeout;
  2306. } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
  2307. timer_active = 4;
  2308. timer_expires = icsk->icsk_timeout;
  2309. } else if (timer_pending(&sk->sk_timer)) {
  2310. timer_active = 2;
  2311. timer_expires = sk->sk_timer.expires;
  2312. } else {
  2313. timer_active = 0;
  2314. timer_expires = jiffies;
  2315. }
  2316. if (sk->sk_state == TCP_LISTEN)
  2317. rx_queue = sk->sk_ack_backlog;
  2318. else
  2319. /*
  2320. * because we dont lock socket, we might find a transient negative value
  2321. */
  2322. rx_queue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0);
  2323. seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
  2324. "%08X %5d %8d %lu %d %pK %lu %lu %u %u %d%n",
  2325. i, src, srcp, dest, destp, sk->sk_state,
  2326. tp->write_seq - tp->snd_una,
  2327. rx_queue,
  2328. timer_active,
  2329. jiffies_delta_to_clock_t(timer_expires - jiffies),
  2330. icsk->icsk_retransmits,
  2331. from_kuid_munged(seq_user_ns(f), sock_i_uid(sk)),
  2332. icsk->icsk_probes_out,
  2333. sock_i_ino(sk),
  2334. atomic_read(&sk->sk_refcnt), sk,
  2335. jiffies_to_clock_t(icsk->icsk_rto),
  2336. jiffies_to_clock_t(icsk->icsk_ack.ato),
  2337. (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
  2338. tp->snd_cwnd,
  2339. sk->sk_state == TCP_LISTEN ?
  2340. (fastopenq ? fastopenq->max_qlen : 0) :
  2341. (tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh),
  2342. len);
  2343. }
  2344. static void get_timewait4_sock(const struct inet_timewait_sock *tw,
  2345. struct seq_file *f, int i, int *len)
  2346. {
  2347. __be32 dest, src;
  2348. __u16 destp, srcp;
  2349. long delta = tw->tw_ttd - jiffies;
  2350. dest = tw->tw_daddr;
  2351. src = tw->tw_rcv_saddr;
  2352. destp = ntohs(tw->tw_dport);
  2353. srcp = ntohs(tw->tw_sport);
  2354. seq_printf(f, "%4d: %08X:%04X %08X:%04X"
  2355. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK%n",
  2356. i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
  2357. 3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0,
  2358. atomic_read(&tw->tw_refcnt), tw, len);
  2359. }
  2360. #define TMPSZ 150
  2361. static int tcp4_seq_show(struct seq_file *seq, void *v)
  2362. {
  2363. struct tcp_iter_state *st;
  2364. int len;
  2365. if (v == SEQ_START_TOKEN) {
  2366. seq_printf(seq, "%-*s\n", TMPSZ - 1,
  2367. " sl local_address rem_address st tx_queue "
  2368. "rx_queue tr tm->when retrnsmt uid timeout "
  2369. "inode");
  2370. goto out;
  2371. }
  2372. st = seq->private;
  2373. switch (st->state) {
  2374. case TCP_SEQ_STATE_LISTENING:
  2375. case TCP_SEQ_STATE_ESTABLISHED:
  2376. get_tcp4_sock(v, seq, st->num, &len);
  2377. break;
  2378. case TCP_SEQ_STATE_OPENREQ:
  2379. get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid, &len);
  2380. break;
  2381. case TCP_SEQ_STATE_TIME_WAIT:
  2382. get_timewait4_sock(v, seq, st->num, &len);
  2383. break;
  2384. }
  2385. seq_printf(seq, "%*s\n", TMPSZ - 1 - len, "");
  2386. out:
  2387. return 0;
  2388. }
  2389. static const struct file_operations tcp_afinfo_seq_fops = {
  2390. .owner = THIS_MODULE,
  2391. .open = tcp_seq_open,
  2392. .read = seq_read,
  2393. .llseek = seq_lseek,
  2394. .release = seq_release_net
  2395. };
  2396. static struct tcp_seq_afinfo tcp4_seq_afinfo = {
  2397. .name = "tcp",
  2398. .family = AF_INET,
  2399. .seq_fops = &tcp_afinfo_seq_fops,
  2400. .seq_ops = {
  2401. .show = tcp4_seq_show,
  2402. },
  2403. };
  2404. static int __net_init tcp4_proc_init_net(struct net *net)
  2405. {
  2406. return tcp_proc_register(net, &tcp4_seq_afinfo);
  2407. }
  2408. static void __net_exit tcp4_proc_exit_net(struct net *net)
  2409. {
  2410. tcp_proc_unregister(net, &tcp4_seq_afinfo);
  2411. }
  2412. static struct pernet_operations tcp4_net_ops = {
  2413. .init = tcp4_proc_init_net,
  2414. .exit = tcp4_proc_exit_net,
  2415. };
  2416. int __init tcp4_proc_init(void)
  2417. {
  2418. return register_pernet_subsys(&tcp4_net_ops);
  2419. }
  2420. void tcp4_proc_exit(void)
  2421. {
  2422. unregister_pernet_subsys(&tcp4_net_ops);
  2423. }
  2424. #endif /* CONFIG_PROC_FS */
  2425. struct sk_buff **tcp4_gro_receive(struct sk_buff **head, struct sk_buff *skb)
  2426. {
  2427. const struct iphdr *iph = skb_gro_network_header(skb);
  2428. __wsum wsum;
  2429. __sum16 sum;
  2430. switch (skb->ip_summed) {
  2431. case CHECKSUM_COMPLETE:
  2432. if (!tcp_v4_check(skb_gro_len(skb), iph->saddr, iph->daddr,
  2433. skb->csum)) {
  2434. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2435. break;
  2436. }
  2437. flush:
  2438. NAPI_GRO_CB(skb)->flush = 1;
  2439. return NULL;
  2440. case CHECKSUM_NONE:
  2441. wsum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
  2442. skb_gro_len(skb), IPPROTO_TCP, 0);
  2443. sum = csum_fold(skb_checksum(skb,
  2444. skb_gro_offset(skb),
  2445. skb_gro_len(skb),
  2446. wsum));
  2447. if (sum)
  2448. goto flush;
  2449. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2450. break;
  2451. }
  2452. return tcp_gro_receive(head, skb);
  2453. }
  2454. int tcp4_gro_complete(struct sk_buff *skb)
  2455. {
  2456. const struct iphdr *iph = ip_hdr(skb);
  2457. struct tcphdr *th = tcp_hdr(skb);
  2458. th->check = ~tcp_v4_check(skb->len - skb_transport_offset(skb),
  2459. iph->saddr, iph->daddr, 0);
  2460. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  2461. return tcp_gro_complete(skb);
  2462. }
  2463. struct proto tcp_prot = {
  2464. .name = "TCP",
  2465. .owner = THIS_MODULE,
  2466. .close = tcp_close,
  2467. .connect = tcp_v4_connect,
  2468. .disconnect = tcp_disconnect,
  2469. .accept = inet_csk_accept,
  2470. .ioctl = tcp_ioctl,
  2471. .init = tcp_v4_init_sock,
  2472. .destroy = tcp_v4_destroy_sock,
  2473. .shutdown = tcp_shutdown,
  2474. .setsockopt = tcp_setsockopt,
  2475. .getsockopt = tcp_getsockopt,
  2476. .recvmsg = tcp_recvmsg,
  2477. .sendmsg = tcp_sendmsg,
  2478. .sendpage = tcp_sendpage,
  2479. .backlog_rcv = tcp_v4_do_rcv,
  2480. .release_cb = tcp_release_cb,
  2481. .mtu_reduced = tcp_v4_mtu_reduced,
  2482. .hash = inet_hash,
  2483. .unhash = inet_unhash,
  2484. .get_port = inet_csk_get_port,
  2485. .enter_memory_pressure = tcp_enter_memory_pressure,
  2486. .sockets_allocated = &tcp_sockets_allocated,
  2487. .orphan_count = &tcp_orphan_count,
  2488. .memory_allocated = &tcp_memory_allocated,
  2489. .memory_pressure = &tcp_memory_pressure,
  2490. .sysctl_wmem = sysctl_tcp_wmem,
  2491. .sysctl_rmem = sysctl_tcp_rmem,
  2492. .max_header = MAX_TCP_HEADER,
  2493. .obj_size = sizeof(struct tcp_sock),
  2494. .slab_flags = SLAB_DESTROY_BY_RCU,
  2495. .twsk_prot = &tcp_timewait_sock_ops,
  2496. .rsk_prot = &tcp_request_sock_ops,
  2497. .h.hashinfo = &tcp_hashinfo,
  2498. .no_autobind = true,
  2499. #ifdef CONFIG_COMPAT
  2500. .compat_setsockopt = compat_tcp_setsockopt,
  2501. .compat_getsockopt = compat_tcp_getsockopt,
  2502. #endif
  2503. #ifdef CONFIG_MEMCG_KMEM
  2504. .init_cgroup = tcp_init_cgroup,
  2505. .destroy_cgroup = tcp_destroy_cgroup,
  2506. .proto_cgroup = tcp_proto_cgroup,
  2507. #endif
  2508. };
  2509. EXPORT_SYMBOL(tcp_prot);
  2510. static int __net_init tcp_sk_init(struct net *net)
  2511. {
  2512. return 0;
  2513. }
  2514. static void __net_exit tcp_sk_exit(struct net *net)
  2515. {
  2516. }
  2517. static void __net_exit tcp_sk_exit_batch(struct list_head *net_exit_list)
  2518. {
  2519. inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET);
  2520. }
  2521. static struct pernet_operations __net_initdata tcp_sk_ops = {
  2522. .init = tcp_sk_init,
  2523. .exit = tcp_sk_exit,
  2524. .exit_batch = tcp_sk_exit_batch,
  2525. };
  2526. void __init tcp_v4_init(void)
  2527. {
  2528. inet_hashinfo_init(&tcp_hashinfo);
  2529. if (register_pernet_subsys(&tcp_sk_ops))
  2530. panic("Failed to create the TCP control socket.\n");
  2531. }