tcp_ipv4.c 69 KB

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