tcp_ipv4.c 60 KB

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