tcp_ipv6.c 58 KB

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