tcp_ipv6.c 52 KB

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