tcp_ipv4.c 66 KB

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