tcp_ipv4.c 67 KB

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