tcp_ipv6.c 53 KB

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