tcp_ipv6.c 49 KB

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