tcp_ipv6.c 51 KB

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