tcp_ipv4.c 61 KB

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