tcp_ipv6.c 50 KB

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