tcp_ipv4.c 68 KB

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