tcp_ipv6.c 51 KB

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