tcp_ipv4.c 62 KB

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