tcp_ipv6.c 50 KB

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