tcp_ipv6.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322
  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/ipv6.h>
  42. #include <linux/icmpv6.h>
  43. #include <linux/random.h>
  44. #include <net/tcp.h>
  45. #include <net/ndisc.h>
  46. #include <net/inet6_hashtables.h>
  47. #include <net/inet6_connection_sock.h>
  48. #include <net/ipv6.h>
  49. #include <net/transp_v6.h>
  50. #include <net/addrconf.h>
  51. #include <net/ip6_route.h>
  52. #include <net/ip6_checksum.h>
  53. #include <net/inet_ecn.h>
  54. #include <net/protocol.h>
  55. #include <net/xfrm.h>
  56. #include <net/snmp.h>
  57. #include <net/dsfield.h>
  58. #include <net/timewait_sock.h>
  59. #include <net/netdma.h>
  60. #include <net/inet_common.h>
  61. #include <net/secure_seq.h>
  62. #include <asm/uaccess.h>
  63. #include <linux/proc_fs.h>
  64. #include <linux/seq_file.h>
  65. #include <linux/crypto.h>
  66. #include <linux/scatterlist.h>
  67. static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb);
  68. static void tcp_v6_reqsk_send_ack(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 void __tcp_v6_send_check(struct sk_buff *skb,
  72. const struct in6_addr *saddr,
  73. const struct in6_addr *daddr);
  74. static const struct inet_connection_sock_af_ops ipv6_mapped;
  75. static const struct inet_connection_sock_af_ops ipv6_specific;
  76. #ifdef CONFIG_TCP_MD5SIG
  77. static const struct tcp_sock_af_ops tcp_sock_ipv6_specific;
  78. static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific;
  79. #else
  80. static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk,
  81. const struct in6_addr *addr)
  82. {
  83. return NULL;
  84. }
  85. #endif
  86. static void tcp_v6_hash(struct sock *sk)
  87. {
  88. if (sk->sk_state != TCP_CLOSE) {
  89. if (inet_csk(sk)->icsk_af_ops == &ipv6_mapped) {
  90. tcp_prot.hash(sk);
  91. return;
  92. }
  93. local_bh_disable();
  94. __inet6_hash(sk, NULL);
  95. local_bh_enable();
  96. }
  97. }
  98. static __inline__ __sum16 tcp_v6_check(int len,
  99. const struct in6_addr *saddr,
  100. const struct in6_addr *daddr,
  101. __wsum base)
  102. {
  103. return csum_ipv6_magic(saddr, daddr, len, IPPROTO_TCP, base);
  104. }
  105. static __u32 tcp_v6_init_sequence(struct sk_buff *skb)
  106. {
  107. return secure_tcpv6_sequence_number(ipv6_hdr(skb)->daddr.s6_addr32,
  108. ipv6_hdr(skb)->saddr.s6_addr32,
  109. tcp_hdr(skb)->dest,
  110. tcp_hdr(skb)->source);
  111. }
  112. static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
  113. int addr_len)
  114. {
  115. struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
  116. struct inet_sock *inet = inet_sk(sk);
  117. struct inet_connection_sock *icsk = inet_csk(sk);
  118. struct ipv6_pinfo *np = inet6_sk(sk);
  119. struct tcp_sock *tp = tcp_sk(sk);
  120. struct in6_addr *saddr = NULL, *final_p, final;
  121. struct rt6_info *rt;
  122. struct flowi6 fl6;
  123. struct dst_entry *dst;
  124. int addr_type;
  125. int err;
  126. if (addr_len < SIN6_LEN_RFC2133)
  127. return -EINVAL;
  128. if (usin->sin6_family != AF_INET6)
  129. return -EAFNOSUPPORT;
  130. memset(&fl6, 0, sizeof(fl6));
  131. if (np->sndflow) {
  132. fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
  133. IP6_ECN_flow_init(fl6.flowlabel);
  134. if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
  135. struct ip6_flowlabel *flowlabel;
  136. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  137. if (flowlabel == NULL)
  138. return -EINVAL;
  139. ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst);
  140. fl6_sock_release(flowlabel);
  141. }
  142. }
  143. /*
  144. * connect() to INADDR_ANY means loopback (BSD'ism).
  145. */
  146. if(ipv6_addr_any(&usin->sin6_addr))
  147. usin->sin6_addr.s6_addr[15] = 0x1;
  148. addr_type = ipv6_addr_type(&usin->sin6_addr);
  149. if(addr_type & IPV6_ADDR_MULTICAST)
  150. return -ENETUNREACH;
  151. if (addr_type&IPV6_ADDR_LINKLOCAL) {
  152. if (addr_len >= sizeof(struct sockaddr_in6) &&
  153. usin->sin6_scope_id) {
  154. /* If interface is set while binding, indices
  155. * must coincide.
  156. */
  157. if (sk->sk_bound_dev_if &&
  158. sk->sk_bound_dev_if != usin->sin6_scope_id)
  159. return -EINVAL;
  160. sk->sk_bound_dev_if = usin->sin6_scope_id;
  161. }
  162. /* Connect to link-local address requires an interface */
  163. if (!sk->sk_bound_dev_if)
  164. return -EINVAL;
  165. }
  166. if (tp->rx_opt.ts_recent_stamp &&
  167. !ipv6_addr_equal(&np->daddr, &usin->sin6_addr)) {
  168. tp->rx_opt.ts_recent = 0;
  169. tp->rx_opt.ts_recent_stamp = 0;
  170. tp->write_seq = 0;
  171. }
  172. ipv6_addr_copy(&np->daddr, &usin->sin6_addr);
  173. np->flow_label = fl6.flowlabel;
  174. /*
  175. * TCP over IPv4
  176. */
  177. if (addr_type == IPV6_ADDR_MAPPED) {
  178. u32 exthdrlen = icsk->icsk_ext_hdr_len;
  179. struct sockaddr_in sin;
  180. SOCK_DEBUG(sk, "connect: ipv4 mapped\n");
  181. if (__ipv6_only_sock(sk))
  182. return -ENETUNREACH;
  183. sin.sin_family = AF_INET;
  184. sin.sin_port = usin->sin6_port;
  185. sin.sin_addr.s_addr = usin->sin6_addr.s6_addr32[3];
  186. icsk->icsk_af_ops = &ipv6_mapped;
  187. sk->sk_backlog_rcv = tcp_v4_do_rcv;
  188. #ifdef CONFIG_TCP_MD5SIG
  189. tp->af_specific = &tcp_sock_ipv6_mapped_specific;
  190. #endif
  191. err = tcp_v4_connect(sk, (struct sockaddr *)&sin, sizeof(sin));
  192. if (err) {
  193. icsk->icsk_ext_hdr_len = exthdrlen;
  194. icsk->icsk_af_ops = &ipv6_specific;
  195. sk->sk_backlog_rcv = tcp_v6_do_rcv;
  196. #ifdef CONFIG_TCP_MD5SIG
  197. tp->af_specific = &tcp_sock_ipv6_specific;
  198. #endif
  199. goto failure;
  200. } else {
  201. ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
  202. ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
  203. &np->rcv_saddr);
  204. }
  205. return err;
  206. }
  207. if (!ipv6_addr_any(&np->rcv_saddr))
  208. saddr = &np->rcv_saddr;
  209. fl6.flowi6_proto = IPPROTO_TCP;
  210. ipv6_addr_copy(&fl6.daddr, &np->daddr);
  211. ipv6_addr_copy(&fl6.saddr,
  212. (saddr ? saddr : &np->saddr));
  213. fl6.flowi6_oif = sk->sk_bound_dev_if;
  214. fl6.flowi6_mark = sk->sk_mark;
  215. fl6.fl6_dport = usin->sin6_port;
  216. fl6.fl6_sport = inet->inet_sport;
  217. final_p = fl6_update_dst(&fl6, np->opt, &final);
  218. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  219. dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true);
  220. if (IS_ERR(dst)) {
  221. err = PTR_ERR(dst);
  222. goto failure;
  223. }
  224. if (saddr == NULL) {
  225. saddr = &fl6.saddr;
  226. ipv6_addr_copy(&np->rcv_saddr, saddr);
  227. }
  228. /* set the source address */
  229. ipv6_addr_copy(&np->saddr, saddr);
  230. inet->inet_rcv_saddr = LOOPBACK4_IPV6;
  231. sk->sk_gso_type = SKB_GSO_TCPV6;
  232. __ip6_dst_store(sk, dst, NULL, NULL);
  233. rt = (struct rt6_info *) dst;
  234. if (tcp_death_row.sysctl_tw_recycle &&
  235. !tp->rx_opt.ts_recent_stamp &&
  236. ipv6_addr_equal(&rt->rt6i_dst.addr, &np->daddr)) {
  237. struct inet_peer *peer = rt6_get_peer(rt);
  238. /*
  239. * VJ's idea. We save last timestamp seen from
  240. * the destination in peer table, when entering state
  241. * TIME-WAIT * and initialize rx_opt.ts_recent from it,
  242. * when trying new connection.
  243. */
  244. if (peer) {
  245. inet_peer_refcheck(peer);
  246. if ((u32)get_seconds() - peer->tcp_ts_stamp <= TCP_PAWS_MSL) {
  247. tp->rx_opt.ts_recent_stamp = peer->tcp_ts_stamp;
  248. tp->rx_opt.ts_recent = peer->tcp_ts;
  249. }
  250. }
  251. }
  252. icsk->icsk_ext_hdr_len = 0;
  253. if (np->opt)
  254. icsk->icsk_ext_hdr_len = (np->opt->opt_flen +
  255. np->opt->opt_nflen);
  256. tp->rx_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
  257. inet->inet_dport = usin->sin6_port;
  258. tcp_set_state(sk, TCP_SYN_SENT);
  259. err = inet6_hash_connect(&tcp_death_row, sk);
  260. if (err)
  261. goto late_failure;
  262. if (!tp->write_seq)
  263. tp->write_seq = secure_tcpv6_sequence_number(np->saddr.s6_addr32,
  264. np->daddr.s6_addr32,
  265. inet->inet_sport,
  266. inet->inet_dport);
  267. err = tcp_connect(sk);
  268. if (err)
  269. goto late_failure;
  270. return 0;
  271. late_failure:
  272. tcp_set_state(sk, TCP_CLOSE);
  273. __sk_dst_reset(sk);
  274. failure:
  275. inet->inet_dport = 0;
  276. sk->sk_route_caps = 0;
  277. return err;
  278. }
  279. static void tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  280. u8 type, u8 code, int offset, __be32 info)
  281. {
  282. const struct ipv6hdr *hdr = (const struct ipv6hdr*)skb->data;
  283. const struct tcphdr *th = (struct tcphdr *)(skb->data+offset);
  284. struct ipv6_pinfo *np;
  285. struct sock *sk;
  286. int err;
  287. struct tcp_sock *tp;
  288. __u32 seq;
  289. struct net *net = dev_net(skb->dev);
  290. sk = inet6_lookup(net, &tcp_hashinfo, &hdr->daddr,
  291. th->dest, &hdr->saddr, th->source, skb->dev->ifindex);
  292. if (sk == NULL) {
  293. ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
  294. ICMP6_MIB_INERRORS);
  295. return;
  296. }
  297. if (sk->sk_state == TCP_TIME_WAIT) {
  298. inet_twsk_put(inet_twsk(sk));
  299. return;
  300. }
  301. bh_lock_sock(sk);
  302. if (sock_owned_by_user(sk))
  303. NET_INC_STATS_BH(net, LINUX_MIB_LOCKDROPPEDICMPS);
  304. if (sk->sk_state == TCP_CLOSE)
  305. goto out;
  306. if (ipv6_hdr(skb)->hop_limit < inet6_sk(sk)->min_hopcount) {
  307. NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
  308. goto out;
  309. }
  310. tp = tcp_sk(sk);
  311. seq = ntohl(th->seq);
  312. if (sk->sk_state != TCP_LISTEN &&
  313. !between(seq, tp->snd_una, tp->snd_nxt)) {
  314. NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
  315. goto out;
  316. }
  317. np = inet6_sk(sk);
  318. if (type == ICMPV6_PKT_TOOBIG) {
  319. struct dst_entry *dst;
  320. if (sock_owned_by_user(sk))
  321. goto out;
  322. if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
  323. goto out;
  324. /* icmp should have updated the destination cache entry */
  325. dst = __sk_dst_check(sk, np->dst_cookie);
  326. if (dst == NULL) {
  327. struct inet_sock *inet = inet_sk(sk);
  328. struct flowi6 fl6;
  329. /* BUGGG_FUTURE: Again, it is not clear how
  330. to handle rthdr case. Ignore this complexity
  331. for now.
  332. */
  333. memset(&fl6, 0, sizeof(fl6));
  334. fl6.flowi6_proto = IPPROTO_TCP;
  335. ipv6_addr_copy(&fl6.daddr, &np->daddr);
  336. ipv6_addr_copy(&fl6.saddr, &np->saddr);
  337. fl6.flowi6_oif = sk->sk_bound_dev_if;
  338. fl6.flowi6_mark = sk->sk_mark;
  339. fl6.fl6_dport = inet->inet_dport;
  340. fl6.fl6_sport = inet->inet_sport;
  341. security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
  342. dst = ip6_dst_lookup_flow(sk, &fl6, NULL, false);
  343. if (IS_ERR(dst)) {
  344. sk->sk_err_soft = -PTR_ERR(dst);
  345. goto out;
  346. }
  347. } else
  348. dst_hold(dst);
  349. if (inet_csk(sk)->icsk_pmtu_cookie > dst_mtu(dst)) {
  350. tcp_sync_mss(sk, dst_mtu(dst));
  351. tcp_simple_retransmit(sk);
  352. } /* else let the usual retransmit timer handle it */
  353. dst_release(dst);
  354. goto out;
  355. }
  356. icmpv6_err_convert(type, code, &err);
  357. /* Might be for an request_sock */
  358. switch (sk->sk_state) {
  359. struct request_sock *req, **prev;
  360. case TCP_LISTEN:
  361. if (sock_owned_by_user(sk))
  362. goto out;
  363. req = inet6_csk_search_req(sk, &prev, th->dest, &hdr->daddr,
  364. &hdr->saddr, inet6_iif(skb));
  365. if (!req)
  366. goto out;
  367. /* ICMPs are not backlogged, hence we cannot get
  368. * an established socket here.
  369. */
  370. WARN_ON(req->sk != NULL);
  371. if (seq != tcp_rsk(req)->snt_isn) {
  372. NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
  373. goto out;
  374. }
  375. inet_csk_reqsk_queue_drop(sk, req, prev);
  376. goto out;
  377. case TCP_SYN_SENT:
  378. case TCP_SYN_RECV: /* Cannot happen.
  379. It can, it SYNs are crossed. --ANK */
  380. if (!sock_owned_by_user(sk)) {
  381. sk->sk_err = err;
  382. sk->sk_error_report(sk); /* Wake people up to see the error (see connect in sock.c) */
  383. tcp_done(sk);
  384. } else
  385. sk->sk_err_soft = err;
  386. goto out;
  387. }
  388. if (!sock_owned_by_user(sk) && np->recverr) {
  389. sk->sk_err = err;
  390. sk->sk_error_report(sk);
  391. } else
  392. sk->sk_err_soft = err;
  393. out:
  394. bh_unlock_sock(sk);
  395. sock_put(sk);
  396. }
  397. static int tcp_v6_send_synack(struct sock *sk, struct request_sock *req,
  398. struct request_values *rvp)
  399. {
  400. struct inet6_request_sock *treq = inet6_rsk(req);
  401. struct ipv6_pinfo *np = inet6_sk(sk);
  402. struct sk_buff * skb;
  403. struct ipv6_txoptions *opt = NULL;
  404. struct in6_addr * final_p, final;
  405. struct flowi6 fl6;
  406. struct dst_entry *dst;
  407. int err;
  408. memset(&fl6, 0, sizeof(fl6));
  409. fl6.flowi6_proto = IPPROTO_TCP;
  410. ipv6_addr_copy(&fl6.daddr, &treq->rmt_addr);
  411. ipv6_addr_copy(&fl6.saddr, &treq->loc_addr);
  412. fl6.flowlabel = 0;
  413. fl6.flowi6_oif = treq->iif;
  414. fl6.flowi6_mark = sk->sk_mark;
  415. fl6.fl6_dport = inet_rsk(req)->rmt_port;
  416. fl6.fl6_sport = inet_rsk(req)->loc_port;
  417. security_req_classify_flow(req, flowi6_to_flowi(&fl6));
  418. opt = np->opt;
  419. final_p = fl6_update_dst(&fl6, opt, &final);
  420. dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false);
  421. if (IS_ERR(dst)) {
  422. err = PTR_ERR(dst);
  423. dst = NULL;
  424. goto done;
  425. }
  426. skb = tcp_make_synack(sk, dst, req, rvp);
  427. err = -ENOMEM;
  428. if (skb) {
  429. __tcp_v6_send_check(skb, &treq->loc_addr, &treq->rmt_addr);
  430. ipv6_addr_copy(&fl6.daddr, &treq->rmt_addr);
  431. err = ip6_xmit(sk, skb, &fl6, opt);
  432. err = net_xmit_eval(err);
  433. }
  434. done:
  435. if (opt && opt != np->opt)
  436. sock_kfree_s(sk, opt, opt->tot_len);
  437. dst_release(dst);
  438. return err;
  439. }
  440. static int tcp_v6_rtx_synack(struct sock *sk, struct request_sock *req,
  441. struct request_values *rvp)
  442. {
  443. TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_RETRANSSEGS);
  444. return tcp_v6_send_synack(sk, req, rvp);
  445. }
  446. static inline void syn_flood_warning(struct sk_buff *skb)
  447. {
  448. #ifdef CONFIG_SYN_COOKIES
  449. if (sysctl_tcp_syncookies)
  450. printk(KERN_INFO
  451. "TCPv6: Possible SYN flooding on port %d. "
  452. "Sending cookies.\n", ntohs(tcp_hdr(skb)->dest));
  453. else
  454. #endif
  455. printk(KERN_INFO
  456. "TCPv6: Possible SYN flooding on port %d. "
  457. "Dropping request.\n", ntohs(tcp_hdr(skb)->dest));
  458. }
  459. static void tcp_v6_reqsk_destructor(struct request_sock *req)
  460. {
  461. kfree_skb(inet6_rsk(req)->pktopts);
  462. }
  463. #ifdef CONFIG_TCP_MD5SIG
  464. static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk,
  465. const struct in6_addr *addr)
  466. {
  467. struct tcp_sock *tp = tcp_sk(sk);
  468. int i;
  469. BUG_ON(tp == NULL);
  470. if (!tp->md5sig_info || !tp->md5sig_info->entries6)
  471. return NULL;
  472. for (i = 0; i < tp->md5sig_info->entries6; i++) {
  473. if (ipv6_addr_equal(&tp->md5sig_info->keys6[i].addr, addr))
  474. return &tp->md5sig_info->keys6[i].base;
  475. }
  476. return NULL;
  477. }
  478. static struct tcp_md5sig_key *tcp_v6_md5_lookup(struct sock *sk,
  479. struct sock *addr_sk)
  480. {
  481. return tcp_v6_md5_do_lookup(sk, &inet6_sk(addr_sk)->daddr);
  482. }
  483. static struct tcp_md5sig_key *tcp_v6_reqsk_md5_lookup(struct sock *sk,
  484. struct request_sock *req)
  485. {
  486. return tcp_v6_md5_do_lookup(sk, &inet6_rsk(req)->rmt_addr);
  487. }
  488. static int tcp_v6_md5_do_add(struct sock *sk, const struct in6_addr *peer,
  489. char *newkey, u8 newkeylen)
  490. {
  491. /* Add key to the list */
  492. struct tcp_md5sig_key *key;
  493. struct tcp_sock *tp = tcp_sk(sk);
  494. struct tcp6_md5sig_key *keys;
  495. key = tcp_v6_md5_do_lookup(sk, peer);
  496. if (key) {
  497. /* modify existing entry - just update that one */
  498. kfree(key->key);
  499. key->key = newkey;
  500. key->keylen = newkeylen;
  501. } else {
  502. /* reallocate new list if current one is full. */
  503. if (!tp->md5sig_info) {
  504. tp->md5sig_info = kzalloc(sizeof(*tp->md5sig_info), GFP_ATOMIC);
  505. if (!tp->md5sig_info) {
  506. kfree(newkey);
  507. return -ENOMEM;
  508. }
  509. sk_nocaps_add(sk, NETIF_F_GSO_MASK);
  510. }
  511. if (tp->md5sig_info->entries6 == 0 &&
  512. tcp_alloc_md5sig_pool(sk) == NULL) {
  513. kfree(newkey);
  514. return -ENOMEM;
  515. }
  516. if (tp->md5sig_info->alloced6 == tp->md5sig_info->entries6) {
  517. keys = kmalloc((sizeof (tp->md5sig_info->keys6[0]) *
  518. (tp->md5sig_info->entries6 + 1)), GFP_ATOMIC);
  519. if (!keys) {
  520. kfree(newkey);
  521. if (tp->md5sig_info->entries6 == 0)
  522. tcp_free_md5sig_pool();
  523. return -ENOMEM;
  524. }
  525. if (tp->md5sig_info->entries6)
  526. memmove(keys, tp->md5sig_info->keys6,
  527. (sizeof (tp->md5sig_info->keys6[0]) *
  528. tp->md5sig_info->entries6));
  529. kfree(tp->md5sig_info->keys6);
  530. tp->md5sig_info->keys6 = keys;
  531. tp->md5sig_info->alloced6++;
  532. }
  533. ipv6_addr_copy(&tp->md5sig_info->keys6[tp->md5sig_info->entries6].addr,
  534. peer);
  535. tp->md5sig_info->keys6[tp->md5sig_info->entries6].base.key = newkey;
  536. tp->md5sig_info->keys6[tp->md5sig_info->entries6].base.keylen = newkeylen;
  537. tp->md5sig_info->entries6++;
  538. }
  539. return 0;
  540. }
  541. static int tcp_v6_md5_add_func(struct sock *sk, struct sock *addr_sk,
  542. u8 *newkey, __u8 newkeylen)
  543. {
  544. return tcp_v6_md5_do_add(sk, &inet6_sk(addr_sk)->daddr,
  545. newkey, newkeylen);
  546. }
  547. static int tcp_v6_md5_do_del(struct sock *sk, const struct in6_addr *peer)
  548. {
  549. struct tcp_sock *tp = tcp_sk(sk);
  550. int i;
  551. for (i = 0; i < tp->md5sig_info->entries6; i++) {
  552. if (ipv6_addr_equal(&tp->md5sig_info->keys6[i].addr, peer)) {
  553. /* Free the key */
  554. kfree(tp->md5sig_info->keys6[i].base.key);
  555. tp->md5sig_info->entries6--;
  556. if (tp->md5sig_info->entries6 == 0) {
  557. kfree(tp->md5sig_info->keys6);
  558. tp->md5sig_info->keys6 = NULL;
  559. tp->md5sig_info->alloced6 = 0;
  560. tcp_free_md5sig_pool();
  561. } else {
  562. /* shrink the database */
  563. if (tp->md5sig_info->entries6 != i)
  564. memmove(&tp->md5sig_info->keys6[i],
  565. &tp->md5sig_info->keys6[i+1],
  566. (tp->md5sig_info->entries6 - i)
  567. * sizeof (tp->md5sig_info->keys6[0]));
  568. }
  569. return 0;
  570. }
  571. }
  572. return -ENOENT;
  573. }
  574. static void tcp_v6_clear_md5_list (struct sock *sk)
  575. {
  576. struct tcp_sock *tp = tcp_sk(sk);
  577. int i;
  578. if (tp->md5sig_info->entries6) {
  579. for (i = 0; i < tp->md5sig_info->entries6; i++)
  580. kfree(tp->md5sig_info->keys6[i].base.key);
  581. tp->md5sig_info->entries6 = 0;
  582. tcp_free_md5sig_pool();
  583. }
  584. kfree(tp->md5sig_info->keys6);
  585. tp->md5sig_info->keys6 = NULL;
  586. tp->md5sig_info->alloced6 = 0;
  587. if (tp->md5sig_info->entries4) {
  588. for (i = 0; i < tp->md5sig_info->entries4; i++)
  589. kfree(tp->md5sig_info->keys4[i].base.key);
  590. tp->md5sig_info->entries4 = 0;
  591. tcp_free_md5sig_pool();
  592. }
  593. kfree(tp->md5sig_info->keys4);
  594. tp->md5sig_info->keys4 = NULL;
  595. tp->md5sig_info->alloced4 = 0;
  596. }
  597. static int tcp_v6_parse_md5_keys (struct sock *sk, char __user *optval,
  598. int optlen)
  599. {
  600. struct tcp_md5sig cmd;
  601. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd.tcpm_addr;
  602. u8 *newkey;
  603. if (optlen < sizeof(cmd))
  604. return -EINVAL;
  605. if (copy_from_user(&cmd, optval, sizeof(cmd)))
  606. return -EFAULT;
  607. if (sin6->sin6_family != AF_INET6)
  608. return -EINVAL;
  609. if (!cmd.tcpm_keylen) {
  610. if (!tcp_sk(sk)->md5sig_info)
  611. return -ENOENT;
  612. if (ipv6_addr_v4mapped(&sin6->sin6_addr))
  613. return tcp_v4_md5_do_del(sk, sin6->sin6_addr.s6_addr32[3]);
  614. return tcp_v6_md5_do_del(sk, &sin6->sin6_addr);
  615. }
  616. if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
  617. return -EINVAL;
  618. if (!tcp_sk(sk)->md5sig_info) {
  619. struct tcp_sock *tp = tcp_sk(sk);
  620. struct tcp_md5sig_info *p;
  621. p = kzalloc(sizeof(struct tcp_md5sig_info), GFP_KERNEL);
  622. if (!p)
  623. return -ENOMEM;
  624. tp->md5sig_info = p;
  625. sk_nocaps_add(sk, NETIF_F_GSO_MASK);
  626. }
  627. newkey = kmemdup(cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
  628. if (!newkey)
  629. return -ENOMEM;
  630. if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
  631. return tcp_v4_md5_do_add(sk, sin6->sin6_addr.s6_addr32[3],
  632. newkey, cmd.tcpm_keylen);
  633. }
  634. return tcp_v6_md5_do_add(sk, &sin6->sin6_addr, newkey, cmd.tcpm_keylen);
  635. }
  636. static int tcp_v6_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp,
  637. const struct in6_addr *daddr,
  638. const struct in6_addr *saddr, int nbytes)
  639. {
  640. struct tcp6_pseudohdr *bp;
  641. struct scatterlist sg;
  642. bp = &hp->md5_blk.ip6;
  643. /* 1. TCP pseudo-header (RFC2460) */
  644. ipv6_addr_copy(&bp->saddr, saddr);
  645. ipv6_addr_copy(&bp->daddr, daddr);
  646. bp->protocol = cpu_to_be32(IPPROTO_TCP);
  647. bp->len = cpu_to_be32(nbytes);
  648. sg_init_one(&sg, bp, sizeof(*bp));
  649. return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp));
  650. }
  651. static int tcp_v6_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key,
  652. const struct in6_addr *daddr, struct in6_addr *saddr,
  653. struct tcphdr *th)
  654. {
  655. struct tcp_md5sig_pool *hp;
  656. struct hash_desc *desc;
  657. hp = tcp_get_md5sig_pool();
  658. if (!hp)
  659. goto clear_hash_noput;
  660. desc = &hp->md5_desc;
  661. if (crypto_hash_init(desc))
  662. goto clear_hash;
  663. if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2))
  664. goto clear_hash;
  665. if (tcp_md5_hash_header(hp, th))
  666. goto clear_hash;
  667. if (tcp_md5_hash_key(hp, key))
  668. goto clear_hash;
  669. if (crypto_hash_final(desc, md5_hash))
  670. goto clear_hash;
  671. tcp_put_md5sig_pool();
  672. return 0;
  673. clear_hash:
  674. tcp_put_md5sig_pool();
  675. clear_hash_noput:
  676. memset(md5_hash, 0, 16);
  677. return 1;
  678. }
  679. static int tcp_v6_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
  680. struct sock *sk, struct request_sock *req,
  681. struct sk_buff *skb)
  682. {
  683. const struct in6_addr *saddr, *daddr;
  684. struct tcp_md5sig_pool *hp;
  685. struct hash_desc *desc;
  686. struct tcphdr *th = tcp_hdr(skb);
  687. if (sk) {
  688. saddr = &inet6_sk(sk)->saddr;
  689. daddr = &inet6_sk(sk)->daddr;
  690. } else if (req) {
  691. saddr = &inet6_rsk(req)->loc_addr;
  692. daddr = &inet6_rsk(req)->rmt_addr;
  693. } else {
  694. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  695. saddr = &ip6h->saddr;
  696. daddr = &ip6h->daddr;
  697. }
  698. hp = tcp_get_md5sig_pool();
  699. if (!hp)
  700. goto clear_hash_noput;
  701. desc = &hp->md5_desc;
  702. if (crypto_hash_init(desc))
  703. goto clear_hash;
  704. if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, skb->len))
  705. goto clear_hash;
  706. if (tcp_md5_hash_header(hp, th))
  707. goto clear_hash;
  708. if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2))
  709. goto clear_hash;
  710. if (tcp_md5_hash_key(hp, key))
  711. goto clear_hash;
  712. if (crypto_hash_final(desc, md5_hash))
  713. goto clear_hash;
  714. tcp_put_md5sig_pool();
  715. return 0;
  716. clear_hash:
  717. tcp_put_md5sig_pool();
  718. clear_hash_noput:
  719. memset(md5_hash, 0, 16);
  720. return 1;
  721. }
  722. static int tcp_v6_inbound_md5_hash (struct sock *sk, struct sk_buff *skb)
  723. {
  724. __u8 *hash_location = NULL;
  725. struct tcp_md5sig_key *hash_expected;
  726. const struct ipv6hdr *ip6h = ipv6_hdr(skb);
  727. struct tcphdr *th = tcp_hdr(skb);
  728. int genhash;
  729. u8 newhash[16];
  730. hash_expected = tcp_v6_md5_do_lookup(sk, &ip6h->saddr);
  731. hash_location = tcp_parse_md5sig_option(th);
  732. /* We've parsed the options - do we have a hash? */
  733. if (!hash_expected && !hash_location)
  734. return 0;
  735. if (hash_expected && !hash_location) {
  736. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND);
  737. return 1;
  738. }
  739. if (!hash_expected && hash_location) {
  740. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED);
  741. return 1;
  742. }
  743. /* check the signature */
  744. genhash = tcp_v6_md5_hash_skb(newhash,
  745. hash_expected,
  746. NULL, NULL, skb);
  747. if (genhash || memcmp(hash_location, newhash, 16) != 0) {
  748. if (net_ratelimit()) {
  749. printk(KERN_INFO "MD5 Hash %s for [%pI6c]:%u->[%pI6c]:%u\n",
  750. genhash ? "failed" : "mismatch",
  751. &ip6h->saddr, ntohs(th->source),
  752. &ip6h->daddr, ntohs(th->dest));
  753. }
  754. return 1;
  755. }
  756. return 0;
  757. }
  758. #endif
  759. struct request_sock_ops tcp6_request_sock_ops __read_mostly = {
  760. .family = AF_INET6,
  761. .obj_size = sizeof(struct tcp6_request_sock),
  762. .rtx_syn_ack = tcp_v6_rtx_synack,
  763. .send_ack = tcp_v6_reqsk_send_ack,
  764. .destructor = tcp_v6_reqsk_destructor,
  765. .send_reset = tcp_v6_send_reset,
  766. .syn_ack_timeout = tcp_syn_ack_timeout,
  767. };
  768. #ifdef CONFIG_TCP_MD5SIG
  769. static const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops = {
  770. .md5_lookup = tcp_v6_reqsk_md5_lookup,
  771. .calc_md5_hash = tcp_v6_md5_hash_skb,
  772. };
  773. #endif
  774. static void __tcp_v6_send_check(struct sk_buff *skb,
  775. const struct in6_addr *saddr, const struct in6_addr *daddr)
  776. {
  777. struct tcphdr *th = tcp_hdr(skb);
  778. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  779. th->check = ~tcp_v6_check(skb->len, saddr, daddr, 0);
  780. skb->csum_start = skb_transport_header(skb) - skb->head;
  781. skb->csum_offset = offsetof(struct tcphdr, check);
  782. } else {
  783. th->check = tcp_v6_check(skb->len, saddr, daddr,
  784. csum_partial(th, th->doff << 2,
  785. skb->csum));
  786. }
  787. }
  788. static void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb)
  789. {
  790. struct ipv6_pinfo *np = inet6_sk(sk);
  791. __tcp_v6_send_check(skb, &np->saddr, &np->daddr);
  792. }
  793. static int tcp_v6_gso_send_check(struct sk_buff *skb)
  794. {
  795. const struct ipv6hdr *ipv6h;
  796. struct tcphdr *th;
  797. if (!pskb_may_pull(skb, sizeof(*th)))
  798. return -EINVAL;
  799. ipv6h = ipv6_hdr(skb);
  800. th = tcp_hdr(skb);
  801. th->check = 0;
  802. skb->ip_summed = CHECKSUM_PARTIAL;
  803. __tcp_v6_send_check(skb, &ipv6h->saddr, &ipv6h->daddr);
  804. return 0;
  805. }
  806. static struct sk_buff **tcp6_gro_receive(struct sk_buff **head,
  807. struct sk_buff *skb)
  808. {
  809. const struct ipv6hdr *iph = skb_gro_network_header(skb);
  810. switch (skb->ip_summed) {
  811. case CHECKSUM_COMPLETE:
  812. if (!tcp_v6_check(skb_gro_len(skb), &iph->saddr, &iph->daddr,
  813. skb->csum)) {
  814. skb->ip_summed = CHECKSUM_UNNECESSARY;
  815. break;
  816. }
  817. /* fall through */
  818. case CHECKSUM_NONE:
  819. NAPI_GRO_CB(skb)->flush = 1;
  820. return NULL;
  821. }
  822. return tcp_gro_receive(head, skb);
  823. }
  824. static int tcp6_gro_complete(struct sk_buff *skb)
  825. {
  826. const struct ipv6hdr *iph = ipv6_hdr(skb);
  827. struct tcphdr *th = tcp_hdr(skb);
  828. th->check = ~tcp_v6_check(skb->len - skb_transport_offset(skb),
  829. &iph->saddr, &iph->daddr, 0);
  830. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  831. return tcp_gro_complete(skb);
  832. }
  833. static void tcp_v6_send_response(struct sk_buff *skb, u32 seq, u32 ack, u32 win,
  834. u32 ts, struct tcp_md5sig_key *key, int rst)
  835. {
  836. struct tcphdr *th = tcp_hdr(skb), *t1;
  837. struct sk_buff *buff;
  838. struct flowi6 fl6;
  839. struct net *net = dev_net(skb_dst(skb)->dev);
  840. struct sock *ctl_sk = net->ipv6.tcp_sk;
  841. unsigned int tot_len = sizeof(struct tcphdr);
  842. struct dst_entry *dst;
  843. __be32 *topt;
  844. if (ts)
  845. tot_len += TCPOLEN_TSTAMP_ALIGNED;
  846. #ifdef CONFIG_TCP_MD5SIG
  847. if (key)
  848. tot_len += TCPOLEN_MD5SIG_ALIGNED;
  849. #endif
  850. buff = alloc_skb(MAX_HEADER + sizeof(struct ipv6hdr) + tot_len,
  851. GFP_ATOMIC);
  852. if (buff == NULL)
  853. return;
  854. skb_reserve(buff, MAX_HEADER + sizeof(struct ipv6hdr) + tot_len);
  855. t1 = (struct tcphdr *) skb_push(buff, tot_len);
  856. skb_reset_transport_header(buff);
  857. /* Swap the send and the receive. */
  858. memset(t1, 0, sizeof(*t1));
  859. t1->dest = th->source;
  860. t1->source = th->dest;
  861. t1->doff = tot_len / 4;
  862. t1->seq = htonl(seq);
  863. t1->ack_seq = htonl(ack);
  864. t1->ack = !rst || !th->ack;
  865. t1->rst = rst;
  866. t1->window = htons(win);
  867. topt = (__be32 *)(t1 + 1);
  868. if (ts) {
  869. *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  870. (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
  871. *topt++ = htonl(tcp_time_stamp);
  872. *topt++ = htonl(ts);
  873. }
  874. #ifdef CONFIG_TCP_MD5SIG
  875. if (key) {
  876. *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  877. (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
  878. tcp_v6_md5_hash_hdr((__u8 *)topt, key,
  879. &ipv6_hdr(skb)->saddr,
  880. &ipv6_hdr(skb)->daddr, t1);
  881. }
  882. #endif
  883. memset(&fl6, 0, sizeof(fl6));
  884. ipv6_addr_copy(&fl6.daddr, &ipv6_hdr(skb)->saddr);
  885. ipv6_addr_copy(&fl6.saddr, &ipv6_hdr(skb)->daddr);
  886. buff->ip_summed = CHECKSUM_PARTIAL;
  887. buff->csum = 0;
  888. __tcp_v6_send_check(buff, &fl6.saddr, &fl6.daddr);
  889. fl6.flowi6_proto = IPPROTO_TCP;
  890. fl6.flowi6_oif = inet6_iif(skb);
  891. fl6.fl6_dport = t1->dest;
  892. fl6.fl6_sport = t1->source;
  893. security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
  894. /* Pass a socket to ip6_dst_lookup either it is for RST
  895. * Underlying function will use this to retrieve the network
  896. * namespace
  897. */
  898. dst = ip6_dst_lookup_flow(ctl_sk, &fl6, NULL, false);
  899. if (!IS_ERR(dst)) {
  900. skb_dst_set(buff, dst);
  901. ip6_xmit(ctl_sk, buff, &fl6, NULL);
  902. TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
  903. if (rst)
  904. TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS);
  905. return;
  906. }
  907. kfree_skb(buff);
  908. }
  909. static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb)
  910. {
  911. struct tcphdr *th = tcp_hdr(skb);
  912. u32 seq = 0, ack_seq = 0;
  913. struct tcp_md5sig_key *key = NULL;
  914. if (th->rst)
  915. return;
  916. if (!ipv6_unicast_destination(skb))
  917. return;
  918. #ifdef CONFIG_TCP_MD5SIG
  919. if (sk)
  920. key = tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->saddr);
  921. #endif
  922. if (th->ack)
  923. seq = ntohl(th->ack_seq);
  924. else
  925. ack_seq = ntohl(th->seq) + th->syn + th->fin + skb->len -
  926. (th->doff << 2);
  927. tcp_v6_send_response(skb, seq, ack_seq, 0, 0, key, 1);
  928. }
  929. static void tcp_v6_send_ack(struct sk_buff *skb, u32 seq, u32 ack, u32 win, u32 ts,
  930. struct tcp_md5sig_key *key)
  931. {
  932. tcp_v6_send_response(skb, seq, ack, win, ts, key, 0);
  933. }
  934. static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb)
  935. {
  936. struct inet_timewait_sock *tw = inet_twsk(sk);
  937. struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
  938. tcp_v6_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
  939. tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
  940. tcptw->tw_ts_recent, tcp_twsk_md5_key(tcptw));
  941. inet_twsk_put(tw);
  942. }
  943. static void tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
  944. struct request_sock *req)
  945. {
  946. tcp_v6_send_ack(skb, tcp_rsk(req)->snt_isn + 1, tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd, req->ts_recent,
  947. tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->daddr));
  948. }
  949. static struct sock *tcp_v6_hnd_req(struct sock *sk,struct sk_buff *skb)
  950. {
  951. struct request_sock *req, **prev;
  952. const struct tcphdr *th = tcp_hdr(skb);
  953. struct sock *nsk;
  954. /* Find possible connection requests. */
  955. req = inet6_csk_search_req(sk, &prev, th->source,
  956. &ipv6_hdr(skb)->saddr,
  957. &ipv6_hdr(skb)->daddr, inet6_iif(skb));
  958. if (req)
  959. return tcp_check_req(sk, skb, req, prev);
  960. nsk = __inet6_lookup_established(sock_net(sk), &tcp_hashinfo,
  961. &ipv6_hdr(skb)->saddr, th->source,
  962. &ipv6_hdr(skb)->daddr, ntohs(th->dest), inet6_iif(skb));
  963. if (nsk) {
  964. if (nsk->sk_state != TCP_TIME_WAIT) {
  965. bh_lock_sock(nsk);
  966. return nsk;
  967. }
  968. inet_twsk_put(inet_twsk(nsk));
  969. return NULL;
  970. }
  971. #ifdef CONFIG_SYN_COOKIES
  972. if (!th->syn)
  973. sk = cookie_v6_check(sk, skb);
  974. #endif
  975. return sk;
  976. }
  977. /* FIXME: this is substantially similar to the ipv4 code.
  978. * Can some kind of merge be done? -- erics
  979. */
  980. static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
  981. {
  982. struct tcp_extend_values tmp_ext;
  983. struct tcp_options_received tmp_opt;
  984. u8 *hash_location;
  985. struct request_sock *req;
  986. struct inet6_request_sock *treq;
  987. struct ipv6_pinfo *np = inet6_sk(sk);
  988. struct tcp_sock *tp = tcp_sk(sk);
  989. __u32 isn = TCP_SKB_CB(skb)->when;
  990. struct dst_entry *dst = NULL;
  991. #ifdef CONFIG_SYN_COOKIES
  992. int want_cookie = 0;
  993. #else
  994. #define want_cookie 0
  995. #endif
  996. if (skb->protocol == htons(ETH_P_IP))
  997. return tcp_v4_conn_request(sk, skb);
  998. if (!ipv6_unicast_destination(skb))
  999. goto drop;
  1000. if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
  1001. if (net_ratelimit())
  1002. syn_flood_warning(skb);
  1003. #ifdef CONFIG_SYN_COOKIES
  1004. if (sysctl_tcp_syncookies)
  1005. want_cookie = 1;
  1006. else
  1007. #endif
  1008. goto drop;
  1009. }
  1010. if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
  1011. goto drop;
  1012. req = inet6_reqsk_alloc(&tcp6_request_sock_ops);
  1013. if (req == NULL)
  1014. goto drop;
  1015. #ifdef CONFIG_TCP_MD5SIG
  1016. tcp_rsk(req)->af_specific = &tcp_request_sock_ipv6_ops;
  1017. #endif
  1018. tcp_clear_options(&tmp_opt);
  1019. tmp_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
  1020. tmp_opt.user_mss = tp->rx_opt.user_mss;
  1021. tcp_parse_options(skb, &tmp_opt, &hash_location, 0);
  1022. if (tmp_opt.cookie_plus > 0 &&
  1023. tmp_opt.saw_tstamp &&
  1024. !tp->rx_opt.cookie_out_never &&
  1025. (sysctl_tcp_cookie_size > 0 ||
  1026. (tp->cookie_values != NULL &&
  1027. tp->cookie_values->cookie_desired > 0))) {
  1028. u8 *c;
  1029. u32 *d;
  1030. u32 *mess = &tmp_ext.cookie_bakery[COOKIE_DIGEST_WORDS];
  1031. int l = tmp_opt.cookie_plus - TCPOLEN_COOKIE_BASE;
  1032. if (tcp_cookie_generator(&tmp_ext.cookie_bakery[0]) != 0)
  1033. goto drop_and_free;
  1034. /* Secret recipe starts with IP addresses */
  1035. d = (__force u32 *)&ipv6_hdr(skb)->daddr.s6_addr32[0];
  1036. *mess++ ^= *d++;
  1037. *mess++ ^= *d++;
  1038. *mess++ ^= *d++;
  1039. *mess++ ^= *d++;
  1040. d = (__force u32 *)&ipv6_hdr(skb)->saddr.s6_addr32[0];
  1041. *mess++ ^= *d++;
  1042. *mess++ ^= *d++;
  1043. *mess++ ^= *d++;
  1044. *mess++ ^= *d++;
  1045. /* plus variable length Initiator Cookie */
  1046. c = (u8 *)mess;
  1047. while (l-- > 0)
  1048. *c++ ^= *hash_location++;
  1049. #ifdef CONFIG_SYN_COOKIES
  1050. want_cookie = 0; /* not our kind of cookie */
  1051. #endif
  1052. tmp_ext.cookie_out_never = 0; /* false */
  1053. tmp_ext.cookie_plus = tmp_opt.cookie_plus;
  1054. } else if (!tp->rx_opt.cookie_in_always) {
  1055. /* redundant indications, but ensure initialization. */
  1056. tmp_ext.cookie_out_never = 1; /* true */
  1057. tmp_ext.cookie_plus = 0;
  1058. } else {
  1059. goto drop_and_free;
  1060. }
  1061. tmp_ext.cookie_in_always = tp->rx_opt.cookie_in_always;
  1062. if (want_cookie && !tmp_opt.saw_tstamp)
  1063. tcp_clear_options(&tmp_opt);
  1064. tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
  1065. tcp_openreq_init(req, &tmp_opt, skb);
  1066. treq = inet6_rsk(req);
  1067. ipv6_addr_copy(&treq->rmt_addr, &ipv6_hdr(skb)->saddr);
  1068. ipv6_addr_copy(&treq->loc_addr, &ipv6_hdr(skb)->daddr);
  1069. if (!want_cookie || tmp_opt.tstamp_ok)
  1070. TCP_ECN_create_request(req, tcp_hdr(skb));
  1071. if (!isn) {
  1072. struct inet_peer *peer = NULL;
  1073. if (ipv6_opt_accepted(sk, skb) ||
  1074. np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
  1075. np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
  1076. atomic_inc(&skb->users);
  1077. treq->pktopts = skb;
  1078. }
  1079. treq->iif = sk->sk_bound_dev_if;
  1080. /* So that link locals have meaning */
  1081. if (!sk->sk_bound_dev_if &&
  1082. ipv6_addr_type(&treq->rmt_addr) & IPV6_ADDR_LINKLOCAL)
  1083. treq->iif = inet6_iif(skb);
  1084. if (want_cookie) {
  1085. isn = cookie_v6_init_sequence(sk, skb, &req->mss);
  1086. req->cookie_ts = tmp_opt.tstamp_ok;
  1087. goto have_isn;
  1088. }
  1089. /* VJ's idea. We save last timestamp seen
  1090. * from the destination in peer table, when entering
  1091. * state TIME-WAIT, and check against it before
  1092. * accepting new connection request.
  1093. *
  1094. * If "isn" is not zero, this request hit alive
  1095. * timewait bucket, so that all the necessary checks
  1096. * are made in the function processing timewait state.
  1097. */
  1098. if (tmp_opt.saw_tstamp &&
  1099. tcp_death_row.sysctl_tw_recycle &&
  1100. (dst = inet6_csk_route_req(sk, req)) != NULL &&
  1101. (peer = rt6_get_peer((struct rt6_info *)dst)) != NULL &&
  1102. ipv6_addr_equal((struct in6_addr *)peer->daddr.addr.a6,
  1103. &treq->rmt_addr)) {
  1104. inet_peer_refcheck(peer);
  1105. if ((u32)get_seconds() - peer->tcp_ts_stamp < TCP_PAWS_MSL &&
  1106. (s32)(peer->tcp_ts - req->ts_recent) >
  1107. TCP_PAWS_WINDOW) {
  1108. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED);
  1109. goto drop_and_release;
  1110. }
  1111. }
  1112. /* Kill the following clause, if you dislike this way. */
  1113. else if (!sysctl_tcp_syncookies &&
  1114. (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
  1115. (sysctl_max_syn_backlog >> 2)) &&
  1116. (!peer || !peer->tcp_ts_stamp) &&
  1117. (!dst || !dst_metric(dst, RTAX_RTT))) {
  1118. /* Without syncookies last quarter of
  1119. * backlog is filled with destinations,
  1120. * proven to be alive.
  1121. * It means that we continue to communicate
  1122. * to destinations, already remembered
  1123. * to the moment of synflood.
  1124. */
  1125. LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open request from %pI6/%u\n",
  1126. &treq->rmt_addr, ntohs(tcp_hdr(skb)->source));
  1127. goto drop_and_release;
  1128. }
  1129. isn = tcp_v6_init_sequence(skb);
  1130. }
  1131. have_isn:
  1132. tcp_rsk(req)->snt_isn = isn;
  1133. security_inet_conn_request(sk, skb, req);
  1134. if (tcp_v6_send_synack(sk, req,
  1135. (struct request_values *)&tmp_ext) ||
  1136. want_cookie)
  1137. goto drop_and_free;
  1138. inet6_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
  1139. return 0;
  1140. drop_and_release:
  1141. dst_release(dst);
  1142. drop_and_free:
  1143. reqsk_free(req);
  1144. drop:
  1145. return 0; /* don't send reset */
  1146. }
  1147. static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
  1148. struct request_sock *req,
  1149. struct dst_entry *dst)
  1150. {
  1151. struct inet6_request_sock *treq;
  1152. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  1153. struct tcp6_sock *newtcp6sk;
  1154. struct inet_sock *newinet;
  1155. struct tcp_sock *newtp;
  1156. struct sock *newsk;
  1157. struct ipv6_txoptions *opt;
  1158. #ifdef CONFIG_TCP_MD5SIG
  1159. struct tcp_md5sig_key *key;
  1160. #endif
  1161. if (skb->protocol == htons(ETH_P_IP)) {
  1162. /*
  1163. * v6 mapped
  1164. */
  1165. newsk = tcp_v4_syn_recv_sock(sk, skb, req, dst);
  1166. if (newsk == NULL)
  1167. return NULL;
  1168. newtcp6sk = (struct tcp6_sock *)newsk;
  1169. inet_sk(newsk)->pinet6 = &newtcp6sk->inet6;
  1170. newinet = inet_sk(newsk);
  1171. newnp = inet6_sk(newsk);
  1172. newtp = tcp_sk(newsk);
  1173. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  1174. ipv6_addr_set_v4mapped(newinet->inet_daddr, &newnp->daddr);
  1175. ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
  1176. ipv6_addr_copy(&newnp->rcv_saddr, &newnp->saddr);
  1177. inet_csk(newsk)->icsk_af_ops = &ipv6_mapped;
  1178. newsk->sk_backlog_rcv = tcp_v4_do_rcv;
  1179. #ifdef CONFIG_TCP_MD5SIG
  1180. newtp->af_specific = &tcp_sock_ipv6_mapped_specific;
  1181. #endif
  1182. newnp->ipv6_ac_list = NULL;
  1183. newnp->ipv6_fl_list = NULL;
  1184. newnp->pktoptions = NULL;
  1185. newnp->opt = NULL;
  1186. newnp->mcast_oif = inet6_iif(skb);
  1187. newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
  1188. /*
  1189. * No need to charge this sock to the relevant IPv6 refcnt debug socks count
  1190. * here, tcp_create_openreq_child now does this for us, see the comment in
  1191. * that function for the gory details. -acme
  1192. */
  1193. /* It is tricky place. Until this moment IPv4 tcp
  1194. worked with IPv6 icsk.icsk_af_ops.
  1195. Sync it now.
  1196. */
  1197. tcp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
  1198. return newsk;
  1199. }
  1200. treq = inet6_rsk(req);
  1201. opt = np->opt;
  1202. if (sk_acceptq_is_full(sk))
  1203. goto out_overflow;
  1204. if (!dst) {
  1205. dst = inet6_csk_route_req(sk, req);
  1206. if (!dst)
  1207. goto out;
  1208. }
  1209. newsk = tcp_create_openreq_child(sk, req, skb);
  1210. if (newsk == NULL)
  1211. goto out_nonewsk;
  1212. /*
  1213. * No need to charge this sock to the relevant IPv6 refcnt debug socks
  1214. * count here, tcp_create_openreq_child now does this for us, see the
  1215. * comment in that function for the gory details. -acme
  1216. */
  1217. newsk->sk_gso_type = SKB_GSO_TCPV6;
  1218. __ip6_dst_store(newsk, dst, NULL, NULL);
  1219. newtcp6sk = (struct tcp6_sock *)newsk;
  1220. inet_sk(newsk)->pinet6 = &newtcp6sk->inet6;
  1221. newtp = tcp_sk(newsk);
  1222. newinet = inet_sk(newsk);
  1223. newnp = inet6_sk(newsk);
  1224. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  1225. ipv6_addr_copy(&newnp->daddr, &treq->rmt_addr);
  1226. ipv6_addr_copy(&newnp->saddr, &treq->loc_addr);
  1227. ipv6_addr_copy(&newnp->rcv_saddr, &treq->loc_addr);
  1228. newsk->sk_bound_dev_if = treq->iif;
  1229. /* Now IPv6 options...
  1230. First: no IPv4 options.
  1231. */
  1232. newinet->inet_opt = NULL;
  1233. newnp->ipv6_ac_list = NULL;
  1234. newnp->ipv6_fl_list = NULL;
  1235. /* Clone RX bits */
  1236. newnp->rxopt.all = np->rxopt.all;
  1237. /* Clone pktoptions received with SYN */
  1238. newnp->pktoptions = NULL;
  1239. if (treq->pktopts != NULL) {
  1240. newnp->pktoptions = skb_clone(treq->pktopts, GFP_ATOMIC);
  1241. kfree_skb(treq->pktopts);
  1242. treq->pktopts = NULL;
  1243. if (newnp->pktoptions)
  1244. skb_set_owner_r(newnp->pktoptions, newsk);
  1245. }
  1246. newnp->opt = NULL;
  1247. newnp->mcast_oif = inet6_iif(skb);
  1248. newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
  1249. /* Clone native IPv6 options from listening socket (if any)
  1250. Yes, keeping reference count would be much more clever,
  1251. but we make one more one thing there: reattach optmem
  1252. to newsk.
  1253. */
  1254. if (opt) {
  1255. newnp->opt = ipv6_dup_options(newsk, opt);
  1256. if (opt != np->opt)
  1257. sock_kfree_s(sk, opt, opt->tot_len);
  1258. }
  1259. inet_csk(newsk)->icsk_ext_hdr_len = 0;
  1260. if (newnp->opt)
  1261. inet_csk(newsk)->icsk_ext_hdr_len = (newnp->opt->opt_nflen +
  1262. newnp->opt->opt_flen);
  1263. tcp_mtup_init(newsk);
  1264. tcp_sync_mss(newsk, dst_mtu(dst));
  1265. newtp->advmss = dst_metric_advmss(dst);
  1266. if (tcp_sk(sk)->rx_opt.user_mss &&
  1267. tcp_sk(sk)->rx_opt.user_mss < newtp->advmss)
  1268. newtp->advmss = tcp_sk(sk)->rx_opt.user_mss;
  1269. tcp_initialize_rcv_mss(newsk);
  1270. newinet->inet_daddr = newinet->inet_saddr = LOOPBACK4_IPV6;
  1271. newinet->inet_rcv_saddr = LOOPBACK4_IPV6;
  1272. #ifdef CONFIG_TCP_MD5SIG
  1273. /* Copy over the MD5 key from the original socket */
  1274. if ((key = tcp_v6_md5_do_lookup(sk, &newnp->daddr)) != NULL) {
  1275. /* We're using one, so create a matching key
  1276. * on the newsk structure. If we fail to get
  1277. * memory, then we end up not copying the key
  1278. * across. Shucks.
  1279. */
  1280. char *newkey = kmemdup(key->key, key->keylen, GFP_ATOMIC);
  1281. if (newkey != NULL)
  1282. tcp_v6_md5_do_add(newsk, &newnp->daddr,
  1283. newkey, key->keylen);
  1284. }
  1285. #endif
  1286. if (__inet_inherit_port(sk, newsk) < 0) {
  1287. sock_put(newsk);
  1288. goto out;
  1289. }
  1290. __inet6_hash(newsk, NULL);
  1291. return newsk;
  1292. out_overflow:
  1293. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
  1294. out_nonewsk:
  1295. if (opt && opt != np->opt)
  1296. sock_kfree_s(sk, opt, opt->tot_len);
  1297. dst_release(dst);
  1298. out:
  1299. NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
  1300. return NULL;
  1301. }
  1302. static __sum16 tcp_v6_checksum_init(struct sk_buff *skb)
  1303. {
  1304. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  1305. if (!tcp_v6_check(skb->len, &ipv6_hdr(skb)->saddr,
  1306. &ipv6_hdr(skb)->daddr, skb->csum)) {
  1307. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1308. return 0;
  1309. }
  1310. }
  1311. skb->csum = ~csum_unfold(tcp_v6_check(skb->len,
  1312. &ipv6_hdr(skb)->saddr,
  1313. &ipv6_hdr(skb)->daddr, 0));
  1314. if (skb->len <= 76) {
  1315. return __skb_checksum_complete(skb);
  1316. }
  1317. return 0;
  1318. }
  1319. /* The socket must have it's spinlock held when we get
  1320. * here.
  1321. *
  1322. * We have a potential double-lock case here, so even when
  1323. * doing backlog processing we use the BH locking scheme.
  1324. * This is because we cannot sleep with the original spinlock
  1325. * held.
  1326. */
  1327. static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
  1328. {
  1329. struct ipv6_pinfo *np = inet6_sk(sk);
  1330. struct tcp_sock *tp;
  1331. struct sk_buff *opt_skb = NULL;
  1332. /* Imagine: socket is IPv6. IPv4 packet arrives,
  1333. goes to IPv4 receive handler and backlogged.
  1334. From backlog it always goes here. Kerboom...
  1335. Fortunately, tcp_rcv_established and rcv_established
  1336. handle them correctly, but it is not case with
  1337. tcp_v6_hnd_req and tcp_v6_send_reset(). --ANK
  1338. */
  1339. if (skb->protocol == htons(ETH_P_IP))
  1340. return tcp_v4_do_rcv(sk, skb);
  1341. #ifdef CONFIG_TCP_MD5SIG
  1342. if (tcp_v6_inbound_md5_hash (sk, skb))
  1343. goto discard;
  1344. #endif
  1345. if (sk_filter(sk, skb))
  1346. goto discard;
  1347. /*
  1348. * socket locking is here for SMP purposes as backlog rcv
  1349. * is currently called with bh processing disabled.
  1350. */
  1351. /* Do Stevens' IPV6_PKTOPTIONS.
  1352. Yes, guys, it is the only place in our code, where we
  1353. may make it not affecting IPv4.
  1354. The rest of code is protocol independent,
  1355. and I do not like idea to uglify IPv4.
  1356. Actually, all the idea behind IPV6_PKTOPTIONS
  1357. looks not very well thought. For now we latch
  1358. options, received in the last packet, enqueued
  1359. by tcp. Feel free to propose better solution.
  1360. --ANK (980728)
  1361. */
  1362. if (np->rxopt.all)
  1363. opt_skb = skb_clone(skb, GFP_ATOMIC);
  1364. if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
  1365. sock_rps_save_rxhash(sk, skb->rxhash);
  1366. if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len))
  1367. goto reset;
  1368. if (opt_skb)
  1369. goto ipv6_pktoptions;
  1370. return 0;
  1371. }
  1372. if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
  1373. goto csum_err;
  1374. if (sk->sk_state == TCP_LISTEN) {
  1375. struct sock *nsk = tcp_v6_hnd_req(sk, skb);
  1376. if (!nsk)
  1377. goto discard;
  1378. /*
  1379. * Queue it on the new socket if the new socket is active,
  1380. * otherwise we just shortcircuit this and continue with
  1381. * the new socket..
  1382. */
  1383. if(nsk != sk) {
  1384. sock_rps_save_rxhash(nsk, skb->rxhash);
  1385. if (tcp_child_process(sk, nsk, skb))
  1386. goto reset;
  1387. if (opt_skb)
  1388. __kfree_skb(opt_skb);
  1389. return 0;
  1390. }
  1391. } else
  1392. sock_rps_save_rxhash(sk, skb->rxhash);
  1393. if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len))
  1394. goto reset;
  1395. if (opt_skb)
  1396. goto ipv6_pktoptions;
  1397. return 0;
  1398. reset:
  1399. tcp_v6_send_reset(sk, skb);
  1400. discard:
  1401. if (opt_skb)
  1402. __kfree_skb(opt_skb);
  1403. kfree_skb(skb);
  1404. return 0;
  1405. csum_err:
  1406. TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
  1407. goto discard;
  1408. ipv6_pktoptions:
  1409. /* Do you ask, what is it?
  1410. 1. skb was enqueued by tcp.
  1411. 2. skb is added to tail of read queue, rather than out of order.
  1412. 3. socket is not in passive state.
  1413. 4. Finally, it really contains options, which user wants to receive.
  1414. */
  1415. tp = tcp_sk(sk);
  1416. if (TCP_SKB_CB(opt_skb)->end_seq == tp->rcv_nxt &&
  1417. !((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) {
  1418. if (np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo)
  1419. np->mcast_oif = inet6_iif(opt_skb);
  1420. if (np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim)
  1421. np->mcast_hops = ipv6_hdr(opt_skb)->hop_limit;
  1422. if (ipv6_opt_accepted(sk, opt_skb)) {
  1423. skb_set_owner_r(opt_skb, sk);
  1424. opt_skb = xchg(&np->pktoptions, opt_skb);
  1425. } else {
  1426. __kfree_skb(opt_skb);
  1427. opt_skb = xchg(&np->pktoptions, NULL);
  1428. }
  1429. }
  1430. kfree_skb(opt_skb);
  1431. return 0;
  1432. }
  1433. static int tcp_v6_rcv(struct sk_buff *skb)
  1434. {
  1435. struct tcphdr *th;
  1436. const struct ipv6hdr *hdr;
  1437. struct sock *sk;
  1438. int ret;
  1439. struct net *net = dev_net(skb->dev);
  1440. if (skb->pkt_type != PACKET_HOST)
  1441. goto discard_it;
  1442. /*
  1443. * Count it even if it's bad.
  1444. */
  1445. TCP_INC_STATS_BH(net, TCP_MIB_INSEGS);
  1446. if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
  1447. goto discard_it;
  1448. th = tcp_hdr(skb);
  1449. if (th->doff < sizeof(struct tcphdr)/4)
  1450. goto bad_packet;
  1451. if (!pskb_may_pull(skb, th->doff*4))
  1452. goto discard_it;
  1453. if (!skb_csum_unnecessary(skb) && tcp_v6_checksum_init(skb))
  1454. goto bad_packet;
  1455. th = tcp_hdr(skb);
  1456. hdr = ipv6_hdr(skb);
  1457. TCP_SKB_CB(skb)->seq = ntohl(th->seq);
  1458. TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
  1459. skb->len - th->doff*4);
  1460. TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
  1461. TCP_SKB_CB(skb)->when = 0;
  1462. TCP_SKB_CB(skb)->flags = ipv6_get_dsfield(hdr);
  1463. TCP_SKB_CB(skb)->sacked = 0;
  1464. sk = __inet6_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
  1465. if (!sk)
  1466. goto no_tcp_socket;
  1467. process:
  1468. if (sk->sk_state == TCP_TIME_WAIT)
  1469. goto do_time_wait;
  1470. if (hdr->hop_limit < inet6_sk(sk)->min_hopcount) {
  1471. NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
  1472. goto discard_and_relse;
  1473. }
  1474. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
  1475. goto discard_and_relse;
  1476. if (sk_filter(sk, skb))
  1477. goto discard_and_relse;
  1478. skb->dev = NULL;
  1479. bh_lock_sock_nested(sk);
  1480. ret = 0;
  1481. if (!sock_owned_by_user(sk)) {
  1482. #ifdef CONFIG_NET_DMA
  1483. struct tcp_sock *tp = tcp_sk(sk);
  1484. if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
  1485. tp->ucopy.dma_chan = dma_find_channel(DMA_MEMCPY);
  1486. if (tp->ucopy.dma_chan)
  1487. ret = tcp_v6_do_rcv(sk, skb);
  1488. else
  1489. #endif
  1490. {
  1491. if (!tcp_prequeue(sk, skb))
  1492. ret = tcp_v6_do_rcv(sk, skb);
  1493. }
  1494. } else if (unlikely(sk_add_backlog(sk, skb))) {
  1495. bh_unlock_sock(sk);
  1496. NET_INC_STATS_BH(net, LINUX_MIB_TCPBACKLOGDROP);
  1497. goto discard_and_relse;
  1498. }
  1499. bh_unlock_sock(sk);
  1500. sock_put(sk);
  1501. return ret ? -1 : 0;
  1502. no_tcp_socket:
  1503. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  1504. goto discard_it;
  1505. if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
  1506. bad_packet:
  1507. TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
  1508. } else {
  1509. tcp_v6_send_reset(NULL, skb);
  1510. }
  1511. discard_it:
  1512. /*
  1513. * Discard frame
  1514. */
  1515. kfree_skb(skb);
  1516. return 0;
  1517. discard_and_relse:
  1518. sock_put(sk);
  1519. goto discard_it;
  1520. do_time_wait:
  1521. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
  1522. inet_twsk_put(inet_twsk(sk));
  1523. goto discard_it;
  1524. }
  1525. if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
  1526. TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
  1527. inet_twsk_put(inet_twsk(sk));
  1528. goto discard_it;
  1529. }
  1530. switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
  1531. case TCP_TW_SYN:
  1532. {
  1533. struct sock *sk2;
  1534. sk2 = inet6_lookup_listener(dev_net(skb->dev), &tcp_hashinfo,
  1535. &ipv6_hdr(skb)->daddr,
  1536. ntohs(th->dest), inet6_iif(skb));
  1537. if (sk2 != NULL) {
  1538. struct inet_timewait_sock *tw = inet_twsk(sk);
  1539. inet_twsk_deschedule(tw, &tcp_death_row);
  1540. inet_twsk_put(tw);
  1541. sk = sk2;
  1542. goto process;
  1543. }
  1544. /* Fall through to ACK */
  1545. }
  1546. case TCP_TW_ACK:
  1547. tcp_v6_timewait_ack(sk, skb);
  1548. break;
  1549. case TCP_TW_RST:
  1550. goto no_tcp_socket;
  1551. case TCP_TW_SUCCESS:;
  1552. }
  1553. goto discard_it;
  1554. }
  1555. static struct inet_peer *tcp_v6_get_peer(struct sock *sk, bool *release_it)
  1556. {
  1557. struct rt6_info *rt = (struct rt6_info *) __sk_dst_get(sk);
  1558. struct ipv6_pinfo *np = inet6_sk(sk);
  1559. struct inet_peer *peer;
  1560. if (!rt ||
  1561. !ipv6_addr_equal(&np->daddr, &rt->rt6i_dst.addr)) {
  1562. peer = inet_getpeer_v6(&np->daddr, 1);
  1563. *release_it = true;
  1564. } else {
  1565. if (!rt->rt6i_peer)
  1566. rt6_bind_peer(rt, 1);
  1567. peer = rt->rt6i_peer;
  1568. *release_it = false;
  1569. }
  1570. return peer;
  1571. }
  1572. static void *tcp_v6_tw_get_peer(struct sock *sk)
  1573. {
  1574. struct inet6_timewait_sock *tw6 = inet6_twsk(sk);
  1575. struct inet_timewait_sock *tw = inet_twsk(sk);
  1576. if (tw->tw_family == AF_INET)
  1577. return tcp_v4_tw_get_peer(sk);
  1578. return inet_getpeer_v6(&tw6->tw_v6_daddr, 1);
  1579. }
  1580. static struct timewait_sock_ops tcp6_timewait_sock_ops = {
  1581. .twsk_obj_size = sizeof(struct tcp6_timewait_sock),
  1582. .twsk_unique = tcp_twsk_unique,
  1583. .twsk_destructor= tcp_twsk_destructor,
  1584. .twsk_getpeer = tcp_v6_tw_get_peer,
  1585. };
  1586. static const struct inet_connection_sock_af_ops ipv6_specific = {
  1587. .queue_xmit = inet6_csk_xmit,
  1588. .send_check = tcp_v6_send_check,
  1589. .rebuild_header = inet6_sk_rebuild_header,
  1590. .conn_request = tcp_v6_conn_request,
  1591. .syn_recv_sock = tcp_v6_syn_recv_sock,
  1592. .get_peer = tcp_v6_get_peer,
  1593. .net_header_len = sizeof(struct ipv6hdr),
  1594. .setsockopt = ipv6_setsockopt,
  1595. .getsockopt = ipv6_getsockopt,
  1596. .addr2sockaddr = inet6_csk_addr2sockaddr,
  1597. .sockaddr_len = sizeof(struct sockaddr_in6),
  1598. .bind_conflict = inet6_csk_bind_conflict,
  1599. #ifdef CONFIG_COMPAT
  1600. .compat_setsockopt = compat_ipv6_setsockopt,
  1601. .compat_getsockopt = compat_ipv6_getsockopt,
  1602. #endif
  1603. };
  1604. #ifdef CONFIG_TCP_MD5SIG
  1605. static const struct tcp_sock_af_ops tcp_sock_ipv6_specific = {
  1606. .md5_lookup = tcp_v6_md5_lookup,
  1607. .calc_md5_hash = tcp_v6_md5_hash_skb,
  1608. .md5_add = tcp_v6_md5_add_func,
  1609. .md5_parse = tcp_v6_parse_md5_keys,
  1610. };
  1611. #endif
  1612. /*
  1613. * TCP over IPv4 via INET6 API
  1614. */
  1615. static const struct inet_connection_sock_af_ops ipv6_mapped = {
  1616. .queue_xmit = ip_queue_xmit,
  1617. .send_check = tcp_v4_send_check,
  1618. .rebuild_header = inet_sk_rebuild_header,
  1619. .conn_request = tcp_v6_conn_request,
  1620. .syn_recv_sock = tcp_v6_syn_recv_sock,
  1621. .get_peer = tcp_v4_get_peer,
  1622. .net_header_len = sizeof(struct iphdr),
  1623. .setsockopt = ipv6_setsockopt,
  1624. .getsockopt = ipv6_getsockopt,
  1625. .addr2sockaddr = inet6_csk_addr2sockaddr,
  1626. .sockaddr_len = sizeof(struct sockaddr_in6),
  1627. .bind_conflict = inet6_csk_bind_conflict,
  1628. #ifdef CONFIG_COMPAT
  1629. .compat_setsockopt = compat_ipv6_setsockopt,
  1630. .compat_getsockopt = compat_ipv6_getsockopt,
  1631. #endif
  1632. };
  1633. #ifdef CONFIG_TCP_MD5SIG
  1634. static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific = {
  1635. .md5_lookup = tcp_v4_md5_lookup,
  1636. .calc_md5_hash = tcp_v4_md5_hash_skb,
  1637. .md5_add = tcp_v6_md5_add_func,
  1638. .md5_parse = tcp_v6_parse_md5_keys,
  1639. };
  1640. #endif
  1641. /* NOTE: A lot of things set to zero explicitly by call to
  1642. * sk_alloc() so need not be done here.
  1643. */
  1644. static int tcp_v6_init_sock(struct sock *sk)
  1645. {
  1646. struct inet_connection_sock *icsk = inet_csk(sk);
  1647. struct tcp_sock *tp = tcp_sk(sk);
  1648. skb_queue_head_init(&tp->out_of_order_queue);
  1649. tcp_init_xmit_timers(sk);
  1650. tcp_prequeue_init(tp);
  1651. icsk->icsk_rto = TCP_TIMEOUT_INIT;
  1652. tp->mdev = TCP_TIMEOUT_INIT;
  1653. /* So many TCP implementations out there (incorrectly) count the
  1654. * initial SYN frame in their delayed-ACK and congestion control
  1655. * algorithms that we must have the following bandaid to talk
  1656. * efficiently to them. -DaveM
  1657. */
  1658. tp->snd_cwnd = 2;
  1659. /* See draft-stevens-tcpca-spec-01 for discussion of the
  1660. * initialization of these values.
  1661. */
  1662. tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
  1663. tp->snd_cwnd_clamp = ~0;
  1664. tp->mss_cache = TCP_MSS_DEFAULT;
  1665. tp->reordering = sysctl_tcp_reordering;
  1666. sk->sk_state = TCP_CLOSE;
  1667. icsk->icsk_af_ops = &ipv6_specific;
  1668. icsk->icsk_ca_ops = &tcp_init_congestion_ops;
  1669. icsk->icsk_sync_mss = tcp_sync_mss;
  1670. sk->sk_write_space = sk_stream_write_space;
  1671. sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
  1672. #ifdef CONFIG_TCP_MD5SIG
  1673. tp->af_specific = &tcp_sock_ipv6_specific;
  1674. #endif
  1675. /* TCP Cookie Transactions */
  1676. if (sysctl_tcp_cookie_size > 0) {
  1677. /* Default, cookies without s_data_payload. */
  1678. tp->cookie_values =
  1679. kzalloc(sizeof(*tp->cookie_values),
  1680. sk->sk_allocation);
  1681. if (tp->cookie_values != NULL)
  1682. kref_init(&tp->cookie_values->kref);
  1683. }
  1684. /* Presumed zeroed, in order of appearance:
  1685. * cookie_in_always, cookie_out_never,
  1686. * s_data_constant, s_data_in, s_data_out
  1687. */
  1688. sk->sk_sndbuf = sysctl_tcp_wmem[1];
  1689. sk->sk_rcvbuf = sysctl_tcp_rmem[1];
  1690. local_bh_disable();
  1691. percpu_counter_inc(&tcp_sockets_allocated);
  1692. local_bh_enable();
  1693. return 0;
  1694. }
  1695. static void tcp_v6_destroy_sock(struct sock *sk)
  1696. {
  1697. #ifdef CONFIG_TCP_MD5SIG
  1698. /* Clean up the MD5 key list */
  1699. if (tcp_sk(sk)->md5sig_info)
  1700. tcp_v6_clear_md5_list(sk);
  1701. #endif
  1702. tcp_v4_destroy_sock(sk);
  1703. inet6_destroy_sock(sk);
  1704. }
  1705. #ifdef CONFIG_PROC_FS
  1706. /* Proc filesystem TCPv6 sock list dumping. */
  1707. static void get_openreq6(struct seq_file *seq,
  1708. struct sock *sk, struct request_sock *req, int i, int uid)
  1709. {
  1710. int ttd = req->expires - jiffies;
  1711. const struct in6_addr *src = &inet6_rsk(req)->loc_addr;
  1712. const struct in6_addr *dest = &inet6_rsk(req)->rmt_addr;
  1713. if (ttd < 0)
  1714. ttd = 0;
  1715. seq_printf(seq,
  1716. "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
  1717. "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
  1718. i,
  1719. src->s6_addr32[0], src->s6_addr32[1],
  1720. src->s6_addr32[2], src->s6_addr32[3],
  1721. ntohs(inet_rsk(req)->loc_port),
  1722. dest->s6_addr32[0], dest->s6_addr32[1],
  1723. dest->s6_addr32[2], dest->s6_addr32[3],
  1724. ntohs(inet_rsk(req)->rmt_port),
  1725. TCP_SYN_RECV,
  1726. 0,0, /* could print option size, but that is af dependent. */
  1727. 1, /* timers active (only the expire timer) */
  1728. jiffies_to_clock_t(ttd),
  1729. req->retrans,
  1730. uid,
  1731. 0, /* non standard timer */
  1732. 0, /* open_requests have no inode */
  1733. 0, req);
  1734. }
  1735. static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
  1736. {
  1737. const struct in6_addr *dest, *src;
  1738. __u16 destp, srcp;
  1739. int timer_active;
  1740. unsigned long timer_expires;
  1741. struct inet_sock *inet = inet_sk(sp);
  1742. struct tcp_sock *tp = tcp_sk(sp);
  1743. const struct inet_connection_sock *icsk = inet_csk(sp);
  1744. struct ipv6_pinfo *np = inet6_sk(sp);
  1745. dest = &np->daddr;
  1746. src = &np->rcv_saddr;
  1747. destp = ntohs(inet->inet_dport);
  1748. srcp = ntohs(inet->inet_sport);
  1749. if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
  1750. timer_active = 1;
  1751. timer_expires = icsk->icsk_timeout;
  1752. } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
  1753. timer_active = 4;
  1754. timer_expires = icsk->icsk_timeout;
  1755. } else if (timer_pending(&sp->sk_timer)) {
  1756. timer_active = 2;
  1757. timer_expires = sp->sk_timer.expires;
  1758. } else {
  1759. timer_active = 0;
  1760. timer_expires = jiffies;
  1761. }
  1762. seq_printf(seq,
  1763. "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
  1764. "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %lu %lu %u %u %d\n",
  1765. i,
  1766. src->s6_addr32[0], src->s6_addr32[1],
  1767. src->s6_addr32[2], src->s6_addr32[3], srcp,
  1768. dest->s6_addr32[0], dest->s6_addr32[1],
  1769. dest->s6_addr32[2], dest->s6_addr32[3], destp,
  1770. sp->sk_state,
  1771. tp->write_seq-tp->snd_una,
  1772. (sp->sk_state == TCP_LISTEN) ? sp->sk_ack_backlog : (tp->rcv_nxt - tp->copied_seq),
  1773. timer_active,
  1774. jiffies_to_clock_t(timer_expires - jiffies),
  1775. icsk->icsk_retransmits,
  1776. sock_i_uid(sp),
  1777. icsk->icsk_probes_out,
  1778. sock_i_ino(sp),
  1779. atomic_read(&sp->sk_refcnt), sp,
  1780. jiffies_to_clock_t(icsk->icsk_rto),
  1781. jiffies_to_clock_t(icsk->icsk_ack.ato),
  1782. (icsk->icsk_ack.quick << 1 ) | icsk->icsk_ack.pingpong,
  1783. tp->snd_cwnd,
  1784. tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh
  1785. );
  1786. }
  1787. static void get_timewait6_sock(struct seq_file *seq,
  1788. struct inet_timewait_sock *tw, int i)
  1789. {
  1790. const struct in6_addr *dest, *src;
  1791. __u16 destp, srcp;
  1792. struct inet6_timewait_sock *tw6 = inet6_twsk((struct sock *)tw);
  1793. int ttd = tw->tw_ttd - jiffies;
  1794. if (ttd < 0)
  1795. ttd = 0;
  1796. dest = &tw6->tw_v6_daddr;
  1797. src = &tw6->tw_v6_rcv_saddr;
  1798. destp = ntohs(tw->tw_dport);
  1799. srcp = ntohs(tw->tw_sport);
  1800. seq_printf(seq,
  1801. "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
  1802. "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
  1803. i,
  1804. src->s6_addr32[0], src->s6_addr32[1],
  1805. src->s6_addr32[2], src->s6_addr32[3], srcp,
  1806. dest->s6_addr32[0], dest->s6_addr32[1],
  1807. dest->s6_addr32[2], dest->s6_addr32[3], destp,
  1808. tw->tw_substate, 0, 0,
  1809. 3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
  1810. atomic_read(&tw->tw_refcnt), tw);
  1811. }
  1812. static int tcp6_seq_show(struct seq_file *seq, void *v)
  1813. {
  1814. struct tcp_iter_state *st;
  1815. if (v == SEQ_START_TOKEN) {
  1816. seq_puts(seq,
  1817. " sl "
  1818. "local_address "
  1819. "remote_address "
  1820. "st tx_queue rx_queue tr tm->when retrnsmt"
  1821. " uid timeout inode\n");
  1822. goto out;
  1823. }
  1824. st = seq->private;
  1825. switch (st->state) {
  1826. case TCP_SEQ_STATE_LISTENING:
  1827. case TCP_SEQ_STATE_ESTABLISHED:
  1828. get_tcp6_sock(seq, v, st->num);
  1829. break;
  1830. case TCP_SEQ_STATE_OPENREQ:
  1831. get_openreq6(seq, st->syn_wait_sk, v, st->num, st->uid);
  1832. break;
  1833. case TCP_SEQ_STATE_TIME_WAIT:
  1834. get_timewait6_sock(seq, v, st->num);
  1835. break;
  1836. }
  1837. out:
  1838. return 0;
  1839. }
  1840. static struct tcp_seq_afinfo tcp6_seq_afinfo = {
  1841. .name = "tcp6",
  1842. .family = AF_INET6,
  1843. .seq_fops = {
  1844. .owner = THIS_MODULE,
  1845. },
  1846. .seq_ops = {
  1847. .show = tcp6_seq_show,
  1848. },
  1849. };
  1850. int __net_init tcp6_proc_init(struct net *net)
  1851. {
  1852. return tcp_proc_register(net, &tcp6_seq_afinfo);
  1853. }
  1854. void tcp6_proc_exit(struct net *net)
  1855. {
  1856. tcp_proc_unregister(net, &tcp6_seq_afinfo);
  1857. }
  1858. #endif
  1859. struct proto tcpv6_prot = {
  1860. .name = "TCPv6",
  1861. .owner = THIS_MODULE,
  1862. .close = tcp_close,
  1863. .connect = tcp_v6_connect,
  1864. .disconnect = tcp_disconnect,
  1865. .accept = inet_csk_accept,
  1866. .ioctl = tcp_ioctl,
  1867. .init = tcp_v6_init_sock,
  1868. .destroy = tcp_v6_destroy_sock,
  1869. .shutdown = tcp_shutdown,
  1870. .setsockopt = tcp_setsockopt,
  1871. .getsockopt = tcp_getsockopt,
  1872. .recvmsg = tcp_recvmsg,
  1873. .sendmsg = tcp_sendmsg,
  1874. .sendpage = tcp_sendpage,
  1875. .backlog_rcv = tcp_v6_do_rcv,
  1876. .hash = tcp_v6_hash,
  1877. .unhash = inet_unhash,
  1878. .get_port = inet_csk_get_port,
  1879. .enter_memory_pressure = tcp_enter_memory_pressure,
  1880. .sockets_allocated = &tcp_sockets_allocated,
  1881. .memory_allocated = &tcp_memory_allocated,
  1882. .memory_pressure = &tcp_memory_pressure,
  1883. .orphan_count = &tcp_orphan_count,
  1884. .sysctl_mem = sysctl_tcp_mem,
  1885. .sysctl_wmem = sysctl_tcp_wmem,
  1886. .sysctl_rmem = sysctl_tcp_rmem,
  1887. .max_header = MAX_TCP_HEADER,
  1888. .obj_size = sizeof(struct tcp6_sock),
  1889. .slab_flags = SLAB_DESTROY_BY_RCU,
  1890. .twsk_prot = &tcp6_timewait_sock_ops,
  1891. .rsk_prot = &tcp6_request_sock_ops,
  1892. .h.hashinfo = &tcp_hashinfo,
  1893. .no_autobind = true,
  1894. #ifdef CONFIG_COMPAT
  1895. .compat_setsockopt = compat_tcp_setsockopt,
  1896. .compat_getsockopt = compat_tcp_getsockopt,
  1897. #endif
  1898. };
  1899. static const struct inet6_protocol tcpv6_protocol = {
  1900. .handler = tcp_v6_rcv,
  1901. .err_handler = tcp_v6_err,
  1902. .gso_send_check = tcp_v6_gso_send_check,
  1903. .gso_segment = tcp_tso_segment,
  1904. .gro_receive = tcp6_gro_receive,
  1905. .gro_complete = tcp6_gro_complete,
  1906. .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
  1907. };
  1908. static struct inet_protosw tcpv6_protosw = {
  1909. .type = SOCK_STREAM,
  1910. .protocol = IPPROTO_TCP,
  1911. .prot = &tcpv6_prot,
  1912. .ops = &inet6_stream_ops,
  1913. .no_check = 0,
  1914. .flags = INET_PROTOSW_PERMANENT |
  1915. INET_PROTOSW_ICSK,
  1916. };
  1917. static int __net_init tcpv6_net_init(struct net *net)
  1918. {
  1919. return inet_ctl_sock_create(&net->ipv6.tcp_sk, PF_INET6,
  1920. SOCK_RAW, IPPROTO_TCP, net);
  1921. }
  1922. static void __net_exit tcpv6_net_exit(struct net *net)
  1923. {
  1924. inet_ctl_sock_destroy(net->ipv6.tcp_sk);
  1925. }
  1926. static void __net_exit tcpv6_net_exit_batch(struct list_head *net_exit_list)
  1927. {
  1928. inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET6);
  1929. }
  1930. static struct pernet_operations tcpv6_net_ops = {
  1931. .init = tcpv6_net_init,
  1932. .exit = tcpv6_net_exit,
  1933. .exit_batch = tcpv6_net_exit_batch,
  1934. };
  1935. int __init tcpv6_init(void)
  1936. {
  1937. int ret;
  1938. ret = inet6_add_protocol(&tcpv6_protocol, IPPROTO_TCP);
  1939. if (ret)
  1940. goto out;
  1941. /* register inet6 protocol */
  1942. ret = inet6_register_protosw(&tcpv6_protosw);
  1943. if (ret)
  1944. goto out_tcpv6_protocol;
  1945. ret = register_pernet_subsys(&tcpv6_net_ops);
  1946. if (ret)
  1947. goto out_tcpv6_protosw;
  1948. out:
  1949. return ret;
  1950. out_tcpv6_protocol:
  1951. inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);
  1952. out_tcpv6_protosw:
  1953. inet6_unregister_protosw(&tcpv6_protosw);
  1954. goto out;
  1955. }
  1956. void tcpv6_exit(void)
  1957. {
  1958. unregister_pernet_subsys(&tcpv6_net_ops);
  1959. inet6_unregister_protosw(&tcpv6_protosw);
  1960. inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);
  1961. }