tcp_ipv6.c 64 KB

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