tcp_ipv6.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224
  1. /*
  2. * TCP over IPv6
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Based on:
  9. * linux/net/ipv4/tcp.c
  10. * linux/net/ipv4/tcp_input.c
  11. * linux/net/ipv4/tcp_output.c
  12. *
  13. * Fixes:
  14. * Hideaki YOSHIFUJI : sin6_scope_id support
  15. * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
  16. * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
  17. * a single port at the same time.
  18. * YOSHIFUJI Hideaki @USAGI: convert /proc/net/tcp6 to seq_file.
  19. *
  20. * This program is free software; you can redistribute it and/or
  21. * modify it under the terms of the GNU General Public License
  22. * as published by the Free Software Foundation; either version
  23. * 2 of the License, or (at your option) any later version.
  24. */
  25. #include <linux/bottom_half.h>
  26. #include <linux/module.h>
  27. #include <linux/errno.h>
  28. #include <linux/types.h>
  29. #include <linux/socket.h>
  30. #include <linux/sockios.h>
  31. #include <linux/net.h>
  32. #include <linux/jiffies.h>
  33. #include <linux/in.h>
  34. #include <linux/in6.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/init.h>
  37. #include <linux/jhash.h>
  38. #include <linux/ipsec.h>
  39. #include <linux/times.h>
  40. #include <linux/slab.h>
  41. #include <linux/ipv6.h>
  42. #include <linux/icmpv6.h>
  43. #include <linux/random.h>
  44. #include <net/tcp.h>
  45. #include <net/ndisc.h>
  46. #include <net/inet6_hashtables.h>
  47. #include <net/inet6_connection_sock.h>
  48. #include <net/ipv6.h>
  49. #include <net/transp_v6.h>
  50. #include <net/addrconf.h>
  51. #include <net/ip6_route.h>
  52. #include <net/ip6_checksum.h>
  53. #include <net/inet_ecn.h>
  54. #include <net/protocol.h>
  55. #include <net/xfrm.h>
  56. #include <net/snmp.h>
  57. #include <net/dsfield.h>
  58. #include <net/timewait_sock.h>
  59. #include <net/netdma.h>
  60. #include <net/inet_common.h>
  61. #include <net/secure_seq.h>
  62. #include <net/tcp_memcontrol.h>
  63. #include <asm/uaccess.h>
  64. #include <linux/proc_fs.h>
  65. #include <linux/seq_file.h>
  66. #include <linux/crypto.h>
  67. #include <linux/scatterlist.h>
  68. static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb);
  69. static void tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
  70. struct request_sock *req);
  71. static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb);
  72. static void __tcp_v6_send_check(struct sk_buff *skb,
  73. const struct in6_addr *saddr,
  74. const struct in6_addr *daddr);
  75. static const struct inet_connection_sock_af_ops ipv6_mapped;
  76. static const struct inet_connection_sock_af_ops ipv6_specific;
  77. #ifdef CONFIG_TCP_MD5SIG
  78. static const struct tcp_sock_af_ops tcp_sock_ipv6_specific;
  79. static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific;
  80. #else
  81. static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk,
  82. const struct in6_addr *addr)
  83. {
  84. return NULL;
  85. }
  86. #endif
  87. static void tcp_v6_hash(struct sock *sk)
  88. {
  89. if (sk->sk_state != TCP_CLOSE) {
  90. if (inet_csk(sk)->icsk_af_ops == &ipv6_mapped) {
  91. tcp_prot.hash(sk);
  92. return;
  93. }
  94. local_bh_disable();
  95. __inet6_hash(sk, NULL);
  96. local_bh_enable();
  97. }
  98. }
  99. static __inline__ __sum16 tcp_v6_check(int len,
  100. const struct in6_addr *saddr,
  101. const struct in6_addr *daddr,
  102. __wsum base)
  103. {
  104. return csum_ipv6_magic(saddr, daddr, len, IPPROTO_TCP, base);
  105. }
  106. static __u32 tcp_v6_init_sequence(const struct sk_buff *skb)
  107. {
  108. return secure_tcpv6_sequence_number(ipv6_hdr(skb)->daddr.s6_addr32,
  109. ipv6_hdr(skb)->saddr.s6_addr32,
  110. tcp_hdr(skb)->dest,
  111. tcp_hdr(skb)->source);
  112. }
  113. static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
  114. int addr_len)
  115. {
  116. struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
  117. struct inet_sock *inet = inet_sk(sk);
  118. struct inet_connection_sock *icsk = inet_csk(sk);
  119. struct ipv6_pinfo *np = inet6_sk(sk);
  120. struct tcp_sock *tp = tcp_sk(sk);
  121. struct in6_addr *saddr = NULL, *final_p, final;
  122. struct ipv6_txoptions *opt;
  123. struct rt6_info *rt;
  124. struct flowi6 fl6;
  125. struct dst_entry *dst;
  126. int addr_type;
  127. int err;
  128. if (addr_len < SIN6_LEN_RFC2133)
  129. return -EINVAL;
  130. if (usin->sin6_family != AF_INET6)
  131. return -EAFNOSUPPORT;
  132. memset(&fl6, 0, sizeof(fl6));
  133. if (np->sndflow) {
  134. fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
  135. IP6_ECN_flow_init(fl6.flowlabel);
  136. if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
  137. struct ip6_flowlabel *flowlabel;
  138. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  139. if (flowlabel == NULL)
  140. return -EINVAL;
  141. usin->sin6_addr = flowlabel->dst;
  142. fl6_sock_release(flowlabel);
  143. }
  144. }
  145. /*
  146. * connect() to INADDR_ANY means loopback (BSD'ism).
  147. */
  148. if(ipv6_addr_any(&usin->sin6_addr))
  149. usin->sin6_addr.s6_addr[15] = 0x1;
  150. addr_type = ipv6_addr_type(&usin->sin6_addr);
  151. if(addr_type & IPV6_ADDR_MULTICAST)
  152. return -ENETUNREACH;
  153. if (addr_type&IPV6_ADDR_LINKLOCAL) {
  154. if (addr_len >= sizeof(struct sockaddr_in6) &&
  155. usin->sin6_scope_id) {
  156. /* If interface is set while binding, indices
  157. * must coincide.
  158. */
  159. if (sk->sk_bound_dev_if &&
  160. sk->sk_bound_dev_if != usin->sin6_scope_id)
  161. return -EINVAL;
  162. sk->sk_bound_dev_if = usin->sin6_scope_id;
  163. }
  164. /* Connect to link-local address requires an interface */
  165. if (!sk->sk_bound_dev_if)
  166. return -EINVAL;
  167. }
  168. if (tp->rx_opt.ts_recent_stamp &&
  169. !ipv6_addr_equal(&np->daddr, &usin->sin6_addr)) {
  170. tp->rx_opt.ts_recent = 0;
  171. tp->rx_opt.ts_recent_stamp = 0;
  172. tp->write_seq = 0;
  173. }
  174. np->daddr = usin->sin6_addr;
  175. np->flow_label = fl6.flowlabel;
  176. /*
  177. * TCP over IPv4
  178. */
  179. if (addr_type == IPV6_ADDR_MAPPED) {
  180. u32 exthdrlen = icsk->icsk_ext_hdr_len;
  181. struct sockaddr_in sin;
  182. SOCK_DEBUG(sk, "connect: ipv4 mapped\n");
  183. if (__ipv6_only_sock(sk))
  184. return -ENETUNREACH;
  185. sin.sin_family = AF_INET;
  186. sin.sin_port = usin->sin6_port;
  187. sin.sin_addr.s_addr = usin->sin6_addr.s6_addr32[3];
  188. icsk->icsk_af_ops = &ipv6_mapped;
  189. sk->sk_backlog_rcv = tcp_v4_do_rcv;
  190. #ifdef CONFIG_TCP_MD5SIG
  191. tp->af_specific = &tcp_sock_ipv6_mapped_specific;
  192. #endif
  193. err = tcp_v4_connect(sk, (struct sockaddr *)&sin, sizeof(sin));
  194. if (err) {
  195. icsk->icsk_ext_hdr_len = exthdrlen;
  196. icsk->icsk_af_ops = &ipv6_specific;
  197. sk->sk_backlog_rcv = tcp_v6_do_rcv;
  198. #ifdef CONFIG_TCP_MD5SIG
  199. tp->af_specific = &tcp_sock_ipv6_specific;
  200. #endif
  201. goto failure;
  202. } else {
  203. ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
  204. ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
  205. &np->rcv_saddr);
  206. }
  207. return err;
  208. }
  209. if (!ipv6_addr_any(&np->rcv_saddr))
  210. saddr = &np->rcv_saddr;
  211. fl6.flowi6_proto = IPPROTO_TCP;
  212. fl6.daddr = np->daddr;
  213. fl6.saddr = saddr ? *saddr : np->saddr;
  214. fl6.flowi6_oif = sk->sk_bound_dev_if;
  215. fl6.flowi6_mark = sk->sk_mark;
  216. fl6.fl6_dport = usin->sin6_port;
  217. fl6.fl6_sport = inet->inet_sport;
  218. opt = rcu_dereference_protected(np->opt, sock_owned_by_user(sk));
  219. final_p = fl6_update_dst(&fl6, opt, &final);
  220. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  221. dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true);
  222. if (IS_ERR(dst)) {
  223. err = PTR_ERR(dst);
  224. goto failure;
  225. }
  226. if (saddr == NULL) {
  227. saddr = &fl6.saddr;
  228. np->rcv_saddr = *saddr;
  229. }
  230. /* set the source address */
  231. np->saddr = *saddr;
  232. inet->inet_rcv_saddr = LOOPBACK4_IPV6;
  233. sk->sk_gso_type = SKB_GSO_TCPV6;
  234. __ip6_dst_store(sk, dst, NULL, NULL);
  235. rt = (struct rt6_info *) dst;
  236. if (tcp_death_row.sysctl_tw_recycle &&
  237. !tp->rx_opt.ts_recent_stamp &&
  238. ipv6_addr_equal(&rt->rt6i_dst.addr, &np->daddr)) {
  239. struct inet_peer *peer = rt6_get_peer(rt);
  240. /*
  241. * VJ's idea. We save last timestamp seen from
  242. * the destination in peer table, when entering state
  243. * TIME-WAIT * and initialize rx_opt.ts_recent from it,
  244. * when trying new connection.
  245. */
  246. if (peer) {
  247. inet_peer_refcheck(peer);
  248. if ((u32)get_seconds() - peer->tcp_ts_stamp <= TCP_PAWS_MSL) {
  249. tp->rx_opt.ts_recent_stamp = peer->tcp_ts_stamp;
  250. tp->rx_opt.ts_recent = peer->tcp_ts;
  251. }
  252. }
  253. }
  254. icsk->icsk_ext_hdr_len = 0;
  255. if (opt)
  256. icsk->icsk_ext_hdr_len = opt->opt_flen +
  257. opt->opt_nflen;
  258. tp->rx_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
  259. inet->inet_dport = usin->sin6_port;
  260. tcp_set_state(sk, TCP_SYN_SENT);
  261. err = inet6_hash_connect(&tcp_death_row, sk);
  262. if (err)
  263. goto late_failure;
  264. if (!tp->write_seq)
  265. tp->write_seq = secure_tcpv6_sequence_number(np->saddr.s6_addr32,
  266. np->daddr.s6_addr32,
  267. inet->inet_sport,
  268. inet->inet_dport);
  269. err = tcp_connect(sk);
  270. if (err)
  271. goto late_failure;
  272. return 0;
  273. late_failure:
  274. tcp_set_state(sk, TCP_CLOSE);
  275. __sk_dst_reset(sk);
  276. failure:
  277. inet->inet_dport = 0;
  278. sk->sk_route_caps = 0;
  279. return err;
  280. }
  281. static void tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  282. u8 type, u8 code, int offset, __be32 info)
  283. {
  284. const struct ipv6hdr *hdr = (const struct ipv6hdr*)skb->data;
  285. const struct tcphdr *th = (struct tcphdr *)(skb->data+offset);
  286. struct ipv6_pinfo *np;
  287. struct sock *sk;
  288. int err;
  289. struct tcp_sock *tp;
  290. __u32 seq;
  291. struct net *net = dev_net(skb->dev);
  292. sk = inet6_lookup(net, &tcp_hashinfo, &hdr->daddr,
  293. th->dest, &hdr->saddr, th->source, skb->dev->ifindex);
  294. if (sk == NULL) {
  295. ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
  296. ICMP6_MIB_INERRORS);
  297. return;
  298. }
  299. if (sk->sk_state == TCP_TIME_WAIT) {
  300. inet_twsk_put(inet_twsk(sk));
  301. return;
  302. }
  303. bh_lock_sock(sk);
  304. if (sock_owned_by_user(sk))
  305. NET_INC_STATS_BH(net, LINUX_MIB_LOCKDROPPEDICMPS);
  306. if (sk->sk_state == TCP_CLOSE)
  307. goto out;
  308. if (ipv6_hdr(skb)->hop_limit < inet6_sk(sk)->min_hopcount) {
  309. NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
  310. goto out;
  311. }
  312. tp = tcp_sk(sk);
  313. seq = ntohl(th->seq);
  314. if (sk->sk_state != TCP_LISTEN &&
  315. !between(seq, tp->snd_una, tp->snd_nxt)) {
  316. NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
  317. goto out;
  318. }
  319. np = inet6_sk(sk);
  320. if (type == ICMPV6_PKT_TOOBIG) {
  321. struct dst_entry *dst;
  322. if (sock_owned_by_user(sk))
  323. goto out;
  324. if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
  325. goto out;
  326. /* icmp should have updated the destination cache entry */
  327. dst = __sk_dst_check(sk, np->dst_cookie);
  328. if (dst == NULL) {
  329. struct inet_sock *inet = inet_sk(sk);
  330. struct flowi6 fl6;
  331. /* BUGGG_FUTURE: Again, it is not clear how
  332. to handle rthdr case. Ignore this complexity
  333. for now.
  334. */
  335. memset(&fl6, 0, sizeof(fl6));
  336. fl6.flowi6_proto = IPPROTO_TCP;
  337. fl6.daddr = np->daddr;
  338. fl6.saddr = np->saddr;
  339. fl6.flowi6_oif = sk->sk_bound_dev_if;
  340. fl6.flowi6_mark = sk->sk_mark;
  341. fl6.fl6_dport = inet->inet_dport;
  342. fl6.fl6_sport = inet->inet_sport;
  343. security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
  344. dst = ip6_dst_lookup_flow(sk, &fl6, NULL, false);
  345. if (IS_ERR(dst)) {
  346. sk->sk_err_soft = -PTR_ERR(dst);
  347. goto out;
  348. }
  349. } else
  350. dst_hold(dst);
  351. if (inet_csk(sk)->icsk_pmtu_cookie > dst_mtu(dst)) {
  352. tcp_sync_mss(sk, dst_mtu(dst));
  353. tcp_simple_retransmit(sk);
  354. } /* else let the usual retransmit timer handle it */
  355. dst_release(dst);
  356. goto out;
  357. }
  358. icmpv6_err_convert(type, code, &err);
  359. /* Might be for an request_sock */
  360. switch (sk->sk_state) {
  361. struct request_sock *req, **prev;
  362. case TCP_LISTEN:
  363. if (sock_owned_by_user(sk))
  364. goto out;
  365. req = inet6_csk_search_req(sk, &prev, th->dest, &hdr->daddr,
  366. &hdr->saddr, inet6_iif(skb));
  367. if (!req)
  368. goto out;
  369. /* ICMPs are not backlogged, hence we cannot get
  370. * an established socket here.
  371. */
  372. WARN_ON(req->sk != NULL);
  373. if (seq != tcp_rsk(req)->snt_isn) {
  374. NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
  375. goto out;
  376. }
  377. inet_csk_reqsk_queue_drop(sk, req, prev);
  378. goto out;
  379. case TCP_SYN_SENT:
  380. case TCP_SYN_RECV: /* Cannot happen.
  381. It can, it SYNs are crossed. --ANK */
  382. if (!sock_owned_by_user(sk)) {
  383. sk->sk_err = err;
  384. sk->sk_error_report(sk); /* Wake people up to see the error (see connect in sock.c) */
  385. tcp_done(sk);
  386. } else
  387. sk->sk_err_soft = err;
  388. goto out;
  389. }
  390. if (!sock_owned_by_user(sk) && np->recverr) {
  391. sk->sk_err = err;
  392. sk->sk_error_report(sk);
  393. } else
  394. sk->sk_err_soft = err;
  395. out:
  396. bh_unlock_sock(sk);
  397. sock_put(sk);
  398. }
  399. static int tcp_v6_send_synack(struct sock *sk, struct request_sock *req,
  400. struct request_values *rvp)
  401. {
  402. struct inet6_request_sock *treq = inet6_rsk(req);
  403. struct ipv6_pinfo *np = inet6_sk(sk);
  404. struct sk_buff * skb;
  405. struct ipv6_txoptions *opt = NULL;
  406. struct in6_addr * final_p, final;
  407. struct flowi6 fl6;
  408. struct dst_entry *dst;
  409. int err;
  410. memset(&fl6, 0, sizeof(fl6));
  411. fl6.flowi6_proto = IPPROTO_TCP;
  412. fl6.daddr = treq->rmt_addr;
  413. fl6.saddr = treq->loc_addr;
  414. fl6.flowlabel = 0;
  415. fl6.flowi6_oif = treq->iif;
  416. fl6.flowi6_mark = inet_rsk(req)->ir_mark;
  417. fl6.fl6_dport = inet_rsk(req)->rmt_port;
  418. fl6.fl6_sport = inet_rsk(req)->loc_port;
  419. security_req_classify_flow(req, flowi6_to_flowi(&fl6));
  420. opt = np->opt;
  421. final_p = fl6_update_dst(&fl6, opt, &final);
  422. dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false);
  423. if (IS_ERR(dst)) {
  424. err = PTR_ERR(dst);
  425. dst = NULL;
  426. goto done;
  427. }
  428. skb = tcp_make_synack(sk, dst, req, rvp);
  429. err = -ENOMEM;
  430. if (skb) {
  431. __tcp_v6_send_check(skb, &treq->loc_addr, &treq->rmt_addr);
  432. fl6.daddr = treq->rmt_addr;
  433. err = ip6_xmit(sk, skb, &fl6, rcu_dereference(np->opt),
  434. np->tclass);
  435. err = net_xmit_eval(err);
  436. }
  437. done:
  438. if (opt && opt != np->opt)
  439. sock_kfree_s(sk, opt, opt->tot_len);
  440. dst_release(dst);
  441. return err;
  442. }
  443. static int tcp_v6_rtx_synack(struct sock *sk, struct request_sock *req,
  444. struct request_values *rvp)
  445. {
  446. TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_RETRANSSEGS);
  447. return tcp_v6_send_synack(sk, req, rvp);
  448. }
  449. static void tcp_v6_reqsk_destructor(struct request_sock *req)
  450. {
  451. kfree_skb(inet6_rsk(req)->pktopts);
  452. }
  453. #ifdef CONFIG_TCP_MD5SIG
  454. static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk,
  455. const struct in6_addr *addr)
  456. {
  457. return tcp_md5_do_lookup(sk, (union tcp_md5_addr *)addr, AF_INET6);
  458. }
  459. static struct tcp_md5sig_key *tcp_v6_md5_lookup(struct sock *sk,
  460. struct sock *addr_sk)
  461. {
  462. return tcp_v6_md5_do_lookup(sk, &inet6_sk(addr_sk)->daddr);
  463. }
  464. static struct tcp_md5sig_key *tcp_v6_reqsk_md5_lookup(struct sock *sk,
  465. struct request_sock *req)
  466. {
  467. return tcp_v6_md5_do_lookup(sk, &inet6_rsk(req)->rmt_addr);
  468. }
  469. static int tcp_v6_parse_md5_keys (struct sock *sk, char __user *optval,
  470. int optlen)
  471. {
  472. struct tcp_md5sig cmd;
  473. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd.tcpm_addr;
  474. if (optlen < sizeof(cmd))
  475. return -EINVAL;
  476. if (copy_from_user(&cmd, optval, sizeof(cmd)))
  477. return -EFAULT;
  478. if (sin6->sin6_family != AF_INET6)
  479. return -EINVAL;
  480. if (!cmd.tcpm_keylen) {
  481. if (ipv6_addr_v4mapped(&sin6->sin6_addr))
  482. return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr32[3],
  483. AF_INET);
  484. return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin6->sin6_addr,
  485. AF_INET6);
  486. }
  487. if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
  488. return -EINVAL;
  489. if (ipv6_addr_v4mapped(&sin6->sin6_addr))
  490. return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr32[3],
  491. AF_INET, cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
  492. return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin6->sin6_addr,
  493. AF_INET6, cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
  494. }
  495. static int tcp_v6_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp,
  496. const struct in6_addr *daddr,
  497. const struct in6_addr *saddr, int nbytes)
  498. {
  499. struct tcp6_pseudohdr *bp;
  500. struct scatterlist sg;
  501. bp = &hp->md5_blk.ip6;
  502. /* 1. TCP pseudo-header (RFC2460) */
  503. bp->saddr = *saddr;
  504. bp->daddr = *daddr;
  505. bp->protocol = cpu_to_be32(IPPROTO_TCP);
  506. bp->len = cpu_to_be32(nbytes);
  507. sg_init_one(&sg, bp, sizeof(*bp));
  508. return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp));
  509. }
  510. static int tcp_v6_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key,
  511. const struct in6_addr *daddr, struct in6_addr *saddr,
  512. const struct tcphdr *th)
  513. {
  514. struct tcp_md5sig_pool *hp;
  515. struct hash_desc *desc;
  516. hp = tcp_get_md5sig_pool();
  517. if (!hp)
  518. goto clear_hash_noput;
  519. desc = &hp->md5_desc;
  520. if (crypto_hash_init(desc))
  521. goto clear_hash;
  522. if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2))
  523. goto clear_hash;
  524. if (tcp_md5_hash_header(hp, th))
  525. goto clear_hash;
  526. if (tcp_md5_hash_key(hp, key))
  527. goto clear_hash;
  528. if (crypto_hash_final(desc, md5_hash))
  529. goto clear_hash;
  530. tcp_put_md5sig_pool();
  531. return 0;
  532. clear_hash:
  533. tcp_put_md5sig_pool();
  534. clear_hash_noput:
  535. memset(md5_hash, 0, 16);
  536. return 1;
  537. }
  538. static int tcp_v6_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
  539. const struct sock *sk,
  540. const struct request_sock *req,
  541. const struct sk_buff *skb)
  542. {
  543. const struct in6_addr *saddr, *daddr;
  544. struct tcp_md5sig_pool *hp;
  545. struct hash_desc *desc;
  546. const struct tcphdr *th = tcp_hdr(skb);
  547. if (sk) {
  548. saddr = &inet6_sk(sk)->saddr;
  549. daddr = &inet6_sk(sk)->daddr;
  550. } else if (req) {
  551. saddr = &inet6_rsk(req)->loc_addr;
  552. daddr = &inet6_rsk(req)->rmt_addr;
  553. } else {
  554. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  555. saddr = &ip6h->saddr;
  556. daddr = &ip6h->daddr;
  557. }
  558. hp = tcp_get_md5sig_pool();
  559. if (!hp)
  560. goto clear_hash_noput;
  561. desc = &hp->md5_desc;
  562. if (crypto_hash_init(desc))
  563. goto clear_hash;
  564. if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, skb->len))
  565. goto clear_hash;
  566. if (tcp_md5_hash_header(hp, th))
  567. goto clear_hash;
  568. if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2))
  569. goto clear_hash;
  570. if (tcp_md5_hash_key(hp, key))
  571. goto clear_hash;
  572. if (crypto_hash_final(desc, md5_hash))
  573. goto clear_hash;
  574. tcp_put_md5sig_pool();
  575. return 0;
  576. clear_hash:
  577. tcp_put_md5sig_pool();
  578. clear_hash_noput:
  579. memset(md5_hash, 0, 16);
  580. return 1;
  581. }
  582. static int tcp_v6_inbound_md5_hash(struct sock *sk, const struct sk_buff *skb)
  583. {
  584. const __u8 *hash_location = NULL;
  585. struct tcp_md5sig_key *hash_expected;
  586. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  587. const struct tcphdr *th = tcp_hdr(skb);
  588. int genhash;
  589. u8 newhash[16];
  590. hash_expected = tcp_v6_md5_do_lookup(sk, &ip6h->saddr);
  591. hash_location = tcp_parse_md5sig_option(th);
  592. /* We've parsed the options - do we have a hash? */
  593. if (!hash_expected && !hash_location)
  594. return 0;
  595. if (hash_expected && !hash_location) {
  596. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND);
  597. return 1;
  598. }
  599. if (!hash_expected && hash_location) {
  600. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED);
  601. return 1;
  602. }
  603. /* check the signature */
  604. genhash = tcp_v6_md5_hash_skb(newhash,
  605. hash_expected,
  606. NULL, NULL, skb);
  607. if (genhash || memcmp(hash_location, newhash, 16) != 0) {
  608. if (net_ratelimit()) {
  609. printk(KERN_INFO "MD5 Hash %s for [%pI6c]:%u->[%pI6c]:%u\n",
  610. genhash ? "failed" : "mismatch",
  611. &ip6h->saddr, ntohs(th->source),
  612. &ip6h->daddr, ntohs(th->dest));
  613. }
  614. return 1;
  615. }
  616. return 0;
  617. }
  618. #endif
  619. struct request_sock_ops tcp6_request_sock_ops __read_mostly = {
  620. .family = AF_INET6,
  621. .obj_size = sizeof(struct tcp6_request_sock),
  622. .rtx_syn_ack = tcp_v6_rtx_synack,
  623. .send_ack = tcp_v6_reqsk_send_ack,
  624. .destructor = tcp_v6_reqsk_destructor,
  625. .send_reset = tcp_v6_send_reset,
  626. .syn_ack_timeout = tcp_syn_ack_timeout,
  627. };
  628. #ifdef CONFIG_TCP_MD5SIG
  629. static const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops = {
  630. .md5_lookup = tcp_v6_reqsk_md5_lookup,
  631. .calc_md5_hash = tcp_v6_md5_hash_skb,
  632. };
  633. #endif
  634. static void __tcp_v6_send_check(struct sk_buff *skb,
  635. const struct in6_addr *saddr, const struct in6_addr *daddr)
  636. {
  637. struct tcphdr *th = tcp_hdr(skb);
  638. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  639. th->check = ~tcp_v6_check(skb->len, saddr, daddr, 0);
  640. skb->csum_start = skb_transport_header(skb) - skb->head;
  641. skb->csum_offset = offsetof(struct tcphdr, check);
  642. } else {
  643. th->check = tcp_v6_check(skb->len, saddr, daddr,
  644. csum_partial(th, th->doff << 2,
  645. skb->csum));
  646. }
  647. }
  648. static void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb)
  649. {
  650. struct ipv6_pinfo *np = inet6_sk(sk);
  651. __tcp_v6_send_check(skb, &np->saddr, &np->daddr);
  652. }
  653. static int tcp_v6_gso_send_check(struct sk_buff *skb)
  654. {
  655. const struct ipv6hdr *ipv6h;
  656. struct tcphdr *th;
  657. if (!pskb_may_pull(skb, sizeof(*th)))
  658. return -EINVAL;
  659. ipv6h = ipv6_hdr(skb);
  660. th = tcp_hdr(skb);
  661. th->check = 0;
  662. skb->ip_summed = CHECKSUM_PARTIAL;
  663. __tcp_v6_send_check(skb, &ipv6h->saddr, &ipv6h->daddr);
  664. return 0;
  665. }
  666. static struct sk_buff **tcp6_gro_receive(struct sk_buff **head,
  667. struct sk_buff *skb)
  668. {
  669. const struct ipv6hdr *iph = skb_gro_network_header(skb);
  670. switch (skb->ip_summed) {
  671. case CHECKSUM_COMPLETE:
  672. if (!tcp_v6_check(skb_gro_len(skb), &iph->saddr, &iph->daddr,
  673. skb->csum)) {
  674. skb->ip_summed = CHECKSUM_UNNECESSARY;
  675. break;
  676. }
  677. /* fall through */
  678. case CHECKSUM_NONE:
  679. NAPI_GRO_CB(skb)->flush = 1;
  680. return NULL;
  681. }
  682. return tcp_gro_receive(head, skb);
  683. }
  684. static int tcp6_gro_complete(struct sk_buff *skb)
  685. {
  686. const struct ipv6hdr *iph = ipv6_hdr(skb);
  687. struct tcphdr *th = tcp_hdr(skb);
  688. th->check = ~tcp_v6_check(skb->len - skb_transport_offset(skb),
  689. &iph->saddr, &iph->daddr, 0);
  690. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  691. return tcp_gro_complete(skb);
  692. }
  693. static void tcp_v6_send_response(struct sk_buff *skb, u32 seq, u32 ack, u32 win,
  694. u32 ts, struct tcp_md5sig_key *key, int rst, u8 tclass)
  695. {
  696. const struct tcphdr *th = tcp_hdr(skb);
  697. struct tcphdr *t1;
  698. struct sk_buff *buff;
  699. struct flowi6 fl6;
  700. struct net *net = dev_net(skb_dst(skb)->dev);
  701. struct sock *ctl_sk = net->ipv6.tcp_sk;
  702. unsigned int tot_len = sizeof(struct tcphdr);
  703. struct dst_entry *dst;
  704. __be32 *topt;
  705. if (ts)
  706. tot_len += TCPOLEN_TSTAMP_ALIGNED;
  707. #ifdef CONFIG_TCP_MD5SIG
  708. if (key)
  709. tot_len += TCPOLEN_MD5SIG_ALIGNED;
  710. #endif
  711. buff = alloc_skb(MAX_HEADER + sizeof(struct ipv6hdr) + tot_len,
  712. GFP_ATOMIC);
  713. if (buff == NULL)
  714. return;
  715. skb_reserve(buff, MAX_HEADER + sizeof(struct ipv6hdr) + tot_len);
  716. t1 = (struct tcphdr *) skb_push(buff, tot_len);
  717. skb_reset_transport_header(buff);
  718. /* Swap the send and the receive. */
  719. memset(t1, 0, sizeof(*t1));
  720. t1->dest = th->source;
  721. t1->source = th->dest;
  722. t1->doff = tot_len / 4;
  723. t1->seq = htonl(seq);
  724. t1->ack_seq = htonl(ack);
  725. t1->ack = !rst || !th->ack;
  726. t1->rst = rst;
  727. t1->window = htons(win);
  728. topt = (__be32 *)(t1 + 1);
  729. if (ts) {
  730. *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  731. (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
  732. *topt++ = htonl(tcp_time_stamp);
  733. *topt++ = htonl(ts);
  734. }
  735. #ifdef CONFIG_TCP_MD5SIG
  736. if (key) {
  737. *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  738. (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
  739. tcp_v6_md5_hash_hdr((__u8 *)topt, key,
  740. &ipv6_hdr(skb)->saddr,
  741. &ipv6_hdr(skb)->daddr, t1);
  742. }
  743. #endif
  744. memset(&fl6, 0, sizeof(fl6));
  745. fl6.daddr = ipv6_hdr(skb)->saddr;
  746. fl6.saddr = ipv6_hdr(skb)->daddr;
  747. buff->ip_summed = CHECKSUM_PARTIAL;
  748. buff->csum = 0;
  749. __tcp_v6_send_check(buff, &fl6.saddr, &fl6.daddr);
  750. fl6.flowi6_proto = IPPROTO_TCP;
  751. if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
  752. fl6.flowi6_oif = inet6_iif(skb);
  753. fl6.flowi6_mark = IP6_REPLY_MARK(net, skb->mark);
  754. fl6.fl6_dport = t1->dest;
  755. fl6.fl6_sport = t1->source;
  756. security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
  757. /* Pass a socket to ip6_dst_lookup either it is for RST
  758. * Underlying function will use this to retrieve the network
  759. * namespace
  760. */
  761. dst = ip6_dst_lookup_flow(ctl_sk, &fl6, NULL, false);
  762. if (!IS_ERR(dst)) {
  763. skb_dst_set(buff, dst);
  764. ip6_xmit(ctl_sk, buff, &fl6, NULL, tclass);
  765. TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
  766. if (rst)
  767. TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS);
  768. return;
  769. }
  770. kfree_skb(buff);
  771. }
  772. static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb)
  773. {
  774. const struct tcphdr *th = tcp_hdr(skb);
  775. u32 seq = 0, ack_seq = 0;
  776. struct tcp_md5sig_key *key = NULL;
  777. #ifdef CONFIG_TCP_MD5SIG
  778. const __u8 *hash_location = NULL;
  779. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  780. unsigned char newhash[16];
  781. int genhash;
  782. struct sock *sk1 = NULL;
  783. #endif
  784. if (th->rst)
  785. return;
  786. if (!ipv6_unicast_destination(skb))
  787. return;
  788. #ifdef CONFIG_TCP_MD5SIG
  789. hash_location = tcp_parse_md5sig_option(th);
  790. if (!sk && hash_location) {
  791. /*
  792. * active side is lost. Try to find listening socket through
  793. * source port, and then find md5 key through listening socket.
  794. * we are not loose security here:
  795. * Incoming packet is checked with md5 hash with finding key,
  796. * no RST generated if md5 hash doesn't match.
  797. */
  798. sk1 = inet6_lookup_listener(dev_net(skb_dst(skb)->dev),
  799. &tcp_hashinfo, &ipv6h->daddr,
  800. ntohs(th->source), inet6_iif(skb));
  801. if (!sk1)
  802. return;
  803. rcu_read_lock();
  804. key = tcp_v6_md5_do_lookup(sk1, &ipv6h->saddr);
  805. if (!key)
  806. goto release_sk1;
  807. genhash = tcp_v6_md5_hash_skb(newhash, key, NULL, NULL, skb);
  808. if (genhash || memcmp(hash_location, newhash, 16) != 0)
  809. goto release_sk1;
  810. } else {
  811. key = sk ? tcp_v6_md5_do_lookup(sk, &ipv6h->saddr) : NULL;
  812. }
  813. #endif
  814. if (th->ack)
  815. seq = ntohl(th->ack_seq);
  816. else
  817. ack_seq = ntohl(th->seq) + th->syn + th->fin + skb->len -
  818. (th->doff << 2);
  819. tcp_v6_send_response(skb, seq, ack_seq, 0, 0, key, 1, 0);
  820. #ifdef CONFIG_TCP_MD5SIG
  821. release_sk1:
  822. if (sk1) {
  823. rcu_read_unlock();
  824. sock_put(sk1);
  825. }
  826. #endif
  827. }
  828. static void tcp_v6_send_ack(struct sk_buff *skb, u32 seq, u32 ack, u32 win, u32 ts,
  829. struct tcp_md5sig_key *key, u8 tclass)
  830. {
  831. tcp_v6_send_response(skb, seq, ack, win, ts, key, 0, tclass);
  832. }
  833. static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb)
  834. {
  835. struct inet_timewait_sock *tw = inet_twsk(sk);
  836. struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
  837. tcp_v6_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
  838. tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
  839. tcptw->tw_ts_recent, tcp_twsk_md5_key(tcptw),
  840. tw->tw_tclass);
  841. inet_twsk_put(tw);
  842. }
  843. static void tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
  844. struct request_sock *req)
  845. {
  846. tcp_v6_send_ack(skb, tcp_rsk(req)->snt_isn + 1, tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd, req->ts_recent,
  847. tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->daddr), 0);
  848. }
  849. static struct sock *tcp_v6_hnd_req(struct sock *sk,struct sk_buff *skb)
  850. {
  851. struct request_sock *req, **prev;
  852. const struct tcphdr *th = tcp_hdr(skb);
  853. struct sock *nsk;
  854. /* Find possible connection requests. */
  855. req = inet6_csk_search_req(sk, &prev, th->source,
  856. &ipv6_hdr(skb)->saddr,
  857. &ipv6_hdr(skb)->daddr, inet6_iif(skb));
  858. if (req)
  859. return tcp_check_req(sk, skb, req, prev);
  860. nsk = __inet6_lookup_established(sock_net(sk), &tcp_hashinfo,
  861. &ipv6_hdr(skb)->saddr, th->source,
  862. &ipv6_hdr(skb)->daddr, ntohs(th->dest), inet6_iif(skb));
  863. if (nsk) {
  864. if (nsk->sk_state != TCP_TIME_WAIT) {
  865. bh_lock_sock(nsk);
  866. return nsk;
  867. }
  868. inet_twsk_put(inet_twsk(nsk));
  869. return NULL;
  870. }
  871. #ifdef CONFIG_SYN_COOKIES
  872. if (!th->syn)
  873. sk = cookie_v6_check(sk, skb);
  874. #endif
  875. return sk;
  876. }
  877. /* FIXME: this is substantially similar to the ipv4 code.
  878. * Can some kind of merge be done? -- erics
  879. */
  880. static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
  881. {
  882. struct tcp_extend_values tmp_ext;
  883. struct tcp_options_received tmp_opt;
  884. const u8 *hash_location;
  885. struct request_sock *req;
  886. struct inet6_request_sock *treq;
  887. struct ipv6_pinfo *np = inet6_sk(sk);
  888. struct tcp_sock *tp = tcp_sk(sk);
  889. __u32 isn = TCP_SKB_CB(skb)->when;
  890. struct dst_entry *dst = NULL;
  891. int want_cookie = 0;
  892. if (skb->protocol == htons(ETH_P_IP))
  893. return tcp_v4_conn_request(sk, skb);
  894. if (!ipv6_unicast_destination(skb))
  895. goto drop;
  896. if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
  897. want_cookie = tcp_syn_flood_action(sk, skb, "TCPv6");
  898. if (!want_cookie)
  899. goto drop;
  900. }
  901. if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
  902. goto drop;
  903. req = inet6_reqsk_alloc(&tcp6_request_sock_ops);
  904. if (req == NULL)
  905. goto drop;
  906. #ifdef CONFIG_TCP_MD5SIG
  907. tcp_rsk(req)->af_specific = &tcp_request_sock_ipv6_ops;
  908. #endif
  909. tcp_clear_options(&tmp_opt);
  910. tmp_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
  911. tmp_opt.user_mss = tp->rx_opt.user_mss;
  912. tcp_parse_options(skb, &tmp_opt, &hash_location, 0);
  913. if (tmp_opt.cookie_plus > 0 &&
  914. tmp_opt.saw_tstamp &&
  915. !tp->rx_opt.cookie_out_never &&
  916. (sysctl_tcp_cookie_size > 0 ||
  917. (tp->cookie_values != NULL &&
  918. tp->cookie_values->cookie_desired > 0))) {
  919. u8 *c;
  920. u32 *d;
  921. u32 *mess = &tmp_ext.cookie_bakery[COOKIE_DIGEST_WORDS];
  922. int l = tmp_opt.cookie_plus - TCPOLEN_COOKIE_BASE;
  923. if (tcp_cookie_generator(&tmp_ext.cookie_bakery[0]) != 0)
  924. goto drop_and_free;
  925. /* Secret recipe starts with IP addresses */
  926. d = (__force u32 *)&ipv6_hdr(skb)->daddr.s6_addr32[0];
  927. *mess++ ^= *d++;
  928. *mess++ ^= *d++;
  929. *mess++ ^= *d++;
  930. *mess++ ^= *d++;
  931. d = (__force u32 *)&ipv6_hdr(skb)->saddr.s6_addr32[0];
  932. *mess++ ^= *d++;
  933. *mess++ ^= *d++;
  934. *mess++ ^= *d++;
  935. *mess++ ^= *d++;
  936. /* plus variable length Initiator Cookie */
  937. c = (u8 *)mess;
  938. while (l-- > 0)
  939. *c++ ^= *hash_location++;
  940. want_cookie = 0; /* not our kind of cookie */
  941. tmp_ext.cookie_out_never = 0; /* false */
  942. tmp_ext.cookie_plus = tmp_opt.cookie_plus;
  943. } else if (!tp->rx_opt.cookie_in_always) {
  944. /* redundant indications, but ensure initialization. */
  945. tmp_ext.cookie_out_never = 1; /* true */
  946. tmp_ext.cookie_plus = 0;
  947. } else {
  948. goto drop_and_free;
  949. }
  950. tmp_ext.cookie_in_always = tp->rx_opt.cookie_in_always;
  951. if (want_cookie && !tmp_opt.saw_tstamp)
  952. tcp_clear_options(&tmp_opt);
  953. tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
  954. tcp_openreq_init(req, &tmp_opt, skb);
  955. treq = inet6_rsk(req);
  956. treq->rmt_addr = ipv6_hdr(skb)->saddr;
  957. treq->loc_addr = ipv6_hdr(skb)->daddr;
  958. if (!want_cookie || tmp_opt.tstamp_ok)
  959. TCP_ECN_create_request(req, tcp_hdr(skb));
  960. treq->iif = sk->sk_bound_dev_if;
  961. inet_rsk(req)->ir_mark = inet_request_mark(sk, skb);
  962. /* So that link locals have meaning */
  963. if (!sk->sk_bound_dev_if &&
  964. ipv6_addr_type(&treq->rmt_addr) & IPV6_ADDR_LINKLOCAL)
  965. treq->iif = inet6_iif(skb);
  966. if (!isn) {
  967. struct inet_peer *peer = NULL;
  968. if (ipv6_opt_accepted(sk, skb) ||
  969. np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
  970. np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
  971. atomic_inc(&skb->users);
  972. treq->pktopts = skb;
  973. }
  974. if (want_cookie) {
  975. isn = cookie_v6_init_sequence(sk, skb, &req->mss);
  976. req->cookie_ts = tmp_opt.tstamp_ok;
  977. goto have_isn;
  978. }
  979. /* VJ's idea. We save last timestamp seen
  980. * from the destination in peer table, when entering
  981. * state TIME-WAIT, and check against it before
  982. * accepting new connection request.
  983. *
  984. * If "isn" is not zero, this request hit alive
  985. * timewait bucket, so that all the necessary checks
  986. * are made in the function processing timewait state.
  987. */
  988. if (tmp_opt.saw_tstamp &&
  989. tcp_death_row.sysctl_tw_recycle &&
  990. (dst = inet6_csk_route_req(sk, req)) != NULL &&
  991. (peer = rt6_get_peer((struct rt6_info *)dst)) != NULL &&
  992. ipv6_addr_equal((struct in6_addr *)peer->daddr.addr.a6,
  993. &treq->rmt_addr)) {
  994. inet_peer_refcheck(peer);
  995. if ((u32)get_seconds() - peer->tcp_ts_stamp < TCP_PAWS_MSL &&
  996. (s32)(peer->tcp_ts - req->ts_recent) >
  997. TCP_PAWS_WINDOW) {
  998. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED);
  999. goto drop_and_release;
  1000. }
  1001. }
  1002. /* Kill the following clause, if you dislike this way. */
  1003. else if (!sysctl_tcp_syncookies &&
  1004. (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
  1005. (sysctl_max_syn_backlog >> 2)) &&
  1006. (!peer || !peer->tcp_ts_stamp) &&
  1007. (!dst || !dst_metric(dst, RTAX_RTT))) {
  1008. /* Without syncookies last quarter of
  1009. * backlog is filled with destinations,
  1010. * proven to be alive.
  1011. * It means that we continue to communicate
  1012. * to destinations, already remembered
  1013. * to the moment of synflood.
  1014. */
  1015. LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open request from %pI6/%u\n",
  1016. &treq->rmt_addr, ntohs(tcp_hdr(skb)->source));
  1017. goto drop_and_release;
  1018. }
  1019. isn = tcp_v6_init_sequence(skb);
  1020. }
  1021. have_isn:
  1022. tcp_rsk(req)->snt_isn = isn;
  1023. tcp_rsk(req)->snt_synack = tcp_time_stamp;
  1024. security_inet_conn_request(sk, skb, req);
  1025. if (tcp_v6_send_synack(sk, req,
  1026. (struct request_values *)&tmp_ext) ||
  1027. want_cookie)
  1028. goto drop_and_free;
  1029. inet6_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
  1030. return 0;
  1031. drop_and_release:
  1032. dst_release(dst);
  1033. drop_and_free:
  1034. reqsk_free(req);
  1035. drop:
  1036. return 0; /* don't send reset */
  1037. }
  1038. static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
  1039. struct request_sock *req,
  1040. struct dst_entry *dst)
  1041. {
  1042. struct inet6_request_sock *treq;
  1043. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  1044. struct tcp6_sock *newtcp6sk;
  1045. struct ipv6_txoptions *opt;
  1046. struct inet_sock *newinet;
  1047. struct tcp_sock *newtp;
  1048. struct sock *newsk;
  1049. #ifdef CONFIG_TCP_MD5SIG
  1050. struct tcp_md5sig_key *key;
  1051. #endif
  1052. if (skb->protocol == htons(ETH_P_IP)) {
  1053. /*
  1054. * v6 mapped
  1055. */
  1056. newsk = tcp_v4_syn_recv_sock(sk, skb, req, dst);
  1057. if (newsk == NULL)
  1058. return NULL;
  1059. newtcp6sk = (struct tcp6_sock *)newsk;
  1060. inet_sk(newsk)->pinet6 = &newtcp6sk->inet6;
  1061. newinet = inet_sk(newsk);
  1062. newnp = inet6_sk(newsk);
  1063. newtp = tcp_sk(newsk);
  1064. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  1065. ipv6_addr_set_v4mapped(newinet->inet_daddr, &newnp->daddr);
  1066. ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
  1067. newnp->rcv_saddr = newnp->saddr;
  1068. inet_csk(newsk)->icsk_af_ops = &ipv6_mapped;
  1069. newsk->sk_backlog_rcv = tcp_v4_do_rcv;
  1070. #ifdef CONFIG_TCP_MD5SIG
  1071. newtp->af_specific = &tcp_sock_ipv6_mapped_specific;
  1072. #endif
  1073. newnp->ipv6_mc_list = NULL;
  1074. newnp->ipv6_ac_list = NULL;
  1075. newnp->ipv6_fl_list = NULL;
  1076. newnp->pktoptions = NULL;
  1077. newnp->opt = NULL;
  1078. newnp->mcast_oif = inet6_iif(skb);
  1079. newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
  1080. newnp->rcv_tclass = ipv6_tclass(ipv6_hdr(skb));
  1081. /*
  1082. * No need to charge this sock to the relevant IPv6 refcnt debug socks count
  1083. * here, tcp_create_openreq_child now does this for us, see the comment in
  1084. * that function for the gory details. -acme
  1085. */
  1086. /* It is tricky place. Until this moment IPv4 tcp
  1087. worked with IPv6 icsk.icsk_af_ops.
  1088. Sync it now.
  1089. */
  1090. tcp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
  1091. return newsk;
  1092. }
  1093. treq = inet6_rsk(req);
  1094. opt = np->opt;
  1095. if (sk_acceptq_is_full(sk))
  1096. goto out_overflow;
  1097. if (!dst) {
  1098. dst = inet6_csk_route_req(sk, req);
  1099. if (!dst)
  1100. goto out;
  1101. }
  1102. newsk = tcp_create_openreq_child(sk, req, skb);
  1103. if (newsk == NULL)
  1104. goto out_nonewsk;
  1105. /*
  1106. * No need to charge this sock to the relevant IPv6 refcnt debug socks
  1107. * count here, tcp_create_openreq_child now does this for us, see the
  1108. * comment in that function for the gory details. -acme
  1109. */
  1110. newsk->sk_gso_type = SKB_GSO_TCPV6;
  1111. __ip6_dst_store(newsk, dst, NULL, NULL);
  1112. newtcp6sk = (struct tcp6_sock *)newsk;
  1113. inet_sk(newsk)->pinet6 = &newtcp6sk->inet6;
  1114. newtp = tcp_sk(newsk);
  1115. newinet = inet_sk(newsk);
  1116. newnp = inet6_sk(newsk);
  1117. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  1118. newnp->daddr = treq->rmt_addr;
  1119. newnp->saddr = treq->loc_addr;
  1120. newnp->rcv_saddr = treq->loc_addr;
  1121. newsk->sk_bound_dev_if = treq->iif;
  1122. /* Now IPv6 options...
  1123. First: no IPv4 options.
  1124. */
  1125. newinet->inet_opt = NULL;
  1126. newnp->ipv6_mc_list = NULL;
  1127. newnp->ipv6_ac_list = NULL;
  1128. newnp->ipv6_fl_list = NULL;
  1129. /* Clone RX bits */
  1130. newnp->rxopt.all = np->rxopt.all;
  1131. /* Clone pktoptions received with SYN */
  1132. newnp->pktoptions = NULL;
  1133. if (treq->pktopts != NULL) {
  1134. newnp->pktoptions = skb_clone(treq->pktopts, GFP_ATOMIC);
  1135. kfree_skb(treq->pktopts);
  1136. treq->pktopts = NULL;
  1137. if (newnp->pktoptions)
  1138. skb_set_owner_r(newnp->pktoptions, newsk);
  1139. }
  1140. newnp->opt = NULL;
  1141. newnp->mcast_oif = inet6_iif(skb);
  1142. newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
  1143. newnp->rcv_tclass = ipv6_tclass(ipv6_hdr(skb));
  1144. /* Clone native IPv6 options from listening socket (if any)
  1145. Yes, keeping reference count would be much more clever,
  1146. but we make one more one thing there: reattach optmem
  1147. to newsk.
  1148. */
  1149. opt = rcu_dereference(np->opt);
  1150. if (opt) {
  1151. opt = ipv6_dup_options(newsk, opt);
  1152. RCU_INIT_POINTER(newnp->opt, opt);
  1153. }
  1154. inet_csk(newsk)->icsk_ext_hdr_len = 0;
  1155. if (opt)
  1156. inet_csk(newsk)->icsk_ext_hdr_len = opt->opt_nflen +
  1157. opt->opt_flen;
  1158. tcp_mtup_init(newsk);
  1159. tcp_sync_mss(newsk, dst_mtu(dst));
  1160. newtp->advmss = dst_metric_advmss(dst);
  1161. if (tcp_sk(sk)->rx_opt.user_mss &&
  1162. tcp_sk(sk)->rx_opt.user_mss < newtp->advmss)
  1163. newtp->advmss = tcp_sk(sk)->rx_opt.user_mss;
  1164. tcp_initialize_rcv_mss(newsk);
  1165. if (tcp_rsk(req)->snt_synack)
  1166. tcp_valid_rtt_meas(newsk,
  1167. tcp_time_stamp - tcp_rsk(req)->snt_synack);
  1168. newtp->total_retrans = req->retrans;
  1169. newinet->inet_daddr = newinet->inet_saddr = LOOPBACK4_IPV6;
  1170. newinet->inet_rcv_saddr = LOOPBACK4_IPV6;
  1171. #ifdef CONFIG_TCP_MD5SIG
  1172. /* Copy over the MD5 key from the original socket */
  1173. if ((key = tcp_v6_md5_do_lookup(sk, &newnp->daddr)) != NULL) {
  1174. /* We're using one, so create a matching key
  1175. * on the newsk structure. If we fail to get
  1176. * memory, then we end up not copying the key
  1177. * across. Shucks.
  1178. */
  1179. tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newnp->daddr,
  1180. AF_INET6, key->key, key->keylen, GFP_ATOMIC);
  1181. }
  1182. #endif
  1183. if (__inet_inherit_port(sk, newsk) < 0) {
  1184. inet_csk_prepare_forced_close(newsk);
  1185. tcp_done(newsk);
  1186. goto out;
  1187. }
  1188. __inet6_hash(newsk, NULL);
  1189. return newsk;
  1190. out_overflow:
  1191. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
  1192. out_nonewsk:
  1193. if (opt && opt != np->opt)
  1194. sock_kfree_s(sk, opt, opt->tot_len);
  1195. dst_release(dst);
  1196. out:
  1197. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
  1198. return NULL;
  1199. }
  1200. static __sum16 tcp_v6_checksum_init(struct sk_buff *skb)
  1201. {
  1202. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  1203. if (!tcp_v6_check(skb->len, &ipv6_hdr(skb)->saddr,
  1204. &ipv6_hdr(skb)->daddr, skb->csum)) {
  1205. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1206. return 0;
  1207. }
  1208. }
  1209. skb->csum = ~csum_unfold(tcp_v6_check(skb->len,
  1210. &ipv6_hdr(skb)->saddr,
  1211. &ipv6_hdr(skb)->daddr, 0));
  1212. if (skb->len <= 76) {
  1213. return __skb_checksum_complete(skb);
  1214. }
  1215. return 0;
  1216. }
  1217. /* The socket must have it's spinlock held when we get
  1218. * here, unless it is a TCP_LISTEN socket.
  1219. *
  1220. * We have a potential double-lock case here, so even when
  1221. * doing backlog processing we use the BH locking scheme.
  1222. * This is because we cannot sleep with the original spinlock
  1223. * held.
  1224. */
  1225. static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
  1226. {
  1227. struct ipv6_pinfo *np = inet6_sk(sk);
  1228. struct tcp_sock *tp;
  1229. struct sk_buff *opt_skb = NULL;
  1230. /* Imagine: socket is IPv6. IPv4 packet arrives,
  1231. goes to IPv4 receive handler and backlogged.
  1232. From backlog it always goes here. Kerboom...
  1233. Fortunately, tcp_rcv_established and rcv_established
  1234. handle them correctly, but it is not case with
  1235. tcp_v6_hnd_req and tcp_v6_send_reset(). --ANK
  1236. */
  1237. if (skb->protocol == htons(ETH_P_IP))
  1238. return tcp_v4_do_rcv(sk, skb);
  1239. #ifdef CONFIG_TCP_MD5SIG
  1240. if (tcp_v6_inbound_md5_hash (sk, skb))
  1241. goto discard;
  1242. #endif
  1243. if (sk_filter(sk, skb))
  1244. goto discard;
  1245. /*
  1246. * socket locking is here for SMP purposes as backlog rcv
  1247. * is currently called with bh processing disabled.
  1248. */
  1249. /* Do Stevens' IPV6_PKTOPTIONS.
  1250. Yes, guys, it is the only place in our code, where we
  1251. may make it not affecting IPv4.
  1252. The rest of code is protocol independent,
  1253. and I do not like idea to uglify IPv4.
  1254. Actually, all the idea behind IPV6_PKTOPTIONS
  1255. looks not very well thought. For now we latch
  1256. options, received in the last packet, enqueued
  1257. by tcp. Feel free to propose better solution.
  1258. --ANK (980728)
  1259. */
  1260. if (np->rxopt.all)
  1261. opt_skb = skb_clone(skb, GFP_ATOMIC);
  1262. if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
  1263. sock_rps_save_rxhash(sk, skb);
  1264. if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len))
  1265. goto reset;
  1266. if (opt_skb)
  1267. goto ipv6_pktoptions;
  1268. return 0;
  1269. }
  1270. if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
  1271. goto csum_err;
  1272. if (sk->sk_state == TCP_LISTEN) {
  1273. struct sock *nsk = tcp_v6_hnd_req(sk, skb);
  1274. if (!nsk)
  1275. goto discard;
  1276. /*
  1277. * Queue it on the new socket if the new socket is active,
  1278. * otherwise we just shortcircuit this and continue with
  1279. * the new socket..
  1280. */
  1281. if(nsk != sk) {
  1282. sock_rps_save_rxhash(nsk, skb);
  1283. if (tcp_child_process(sk, nsk, skb))
  1284. goto reset;
  1285. if (opt_skb)
  1286. __kfree_skb(opt_skb);
  1287. return 0;
  1288. }
  1289. } else
  1290. sock_rps_save_rxhash(sk, skb);
  1291. if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len))
  1292. goto reset;
  1293. if (opt_skb)
  1294. goto ipv6_pktoptions;
  1295. return 0;
  1296. reset:
  1297. tcp_v6_send_reset(sk, skb);
  1298. discard:
  1299. if (opt_skb)
  1300. __kfree_skb(opt_skb);
  1301. kfree_skb(skb);
  1302. return 0;
  1303. csum_err:
  1304. TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
  1305. goto discard;
  1306. ipv6_pktoptions:
  1307. /* Do you ask, what is it?
  1308. 1. skb was enqueued by tcp.
  1309. 2. skb is added to tail of read queue, rather than out of order.
  1310. 3. socket is not in passive state.
  1311. 4. Finally, it really contains options, which user wants to receive.
  1312. */
  1313. tp = tcp_sk(sk);
  1314. if (TCP_SKB_CB(opt_skb)->end_seq == tp->rcv_nxt &&
  1315. !((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) {
  1316. if (np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo)
  1317. np->mcast_oif = inet6_iif(opt_skb);
  1318. if (np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim)
  1319. np->mcast_hops = ipv6_hdr(opt_skb)->hop_limit;
  1320. if (np->rxopt.bits.rxtclass)
  1321. np->rcv_tclass = ipv6_tclass(ipv6_hdr(opt_skb));
  1322. if (ipv6_opt_accepted(sk, opt_skb)) {
  1323. skb_set_owner_r(opt_skb, sk);
  1324. opt_skb = xchg(&np->pktoptions, opt_skb);
  1325. } else {
  1326. __kfree_skb(opt_skb);
  1327. opt_skb = xchg(&np->pktoptions, NULL);
  1328. }
  1329. }
  1330. kfree_skb(opt_skb);
  1331. return 0;
  1332. }
  1333. static int tcp_v6_rcv(struct sk_buff *skb)
  1334. {
  1335. const struct tcphdr *th;
  1336. const struct ipv6hdr *hdr;
  1337. struct sock *sk;
  1338. int ret;
  1339. struct net *net = dev_net(skb->dev);
  1340. if (skb->pkt_type != PACKET_HOST)
  1341. goto discard_it;
  1342. /*
  1343. * Count it even if it's bad.
  1344. */
  1345. TCP_INC_STATS_BH(net, TCP_MIB_INSEGS);
  1346. if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
  1347. goto discard_it;
  1348. th = tcp_hdr(skb);
  1349. if (th->doff < sizeof(struct tcphdr)/4)
  1350. goto bad_packet;
  1351. if (!pskb_may_pull(skb, th->doff*4))
  1352. goto discard_it;
  1353. if (!skb_csum_unnecessary(skb) && tcp_v6_checksum_init(skb))
  1354. goto bad_packet;
  1355. th = tcp_hdr(skb);
  1356. hdr = ipv6_hdr(skb);
  1357. TCP_SKB_CB(skb)->seq = ntohl(th->seq);
  1358. TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
  1359. skb->len - th->doff*4);
  1360. TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
  1361. TCP_SKB_CB(skb)->when = 0;
  1362. TCP_SKB_CB(skb)->ip_dsfield = ipv6_get_dsfield(hdr);
  1363. TCP_SKB_CB(skb)->sacked = 0;
  1364. sk = __inet6_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
  1365. if (!sk)
  1366. goto no_tcp_socket;
  1367. process:
  1368. if (sk->sk_state == TCP_TIME_WAIT)
  1369. goto do_time_wait;
  1370. if (hdr->hop_limit < inet6_sk(sk)->min_hopcount) {
  1371. NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
  1372. goto discard_and_relse;
  1373. }
  1374. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
  1375. goto discard_and_relse;
  1376. if (sk_filter(sk, skb))
  1377. goto discard_and_relse;
  1378. skb->dev = NULL;
  1379. if (sk->sk_state == TCP_LISTEN) {
  1380. ret = tcp_v6_do_rcv(sk, skb);
  1381. goto put_and_return;
  1382. }
  1383. bh_lock_sock_nested(sk);
  1384. ret = 0;
  1385. if (!sock_owned_by_user(sk)) {
  1386. #ifdef CONFIG_NET_DMA
  1387. struct tcp_sock *tp = tcp_sk(sk);
  1388. if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
  1389. tp->ucopy.dma_chan = net_dma_find_channel();
  1390. if (tp->ucopy.dma_chan)
  1391. ret = tcp_v6_do_rcv(sk, skb);
  1392. else
  1393. #endif
  1394. {
  1395. if (!tcp_prequeue(sk, skb))
  1396. ret = tcp_v6_do_rcv(sk, skb);
  1397. }
  1398. } else if (unlikely(sk_add_backlog(sk, skb))) {
  1399. bh_unlock_sock(sk);
  1400. NET_INC_STATS_BH(net, LINUX_MIB_TCPBACKLOGDROP);
  1401. goto discard_and_relse;
  1402. }
  1403. bh_unlock_sock(sk);
  1404. put_and_return:
  1405. sock_put(sk);
  1406. return ret ? -1 : 0;
  1407. no_tcp_socket:
  1408. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  1409. goto discard_it;
  1410. if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
  1411. bad_packet:
  1412. TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
  1413. } else {
  1414. tcp_v6_send_reset(NULL, skb);
  1415. }
  1416. discard_it:
  1417. /*
  1418. * Discard frame
  1419. */
  1420. kfree_skb(skb);
  1421. return 0;
  1422. discard_and_relse:
  1423. sock_put(sk);
  1424. goto discard_it;
  1425. do_time_wait:
  1426. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  1427. inet_twsk_put(inet_twsk(sk));
  1428. goto discard_it;
  1429. }
  1430. if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
  1431. TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
  1432. inet_twsk_put(inet_twsk(sk));
  1433. goto discard_it;
  1434. }
  1435. switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
  1436. case TCP_TW_SYN:
  1437. {
  1438. struct sock *sk2;
  1439. sk2 = inet6_lookup_listener(dev_net(skb->dev), &tcp_hashinfo,
  1440. &ipv6_hdr(skb)->daddr,
  1441. ntohs(th->dest), inet6_iif(skb));
  1442. if (sk2 != NULL) {
  1443. struct inet_timewait_sock *tw = inet_twsk(sk);
  1444. inet_twsk_deschedule(tw, &tcp_death_row);
  1445. inet_twsk_put(tw);
  1446. sk = sk2;
  1447. goto process;
  1448. }
  1449. /* Fall through to ACK */
  1450. }
  1451. case TCP_TW_ACK:
  1452. tcp_v6_timewait_ack(sk, skb);
  1453. break;
  1454. case TCP_TW_RST:
  1455. goto no_tcp_socket;
  1456. case TCP_TW_SUCCESS:;
  1457. }
  1458. goto discard_it;
  1459. }
  1460. static struct inet_peer *tcp_v6_get_peer(struct sock *sk, bool *release_it)
  1461. {
  1462. struct rt6_info *rt = (struct rt6_info *) __sk_dst_get(sk);
  1463. struct ipv6_pinfo *np = inet6_sk(sk);
  1464. struct inet_peer *peer;
  1465. if (!rt ||
  1466. !ipv6_addr_equal(&np->daddr, &rt->rt6i_dst.addr)) {
  1467. peer = inet_getpeer_v6(&np->daddr, 1);
  1468. *release_it = true;
  1469. } else {
  1470. if (!rt->rt6i_peer)
  1471. rt6_bind_peer(rt, 1);
  1472. peer = rt->rt6i_peer;
  1473. *release_it = false;
  1474. }
  1475. return peer;
  1476. }
  1477. static void *tcp_v6_tw_get_peer(struct sock *sk)
  1478. {
  1479. const struct inet6_timewait_sock *tw6 = inet6_twsk(sk);
  1480. const struct inet_timewait_sock *tw = inet_twsk(sk);
  1481. if (tw->tw_family == AF_INET)
  1482. return tcp_v4_tw_get_peer(sk);
  1483. return inet_getpeer_v6(&tw6->tw_v6_daddr, 1);
  1484. }
  1485. static struct timewait_sock_ops tcp6_timewait_sock_ops = {
  1486. .twsk_obj_size = sizeof(struct tcp6_timewait_sock),
  1487. .twsk_unique = tcp_twsk_unique,
  1488. .twsk_destructor= tcp_twsk_destructor,
  1489. .twsk_getpeer = tcp_v6_tw_get_peer,
  1490. };
  1491. static const struct inet_connection_sock_af_ops ipv6_specific = {
  1492. .queue_xmit = inet6_csk_xmit,
  1493. .send_check = tcp_v6_send_check,
  1494. .rebuild_header = inet6_sk_rebuild_header,
  1495. .conn_request = tcp_v6_conn_request,
  1496. .syn_recv_sock = tcp_v6_syn_recv_sock,
  1497. .get_peer = tcp_v6_get_peer,
  1498. .net_header_len = sizeof(struct ipv6hdr),
  1499. .setsockopt = ipv6_setsockopt,
  1500. .getsockopt = ipv6_getsockopt,
  1501. .addr2sockaddr = inet6_csk_addr2sockaddr,
  1502. .sockaddr_len = sizeof(struct sockaddr_in6),
  1503. .bind_conflict = inet6_csk_bind_conflict,
  1504. #ifdef CONFIG_COMPAT
  1505. .compat_setsockopt = compat_ipv6_setsockopt,
  1506. .compat_getsockopt = compat_ipv6_getsockopt,
  1507. #endif
  1508. };
  1509. #ifdef CONFIG_TCP_MD5SIG
  1510. static const struct tcp_sock_af_ops tcp_sock_ipv6_specific = {
  1511. .md5_lookup = tcp_v6_md5_lookup,
  1512. .calc_md5_hash = tcp_v6_md5_hash_skb,
  1513. .md5_parse = tcp_v6_parse_md5_keys,
  1514. };
  1515. #endif
  1516. /*
  1517. * TCP over IPv4 via INET6 API
  1518. */
  1519. static const struct inet_connection_sock_af_ops ipv6_mapped = {
  1520. .queue_xmit = ip_queue_xmit,
  1521. .send_check = tcp_v4_send_check,
  1522. .rebuild_header = inet_sk_rebuild_header,
  1523. .conn_request = tcp_v6_conn_request,
  1524. .syn_recv_sock = tcp_v6_syn_recv_sock,
  1525. .get_peer = tcp_v4_get_peer,
  1526. .net_header_len = sizeof(struct iphdr),
  1527. .setsockopt = ipv6_setsockopt,
  1528. .getsockopt = ipv6_getsockopt,
  1529. .addr2sockaddr = inet6_csk_addr2sockaddr,
  1530. .sockaddr_len = sizeof(struct sockaddr_in6),
  1531. .bind_conflict = inet6_csk_bind_conflict,
  1532. #ifdef CONFIG_COMPAT
  1533. .compat_setsockopt = compat_ipv6_setsockopt,
  1534. .compat_getsockopt = compat_ipv6_getsockopt,
  1535. #endif
  1536. };
  1537. #ifdef CONFIG_TCP_MD5SIG
  1538. static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific = {
  1539. .md5_lookup = tcp_v4_md5_lookup,
  1540. .calc_md5_hash = tcp_v4_md5_hash_skb,
  1541. .md5_parse = tcp_v6_parse_md5_keys,
  1542. };
  1543. #endif
  1544. /* NOTE: A lot of things set to zero explicitly by call to
  1545. * sk_alloc() so need not be done here.
  1546. */
  1547. static int tcp_v6_init_sock(struct sock *sk)
  1548. {
  1549. struct inet_connection_sock *icsk = inet_csk(sk);
  1550. struct tcp_sock *tp = tcp_sk(sk);
  1551. skb_queue_head_init(&tp->out_of_order_queue);
  1552. tcp_init_xmit_timers(sk);
  1553. tcp_prequeue_init(tp);
  1554. icsk->icsk_rto = TCP_TIMEOUT_INIT;
  1555. tp->mdev = TCP_TIMEOUT_INIT;
  1556. /* So many TCP implementations out there (incorrectly) count the
  1557. * initial SYN frame in their delayed-ACK and congestion control
  1558. * algorithms that we must have the following bandaid to talk
  1559. * efficiently to them. -DaveM
  1560. */
  1561. tp->snd_cwnd = 2;
  1562. /* See draft-stevens-tcpca-spec-01 for discussion of the
  1563. * initialization of these values.
  1564. */
  1565. tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
  1566. tp->snd_cwnd_clamp = ~0;
  1567. tp->mss_cache = TCP_MSS_DEFAULT;
  1568. tp->reordering = sysctl_tcp_reordering;
  1569. sk->sk_state = TCP_CLOSE;
  1570. icsk->icsk_af_ops = &ipv6_specific;
  1571. icsk->icsk_ca_ops = &tcp_init_congestion_ops;
  1572. icsk->icsk_sync_mss = tcp_sync_mss;
  1573. sk->sk_write_space = sk_stream_write_space;
  1574. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  1575. #ifdef CONFIG_TCP_MD5SIG
  1576. tp->af_specific = &tcp_sock_ipv6_specific;
  1577. #endif
  1578. /* TCP Cookie Transactions */
  1579. if (sysctl_tcp_cookie_size > 0) {
  1580. /* Default, cookies without s_data_payload. */
  1581. tp->cookie_values =
  1582. kzalloc(sizeof(*tp->cookie_values),
  1583. sk->sk_allocation);
  1584. if (tp->cookie_values != NULL)
  1585. kref_init(&tp->cookie_values->kref);
  1586. }
  1587. /* Presumed zeroed, in order of appearance:
  1588. * cookie_in_always, cookie_out_never,
  1589. * s_data_constant, s_data_in, s_data_out
  1590. */
  1591. sk->sk_sndbuf = sysctl_tcp_wmem[1];
  1592. sk->sk_rcvbuf = sysctl_tcp_rmem[1];
  1593. local_bh_disable();
  1594. sock_update_memcg(sk);
  1595. sk_sockets_allocated_inc(sk);
  1596. local_bh_enable();
  1597. return 0;
  1598. }
  1599. static void tcp_v6_destroy_sock(struct sock *sk)
  1600. {
  1601. tcp_v4_destroy_sock(sk);
  1602. inet6_destroy_sock(sk);
  1603. }
  1604. #ifdef CONFIG_PROC_FS
  1605. /* Proc filesystem TCPv6 sock list dumping. */
  1606. static void get_openreq6(struct seq_file *seq,
  1607. const struct sock *sk, struct request_sock *req, int i, int uid)
  1608. {
  1609. int ttd = req->expires - jiffies;
  1610. const struct in6_addr *src = &inet6_rsk(req)->loc_addr;
  1611. const struct in6_addr *dest = &inet6_rsk(req)->rmt_addr;
  1612. if (ttd < 0)
  1613. ttd = 0;
  1614. seq_printf(seq,
  1615. "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
  1616. "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
  1617. i,
  1618. src->s6_addr32[0], src->s6_addr32[1],
  1619. src->s6_addr32[2], src->s6_addr32[3],
  1620. ntohs(inet_rsk(req)->loc_port),
  1621. dest->s6_addr32[0], dest->s6_addr32[1],
  1622. dest->s6_addr32[2], dest->s6_addr32[3],
  1623. ntohs(inet_rsk(req)->rmt_port),
  1624. TCP_SYN_RECV,
  1625. 0,0, /* could print option size, but that is af dependent. */
  1626. 1, /* timers active (only the expire timer) */
  1627. jiffies_to_clock_t(ttd),
  1628. req->retrans,
  1629. uid,
  1630. 0, /* non standard timer */
  1631. 0, /* open_requests have no inode */
  1632. 0, req);
  1633. }
  1634. static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
  1635. {
  1636. const struct in6_addr *dest, *src;
  1637. __u16 destp, srcp;
  1638. int timer_active;
  1639. unsigned long timer_expires;
  1640. const struct inet_sock *inet = inet_sk(sp);
  1641. const struct tcp_sock *tp = tcp_sk(sp);
  1642. const struct inet_connection_sock *icsk = inet_csk(sp);
  1643. const struct ipv6_pinfo *np = inet6_sk(sp);
  1644. dest = &np->daddr;
  1645. src = &np->rcv_saddr;
  1646. destp = ntohs(inet->inet_dport);
  1647. srcp = ntohs(inet->inet_sport);
  1648. if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
  1649. timer_active = 1;
  1650. timer_expires = icsk->icsk_timeout;
  1651. } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
  1652. timer_active = 4;
  1653. timer_expires = icsk->icsk_timeout;
  1654. } else if (timer_pending(&sp->sk_timer)) {
  1655. timer_active = 2;
  1656. timer_expires = sp->sk_timer.expires;
  1657. } else {
  1658. timer_active = 0;
  1659. timer_expires = jiffies;
  1660. }
  1661. seq_printf(seq,
  1662. "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
  1663. "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %lu %lu %u %u %d\n",
  1664. i,
  1665. src->s6_addr32[0], src->s6_addr32[1],
  1666. src->s6_addr32[2], src->s6_addr32[3], srcp,
  1667. dest->s6_addr32[0], dest->s6_addr32[1],
  1668. dest->s6_addr32[2], dest->s6_addr32[3], destp,
  1669. sp->sk_state,
  1670. tp->write_seq-tp->snd_una,
  1671. (sp->sk_state == TCP_LISTEN) ? sp->sk_ack_backlog : (tp->rcv_nxt - tp->copied_seq),
  1672. timer_active,
  1673. jiffies_to_clock_t(timer_expires - jiffies),
  1674. icsk->icsk_retransmits,
  1675. sock_i_uid(sp),
  1676. icsk->icsk_probes_out,
  1677. sock_i_ino(sp),
  1678. atomic_read(&sp->sk_refcnt), sp,
  1679. jiffies_to_clock_t(icsk->icsk_rto),
  1680. jiffies_to_clock_t(icsk->icsk_ack.ato),
  1681. (icsk->icsk_ack.quick << 1 ) | icsk->icsk_ack.pingpong,
  1682. tp->snd_cwnd,
  1683. tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh
  1684. );
  1685. }
  1686. static void get_timewait6_sock(struct seq_file *seq,
  1687. struct inet_timewait_sock *tw, int i)
  1688. {
  1689. const struct in6_addr *dest, *src;
  1690. __u16 destp, srcp;
  1691. const struct inet6_timewait_sock *tw6 = inet6_twsk((struct sock *)tw);
  1692. int ttd = tw->tw_ttd - jiffies;
  1693. if (ttd < 0)
  1694. ttd = 0;
  1695. dest = &tw6->tw_v6_daddr;
  1696. src = &tw6->tw_v6_rcv_saddr;
  1697. destp = ntohs(tw->tw_dport);
  1698. srcp = ntohs(tw->tw_sport);
  1699. seq_printf(seq,
  1700. "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
  1701. "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
  1702. i,
  1703. src->s6_addr32[0], src->s6_addr32[1],
  1704. src->s6_addr32[2], src->s6_addr32[3], srcp,
  1705. dest->s6_addr32[0], dest->s6_addr32[1],
  1706. dest->s6_addr32[2], dest->s6_addr32[3], destp,
  1707. tw->tw_substate, 0, 0,
  1708. 3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
  1709. atomic_read(&tw->tw_refcnt), tw);
  1710. }
  1711. static int tcp6_seq_show(struct seq_file *seq, void *v)
  1712. {
  1713. struct tcp_iter_state *st;
  1714. if (v == SEQ_START_TOKEN) {
  1715. seq_puts(seq,
  1716. " sl "
  1717. "local_address "
  1718. "remote_address "
  1719. "st tx_queue rx_queue tr tm->when retrnsmt"
  1720. " uid timeout inode\n");
  1721. goto out;
  1722. }
  1723. st = seq->private;
  1724. switch (st->state) {
  1725. case TCP_SEQ_STATE_LISTENING:
  1726. case TCP_SEQ_STATE_ESTABLISHED:
  1727. get_tcp6_sock(seq, v, st->num);
  1728. break;
  1729. case TCP_SEQ_STATE_OPENREQ:
  1730. get_openreq6(seq, st->syn_wait_sk, v, st->num, st->uid);
  1731. break;
  1732. case TCP_SEQ_STATE_TIME_WAIT:
  1733. get_timewait6_sock(seq, v, st->num);
  1734. break;
  1735. }
  1736. out:
  1737. return 0;
  1738. }
  1739. static const struct file_operations tcp6_afinfo_seq_fops = {
  1740. .owner = THIS_MODULE,
  1741. .open = tcp_seq_open,
  1742. .read = seq_read,
  1743. .llseek = seq_lseek,
  1744. .release = seq_release_net
  1745. };
  1746. static struct tcp_seq_afinfo tcp6_seq_afinfo = {
  1747. .name = "tcp6",
  1748. .family = AF_INET6,
  1749. .seq_fops = &tcp6_afinfo_seq_fops,
  1750. .seq_ops = {
  1751. .show = tcp6_seq_show,
  1752. },
  1753. };
  1754. int __net_init tcp6_proc_init(struct net *net)
  1755. {
  1756. return tcp_proc_register(net, &tcp6_seq_afinfo);
  1757. }
  1758. void tcp6_proc_exit(struct net *net)
  1759. {
  1760. tcp_proc_unregister(net, &tcp6_seq_afinfo);
  1761. }
  1762. #endif
  1763. static void tcp_v6_clear_sk(struct sock *sk, int size)
  1764. {
  1765. struct inet_sock *inet = inet_sk(sk);
  1766. /* we do not want to clear pinet6 field, because of RCU lookups */
  1767. sk_prot_clear_nulls(sk, offsetof(struct inet_sock, pinet6));
  1768. size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6);
  1769. memset(&inet->pinet6 + 1, 0, size);
  1770. }
  1771. struct proto tcpv6_prot = {
  1772. .name = "TCPv6",
  1773. .owner = THIS_MODULE,
  1774. .close = tcp_close,
  1775. .connect = tcp_v6_connect,
  1776. .disconnect = tcp_disconnect,
  1777. .accept = inet_csk_accept,
  1778. .ioctl = tcp_ioctl,
  1779. .init = tcp_v6_init_sock,
  1780. .destroy = tcp_v6_destroy_sock,
  1781. .shutdown = tcp_shutdown,
  1782. .setsockopt = tcp_setsockopt,
  1783. .getsockopt = tcp_getsockopt,
  1784. .recvmsg = tcp_recvmsg,
  1785. .sendmsg = tcp_sendmsg,
  1786. .sendpage = tcp_sendpage,
  1787. .backlog_rcv = tcp_v6_do_rcv,
  1788. .hash = tcp_v6_hash,
  1789. .unhash = inet_unhash,
  1790. .get_port = inet_csk_get_port,
  1791. .enter_memory_pressure = tcp_enter_memory_pressure,
  1792. .sockets_allocated = &tcp_sockets_allocated,
  1793. .memory_allocated = &tcp_memory_allocated,
  1794. .memory_pressure = &tcp_memory_pressure,
  1795. .orphan_count = &tcp_orphan_count,
  1796. .sysctl_wmem = sysctl_tcp_wmem,
  1797. .sysctl_rmem = sysctl_tcp_rmem,
  1798. .max_header = MAX_TCP_HEADER,
  1799. .obj_size = sizeof(struct tcp6_sock),
  1800. .slab_flags = SLAB_DESTROY_BY_RCU,
  1801. .twsk_prot = &tcp6_timewait_sock_ops,
  1802. .rsk_prot = &tcp6_request_sock_ops,
  1803. .h.hashinfo = &tcp_hashinfo,
  1804. .no_autobind = true,
  1805. #ifdef CONFIG_COMPAT
  1806. .compat_setsockopt = compat_tcp_setsockopt,
  1807. .compat_getsockopt = compat_tcp_getsockopt,
  1808. #endif
  1809. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
  1810. .proto_cgroup = tcp_proto_cgroup,
  1811. #endif
  1812. .clear_sk = tcp_v6_clear_sk,
  1813. .diag_destroy = tcp_abort,
  1814. };
  1815. static const struct inet6_protocol tcpv6_protocol = {
  1816. .handler = tcp_v6_rcv,
  1817. .err_handler = tcp_v6_err,
  1818. .gso_send_check = tcp_v6_gso_send_check,
  1819. .gso_segment = tcp_tso_segment,
  1820. .gro_receive = tcp6_gro_receive,
  1821. .gro_complete = tcp6_gro_complete,
  1822. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1823. };
  1824. static struct inet_protosw tcpv6_protosw = {
  1825. .type = SOCK_STREAM,
  1826. .protocol = IPPROTO_TCP,
  1827. .prot = &tcpv6_prot,
  1828. .ops = &inet6_stream_ops,
  1829. .no_check = 0,
  1830. .flags = INET_PROTOSW_PERMANENT |
  1831. INET_PROTOSW_ICSK,
  1832. };
  1833. static int __net_init tcpv6_net_init(struct net *net)
  1834. {
  1835. return inet_ctl_sock_create(&net->ipv6.tcp_sk, PF_INET6,
  1836. SOCK_RAW, IPPROTO_TCP, net);
  1837. }
  1838. static void __net_exit tcpv6_net_exit(struct net *net)
  1839. {
  1840. inet_ctl_sock_destroy(net->ipv6.tcp_sk);
  1841. }
  1842. static void __net_exit tcpv6_net_exit_batch(struct list_head *net_exit_list)
  1843. {
  1844. inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET6);
  1845. }
  1846. static struct pernet_operations tcpv6_net_ops = {
  1847. .init = tcpv6_net_init,
  1848. .exit = tcpv6_net_exit,
  1849. .exit_batch = tcpv6_net_exit_batch,
  1850. };
  1851. int __init tcpv6_init(void)
  1852. {
  1853. int ret;
  1854. ret = inet6_add_protocol(&tcpv6_protocol, IPPROTO_TCP);
  1855. if (ret)
  1856. goto out;
  1857. /* register inet6 protocol */
  1858. ret = inet6_register_protosw(&tcpv6_protosw);
  1859. if (ret)
  1860. goto out_tcpv6_protocol;
  1861. ret = register_pernet_subsys(&tcpv6_net_ops);
  1862. if (ret)
  1863. goto out_tcpv6_protosw;
  1864. out:
  1865. return ret;
  1866. out_tcpv6_protocol:
  1867. inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);
  1868. out_tcpv6_protosw:
  1869. inet6_unregister_protosw(&tcpv6_protosw);
  1870. goto out;
  1871. }
  1872. void tcpv6_exit(void)
  1873. {
  1874. unregister_pernet_subsys(&tcpv6_net_ops);
  1875. inet6_unregister_protosw(&tcpv6_protosw);
  1876. inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);
  1877. }