tcp_ipv6.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * TCP over IPv6
  4. * Linux INET6 implementation
  5. *
  6. * Authors:
  7. * Pedro Roque <[email protected]>
  8. *
  9. * Based on:
  10. * linux/net/ipv4/tcp.c
  11. * linux/net/ipv4/tcp_input.c
  12. * linux/net/ipv4/tcp_output.c
  13. *
  14. * Fixes:
  15. * Hideaki YOSHIFUJI : sin6_scope_id support
  16. * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
  17. * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
  18. * a single port at the same time.
  19. * YOSHIFUJI Hideaki @USAGI: convert /proc/net/tcp6 to seq_file.
  20. */
  21. #include <linux/bottom_half.h>
  22. #include <linux/module.h>
  23. #include <linux/errno.h>
  24. #include <linux/types.h>
  25. #include <linux/socket.h>
  26. #include <linux/sockios.h>
  27. #include <linux/net.h>
  28. #include <linux/jiffies.h>
  29. #include <linux/in.h>
  30. #include <linux/in6.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/init.h>
  33. #include <linux/jhash.h>
  34. #include <linux/ipsec.h>
  35. #include <linux/times.h>
  36. #include <linux/slab.h>
  37. #include <linux/uaccess.h>
  38. #include <linux/ipv6.h>
  39. #include <linux/icmpv6.h>
  40. #include <linux/random.h>
  41. #include <linux/indirect_call_wrapper.h>
  42. #include <net/tcp.h>
  43. #include <net/ndisc.h>
  44. #include <net/inet6_hashtables.h>
  45. #include <net/inet6_connection_sock.h>
  46. #include <net/ipv6.h>
  47. #include <net/transp_v6.h>
  48. #include <net/addrconf.h>
  49. #include <net/ip6_route.h>
  50. #include <net/ip6_checksum.h>
  51. #include <net/inet_ecn.h>
  52. #include <net/protocol.h>
  53. #include <net/xfrm.h>
  54. #include <net/snmp.h>
  55. #include <net/dsfield.h>
  56. #include <net/timewait_sock.h>
  57. #include <net/inet_common.h>
  58. #include <net/secure_seq.h>
  59. #include <net/busy_poll.h>
  60. #include <linux/proc_fs.h>
  61. #include <linux/seq_file.h>
  62. #include <crypto/hash.h>
  63. #include <linux/scatterlist.h>
  64. #include <trace/events/tcp.h>
  65. static void tcp_v6_send_reset(const struct sock *sk, struct sk_buff *skb);
  66. static void tcp_v6_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
  67. struct request_sock *req);
  68. INDIRECT_CALLABLE_SCOPE int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb);
  69. static const struct inet_connection_sock_af_ops ipv6_mapped;
  70. const struct inet_connection_sock_af_ops ipv6_specific;
  71. #ifdef CONFIG_TCP_MD5SIG
  72. static const struct tcp_sock_af_ops tcp_sock_ipv6_specific;
  73. static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific;
  74. #else
  75. static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(const struct sock *sk,
  76. const struct in6_addr *addr,
  77. int l3index)
  78. {
  79. return NULL;
  80. }
  81. #endif
  82. /* Helper returning the inet6 address from a given tcp socket.
  83. * It can be used in TCP stack instead of inet6_sk(sk).
  84. * This avoids a dereference and allow compiler optimizations.
  85. * It is a specialized version of inet6_sk_generic().
  86. */
  87. static struct ipv6_pinfo *tcp_inet6_sk(const struct sock *sk)
  88. {
  89. unsigned int offset = sizeof(struct tcp6_sock) - sizeof(struct ipv6_pinfo);
  90. return (struct ipv6_pinfo *)(((u8 *)sk) + offset);
  91. }
  92. static void inet6_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb)
  93. {
  94. struct dst_entry *dst = skb_dst(skb);
  95. if (dst && dst_hold_safe(dst)) {
  96. const struct rt6_info *rt = (const struct rt6_info *)dst;
  97. rcu_assign_pointer(sk->sk_rx_dst, dst);
  98. sk->sk_rx_dst_ifindex = skb->skb_iif;
  99. sk->sk_rx_dst_cookie = rt6_get_cookie(rt);
  100. }
  101. }
  102. static u32 tcp_v6_init_seq(const struct sk_buff *skb)
  103. {
  104. return secure_tcpv6_seq(ipv6_hdr(skb)->daddr.s6_addr32,
  105. ipv6_hdr(skb)->saddr.s6_addr32,
  106. tcp_hdr(skb)->dest,
  107. tcp_hdr(skb)->source);
  108. }
  109. static u32 tcp_v6_init_ts_off(const struct net *net, const struct sk_buff *skb)
  110. {
  111. return secure_tcpv6_ts_off(net, ipv6_hdr(skb)->daddr.s6_addr32,
  112. ipv6_hdr(skb)->saddr.s6_addr32);
  113. }
  114. static int tcp_v6_pre_connect(struct sock *sk, struct sockaddr *uaddr,
  115. int addr_len)
  116. {
  117. /* This check is replicated from tcp_v6_connect() and intended to
  118. * prevent BPF program called below from accessing bytes that are out
  119. * of the bound specified by user in addr_len.
  120. */
  121. if (addr_len < SIN6_LEN_RFC2133)
  122. return -EINVAL;
  123. sock_owned_by_me(sk);
  124. return BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr);
  125. }
  126. static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
  127. int addr_len)
  128. {
  129. struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
  130. struct inet_connection_sock *icsk = inet_csk(sk);
  131. struct in6_addr *saddr = NULL, *final_p, final;
  132. struct inet_timewait_death_row *tcp_death_row;
  133. struct ipv6_pinfo *np = tcp_inet6_sk(sk);
  134. struct inet_sock *inet = inet_sk(sk);
  135. struct tcp_sock *tp = tcp_sk(sk);
  136. struct net *net = sock_net(sk);
  137. struct ipv6_txoptions *opt;
  138. struct dst_entry *dst;
  139. struct flowi6 fl6;
  140. int addr_type;
  141. int err;
  142. if (addr_len < SIN6_LEN_RFC2133)
  143. return -EINVAL;
  144. if (usin->sin6_family != AF_INET6)
  145. return -EAFNOSUPPORT;
  146. memset(&fl6, 0, sizeof(fl6));
  147. if (np->sndflow) {
  148. fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
  149. IP6_ECN_flow_init(fl6.flowlabel);
  150. if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
  151. struct ip6_flowlabel *flowlabel;
  152. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  153. if (IS_ERR(flowlabel))
  154. return -EINVAL;
  155. fl6_sock_release(flowlabel);
  156. }
  157. }
  158. /*
  159. * connect() to INADDR_ANY means loopback (BSD'ism).
  160. */
  161. if (ipv6_addr_any(&usin->sin6_addr)) {
  162. if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
  163. ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
  164. &usin->sin6_addr);
  165. else
  166. usin->sin6_addr = in6addr_loopback;
  167. }
  168. addr_type = ipv6_addr_type(&usin->sin6_addr);
  169. if (addr_type & IPV6_ADDR_MULTICAST)
  170. return -ENETUNREACH;
  171. if (addr_type&IPV6_ADDR_LINKLOCAL) {
  172. if (addr_len >= sizeof(struct sockaddr_in6) &&
  173. usin->sin6_scope_id) {
  174. /* If interface is set while binding, indices
  175. * must coincide.
  176. */
  177. if (!sk_dev_equal_l3scope(sk, usin->sin6_scope_id))
  178. return -EINVAL;
  179. sk->sk_bound_dev_if = usin->sin6_scope_id;
  180. }
  181. /* Connect to link-local address requires an interface */
  182. if (!sk->sk_bound_dev_if)
  183. return -EINVAL;
  184. }
  185. if (tp->rx_opt.ts_recent_stamp &&
  186. !ipv6_addr_equal(&sk->sk_v6_daddr, &usin->sin6_addr)) {
  187. tp->rx_opt.ts_recent = 0;
  188. tp->rx_opt.ts_recent_stamp = 0;
  189. WRITE_ONCE(tp->write_seq, 0);
  190. }
  191. sk->sk_v6_daddr = usin->sin6_addr;
  192. np->flow_label = fl6.flowlabel;
  193. /*
  194. * TCP over IPv4
  195. */
  196. if (addr_type & IPV6_ADDR_MAPPED) {
  197. u32 exthdrlen = icsk->icsk_ext_hdr_len;
  198. struct sockaddr_in sin;
  199. if (ipv6_only_sock(sk))
  200. return -ENETUNREACH;
  201. sin.sin_family = AF_INET;
  202. sin.sin_port = usin->sin6_port;
  203. sin.sin_addr.s_addr = usin->sin6_addr.s6_addr32[3];
  204. /* Paired with READ_ONCE() in tcp_(get|set)sockopt() */
  205. WRITE_ONCE(icsk->icsk_af_ops, &ipv6_mapped);
  206. if (sk_is_mptcp(sk))
  207. mptcpv6_handle_mapped(sk, true);
  208. sk->sk_backlog_rcv = tcp_v4_do_rcv;
  209. #ifdef CONFIG_TCP_MD5SIG
  210. tp->af_specific = &tcp_sock_ipv6_mapped_specific;
  211. #endif
  212. err = tcp_v4_connect(sk, (struct sockaddr *)&sin, sizeof(sin));
  213. if (err) {
  214. icsk->icsk_ext_hdr_len = exthdrlen;
  215. /* Paired with READ_ONCE() in tcp_(get|set)sockopt() */
  216. WRITE_ONCE(icsk->icsk_af_ops, &ipv6_specific);
  217. if (sk_is_mptcp(sk))
  218. mptcpv6_handle_mapped(sk, false);
  219. sk->sk_backlog_rcv = tcp_v6_do_rcv;
  220. #ifdef CONFIG_TCP_MD5SIG
  221. tp->af_specific = &tcp_sock_ipv6_specific;
  222. #endif
  223. goto failure;
  224. }
  225. np->saddr = sk->sk_v6_rcv_saddr;
  226. return err;
  227. }
  228. if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr))
  229. saddr = &sk->sk_v6_rcv_saddr;
  230. fl6.flowi6_proto = IPPROTO_TCP;
  231. fl6.daddr = sk->sk_v6_daddr;
  232. fl6.saddr = saddr ? *saddr : np->saddr;
  233. fl6.flowlabel = ip6_make_flowinfo(np->tclass, np->flow_label);
  234. fl6.flowi6_oif = sk->sk_bound_dev_if;
  235. fl6.flowi6_mark = sk->sk_mark;
  236. fl6.fl6_dport = usin->sin6_port;
  237. fl6.fl6_sport = inet->inet_sport;
  238. fl6.flowi6_uid = sk->sk_uid;
  239. opt = rcu_dereference_protected(np->opt, lockdep_sock_is_held(sk));
  240. final_p = fl6_update_dst(&fl6, opt, &final);
  241. security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
  242. dst = ip6_dst_lookup_flow(net, sk, &fl6, final_p);
  243. if (IS_ERR(dst)) {
  244. err = PTR_ERR(dst);
  245. goto failure;
  246. }
  247. tcp_death_row = &sock_net(sk)->ipv4.tcp_death_row;
  248. if (!saddr) {
  249. saddr = &fl6.saddr;
  250. err = inet_bhash2_update_saddr(sk, saddr, AF_INET6);
  251. if (err)
  252. goto failure;
  253. }
  254. /* set the source address */
  255. np->saddr = *saddr;
  256. inet->inet_rcv_saddr = LOOPBACK4_IPV6;
  257. sk->sk_gso_type = SKB_GSO_TCPV6;
  258. ip6_dst_store(sk, dst, NULL, NULL);
  259. icsk->icsk_ext_hdr_len = 0;
  260. if (opt)
  261. icsk->icsk_ext_hdr_len = opt->opt_flen +
  262. opt->opt_nflen;
  263. tp->rx_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
  264. inet->inet_dport = usin->sin6_port;
  265. tcp_set_state(sk, TCP_SYN_SENT);
  266. err = inet6_hash_connect(tcp_death_row, sk);
  267. if (err)
  268. goto late_failure;
  269. sk_set_txhash(sk);
  270. if (likely(!tp->repair)) {
  271. if (!tp->write_seq)
  272. WRITE_ONCE(tp->write_seq,
  273. secure_tcpv6_seq(np->saddr.s6_addr32,
  274. sk->sk_v6_daddr.s6_addr32,
  275. inet->inet_sport,
  276. inet->inet_dport));
  277. tp->tsoffset = secure_tcpv6_ts_off(net, np->saddr.s6_addr32,
  278. sk->sk_v6_daddr.s6_addr32);
  279. }
  280. if (tcp_fastopen_defer_connect(sk, &err))
  281. return err;
  282. if (err)
  283. goto late_failure;
  284. err = tcp_connect(sk);
  285. if (err)
  286. goto late_failure;
  287. return 0;
  288. late_failure:
  289. tcp_set_state(sk, TCP_CLOSE);
  290. inet_bhash2_reset_saddr(sk);
  291. failure:
  292. inet->inet_dport = 0;
  293. sk->sk_route_caps = 0;
  294. return err;
  295. }
  296. static void tcp_v6_mtu_reduced(struct sock *sk)
  297. {
  298. struct dst_entry *dst;
  299. u32 mtu;
  300. if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
  301. return;
  302. mtu = READ_ONCE(tcp_sk(sk)->mtu_info);
  303. /* Drop requests trying to increase our current mss.
  304. * Check done in __ip6_rt_update_pmtu() is too late.
  305. */
  306. if (tcp_mtu_to_mss(sk, mtu) >= tcp_sk(sk)->mss_cache)
  307. return;
  308. dst = inet6_csk_update_pmtu(sk, mtu);
  309. if (!dst)
  310. return;
  311. if (inet_csk(sk)->icsk_pmtu_cookie > dst_mtu(dst)) {
  312. tcp_sync_mss(sk, dst_mtu(dst));
  313. tcp_simple_retransmit(sk);
  314. }
  315. }
  316. static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  317. u8 type, u8 code, int offset, __be32 info)
  318. {
  319. const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
  320. const struct tcphdr *th = (struct tcphdr *)(skb->data+offset);
  321. struct net *net = dev_net(skb->dev);
  322. struct request_sock *fastopen;
  323. struct ipv6_pinfo *np;
  324. struct tcp_sock *tp;
  325. __u32 seq, snd_una;
  326. struct sock *sk;
  327. bool fatal;
  328. int err;
  329. sk = __inet6_lookup_established(net, net->ipv4.tcp_death_row.hashinfo,
  330. &hdr->daddr, th->dest,
  331. &hdr->saddr, ntohs(th->source),
  332. skb->dev->ifindex, inet6_sdif(skb));
  333. if (!sk) {
  334. __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
  335. ICMP6_MIB_INERRORS);
  336. return -ENOENT;
  337. }
  338. if (sk->sk_state == TCP_TIME_WAIT) {
  339. inet_twsk_put(inet_twsk(sk));
  340. return 0;
  341. }
  342. seq = ntohl(th->seq);
  343. fatal = icmpv6_err_convert(type, code, &err);
  344. if (sk->sk_state == TCP_NEW_SYN_RECV) {
  345. tcp_req_err(sk, seq, fatal);
  346. return 0;
  347. }
  348. bh_lock_sock(sk);
  349. if (sock_owned_by_user(sk) && type != ICMPV6_PKT_TOOBIG)
  350. __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
  351. if (sk->sk_state == TCP_CLOSE)
  352. goto out;
  353. if (static_branch_unlikely(&ip6_min_hopcount)) {
  354. /* min_hopcount can be changed concurrently from do_ipv6_setsockopt() */
  355. if (ipv6_hdr(skb)->hop_limit < READ_ONCE(tcp_inet6_sk(sk)->min_hopcount)) {
  356. __NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
  357. goto out;
  358. }
  359. }
  360. tp = tcp_sk(sk);
  361. /* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */
  362. fastopen = rcu_dereference(tp->fastopen_rsk);
  363. snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : tp->snd_una;
  364. if (sk->sk_state != TCP_LISTEN &&
  365. !between(seq, snd_una, tp->snd_nxt)) {
  366. __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
  367. goto out;
  368. }
  369. np = tcp_inet6_sk(sk);
  370. if (type == NDISC_REDIRECT) {
  371. if (!sock_owned_by_user(sk)) {
  372. struct dst_entry *dst = __sk_dst_check(sk, np->dst_cookie);
  373. if (dst)
  374. dst->ops->redirect(dst, sk, skb);
  375. }
  376. goto out;
  377. }
  378. if (type == ICMPV6_PKT_TOOBIG) {
  379. u32 mtu = ntohl(info);
  380. /* We are not interested in TCP_LISTEN and open_requests
  381. * (SYN-ACKs send out by Linux are always <576bytes so
  382. * they should go through unfragmented).
  383. */
  384. if (sk->sk_state == TCP_LISTEN)
  385. goto out;
  386. if (!ip6_sk_accept_pmtu(sk))
  387. goto out;
  388. if (mtu < IPV6_MIN_MTU)
  389. goto out;
  390. WRITE_ONCE(tp->mtu_info, mtu);
  391. if (!sock_owned_by_user(sk))
  392. tcp_v6_mtu_reduced(sk);
  393. else if (!test_and_set_bit(TCP_MTU_REDUCED_DEFERRED,
  394. &sk->sk_tsq_flags))
  395. sock_hold(sk);
  396. goto out;
  397. }
  398. /* Might be for an request_sock */
  399. switch (sk->sk_state) {
  400. case TCP_SYN_SENT:
  401. case TCP_SYN_RECV:
  402. /* Only in fast or simultaneous open. If a fast open socket is
  403. * already accepted it is treated as a connected one below.
  404. */
  405. if (fastopen && !fastopen->sk)
  406. break;
  407. ipv6_icmp_error(sk, skb, err, th->dest, ntohl(info), (u8 *)th);
  408. if (!sock_owned_by_user(sk)) {
  409. sk->sk_err = err;
  410. sk_error_report(sk); /* Wake people up to see the error (see connect in sock.c) */
  411. tcp_done(sk);
  412. } else
  413. sk->sk_err_soft = err;
  414. goto out;
  415. case TCP_LISTEN:
  416. break;
  417. default:
  418. /* check if this ICMP message allows revert of backoff.
  419. * (see RFC 6069)
  420. */
  421. if (!fastopen && type == ICMPV6_DEST_UNREACH &&
  422. code == ICMPV6_NOROUTE)
  423. tcp_ld_RTO_revert(sk, seq);
  424. }
  425. if (!sock_owned_by_user(sk) && np->recverr) {
  426. sk->sk_err = err;
  427. sk_error_report(sk);
  428. } else
  429. sk->sk_err_soft = err;
  430. out:
  431. bh_unlock_sock(sk);
  432. sock_put(sk);
  433. return 0;
  434. }
  435. static int tcp_v6_send_synack(const struct sock *sk, struct dst_entry *dst,
  436. struct flowi *fl,
  437. struct request_sock *req,
  438. struct tcp_fastopen_cookie *foc,
  439. enum tcp_synack_type synack_type,
  440. struct sk_buff *syn_skb)
  441. {
  442. struct inet_request_sock *ireq = inet_rsk(req);
  443. struct ipv6_pinfo *np = tcp_inet6_sk(sk);
  444. struct ipv6_txoptions *opt;
  445. struct flowi6 *fl6 = &fl->u.ip6;
  446. struct sk_buff *skb;
  447. int err = -ENOMEM;
  448. u8 tclass;
  449. /* First, grab a route. */
  450. if (!dst && (dst = inet6_csk_route_req(sk, fl6, req,
  451. IPPROTO_TCP)) == NULL)
  452. goto done;
  453. skb = tcp_make_synack(sk, dst, req, foc, synack_type, syn_skb);
  454. if (skb) {
  455. __tcp_v6_send_check(skb, &ireq->ir_v6_loc_addr,
  456. &ireq->ir_v6_rmt_addr);
  457. fl6->daddr = ireq->ir_v6_rmt_addr;
  458. if (np->repflow && ireq->pktopts)
  459. fl6->flowlabel = ip6_flowlabel(ipv6_hdr(ireq->pktopts));
  460. tclass = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_reflect_tos) ?
  461. (tcp_rsk(req)->syn_tos & ~INET_ECN_MASK) |
  462. (np->tclass & INET_ECN_MASK) :
  463. np->tclass;
  464. if (!INET_ECN_is_capable(tclass) &&
  465. tcp_bpf_ca_needs_ecn((struct sock *)req))
  466. tclass |= INET_ECN_ECT_0;
  467. rcu_read_lock();
  468. opt = ireq->ipv6_opt;
  469. if (!opt)
  470. opt = rcu_dereference(np->opt);
  471. err = ip6_xmit(sk, skb, fl6, skb->mark ? : READ_ONCE(sk->sk_mark),
  472. opt, tclass, sk->sk_priority);
  473. rcu_read_unlock();
  474. err = net_xmit_eval(err);
  475. }
  476. done:
  477. return err;
  478. }
  479. static void tcp_v6_reqsk_destructor(struct request_sock *req)
  480. {
  481. kfree(inet_rsk(req)->ipv6_opt);
  482. consume_skb(inet_rsk(req)->pktopts);
  483. }
  484. #ifdef CONFIG_TCP_MD5SIG
  485. static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(const struct sock *sk,
  486. const struct in6_addr *addr,
  487. int l3index)
  488. {
  489. return tcp_md5_do_lookup(sk, l3index,
  490. (union tcp_md5_addr *)addr, AF_INET6);
  491. }
  492. static struct tcp_md5sig_key *tcp_v6_md5_lookup(const struct sock *sk,
  493. const struct sock *addr_sk)
  494. {
  495. int l3index;
  496. l3index = l3mdev_master_ifindex_by_index(sock_net(sk),
  497. addr_sk->sk_bound_dev_if);
  498. return tcp_v6_md5_do_lookup(sk, &addr_sk->sk_v6_daddr,
  499. l3index);
  500. }
  501. static int tcp_v6_parse_md5_keys(struct sock *sk, int optname,
  502. sockptr_t optval, int optlen)
  503. {
  504. struct tcp_md5sig cmd;
  505. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd.tcpm_addr;
  506. int l3index = 0;
  507. u8 prefixlen;
  508. u8 flags;
  509. if (optlen < sizeof(cmd))
  510. return -EINVAL;
  511. if (copy_from_sockptr(&cmd, optval, sizeof(cmd)))
  512. return -EFAULT;
  513. if (sin6->sin6_family != AF_INET6)
  514. return -EINVAL;
  515. flags = cmd.tcpm_flags & TCP_MD5SIG_FLAG_IFINDEX;
  516. if (optname == TCP_MD5SIG_EXT &&
  517. cmd.tcpm_flags & TCP_MD5SIG_FLAG_PREFIX) {
  518. prefixlen = cmd.tcpm_prefixlen;
  519. if (prefixlen > 128 || (ipv6_addr_v4mapped(&sin6->sin6_addr) &&
  520. prefixlen > 32))
  521. return -EINVAL;
  522. } else {
  523. prefixlen = ipv6_addr_v4mapped(&sin6->sin6_addr) ? 32 : 128;
  524. }
  525. if (optname == TCP_MD5SIG_EXT && cmd.tcpm_ifindex &&
  526. cmd.tcpm_flags & TCP_MD5SIG_FLAG_IFINDEX) {
  527. struct net_device *dev;
  528. rcu_read_lock();
  529. dev = dev_get_by_index_rcu(sock_net(sk), cmd.tcpm_ifindex);
  530. if (dev && netif_is_l3_master(dev))
  531. l3index = dev->ifindex;
  532. rcu_read_unlock();
  533. /* ok to reference set/not set outside of rcu;
  534. * right now device MUST be an L3 master
  535. */
  536. if (!dev || !l3index)
  537. return -EINVAL;
  538. }
  539. if (!cmd.tcpm_keylen) {
  540. if (ipv6_addr_v4mapped(&sin6->sin6_addr))
  541. return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr32[3],
  542. AF_INET, prefixlen,
  543. l3index, flags);
  544. return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin6->sin6_addr,
  545. AF_INET6, prefixlen, l3index, flags);
  546. }
  547. if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
  548. return -EINVAL;
  549. if (ipv6_addr_v4mapped(&sin6->sin6_addr))
  550. return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr32[3],
  551. AF_INET, prefixlen, l3index, flags,
  552. cmd.tcpm_key, cmd.tcpm_keylen,
  553. GFP_KERNEL);
  554. return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin6->sin6_addr,
  555. AF_INET6, prefixlen, l3index, flags,
  556. cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
  557. }
  558. static int tcp_v6_md5_hash_headers(struct tcp_md5sig_pool *hp,
  559. const struct in6_addr *daddr,
  560. const struct in6_addr *saddr,
  561. const struct tcphdr *th, int nbytes)
  562. {
  563. struct tcp6_pseudohdr *bp;
  564. struct scatterlist sg;
  565. struct tcphdr *_th;
  566. bp = hp->scratch;
  567. /* 1. TCP pseudo-header (RFC2460) */
  568. bp->saddr = *saddr;
  569. bp->daddr = *daddr;
  570. bp->protocol = cpu_to_be32(IPPROTO_TCP);
  571. bp->len = cpu_to_be32(nbytes);
  572. _th = (struct tcphdr *)(bp + 1);
  573. memcpy(_th, th, sizeof(*th));
  574. _th->check = 0;
  575. sg_init_one(&sg, bp, sizeof(*bp) + sizeof(*th));
  576. ahash_request_set_crypt(hp->md5_req, &sg, NULL,
  577. sizeof(*bp) + sizeof(*th));
  578. return crypto_ahash_update(hp->md5_req);
  579. }
  580. static int tcp_v6_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
  581. const struct in6_addr *daddr, struct in6_addr *saddr,
  582. const struct tcphdr *th)
  583. {
  584. struct tcp_md5sig_pool *hp;
  585. struct ahash_request *req;
  586. hp = tcp_get_md5sig_pool();
  587. if (!hp)
  588. goto clear_hash_noput;
  589. req = hp->md5_req;
  590. if (crypto_ahash_init(req))
  591. goto clear_hash;
  592. if (tcp_v6_md5_hash_headers(hp, daddr, saddr, th, th->doff << 2))
  593. goto clear_hash;
  594. if (tcp_md5_hash_key(hp, key))
  595. goto clear_hash;
  596. ahash_request_set_crypt(req, NULL, md5_hash, 0);
  597. if (crypto_ahash_final(req))
  598. goto clear_hash;
  599. tcp_put_md5sig_pool();
  600. return 0;
  601. clear_hash:
  602. tcp_put_md5sig_pool();
  603. clear_hash_noput:
  604. memset(md5_hash, 0, 16);
  605. return 1;
  606. }
  607. static int tcp_v6_md5_hash_skb(char *md5_hash,
  608. const struct tcp_md5sig_key *key,
  609. const struct sock *sk,
  610. const struct sk_buff *skb)
  611. {
  612. const struct in6_addr *saddr, *daddr;
  613. struct tcp_md5sig_pool *hp;
  614. struct ahash_request *req;
  615. const struct tcphdr *th = tcp_hdr(skb);
  616. if (sk) { /* valid for establish/request sockets */
  617. saddr = &sk->sk_v6_rcv_saddr;
  618. daddr = &sk->sk_v6_daddr;
  619. } else {
  620. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  621. saddr = &ip6h->saddr;
  622. daddr = &ip6h->daddr;
  623. }
  624. hp = tcp_get_md5sig_pool();
  625. if (!hp)
  626. goto clear_hash_noput;
  627. req = hp->md5_req;
  628. if (crypto_ahash_init(req))
  629. goto clear_hash;
  630. if (tcp_v6_md5_hash_headers(hp, daddr, saddr, th, skb->len))
  631. goto clear_hash;
  632. if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2))
  633. goto clear_hash;
  634. if (tcp_md5_hash_key(hp, key))
  635. goto clear_hash;
  636. ahash_request_set_crypt(req, NULL, md5_hash, 0);
  637. if (crypto_ahash_final(req))
  638. goto clear_hash;
  639. tcp_put_md5sig_pool();
  640. return 0;
  641. clear_hash:
  642. tcp_put_md5sig_pool();
  643. clear_hash_noput:
  644. memset(md5_hash, 0, 16);
  645. return 1;
  646. }
  647. #endif
  648. static void tcp_v6_init_req(struct request_sock *req,
  649. const struct sock *sk_listener,
  650. struct sk_buff *skb)
  651. {
  652. bool l3_slave = ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags);
  653. struct inet_request_sock *ireq = inet_rsk(req);
  654. const struct ipv6_pinfo *np = tcp_inet6_sk(sk_listener);
  655. ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
  656. ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
  657. /* So that link locals have meaning */
  658. if ((!sk_listener->sk_bound_dev_if || l3_slave) &&
  659. ipv6_addr_type(&ireq->ir_v6_rmt_addr) & IPV6_ADDR_LINKLOCAL)
  660. ireq->ir_iif = tcp_v6_iif(skb);
  661. if (!TCP_SKB_CB(skb)->tcp_tw_isn &&
  662. (ipv6_opt_accepted(sk_listener, skb, &TCP_SKB_CB(skb)->header.h6) ||
  663. np->rxopt.bits.rxinfo ||
  664. np->rxopt.bits.rxoinfo || np->rxopt.bits.rxhlim ||
  665. np->rxopt.bits.rxohlim || np->repflow)) {
  666. refcount_inc(&skb->users);
  667. ireq->pktopts = skb;
  668. }
  669. }
  670. static struct dst_entry *tcp_v6_route_req(const struct sock *sk,
  671. struct sk_buff *skb,
  672. struct flowi *fl,
  673. struct request_sock *req)
  674. {
  675. tcp_v6_init_req(req, sk, skb);
  676. if (security_inet_conn_request(sk, skb, req))
  677. return NULL;
  678. return inet6_csk_route_req(sk, &fl->u.ip6, req, IPPROTO_TCP);
  679. }
  680. struct request_sock_ops tcp6_request_sock_ops __read_mostly = {
  681. .family = AF_INET6,
  682. .obj_size = sizeof(struct tcp6_request_sock),
  683. .rtx_syn_ack = tcp_rtx_synack,
  684. .send_ack = tcp_v6_reqsk_send_ack,
  685. .destructor = tcp_v6_reqsk_destructor,
  686. .send_reset = tcp_v6_send_reset,
  687. .syn_ack_timeout = tcp_syn_ack_timeout,
  688. };
  689. const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops = {
  690. .mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) -
  691. sizeof(struct ipv6hdr),
  692. #ifdef CONFIG_TCP_MD5SIG
  693. .req_md5_lookup = tcp_v6_md5_lookup,
  694. .calc_md5_hash = tcp_v6_md5_hash_skb,
  695. #endif
  696. #ifdef CONFIG_SYN_COOKIES
  697. .cookie_init_seq = cookie_v6_init_sequence,
  698. #endif
  699. .route_req = tcp_v6_route_req,
  700. .init_seq = tcp_v6_init_seq,
  701. .init_ts_off = tcp_v6_init_ts_off,
  702. .send_synack = tcp_v6_send_synack,
  703. };
  704. static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32 seq,
  705. u32 ack, u32 win, u32 tsval, u32 tsecr,
  706. int oif, struct tcp_md5sig_key *key, int rst,
  707. u8 tclass, __be32 label, u32 priority, u32 txhash)
  708. {
  709. const struct tcphdr *th = tcp_hdr(skb);
  710. struct tcphdr *t1;
  711. struct sk_buff *buff;
  712. struct flowi6 fl6;
  713. struct net *net = sk ? sock_net(sk) : dev_net(skb_dst(skb)->dev);
  714. struct sock *ctl_sk = net->ipv6.tcp_sk;
  715. unsigned int tot_len = sizeof(struct tcphdr);
  716. __be32 mrst = 0, *topt;
  717. struct dst_entry *dst;
  718. __u32 mark = 0;
  719. if (tsecr)
  720. tot_len += TCPOLEN_TSTAMP_ALIGNED;
  721. #ifdef CONFIG_TCP_MD5SIG
  722. if (key)
  723. tot_len += TCPOLEN_MD5SIG_ALIGNED;
  724. #endif
  725. #ifdef CONFIG_MPTCP
  726. if (rst && !key) {
  727. mrst = mptcp_reset_option(skb);
  728. if (mrst)
  729. tot_len += sizeof(__be32);
  730. }
  731. #endif
  732. buff = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
  733. if (!buff)
  734. return;
  735. skb_reserve(buff, MAX_TCP_HEADER);
  736. t1 = skb_push(buff, tot_len);
  737. skb_reset_transport_header(buff);
  738. /* Swap the send and the receive. */
  739. memset(t1, 0, sizeof(*t1));
  740. t1->dest = th->source;
  741. t1->source = th->dest;
  742. t1->doff = tot_len / 4;
  743. t1->seq = htonl(seq);
  744. t1->ack_seq = htonl(ack);
  745. t1->ack = !rst || !th->ack;
  746. t1->rst = rst;
  747. t1->window = htons(win);
  748. topt = (__be32 *)(t1 + 1);
  749. if (tsecr) {
  750. *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  751. (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
  752. *topt++ = htonl(tsval);
  753. *topt++ = htonl(tsecr);
  754. }
  755. if (mrst)
  756. *topt++ = mrst;
  757. #ifdef CONFIG_TCP_MD5SIG
  758. if (key) {
  759. *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  760. (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
  761. tcp_v6_md5_hash_hdr((__u8 *)topt, key,
  762. &ipv6_hdr(skb)->saddr,
  763. &ipv6_hdr(skb)->daddr, t1);
  764. }
  765. #endif
  766. memset(&fl6, 0, sizeof(fl6));
  767. fl6.daddr = ipv6_hdr(skb)->saddr;
  768. fl6.saddr = ipv6_hdr(skb)->daddr;
  769. fl6.flowlabel = label;
  770. buff->ip_summed = CHECKSUM_PARTIAL;
  771. __tcp_v6_send_check(buff, &fl6.saddr, &fl6.daddr);
  772. fl6.flowi6_proto = IPPROTO_TCP;
  773. if (rt6_need_strict(&fl6.daddr) && !oif)
  774. fl6.flowi6_oif = tcp_v6_iif(skb);
  775. else {
  776. if (!oif && netif_index_is_l3_master(net, skb->skb_iif))
  777. oif = skb->skb_iif;
  778. fl6.flowi6_oif = oif;
  779. }
  780. if (sk) {
  781. if (sk->sk_state == TCP_TIME_WAIT)
  782. mark = inet_twsk(sk)->tw_mark;
  783. else
  784. mark = READ_ONCE(sk->sk_mark);
  785. skb_set_delivery_time(buff, tcp_transmit_time(sk), true);
  786. }
  787. if (txhash) {
  788. /* autoflowlabel/skb_get_hash_flowi6 rely on buff->hash */
  789. skb_set_hash(buff, txhash, PKT_HASH_TYPE_L4);
  790. }
  791. fl6.flowi6_mark = IP6_REPLY_MARK(net, skb->mark) ?: mark;
  792. fl6.fl6_dport = t1->dest;
  793. fl6.fl6_sport = t1->source;
  794. fl6.flowi6_uid = sock_net_uid(net, sk && sk_fullsock(sk) ? sk : NULL);
  795. security_skb_classify_flow(skb, flowi6_to_flowi_common(&fl6));
  796. /* Pass a socket to ip6_dst_lookup either it is for RST
  797. * Underlying function will use this to retrieve the network
  798. * namespace
  799. */
  800. if (sk && sk->sk_state != TCP_TIME_WAIT)
  801. dst = ip6_dst_lookup_flow(net, sk, &fl6, NULL); /*sk's xfrm_policy can be referred*/
  802. else
  803. dst = ip6_dst_lookup_flow(net, ctl_sk, &fl6, NULL);
  804. if (!IS_ERR(dst)) {
  805. skb_dst_set(buff, dst);
  806. ip6_xmit(ctl_sk, buff, &fl6, fl6.flowi6_mark, NULL,
  807. tclass & ~INET_ECN_MASK, priority);
  808. TCP_INC_STATS(net, TCP_MIB_OUTSEGS);
  809. if (rst)
  810. TCP_INC_STATS(net, TCP_MIB_OUTRSTS);
  811. return;
  812. }
  813. kfree_skb(buff);
  814. }
  815. static void tcp_v6_send_reset(const struct sock *sk, struct sk_buff *skb)
  816. {
  817. const struct tcphdr *th = tcp_hdr(skb);
  818. struct ipv6hdr *ipv6h = ipv6_hdr(skb);
  819. u32 seq = 0, ack_seq = 0;
  820. struct tcp_md5sig_key *key = NULL;
  821. #ifdef CONFIG_TCP_MD5SIG
  822. const __u8 *hash_location = NULL;
  823. unsigned char newhash[16];
  824. int genhash;
  825. struct sock *sk1 = NULL;
  826. #endif
  827. __be32 label = 0;
  828. u32 priority = 0;
  829. struct net *net;
  830. u32 txhash = 0;
  831. int oif = 0;
  832. if (th->rst)
  833. return;
  834. /* If sk not NULL, it means we did a successful lookup and incoming
  835. * route had to be correct. prequeue might have dropped our dst.
  836. */
  837. if (!sk && !ipv6_unicast_destination(skb))
  838. return;
  839. net = sk ? sock_net(sk) : dev_net(skb_dst(skb)->dev);
  840. #ifdef CONFIG_TCP_MD5SIG
  841. rcu_read_lock();
  842. hash_location = tcp_parse_md5sig_option(th);
  843. if (sk && sk_fullsock(sk)) {
  844. int l3index;
  845. /* sdif set, means packet ingressed via a device
  846. * in an L3 domain and inet_iif is set to it.
  847. */
  848. l3index = tcp_v6_sdif(skb) ? tcp_v6_iif_l3_slave(skb) : 0;
  849. key = tcp_v6_md5_do_lookup(sk, &ipv6h->saddr, l3index);
  850. } else if (hash_location) {
  851. int dif = tcp_v6_iif_l3_slave(skb);
  852. int sdif = tcp_v6_sdif(skb);
  853. int l3index;
  854. /*
  855. * active side is lost. Try to find listening socket through
  856. * source port, and then find md5 key through listening socket.
  857. * we are not loose security here:
  858. * Incoming packet is checked with md5 hash with finding key,
  859. * no RST generated if md5 hash doesn't match.
  860. */
  861. sk1 = inet6_lookup_listener(net, net->ipv4.tcp_death_row.hashinfo,
  862. NULL, 0, &ipv6h->saddr, th->source,
  863. &ipv6h->daddr, ntohs(th->source),
  864. dif, sdif);
  865. if (!sk1)
  866. goto out;
  867. /* sdif set, means packet ingressed via a device
  868. * in an L3 domain and dif is set to it.
  869. */
  870. l3index = tcp_v6_sdif(skb) ? dif : 0;
  871. key = tcp_v6_md5_do_lookup(sk1, &ipv6h->saddr, l3index);
  872. if (!key)
  873. goto out;
  874. genhash = tcp_v6_md5_hash_skb(newhash, key, NULL, skb);
  875. if (genhash || memcmp(hash_location, newhash, 16) != 0)
  876. goto out;
  877. }
  878. #endif
  879. if (th->ack)
  880. seq = ntohl(th->ack_seq);
  881. else
  882. ack_seq = ntohl(th->seq) + th->syn + th->fin + skb->len -
  883. (th->doff << 2);
  884. if (sk) {
  885. oif = sk->sk_bound_dev_if;
  886. if (sk_fullsock(sk)) {
  887. const struct ipv6_pinfo *np = tcp_inet6_sk(sk);
  888. trace_tcp_send_reset(sk, skb);
  889. if (np->repflow)
  890. label = ip6_flowlabel(ipv6h);
  891. priority = sk->sk_priority;
  892. txhash = sk->sk_txhash;
  893. }
  894. if (sk->sk_state == TCP_TIME_WAIT) {
  895. label = cpu_to_be32(inet_twsk(sk)->tw_flowlabel);
  896. priority = inet_twsk(sk)->tw_priority;
  897. txhash = inet_twsk(sk)->tw_txhash;
  898. }
  899. } else {
  900. if (net->ipv6.sysctl.flowlabel_reflect & FLOWLABEL_REFLECT_TCP_RESET)
  901. label = ip6_flowlabel(ipv6h);
  902. }
  903. tcp_v6_send_response(sk, skb, seq, ack_seq, 0, 0, 0, oif, key, 1,
  904. ipv6_get_dsfield(ipv6h), label, priority, txhash);
  905. #ifdef CONFIG_TCP_MD5SIG
  906. out:
  907. rcu_read_unlock();
  908. #endif
  909. }
  910. static void tcp_v6_send_ack(const struct sock *sk, struct sk_buff *skb, u32 seq,
  911. u32 ack, u32 win, u32 tsval, u32 tsecr, int oif,
  912. struct tcp_md5sig_key *key, u8 tclass,
  913. __be32 label, u32 priority, u32 txhash)
  914. {
  915. tcp_v6_send_response(sk, skb, seq, ack, win, tsval, tsecr, oif, key, 0,
  916. tclass, label, priority, txhash);
  917. }
  918. static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb)
  919. {
  920. struct inet_timewait_sock *tw = inet_twsk(sk);
  921. struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
  922. tcp_v6_send_ack(sk, skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
  923. tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
  924. tcp_time_stamp_raw() + tcptw->tw_ts_offset,
  925. tcptw->tw_ts_recent, tw->tw_bound_dev_if, tcp_twsk_md5_key(tcptw),
  926. tw->tw_tclass, cpu_to_be32(tw->tw_flowlabel), tw->tw_priority,
  927. tw->tw_txhash);
  928. inet_twsk_put(tw);
  929. }
  930. static void tcp_v6_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
  931. struct request_sock *req)
  932. {
  933. int l3index;
  934. l3index = tcp_v6_sdif(skb) ? tcp_v6_iif_l3_slave(skb) : 0;
  935. /* sk->sk_state == TCP_LISTEN -> for regular TCP_SYN_RECV
  936. * sk->sk_state == TCP_SYN_RECV -> for Fast Open.
  937. */
  938. /* RFC 7323 2.3
  939. * The window field (SEG.WND) of every outgoing segment, with the
  940. * exception of <SYN> segments, MUST be right-shifted by
  941. * Rcv.Wind.Shift bits:
  942. */
  943. tcp_v6_send_ack(sk, skb, (sk->sk_state == TCP_LISTEN) ?
  944. tcp_rsk(req)->snt_isn + 1 : tcp_sk(sk)->snd_nxt,
  945. tcp_rsk(req)->rcv_nxt,
  946. req->rsk_rcv_wnd >> inet_rsk(req)->rcv_wscale,
  947. tcp_time_stamp_raw() + tcp_rsk(req)->ts_off,
  948. READ_ONCE(req->ts_recent), sk->sk_bound_dev_if,
  949. tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->saddr, l3index),
  950. ipv6_get_dsfield(ipv6_hdr(skb)), 0,
  951. READ_ONCE(sk->sk_priority),
  952. READ_ONCE(tcp_rsk(req)->txhash));
  953. }
  954. static struct sock *tcp_v6_cookie_check(struct sock *sk, struct sk_buff *skb)
  955. {
  956. #ifdef CONFIG_SYN_COOKIES
  957. const struct tcphdr *th = tcp_hdr(skb);
  958. if (!th->syn)
  959. sk = cookie_v6_check(sk, skb);
  960. #endif
  961. return sk;
  962. }
  963. u16 tcp_v6_get_syncookie(struct sock *sk, struct ipv6hdr *iph,
  964. struct tcphdr *th, u32 *cookie)
  965. {
  966. u16 mss = 0;
  967. #ifdef CONFIG_SYN_COOKIES
  968. mss = tcp_get_syncookie_mss(&tcp6_request_sock_ops,
  969. &tcp_request_sock_ipv6_ops, sk, th);
  970. if (mss) {
  971. *cookie = __cookie_v6_init_sequence(iph, th, &mss);
  972. tcp_synq_overflow(sk);
  973. }
  974. #endif
  975. return mss;
  976. }
  977. static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
  978. {
  979. if (skb->protocol == htons(ETH_P_IP))
  980. return tcp_v4_conn_request(sk, skb);
  981. if (!ipv6_unicast_destination(skb))
  982. goto drop;
  983. if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) {
  984. __IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS);
  985. return 0;
  986. }
  987. return tcp_conn_request(&tcp6_request_sock_ops,
  988. &tcp_request_sock_ipv6_ops, sk, skb);
  989. drop:
  990. tcp_listendrop(sk);
  991. return 0; /* don't send reset */
  992. }
  993. static void tcp_v6_restore_cb(struct sk_buff *skb)
  994. {
  995. /* We need to move header back to the beginning if xfrm6_policy_check()
  996. * and tcp_v6_fill_cb() are going to be called again.
  997. * ip6_datagram_recv_specific_ctl() also expects IP6CB to be there.
  998. */
  999. memmove(IP6CB(skb), &TCP_SKB_CB(skb)->header.h6,
  1000. sizeof(struct inet6_skb_parm));
  1001. }
  1002. static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
  1003. struct request_sock *req,
  1004. struct dst_entry *dst,
  1005. struct request_sock *req_unhash,
  1006. bool *own_req)
  1007. {
  1008. struct inet_request_sock *ireq;
  1009. struct ipv6_pinfo *newnp;
  1010. const struct ipv6_pinfo *np = tcp_inet6_sk(sk);
  1011. struct ipv6_txoptions *opt;
  1012. struct inet_sock *newinet;
  1013. bool found_dup_sk = false;
  1014. struct tcp_sock *newtp;
  1015. struct sock *newsk;
  1016. #ifdef CONFIG_TCP_MD5SIG
  1017. struct tcp_md5sig_key *key;
  1018. int l3index;
  1019. #endif
  1020. struct flowi6 fl6;
  1021. if (skb->protocol == htons(ETH_P_IP)) {
  1022. /*
  1023. * v6 mapped
  1024. */
  1025. newsk = tcp_v4_syn_recv_sock(sk, skb, req, dst,
  1026. req_unhash, own_req);
  1027. if (!newsk)
  1028. return NULL;
  1029. inet_sk(newsk)->pinet6 = tcp_inet6_sk(newsk);
  1030. newnp = tcp_inet6_sk(newsk);
  1031. newtp = tcp_sk(newsk);
  1032. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  1033. newnp->saddr = newsk->sk_v6_rcv_saddr;
  1034. inet_csk(newsk)->icsk_af_ops = &ipv6_mapped;
  1035. if (sk_is_mptcp(newsk))
  1036. mptcpv6_handle_mapped(newsk, true);
  1037. newsk->sk_backlog_rcv = tcp_v4_do_rcv;
  1038. #ifdef CONFIG_TCP_MD5SIG
  1039. newtp->af_specific = &tcp_sock_ipv6_mapped_specific;
  1040. #endif
  1041. newnp->ipv6_mc_list = NULL;
  1042. newnp->ipv6_ac_list = NULL;
  1043. newnp->ipv6_fl_list = NULL;
  1044. newnp->pktoptions = NULL;
  1045. newnp->opt = NULL;
  1046. newnp->mcast_oif = inet_iif(skb);
  1047. newnp->mcast_hops = ip_hdr(skb)->ttl;
  1048. newnp->rcv_flowinfo = 0;
  1049. if (np->repflow)
  1050. newnp->flow_label = 0;
  1051. /*
  1052. * No need to charge this sock to the relevant IPv6 refcnt debug socks count
  1053. * here, tcp_create_openreq_child now does this for us, see the comment in
  1054. * that function for the gory details. -acme
  1055. */
  1056. /* It is tricky place. Until this moment IPv4 tcp
  1057. worked with IPv6 icsk.icsk_af_ops.
  1058. Sync it now.
  1059. */
  1060. tcp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
  1061. return newsk;
  1062. }
  1063. ireq = inet_rsk(req);
  1064. if (sk_acceptq_is_full(sk))
  1065. goto out_overflow;
  1066. if (!dst) {
  1067. dst = inet6_csk_route_req(sk, &fl6, req, IPPROTO_TCP);
  1068. if (!dst)
  1069. goto out;
  1070. }
  1071. newsk = tcp_create_openreq_child(sk, req, skb);
  1072. if (!newsk)
  1073. goto out_nonewsk;
  1074. /*
  1075. * No need to charge this sock to the relevant IPv6 refcnt debug socks
  1076. * count here, tcp_create_openreq_child now does this for us, see the
  1077. * comment in that function for the gory details. -acme
  1078. */
  1079. newsk->sk_gso_type = SKB_GSO_TCPV6;
  1080. ip6_dst_store(newsk, dst, NULL, NULL);
  1081. inet6_sk_rx_dst_set(newsk, skb);
  1082. inet_sk(newsk)->pinet6 = tcp_inet6_sk(newsk);
  1083. newtp = tcp_sk(newsk);
  1084. newinet = inet_sk(newsk);
  1085. newnp = tcp_inet6_sk(newsk);
  1086. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  1087. newsk->sk_v6_daddr = ireq->ir_v6_rmt_addr;
  1088. newnp->saddr = ireq->ir_v6_loc_addr;
  1089. newsk->sk_v6_rcv_saddr = ireq->ir_v6_loc_addr;
  1090. newsk->sk_bound_dev_if = ireq->ir_iif;
  1091. /* Now IPv6 options...
  1092. First: no IPv4 options.
  1093. */
  1094. newinet->inet_opt = NULL;
  1095. newnp->ipv6_mc_list = NULL;
  1096. newnp->ipv6_ac_list = NULL;
  1097. newnp->ipv6_fl_list = NULL;
  1098. /* Clone RX bits */
  1099. newnp->rxopt.all = np->rxopt.all;
  1100. newnp->pktoptions = NULL;
  1101. newnp->opt = NULL;
  1102. newnp->mcast_oif = tcp_v6_iif(skb);
  1103. newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
  1104. newnp->rcv_flowinfo = ip6_flowinfo(ipv6_hdr(skb));
  1105. if (np->repflow)
  1106. newnp->flow_label = ip6_flowlabel(ipv6_hdr(skb));
  1107. /* Set ToS of the new socket based upon the value of incoming SYN.
  1108. * ECT bits are set later in tcp_init_transfer().
  1109. */
  1110. if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_reflect_tos))
  1111. newnp->tclass = tcp_rsk(req)->syn_tos & ~INET_ECN_MASK;
  1112. /* Clone native IPv6 options from listening socket (if any)
  1113. Yes, keeping reference count would be much more clever,
  1114. but we make one more one thing there: reattach optmem
  1115. to newsk.
  1116. */
  1117. opt = ireq->ipv6_opt;
  1118. if (!opt)
  1119. opt = rcu_dereference(np->opt);
  1120. if (opt) {
  1121. opt = ipv6_dup_options(newsk, opt);
  1122. RCU_INIT_POINTER(newnp->opt, opt);
  1123. }
  1124. inet_csk(newsk)->icsk_ext_hdr_len = 0;
  1125. if (opt)
  1126. inet_csk(newsk)->icsk_ext_hdr_len = opt->opt_nflen +
  1127. opt->opt_flen;
  1128. tcp_ca_openreq_child(newsk, dst);
  1129. tcp_sync_mss(newsk, dst_mtu(dst));
  1130. newtp->advmss = tcp_mss_clamp(tcp_sk(sk), dst_metric_advmss(dst));
  1131. tcp_initialize_rcv_mss(newsk);
  1132. newinet->inet_daddr = newinet->inet_saddr = LOOPBACK4_IPV6;
  1133. newinet->inet_rcv_saddr = LOOPBACK4_IPV6;
  1134. #ifdef CONFIG_TCP_MD5SIG
  1135. l3index = l3mdev_master_ifindex_by_index(sock_net(sk), ireq->ir_iif);
  1136. /* Copy over the MD5 key from the original socket */
  1137. key = tcp_v6_md5_do_lookup(sk, &newsk->sk_v6_daddr, l3index);
  1138. if (key) {
  1139. /* We're using one, so create a matching key
  1140. * on the newsk structure. If we fail to get
  1141. * memory, then we end up not copying the key
  1142. * across. Shucks.
  1143. */
  1144. tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newsk->sk_v6_daddr,
  1145. AF_INET6, 128, l3index, key->flags, key->key, key->keylen,
  1146. sk_gfp_mask(sk, GFP_ATOMIC));
  1147. }
  1148. #endif
  1149. if (__inet_inherit_port(sk, newsk) < 0) {
  1150. inet_csk_prepare_forced_close(newsk);
  1151. tcp_done(newsk);
  1152. goto out;
  1153. }
  1154. *own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash),
  1155. &found_dup_sk);
  1156. if (*own_req) {
  1157. tcp_move_syn(newtp, req);
  1158. /* Clone pktoptions received with SYN, if we own the req */
  1159. if (ireq->pktopts) {
  1160. newnp->pktoptions = skb_clone_and_charge_r(ireq->pktopts, newsk);
  1161. consume_skb(ireq->pktopts);
  1162. ireq->pktopts = NULL;
  1163. if (newnp->pktoptions)
  1164. tcp_v6_restore_cb(newnp->pktoptions);
  1165. }
  1166. } else {
  1167. if (!req_unhash && found_dup_sk) {
  1168. /* This code path should only be executed in the
  1169. * syncookie case only
  1170. */
  1171. bh_unlock_sock(newsk);
  1172. sock_put(newsk);
  1173. newsk = NULL;
  1174. }
  1175. }
  1176. return newsk;
  1177. out_overflow:
  1178. __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
  1179. out_nonewsk:
  1180. dst_release(dst);
  1181. out:
  1182. tcp_listendrop(sk);
  1183. return NULL;
  1184. }
  1185. INDIRECT_CALLABLE_DECLARE(struct dst_entry *ipv4_dst_check(struct dst_entry *,
  1186. u32));
  1187. /* The socket must have it's spinlock held when we get
  1188. * here, unless it is a TCP_LISTEN socket.
  1189. *
  1190. * We have a potential double-lock case here, so even when
  1191. * doing backlog processing we use the BH locking scheme.
  1192. * This is because we cannot sleep with the original spinlock
  1193. * held.
  1194. */
  1195. INDIRECT_CALLABLE_SCOPE
  1196. int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
  1197. {
  1198. struct ipv6_pinfo *np = tcp_inet6_sk(sk);
  1199. struct sk_buff *opt_skb = NULL;
  1200. enum skb_drop_reason reason;
  1201. struct tcp_sock *tp;
  1202. /* Imagine: socket is IPv6. IPv4 packet arrives,
  1203. goes to IPv4 receive handler and backlogged.
  1204. From backlog it always goes here. Kerboom...
  1205. Fortunately, tcp_rcv_established and rcv_established
  1206. handle them correctly, but it is not case with
  1207. tcp_v6_hnd_req and tcp_v6_send_reset(). --ANK
  1208. */
  1209. if (skb->protocol == htons(ETH_P_IP))
  1210. return tcp_v4_do_rcv(sk, skb);
  1211. /*
  1212. * socket locking is here for SMP purposes as backlog rcv
  1213. * is currently called with bh processing disabled.
  1214. */
  1215. /* Do Stevens' IPV6_PKTOPTIONS.
  1216. Yes, guys, it is the only place in our code, where we
  1217. may make it not affecting IPv4.
  1218. The rest of code is protocol independent,
  1219. and I do not like idea to uglify IPv4.
  1220. Actually, all the idea behind IPV6_PKTOPTIONS
  1221. looks not very well thought. For now we latch
  1222. options, received in the last packet, enqueued
  1223. by tcp. Feel free to propose better solution.
  1224. --ANK (980728)
  1225. */
  1226. if (np->rxopt.all)
  1227. opt_skb = skb_clone_and_charge_r(skb, sk);
  1228. reason = SKB_DROP_REASON_NOT_SPECIFIED;
  1229. if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
  1230. struct dst_entry *dst;
  1231. dst = rcu_dereference_protected(sk->sk_rx_dst,
  1232. lockdep_sock_is_held(sk));
  1233. sock_rps_save_rxhash(sk, skb);
  1234. sk_mark_napi_id(sk, skb);
  1235. if (dst) {
  1236. if (sk->sk_rx_dst_ifindex != skb->skb_iif ||
  1237. INDIRECT_CALL_1(dst->ops->check, ip6_dst_check,
  1238. dst, sk->sk_rx_dst_cookie) == NULL) {
  1239. RCU_INIT_POINTER(sk->sk_rx_dst, NULL);
  1240. dst_release(dst);
  1241. }
  1242. }
  1243. tcp_rcv_established(sk, skb);
  1244. if (opt_skb)
  1245. goto ipv6_pktoptions;
  1246. return 0;
  1247. }
  1248. if (tcp_checksum_complete(skb))
  1249. goto csum_err;
  1250. if (sk->sk_state == TCP_LISTEN) {
  1251. struct sock *nsk = tcp_v6_cookie_check(sk, skb);
  1252. if (!nsk)
  1253. goto discard;
  1254. if (nsk != sk) {
  1255. if (tcp_child_process(sk, nsk, skb))
  1256. goto reset;
  1257. if (opt_skb)
  1258. __kfree_skb(opt_skb);
  1259. return 0;
  1260. }
  1261. } else
  1262. sock_rps_save_rxhash(sk, skb);
  1263. if (tcp_rcv_state_process(sk, skb))
  1264. goto reset;
  1265. if (opt_skb)
  1266. goto ipv6_pktoptions;
  1267. return 0;
  1268. reset:
  1269. tcp_v6_send_reset(sk, skb);
  1270. discard:
  1271. if (opt_skb)
  1272. __kfree_skb(opt_skb);
  1273. kfree_skb_reason(skb, reason);
  1274. return 0;
  1275. csum_err:
  1276. reason = SKB_DROP_REASON_TCP_CSUM;
  1277. trace_tcp_bad_csum(skb);
  1278. TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS);
  1279. TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
  1280. goto discard;
  1281. ipv6_pktoptions:
  1282. /* Do you ask, what is it?
  1283. 1. skb was enqueued by tcp.
  1284. 2. skb is added to tail of read queue, rather than out of order.
  1285. 3. socket is not in passive state.
  1286. 4. Finally, it really contains options, which user wants to receive.
  1287. */
  1288. tp = tcp_sk(sk);
  1289. if (TCP_SKB_CB(opt_skb)->end_seq == tp->rcv_nxt &&
  1290. !((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) {
  1291. if (np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo)
  1292. np->mcast_oif = tcp_v6_iif(opt_skb);
  1293. if (np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim)
  1294. np->mcast_hops = ipv6_hdr(opt_skb)->hop_limit;
  1295. if (np->rxopt.bits.rxflow || np->rxopt.bits.rxtclass)
  1296. np->rcv_flowinfo = ip6_flowinfo(ipv6_hdr(opt_skb));
  1297. if (np->repflow)
  1298. np->flow_label = ip6_flowlabel(ipv6_hdr(opt_skb));
  1299. if (ipv6_opt_accepted(sk, opt_skb, &TCP_SKB_CB(opt_skb)->header.h6)) {
  1300. tcp_v6_restore_cb(opt_skb);
  1301. opt_skb = xchg(&np->pktoptions, opt_skb);
  1302. } else {
  1303. __kfree_skb(opt_skb);
  1304. opt_skb = xchg(&np->pktoptions, NULL);
  1305. }
  1306. }
  1307. consume_skb(opt_skb);
  1308. return 0;
  1309. }
  1310. static void tcp_v6_fill_cb(struct sk_buff *skb, const struct ipv6hdr *hdr,
  1311. const struct tcphdr *th)
  1312. {
  1313. /* This is tricky: we move IP6CB at its correct location into
  1314. * TCP_SKB_CB(). It must be done after xfrm6_policy_check(), because
  1315. * _decode_session6() uses IP6CB().
  1316. * barrier() makes sure compiler won't play aliasing games.
  1317. */
  1318. memmove(&TCP_SKB_CB(skb)->header.h6, IP6CB(skb),
  1319. sizeof(struct inet6_skb_parm));
  1320. barrier();
  1321. TCP_SKB_CB(skb)->seq = ntohl(th->seq);
  1322. TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
  1323. skb->len - th->doff*4);
  1324. TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
  1325. TCP_SKB_CB(skb)->tcp_flags = tcp_flag_byte(th);
  1326. TCP_SKB_CB(skb)->tcp_tw_isn = 0;
  1327. TCP_SKB_CB(skb)->ip_dsfield = ipv6_get_dsfield(hdr);
  1328. TCP_SKB_CB(skb)->sacked = 0;
  1329. TCP_SKB_CB(skb)->has_rxtstamp =
  1330. skb->tstamp || skb_hwtstamps(skb)->hwtstamp;
  1331. }
  1332. INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)
  1333. {
  1334. enum skb_drop_reason drop_reason;
  1335. int sdif = inet6_sdif(skb);
  1336. int dif = inet6_iif(skb);
  1337. const struct tcphdr *th;
  1338. const struct ipv6hdr *hdr;
  1339. bool refcounted;
  1340. struct sock *sk;
  1341. int ret;
  1342. struct net *net = dev_net(skb->dev);
  1343. drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
  1344. if (skb->pkt_type != PACKET_HOST)
  1345. goto discard_it;
  1346. /*
  1347. * Count it even if it's bad.
  1348. */
  1349. __TCP_INC_STATS(net, TCP_MIB_INSEGS);
  1350. if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
  1351. goto discard_it;
  1352. th = (const struct tcphdr *)skb->data;
  1353. if (unlikely(th->doff < sizeof(struct tcphdr) / 4)) {
  1354. drop_reason = SKB_DROP_REASON_PKT_TOO_SMALL;
  1355. goto bad_packet;
  1356. }
  1357. if (!pskb_may_pull(skb, th->doff*4))
  1358. goto discard_it;
  1359. if (skb_checksum_init(skb, IPPROTO_TCP, ip6_compute_pseudo))
  1360. goto csum_error;
  1361. th = (const struct tcphdr *)skb->data;
  1362. hdr = ipv6_hdr(skb);
  1363. lookup:
  1364. sk = __inet6_lookup_skb(net->ipv4.tcp_death_row.hashinfo, skb, __tcp_hdrlen(th),
  1365. th->source, th->dest, inet6_iif(skb), sdif,
  1366. &refcounted);
  1367. if (!sk)
  1368. goto no_tcp_socket;
  1369. process:
  1370. if (sk->sk_state == TCP_TIME_WAIT)
  1371. goto do_time_wait;
  1372. if (sk->sk_state == TCP_NEW_SYN_RECV) {
  1373. struct request_sock *req = inet_reqsk(sk);
  1374. bool req_stolen = false;
  1375. struct sock *nsk;
  1376. sk = req->rsk_listener;
  1377. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
  1378. drop_reason = SKB_DROP_REASON_XFRM_POLICY;
  1379. else
  1380. drop_reason = tcp_inbound_md5_hash(sk, skb,
  1381. &hdr->saddr, &hdr->daddr,
  1382. AF_INET6, dif, sdif);
  1383. if (drop_reason) {
  1384. sk_drops_add(sk, skb);
  1385. reqsk_put(req);
  1386. goto discard_it;
  1387. }
  1388. if (tcp_checksum_complete(skb)) {
  1389. reqsk_put(req);
  1390. goto csum_error;
  1391. }
  1392. if (unlikely(sk->sk_state != TCP_LISTEN)) {
  1393. nsk = reuseport_migrate_sock(sk, req_to_sk(req), skb);
  1394. if (!nsk) {
  1395. inet_csk_reqsk_queue_drop_and_put(sk, req);
  1396. goto lookup;
  1397. }
  1398. sk = nsk;
  1399. /* reuseport_migrate_sock() has already held one sk_refcnt
  1400. * before returning.
  1401. */
  1402. } else {
  1403. sock_hold(sk);
  1404. }
  1405. refcounted = true;
  1406. nsk = NULL;
  1407. if (!tcp_filter(sk, skb)) {
  1408. th = (const struct tcphdr *)skb->data;
  1409. hdr = ipv6_hdr(skb);
  1410. tcp_v6_fill_cb(skb, hdr, th);
  1411. nsk = tcp_check_req(sk, skb, req, false, &req_stolen);
  1412. } else {
  1413. drop_reason = SKB_DROP_REASON_SOCKET_FILTER;
  1414. }
  1415. if (!nsk) {
  1416. reqsk_put(req);
  1417. if (req_stolen) {
  1418. /* Another cpu got exclusive access to req
  1419. * and created a full blown socket.
  1420. * Try to feed this packet to this socket
  1421. * instead of discarding it.
  1422. */
  1423. tcp_v6_restore_cb(skb);
  1424. sock_put(sk);
  1425. goto lookup;
  1426. }
  1427. goto discard_and_relse;
  1428. }
  1429. nf_reset_ct(skb);
  1430. if (nsk == sk) {
  1431. reqsk_put(req);
  1432. tcp_v6_restore_cb(skb);
  1433. } else if (tcp_child_process(sk, nsk, skb)) {
  1434. tcp_v6_send_reset(nsk, skb);
  1435. goto discard_and_relse;
  1436. } else {
  1437. sock_put(sk);
  1438. return 0;
  1439. }
  1440. }
  1441. if (static_branch_unlikely(&ip6_min_hopcount)) {
  1442. /* min_hopcount can be changed concurrently from do_ipv6_setsockopt() */
  1443. if (hdr->hop_limit < READ_ONCE(tcp_inet6_sk(sk)->min_hopcount)) {
  1444. __NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
  1445. goto discard_and_relse;
  1446. }
  1447. }
  1448. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
  1449. drop_reason = SKB_DROP_REASON_XFRM_POLICY;
  1450. goto discard_and_relse;
  1451. }
  1452. drop_reason = tcp_inbound_md5_hash(sk, skb, &hdr->saddr, &hdr->daddr,
  1453. AF_INET6, dif, sdif);
  1454. if (drop_reason)
  1455. goto discard_and_relse;
  1456. nf_reset_ct(skb);
  1457. if (tcp_filter(sk, skb)) {
  1458. drop_reason = SKB_DROP_REASON_SOCKET_FILTER;
  1459. goto discard_and_relse;
  1460. }
  1461. th = (const struct tcphdr *)skb->data;
  1462. hdr = ipv6_hdr(skb);
  1463. tcp_v6_fill_cb(skb, hdr, th);
  1464. skb->dev = NULL;
  1465. if (sk->sk_state == TCP_LISTEN) {
  1466. ret = tcp_v6_do_rcv(sk, skb);
  1467. goto put_and_return;
  1468. }
  1469. sk_incoming_cpu_update(sk);
  1470. bh_lock_sock_nested(sk);
  1471. tcp_segs_in(tcp_sk(sk), skb);
  1472. ret = 0;
  1473. if (!sock_owned_by_user(sk)) {
  1474. ret = tcp_v6_do_rcv(sk, skb);
  1475. } else {
  1476. if (tcp_add_backlog(sk, skb, &drop_reason))
  1477. goto discard_and_relse;
  1478. }
  1479. bh_unlock_sock(sk);
  1480. put_and_return:
  1481. if (refcounted)
  1482. sock_put(sk);
  1483. return ret ? -1 : 0;
  1484. no_tcp_socket:
  1485. drop_reason = SKB_DROP_REASON_NO_SOCKET;
  1486. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  1487. goto discard_it;
  1488. tcp_v6_fill_cb(skb, hdr, th);
  1489. if (tcp_checksum_complete(skb)) {
  1490. csum_error:
  1491. drop_reason = SKB_DROP_REASON_TCP_CSUM;
  1492. trace_tcp_bad_csum(skb);
  1493. __TCP_INC_STATS(net, TCP_MIB_CSUMERRORS);
  1494. bad_packet:
  1495. __TCP_INC_STATS(net, TCP_MIB_INERRS);
  1496. } else {
  1497. tcp_v6_send_reset(NULL, skb);
  1498. }
  1499. discard_it:
  1500. SKB_DR_OR(drop_reason, NOT_SPECIFIED);
  1501. kfree_skb_reason(skb, drop_reason);
  1502. return 0;
  1503. discard_and_relse:
  1504. sk_drops_add(sk, skb);
  1505. if (refcounted)
  1506. sock_put(sk);
  1507. goto discard_it;
  1508. do_time_wait:
  1509. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  1510. drop_reason = SKB_DROP_REASON_XFRM_POLICY;
  1511. inet_twsk_put(inet_twsk(sk));
  1512. goto discard_it;
  1513. }
  1514. tcp_v6_fill_cb(skb, hdr, th);
  1515. if (tcp_checksum_complete(skb)) {
  1516. inet_twsk_put(inet_twsk(sk));
  1517. goto csum_error;
  1518. }
  1519. switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
  1520. case TCP_TW_SYN:
  1521. {
  1522. struct sock *sk2;
  1523. sk2 = inet6_lookup_listener(net, net->ipv4.tcp_death_row.hashinfo,
  1524. skb, __tcp_hdrlen(th),
  1525. &ipv6_hdr(skb)->saddr, th->source,
  1526. &ipv6_hdr(skb)->daddr,
  1527. ntohs(th->dest),
  1528. tcp_v6_iif_l3_slave(skb),
  1529. sdif);
  1530. if (sk2) {
  1531. struct inet_timewait_sock *tw = inet_twsk(sk);
  1532. inet_twsk_deschedule_put(tw);
  1533. sk = sk2;
  1534. tcp_v6_restore_cb(skb);
  1535. refcounted = false;
  1536. goto process;
  1537. }
  1538. }
  1539. /* to ACK */
  1540. fallthrough;
  1541. case TCP_TW_ACK:
  1542. tcp_v6_timewait_ack(sk, skb);
  1543. break;
  1544. case TCP_TW_RST:
  1545. tcp_v6_send_reset(sk, skb);
  1546. inet_twsk_deschedule_put(inet_twsk(sk));
  1547. goto discard_it;
  1548. case TCP_TW_SUCCESS:
  1549. ;
  1550. }
  1551. goto discard_it;
  1552. }
  1553. void tcp_v6_early_demux(struct sk_buff *skb)
  1554. {
  1555. struct net *net = dev_net(skb->dev);
  1556. const struct ipv6hdr *hdr;
  1557. const struct tcphdr *th;
  1558. struct sock *sk;
  1559. if (skb->pkt_type != PACKET_HOST)
  1560. return;
  1561. if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct tcphdr)))
  1562. return;
  1563. hdr = ipv6_hdr(skb);
  1564. th = tcp_hdr(skb);
  1565. if (th->doff < sizeof(struct tcphdr) / 4)
  1566. return;
  1567. /* Note : We use inet6_iif() here, not tcp_v6_iif() */
  1568. sk = __inet6_lookup_established(net, net->ipv4.tcp_death_row.hashinfo,
  1569. &hdr->saddr, th->source,
  1570. &hdr->daddr, ntohs(th->dest),
  1571. inet6_iif(skb), inet6_sdif(skb));
  1572. if (sk) {
  1573. skb->sk = sk;
  1574. skb->destructor = sock_edemux;
  1575. if (sk_fullsock(sk)) {
  1576. struct dst_entry *dst = rcu_dereference(sk->sk_rx_dst);
  1577. if (dst)
  1578. dst = dst_check(dst, sk->sk_rx_dst_cookie);
  1579. if (dst &&
  1580. sk->sk_rx_dst_ifindex == skb->skb_iif)
  1581. skb_dst_set_noref(skb, dst);
  1582. }
  1583. }
  1584. }
  1585. static struct timewait_sock_ops tcp6_timewait_sock_ops = {
  1586. .twsk_obj_size = sizeof(struct tcp6_timewait_sock),
  1587. .twsk_unique = tcp_twsk_unique,
  1588. .twsk_destructor = tcp_twsk_destructor,
  1589. };
  1590. INDIRECT_CALLABLE_SCOPE void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb)
  1591. {
  1592. __tcp_v6_send_check(skb, &sk->sk_v6_rcv_saddr, &sk->sk_v6_daddr);
  1593. }
  1594. const struct inet_connection_sock_af_ops ipv6_specific = {
  1595. .queue_xmit = inet6_csk_xmit,
  1596. .send_check = tcp_v6_send_check,
  1597. .rebuild_header = inet6_sk_rebuild_header,
  1598. .sk_rx_dst_set = inet6_sk_rx_dst_set,
  1599. .conn_request = tcp_v6_conn_request,
  1600. .syn_recv_sock = tcp_v6_syn_recv_sock,
  1601. .net_header_len = sizeof(struct ipv6hdr),
  1602. .net_frag_header_len = sizeof(struct frag_hdr),
  1603. .setsockopt = ipv6_setsockopt,
  1604. .getsockopt = ipv6_getsockopt,
  1605. .addr2sockaddr = inet6_csk_addr2sockaddr,
  1606. .sockaddr_len = sizeof(struct sockaddr_in6),
  1607. .mtu_reduced = tcp_v6_mtu_reduced,
  1608. };
  1609. #ifdef CONFIG_TCP_MD5SIG
  1610. static const struct tcp_sock_af_ops tcp_sock_ipv6_specific = {
  1611. .md5_lookup = tcp_v6_md5_lookup,
  1612. .calc_md5_hash = tcp_v6_md5_hash_skb,
  1613. .md5_parse = tcp_v6_parse_md5_keys,
  1614. };
  1615. #endif
  1616. /*
  1617. * TCP over IPv4 via INET6 API
  1618. */
  1619. static const struct inet_connection_sock_af_ops ipv6_mapped = {
  1620. .queue_xmit = ip_queue_xmit,
  1621. .send_check = tcp_v4_send_check,
  1622. .rebuild_header = inet_sk_rebuild_header,
  1623. .sk_rx_dst_set = inet_sk_rx_dst_set,
  1624. .conn_request = tcp_v6_conn_request,
  1625. .syn_recv_sock = tcp_v6_syn_recv_sock,
  1626. .net_header_len = sizeof(struct iphdr),
  1627. .setsockopt = ipv6_setsockopt,
  1628. .getsockopt = ipv6_getsockopt,
  1629. .addr2sockaddr = inet6_csk_addr2sockaddr,
  1630. .sockaddr_len = sizeof(struct sockaddr_in6),
  1631. .mtu_reduced = tcp_v4_mtu_reduced,
  1632. };
  1633. #ifdef CONFIG_TCP_MD5SIG
  1634. static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific = {
  1635. .md5_lookup = tcp_v4_md5_lookup,
  1636. .calc_md5_hash = tcp_v4_md5_hash_skb,
  1637. .md5_parse = tcp_v6_parse_md5_keys,
  1638. };
  1639. #endif
  1640. /* NOTE: A lot of things set to zero explicitly by call to
  1641. * sk_alloc() so need not be done here.
  1642. */
  1643. static int tcp_v6_init_sock(struct sock *sk)
  1644. {
  1645. struct inet_connection_sock *icsk = inet_csk(sk);
  1646. tcp_init_sock(sk);
  1647. icsk->icsk_af_ops = &ipv6_specific;
  1648. #ifdef CONFIG_TCP_MD5SIG
  1649. tcp_sk(sk)->af_specific = &tcp_sock_ipv6_specific;
  1650. #endif
  1651. return 0;
  1652. }
  1653. #ifdef CONFIG_PROC_FS
  1654. /* Proc filesystem TCPv6 sock list dumping. */
  1655. static void get_openreq6(struct seq_file *seq,
  1656. const struct request_sock *req, int i)
  1657. {
  1658. long ttd = req->rsk_timer.expires - jiffies;
  1659. const struct in6_addr *src = &inet_rsk(req)->ir_v6_loc_addr;
  1660. const struct in6_addr *dest = &inet_rsk(req)->ir_v6_rmt_addr;
  1661. if (ttd < 0)
  1662. ttd = 0;
  1663. seq_printf(seq,
  1664. "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
  1665. "%02X %08X:%08X %02X:%08lX %08X %5u %8d %d %d %pK\n",
  1666. i,
  1667. src->s6_addr32[0], src->s6_addr32[1],
  1668. src->s6_addr32[2], src->s6_addr32[3],
  1669. inet_rsk(req)->ir_num,
  1670. dest->s6_addr32[0], dest->s6_addr32[1],
  1671. dest->s6_addr32[2], dest->s6_addr32[3],
  1672. ntohs(inet_rsk(req)->ir_rmt_port),
  1673. TCP_SYN_RECV,
  1674. 0, 0, /* could print option size, but that is af dependent. */
  1675. 1, /* timers active (only the expire timer) */
  1676. jiffies_to_clock_t(ttd),
  1677. req->num_timeout,
  1678. from_kuid_munged(seq_user_ns(seq),
  1679. sock_i_uid(req->rsk_listener)),
  1680. 0, /* non standard timer */
  1681. 0, /* open_requests have no inode */
  1682. 0, req);
  1683. }
  1684. static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
  1685. {
  1686. const struct in6_addr *dest, *src;
  1687. __u16 destp, srcp;
  1688. int timer_active;
  1689. unsigned long timer_expires;
  1690. const struct inet_sock *inet = inet_sk(sp);
  1691. const struct tcp_sock *tp = tcp_sk(sp);
  1692. const struct inet_connection_sock *icsk = inet_csk(sp);
  1693. const struct fastopen_queue *fastopenq = &icsk->icsk_accept_queue.fastopenq;
  1694. int rx_queue;
  1695. int state;
  1696. dest = &sp->sk_v6_daddr;
  1697. src = &sp->sk_v6_rcv_saddr;
  1698. destp = ntohs(inet->inet_dport);
  1699. srcp = ntohs(inet->inet_sport);
  1700. if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
  1701. icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
  1702. icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
  1703. timer_active = 1;
  1704. timer_expires = icsk->icsk_timeout;
  1705. } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
  1706. timer_active = 4;
  1707. timer_expires = icsk->icsk_timeout;
  1708. } else if (timer_pending(&sp->sk_timer)) {
  1709. timer_active = 2;
  1710. timer_expires = sp->sk_timer.expires;
  1711. } else {
  1712. timer_active = 0;
  1713. timer_expires = jiffies;
  1714. }
  1715. state = inet_sk_state_load(sp);
  1716. if (state == TCP_LISTEN)
  1717. rx_queue = READ_ONCE(sp->sk_ack_backlog);
  1718. else
  1719. /* Because we don't lock the socket,
  1720. * we might find a transient negative value.
  1721. */
  1722. rx_queue = max_t(int, READ_ONCE(tp->rcv_nxt) -
  1723. READ_ONCE(tp->copied_seq), 0);
  1724. seq_printf(seq,
  1725. "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
  1726. "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %lu %lu %u %u %d\n",
  1727. i,
  1728. src->s6_addr32[0], src->s6_addr32[1],
  1729. src->s6_addr32[2], src->s6_addr32[3], srcp,
  1730. dest->s6_addr32[0], dest->s6_addr32[1],
  1731. dest->s6_addr32[2], dest->s6_addr32[3], destp,
  1732. state,
  1733. READ_ONCE(tp->write_seq) - tp->snd_una,
  1734. rx_queue,
  1735. timer_active,
  1736. jiffies_delta_to_clock_t(timer_expires - jiffies),
  1737. icsk->icsk_retransmits,
  1738. from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
  1739. icsk->icsk_probes_out,
  1740. sock_i_ino(sp),
  1741. refcount_read(&sp->sk_refcnt), sp,
  1742. jiffies_to_clock_t(icsk->icsk_rto),
  1743. jiffies_to_clock_t(icsk->icsk_ack.ato),
  1744. (icsk->icsk_ack.quick << 1) | inet_csk_in_pingpong_mode(sp),
  1745. tcp_snd_cwnd(tp),
  1746. state == TCP_LISTEN ?
  1747. fastopenq->max_qlen :
  1748. (tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh)
  1749. );
  1750. }
  1751. static void get_timewait6_sock(struct seq_file *seq,
  1752. struct inet_timewait_sock *tw, int i)
  1753. {
  1754. long delta = tw->tw_timer.expires - jiffies;
  1755. const struct in6_addr *dest, *src;
  1756. __u16 destp, srcp;
  1757. dest = &tw->tw_v6_daddr;
  1758. src = &tw->tw_v6_rcv_saddr;
  1759. destp = ntohs(tw->tw_dport);
  1760. srcp = ntohs(tw->tw_sport);
  1761. seq_printf(seq,
  1762. "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
  1763. "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
  1764. i,
  1765. src->s6_addr32[0], src->s6_addr32[1],
  1766. src->s6_addr32[2], src->s6_addr32[3], srcp,
  1767. dest->s6_addr32[0], dest->s6_addr32[1],
  1768. dest->s6_addr32[2], dest->s6_addr32[3], destp,
  1769. tw->tw_substate, 0, 0,
  1770. 3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0,
  1771. refcount_read(&tw->tw_refcnt), tw);
  1772. }
  1773. static int tcp6_seq_show(struct seq_file *seq, void *v)
  1774. {
  1775. struct tcp_iter_state *st;
  1776. struct sock *sk = v;
  1777. if (v == SEQ_START_TOKEN) {
  1778. seq_puts(seq,
  1779. " sl "
  1780. "local_address "
  1781. "remote_address "
  1782. "st tx_queue rx_queue tr tm->when retrnsmt"
  1783. " uid timeout inode\n");
  1784. goto out;
  1785. }
  1786. st = seq->private;
  1787. if (sk->sk_state == TCP_TIME_WAIT)
  1788. get_timewait6_sock(seq, v, st->num);
  1789. else if (sk->sk_state == TCP_NEW_SYN_RECV)
  1790. get_openreq6(seq, v, st->num);
  1791. else
  1792. get_tcp6_sock(seq, v, st->num);
  1793. out:
  1794. return 0;
  1795. }
  1796. static const struct seq_operations tcp6_seq_ops = {
  1797. .show = tcp6_seq_show,
  1798. .start = tcp_seq_start,
  1799. .next = tcp_seq_next,
  1800. .stop = tcp_seq_stop,
  1801. };
  1802. static struct tcp_seq_afinfo tcp6_seq_afinfo = {
  1803. .family = AF_INET6,
  1804. };
  1805. int __net_init tcp6_proc_init(struct net *net)
  1806. {
  1807. if (!proc_create_net_data("tcp6", 0444, net->proc_net, &tcp6_seq_ops,
  1808. sizeof(struct tcp_iter_state), &tcp6_seq_afinfo))
  1809. return -ENOMEM;
  1810. return 0;
  1811. }
  1812. void tcp6_proc_exit(struct net *net)
  1813. {
  1814. remove_proc_entry("tcp6", net->proc_net);
  1815. }
  1816. #endif
  1817. struct proto tcpv6_prot = {
  1818. .name = "TCPv6",
  1819. .owner = THIS_MODULE,
  1820. .close = tcp_close,
  1821. .pre_connect = tcp_v6_pre_connect,
  1822. .connect = tcp_v6_connect,
  1823. .disconnect = tcp_disconnect,
  1824. .accept = inet_csk_accept,
  1825. .ioctl = tcp_ioctl,
  1826. .init = tcp_v6_init_sock,
  1827. .destroy = tcp_v4_destroy_sock,
  1828. .shutdown = tcp_shutdown,
  1829. .setsockopt = tcp_setsockopt,
  1830. .getsockopt = tcp_getsockopt,
  1831. .bpf_bypass_getsockopt = tcp_bpf_bypass_getsockopt,
  1832. .keepalive = tcp_set_keepalive,
  1833. .recvmsg = tcp_recvmsg,
  1834. .sendmsg = tcp_sendmsg,
  1835. .sendpage = tcp_sendpage,
  1836. .backlog_rcv = tcp_v6_do_rcv,
  1837. .release_cb = tcp_release_cb,
  1838. .hash = inet6_hash,
  1839. .unhash = inet_unhash,
  1840. .get_port = inet_csk_get_port,
  1841. .put_port = inet_put_port,
  1842. #ifdef CONFIG_BPF_SYSCALL
  1843. .psock_update_sk_prot = tcp_bpf_update_proto,
  1844. #endif
  1845. .enter_memory_pressure = tcp_enter_memory_pressure,
  1846. .leave_memory_pressure = tcp_leave_memory_pressure,
  1847. .stream_memory_free = tcp_stream_memory_free,
  1848. .sockets_allocated = &tcp_sockets_allocated,
  1849. .memory_allocated = &tcp_memory_allocated,
  1850. .per_cpu_fw_alloc = &tcp_memory_per_cpu_fw_alloc,
  1851. .memory_pressure = &tcp_memory_pressure,
  1852. .orphan_count = &tcp_orphan_count,
  1853. .sysctl_mem = sysctl_tcp_mem,
  1854. .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_tcp_wmem),
  1855. .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_tcp_rmem),
  1856. .max_header = MAX_TCP_HEADER,
  1857. .obj_size = sizeof(struct tcp6_sock),
  1858. .slab_flags = SLAB_TYPESAFE_BY_RCU,
  1859. .twsk_prot = &tcp6_timewait_sock_ops,
  1860. .rsk_prot = &tcp6_request_sock_ops,
  1861. .h.hashinfo = NULL,
  1862. .no_autobind = true,
  1863. .diag_destroy = tcp_abort,
  1864. };
  1865. EXPORT_SYMBOL_GPL(tcpv6_prot);
  1866. static const struct inet6_protocol tcpv6_protocol = {
  1867. .handler = tcp_v6_rcv,
  1868. .err_handler = tcp_v6_err,
  1869. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1870. };
  1871. static struct inet_protosw tcpv6_protosw = {
  1872. .type = SOCK_STREAM,
  1873. .protocol = IPPROTO_TCP,
  1874. .prot = &tcpv6_prot,
  1875. .ops = &inet6_stream_ops,
  1876. .flags = INET_PROTOSW_PERMANENT |
  1877. INET_PROTOSW_ICSK,
  1878. };
  1879. static int __net_init tcpv6_net_init(struct net *net)
  1880. {
  1881. return inet_ctl_sock_create(&net->ipv6.tcp_sk, PF_INET6,
  1882. SOCK_RAW, IPPROTO_TCP, net);
  1883. }
  1884. static void __net_exit tcpv6_net_exit(struct net *net)
  1885. {
  1886. inet_ctl_sock_destroy(net->ipv6.tcp_sk);
  1887. }
  1888. static void __net_exit tcpv6_net_exit_batch(struct list_head *net_exit_list)
  1889. {
  1890. tcp_twsk_purge(net_exit_list, AF_INET6);
  1891. }
  1892. static struct pernet_operations tcpv6_net_ops = {
  1893. .init = tcpv6_net_init,
  1894. .exit = tcpv6_net_exit,
  1895. .exit_batch = tcpv6_net_exit_batch,
  1896. };
  1897. int __init tcpv6_init(void)
  1898. {
  1899. int ret;
  1900. ret = inet6_add_protocol(&tcpv6_protocol, IPPROTO_TCP);
  1901. if (ret)
  1902. goto out;
  1903. /* register inet6 protocol */
  1904. ret = inet6_register_protosw(&tcpv6_protosw);
  1905. if (ret)
  1906. goto out_tcpv6_protocol;
  1907. ret = register_pernet_subsys(&tcpv6_net_ops);
  1908. if (ret)
  1909. goto out_tcpv6_protosw;
  1910. ret = mptcpv6_init();
  1911. if (ret)
  1912. goto out_tcpv6_pernet_subsys;
  1913. out:
  1914. return ret;
  1915. out_tcpv6_pernet_subsys:
  1916. unregister_pernet_subsys(&tcpv6_net_ops);
  1917. out_tcpv6_protosw:
  1918. inet6_unregister_protosw(&tcpv6_protosw);
  1919. out_tcpv6_protocol:
  1920. inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);
  1921. goto out;
  1922. }
  1923. void tcpv6_exit(void)
  1924. {
  1925. unregister_pernet_subsys(&tcpv6_net_ops);
  1926. inet6_unregister_protosw(&tcpv6_protosw);
  1927. inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);
  1928. }