ipv6.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. /*
  2. * DCCP over IPv6
  3. * Linux INET6 implementation
  4. *
  5. * Based on net/dccp6/ipv6.c
  6. *
  7. * Arnaldo Carvalho de Melo <acme@ghostprotocols.net>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/random.h>
  16. #include <linux/slab.h>
  17. #include <linux/xfrm.h>
  18. #include <net/addrconf.h>
  19. #include <net/inet_common.h>
  20. #include <net/inet_hashtables.h>
  21. #include <net/inet_sock.h>
  22. #include <net/inet6_connection_sock.h>
  23. #include <net/inet6_hashtables.h>
  24. #include <net/ip6_route.h>
  25. #include <net/ipv6.h>
  26. #include <net/protocol.h>
  27. #include <net/transp_v6.h>
  28. #include <net/ip6_checksum.h>
  29. #include <net/xfrm.h>
  30. #include <net/secure_seq.h>
  31. #include "dccp.h"
  32. #include "ipv6.h"
  33. #include "feat.h"
  34. /* The per-net dccp.v6_ctl_sk is used for sending RSTs and ACKs */
  35. static const struct inet_connection_sock_af_ops dccp_ipv6_mapped;
  36. static const struct inet_connection_sock_af_ops dccp_ipv6_af_ops;
  37. /* add pseudo-header to DCCP checksum stored in skb->csum */
  38. static inline __sum16 dccp_v6_csum_finish(struct sk_buff *skb,
  39. const struct in6_addr *saddr,
  40. const struct in6_addr *daddr)
  41. {
  42. return csum_ipv6_magic(saddr, daddr, skb->len, IPPROTO_DCCP, skb->csum);
  43. }
  44. static inline void dccp_v6_send_check(struct sock *sk, struct sk_buff *skb)
  45. {
  46. struct ipv6_pinfo *np = inet6_sk(sk);
  47. struct dccp_hdr *dh = dccp_hdr(skb);
  48. dccp_csum_outgoing(skb);
  49. dh->dccph_checksum = dccp_v6_csum_finish(skb, &np->saddr, &sk->sk_v6_daddr);
  50. }
  51. static inline __u64 dccp_v6_init_sequence(struct sk_buff *skb)
  52. {
  53. return secure_dccpv6_sequence_number(ipv6_hdr(skb)->daddr.s6_addr32,
  54. ipv6_hdr(skb)->saddr.s6_addr32,
  55. dccp_hdr(skb)->dccph_dport,
  56. dccp_hdr(skb)->dccph_sport );
  57. }
  58. static void dccp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  59. u8 type, u8 code, int offset, __be32 info)
  60. {
  61. const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
  62. const struct dccp_hdr *dh;
  63. struct dccp_sock *dp;
  64. struct ipv6_pinfo *np;
  65. struct sock *sk;
  66. int err;
  67. __u64 seq;
  68. struct net *net = dev_net(skb->dev);
  69. /* Only need dccph_dport & dccph_sport which are the first
  70. * 4 bytes in dccp header.
  71. * Our caller (icmpv6_notify()) already pulled 8 bytes for us.
  72. */
  73. BUILD_BUG_ON(offsetofend(struct dccp_hdr, dccph_sport) > 8);
  74. BUILD_BUG_ON(offsetofend(struct dccp_hdr, dccph_dport) > 8);
  75. dh = (struct dccp_hdr *)(skb->data + offset);
  76. sk = __inet6_lookup_established(net, &dccp_hashinfo,
  77. &hdr->daddr, dh->dccph_dport,
  78. &hdr->saddr, ntohs(dh->dccph_sport),
  79. inet6_iif(skb));
  80. if (!sk) {
  81. __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
  82. ICMP6_MIB_INERRORS);
  83. return;
  84. }
  85. if (sk->sk_state == DCCP_TIME_WAIT) {
  86. inet_twsk_put(inet_twsk(sk));
  87. return;
  88. }
  89. seq = dccp_hdr_seq(dh);
  90. if (sk->sk_state == DCCP_NEW_SYN_RECV)
  91. return dccp_req_err(sk, seq);
  92. bh_lock_sock(sk);
  93. if (sock_owned_by_user(sk))
  94. __NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
  95. if (sk->sk_state == DCCP_CLOSED)
  96. goto out;
  97. dp = dccp_sk(sk);
  98. if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_LISTEN) &&
  99. !between48(seq, dp->dccps_awl, dp->dccps_awh)) {
  100. __NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
  101. goto out;
  102. }
  103. np = inet6_sk(sk);
  104. if (type == NDISC_REDIRECT) {
  105. if (!sock_owned_by_user(sk)) {
  106. struct dst_entry *dst = __sk_dst_check(sk, np->dst_cookie);
  107. if (dst)
  108. dst->ops->redirect(dst, sk, skb);
  109. }
  110. goto out;
  111. }
  112. if (type == ICMPV6_PKT_TOOBIG) {
  113. struct dst_entry *dst = NULL;
  114. if (!ip6_sk_accept_pmtu(sk))
  115. goto out;
  116. if (sock_owned_by_user(sk))
  117. goto out;
  118. if ((1 << sk->sk_state) & (DCCPF_LISTEN | DCCPF_CLOSED))
  119. goto out;
  120. dst = inet6_csk_update_pmtu(sk, ntohl(info));
  121. if (!dst)
  122. goto out;
  123. if (inet_csk(sk)->icsk_pmtu_cookie > dst_mtu(dst))
  124. dccp_sync_mss(sk, dst_mtu(dst));
  125. goto out;
  126. }
  127. icmpv6_err_convert(type, code, &err);
  128. /* Might be for an request_sock */
  129. switch (sk->sk_state) {
  130. case DCCP_REQUESTING:
  131. case DCCP_RESPOND: /* Cannot happen.
  132. It can, it SYNs are crossed. --ANK */
  133. if (!sock_owned_by_user(sk)) {
  134. __DCCP_INC_STATS(DCCP_MIB_ATTEMPTFAILS);
  135. sk->sk_err = err;
  136. /*
  137. * Wake people up to see the error
  138. * (see connect in sock.c)
  139. */
  140. sk->sk_error_report(sk);
  141. dccp_done(sk);
  142. } else
  143. sk->sk_err_soft = err;
  144. goto out;
  145. }
  146. if (!sock_owned_by_user(sk) && np->recverr) {
  147. sk->sk_err = err;
  148. sk->sk_error_report(sk);
  149. } else
  150. sk->sk_err_soft = err;
  151. out:
  152. bh_unlock_sock(sk);
  153. sock_put(sk);
  154. }
  155. static int dccp_v6_send_response(const struct sock *sk, struct request_sock *req)
  156. {
  157. struct inet_request_sock *ireq = inet_rsk(req);
  158. struct ipv6_pinfo *np = inet6_sk(sk);
  159. struct sk_buff *skb;
  160. struct in6_addr *final_p, final;
  161. struct flowi6 fl6;
  162. int err = -1;
  163. struct dst_entry *dst;
  164. memset(&fl6, 0, sizeof(fl6));
  165. fl6.flowi6_proto = IPPROTO_DCCP;
  166. fl6.daddr = ireq->ir_v6_rmt_addr;
  167. fl6.saddr = ireq->ir_v6_loc_addr;
  168. fl6.flowlabel = 0;
  169. fl6.flowi6_oif = ireq->ir_iif;
  170. fl6.fl6_dport = ireq->ir_rmt_port;
  171. fl6.fl6_sport = htons(ireq->ir_num);
  172. security_req_classify_flow(req, flowi6_to_flowi(&fl6));
  173. rcu_read_lock();
  174. final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt), &final);
  175. rcu_read_unlock();
  176. dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
  177. if (IS_ERR(dst)) {
  178. err = PTR_ERR(dst);
  179. dst = NULL;
  180. goto done;
  181. }
  182. skb = dccp_make_response(sk, dst, req);
  183. if (skb != NULL) {
  184. struct dccp_hdr *dh = dccp_hdr(skb);
  185. struct ipv6_txoptions *opt;
  186. dh->dccph_checksum = dccp_v6_csum_finish(skb,
  187. &ireq->ir_v6_loc_addr,
  188. &ireq->ir_v6_rmt_addr);
  189. fl6.daddr = ireq->ir_v6_rmt_addr;
  190. rcu_read_lock();
  191. opt = ireq->ipv6_opt;
  192. if (!opt)
  193. opt = rcu_dereference(np->opt);
  194. err = ip6_xmit(sk, skb, &fl6, sk->sk_mark, opt, np->tclass);
  195. rcu_read_unlock();
  196. err = net_xmit_eval(err);
  197. }
  198. done:
  199. dst_release(dst);
  200. return err;
  201. }
  202. static void dccp_v6_reqsk_destructor(struct request_sock *req)
  203. {
  204. dccp_feat_list_purge(&dccp_rsk(req)->dreq_featneg);
  205. kfree(inet_rsk(req)->ipv6_opt);
  206. kfree_skb(inet_rsk(req)->pktopts);
  207. }
  208. static void dccp_v6_ctl_send_reset(const struct sock *sk, struct sk_buff *rxskb)
  209. {
  210. const struct ipv6hdr *rxip6h;
  211. struct sk_buff *skb;
  212. struct flowi6 fl6;
  213. struct net *net = dev_net(skb_dst(rxskb)->dev);
  214. struct sock *ctl_sk = net->dccp.v6_ctl_sk;
  215. struct dst_entry *dst;
  216. if (dccp_hdr(rxskb)->dccph_type == DCCP_PKT_RESET)
  217. return;
  218. if (!ipv6_unicast_destination(rxskb))
  219. return;
  220. skb = dccp_ctl_make_reset(ctl_sk, rxskb);
  221. if (skb == NULL)
  222. return;
  223. rxip6h = ipv6_hdr(rxskb);
  224. dccp_hdr(skb)->dccph_checksum = dccp_v6_csum_finish(skb, &rxip6h->saddr,
  225. &rxip6h->daddr);
  226. memset(&fl6, 0, sizeof(fl6));
  227. fl6.daddr = rxip6h->saddr;
  228. fl6.saddr = rxip6h->daddr;
  229. fl6.flowi6_proto = IPPROTO_DCCP;
  230. fl6.flowi6_oif = inet6_iif(rxskb);
  231. fl6.fl6_dport = dccp_hdr(skb)->dccph_dport;
  232. fl6.fl6_sport = dccp_hdr(skb)->dccph_sport;
  233. security_skb_classify_flow(rxskb, flowi6_to_flowi(&fl6));
  234. /* sk = NULL, but it is safe for now. RST socket required. */
  235. dst = ip6_dst_lookup_flow(ctl_sk, &fl6, NULL);
  236. if (!IS_ERR(dst)) {
  237. skb_dst_set(skb, dst);
  238. ip6_xmit(ctl_sk, skb, &fl6, 0, NULL, 0);
  239. DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
  240. DCCP_INC_STATS(DCCP_MIB_OUTRSTS);
  241. return;
  242. }
  243. kfree_skb(skb);
  244. }
  245. static struct request_sock_ops dccp6_request_sock_ops = {
  246. .family = AF_INET6,
  247. .obj_size = sizeof(struct dccp6_request_sock),
  248. .rtx_syn_ack = dccp_v6_send_response,
  249. .send_ack = dccp_reqsk_send_ack,
  250. .destructor = dccp_v6_reqsk_destructor,
  251. .send_reset = dccp_v6_ctl_send_reset,
  252. .syn_ack_timeout = dccp_syn_ack_timeout,
  253. };
  254. static int dccp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
  255. {
  256. struct request_sock *req;
  257. struct dccp_request_sock *dreq;
  258. struct inet_request_sock *ireq;
  259. struct ipv6_pinfo *np = inet6_sk(sk);
  260. const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
  261. struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
  262. if (skb->protocol == htons(ETH_P_IP))
  263. return dccp_v4_conn_request(sk, skb);
  264. if (!ipv6_unicast_destination(skb))
  265. return 0; /* discard, don't send a reset here */
  266. if (dccp_bad_service_code(sk, service)) {
  267. dcb->dccpd_reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
  268. goto drop;
  269. }
  270. /*
  271. * There are no SYN attacks on IPv6, yet...
  272. */
  273. dcb->dccpd_reset_code = DCCP_RESET_CODE_TOO_BUSY;
  274. if (inet_csk_reqsk_queue_is_full(sk))
  275. goto drop;
  276. if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
  277. goto drop;
  278. req = inet_reqsk_alloc(&dccp6_request_sock_ops, sk, true);
  279. if (req == NULL)
  280. goto drop;
  281. if (dccp_reqsk_init(req, dccp_sk(sk), skb))
  282. goto drop_and_free;
  283. dreq = dccp_rsk(req);
  284. if (dccp_parse_options(sk, dreq, skb))
  285. goto drop_and_free;
  286. if (security_inet_conn_request(sk, skb, req))
  287. goto drop_and_free;
  288. ireq = inet_rsk(req);
  289. ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
  290. ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
  291. ireq->ireq_family = AF_INET6;
  292. ireq->ir_mark = inet_request_mark(sk, skb);
  293. if (ipv6_opt_accepted(sk, skb, IP6CB(skb)) ||
  294. np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
  295. np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
  296. atomic_inc(&skb->users);
  297. ireq->pktopts = skb;
  298. }
  299. ireq->ir_iif = sk->sk_bound_dev_if;
  300. /* So that link locals have meaning */
  301. if (!sk->sk_bound_dev_if &&
  302. ipv6_addr_type(&ireq->ir_v6_rmt_addr) & IPV6_ADDR_LINKLOCAL)
  303. ireq->ir_iif = inet6_iif(skb);
  304. /*
  305. * Step 3: Process LISTEN state
  306. *
  307. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
  308. *
  309. * Setting S.SWL/S.SWH to is deferred to dccp_create_openreq_child().
  310. */
  311. dreq->dreq_isr = dcb->dccpd_seq;
  312. dreq->dreq_gsr = dreq->dreq_isr;
  313. dreq->dreq_iss = dccp_v6_init_sequence(skb);
  314. dreq->dreq_gss = dreq->dreq_iss;
  315. dreq->dreq_service = service;
  316. if (dccp_v6_send_response(sk, req))
  317. goto drop_and_free;
  318. inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
  319. reqsk_put(req);
  320. return 0;
  321. drop_and_free:
  322. reqsk_free(req);
  323. drop:
  324. __DCCP_INC_STATS(DCCP_MIB_ATTEMPTFAILS);
  325. return -1;
  326. }
  327. static struct sock *dccp_v6_request_recv_sock(const struct sock *sk,
  328. struct sk_buff *skb,
  329. struct request_sock *req,
  330. struct dst_entry *dst,
  331. struct request_sock *req_unhash,
  332. bool *own_req)
  333. {
  334. struct inet_request_sock *ireq = inet_rsk(req);
  335. struct ipv6_pinfo *newnp;
  336. const struct ipv6_pinfo *np = inet6_sk(sk);
  337. struct ipv6_txoptions *opt;
  338. struct inet_sock *newinet;
  339. struct dccp6_sock *newdp6;
  340. struct sock *newsk;
  341. if (skb->protocol == htons(ETH_P_IP)) {
  342. /*
  343. * v6 mapped
  344. */
  345. newsk = dccp_v4_request_recv_sock(sk, skb, req, dst,
  346. req_unhash, own_req);
  347. if (newsk == NULL)
  348. return NULL;
  349. newdp6 = (struct dccp6_sock *)newsk;
  350. newinet = inet_sk(newsk);
  351. newinet->pinet6 = &newdp6->inet6;
  352. newnp = inet6_sk(newsk);
  353. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  354. newnp->saddr = newsk->sk_v6_rcv_saddr;
  355. inet_csk(newsk)->icsk_af_ops = &dccp_ipv6_mapped;
  356. newsk->sk_backlog_rcv = dccp_v4_do_rcv;
  357. newnp->pktoptions = NULL;
  358. newnp->opt = NULL;
  359. newnp->ipv6_mc_list = NULL;
  360. newnp->ipv6_ac_list = NULL;
  361. newnp->ipv6_fl_list = NULL;
  362. newnp->mcast_oif = inet6_iif(skb);
  363. newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
  364. /*
  365. * No need to charge this sock to the relevant IPv6 refcnt debug socks count
  366. * here, dccp_create_openreq_child now does this for us, see the comment in
  367. * that function for the gory details. -acme
  368. */
  369. /* It is tricky place. Until this moment IPv4 tcp
  370. worked with IPv6 icsk.icsk_af_ops.
  371. Sync it now.
  372. */
  373. dccp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
  374. return newsk;
  375. }
  376. if (sk_acceptq_is_full(sk))
  377. goto out_overflow;
  378. if (!dst) {
  379. struct flowi6 fl6;
  380. dst = inet6_csk_route_req(sk, &fl6, req, IPPROTO_DCCP);
  381. if (!dst)
  382. goto out;
  383. }
  384. newsk = dccp_create_openreq_child(sk, req, skb);
  385. if (newsk == NULL)
  386. goto out_nonewsk;
  387. /*
  388. * No need to charge this sock to the relevant IPv6 refcnt debug socks
  389. * count here, dccp_create_openreq_child now does this for us, see the
  390. * comment in that function for the gory details. -acme
  391. */
  392. ip6_dst_store(newsk, dst, NULL, NULL);
  393. newsk->sk_route_caps = dst->dev->features & ~(NETIF_F_IP_CSUM |
  394. NETIF_F_TSO);
  395. newdp6 = (struct dccp6_sock *)newsk;
  396. newinet = inet_sk(newsk);
  397. newinet->pinet6 = &newdp6->inet6;
  398. newnp = inet6_sk(newsk);
  399. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  400. newsk->sk_v6_daddr = ireq->ir_v6_rmt_addr;
  401. newnp->saddr = ireq->ir_v6_loc_addr;
  402. newsk->sk_v6_rcv_saddr = ireq->ir_v6_loc_addr;
  403. newsk->sk_bound_dev_if = ireq->ir_iif;
  404. /* Now IPv6 options...
  405. First: no IPv4 options.
  406. */
  407. newinet->inet_opt = NULL;
  408. /* Clone RX bits */
  409. newnp->rxopt.all = np->rxopt.all;
  410. newnp->ipv6_mc_list = NULL;
  411. newnp->ipv6_ac_list = NULL;
  412. newnp->ipv6_fl_list = NULL;
  413. newnp->pktoptions = NULL;
  414. newnp->opt = NULL;
  415. newnp->mcast_oif = inet6_iif(skb);
  416. newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
  417. /*
  418. * Clone native IPv6 options from listening socket (if any)
  419. *
  420. * Yes, keeping reference count would be much more clever, but we make
  421. * one more one thing there: reattach optmem to newsk.
  422. */
  423. opt = ireq->ipv6_opt;
  424. if (!opt)
  425. opt = rcu_dereference(np->opt);
  426. if (opt) {
  427. opt = ipv6_dup_options(newsk, opt);
  428. RCU_INIT_POINTER(newnp->opt, opt);
  429. }
  430. inet_csk(newsk)->icsk_ext_hdr_len = 0;
  431. if (opt)
  432. inet_csk(newsk)->icsk_ext_hdr_len = opt->opt_nflen +
  433. opt->opt_flen;
  434. dccp_sync_mss(newsk, dst_mtu(dst));
  435. newinet->inet_daddr = newinet->inet_saddr = LOOPBACK4_IPV6;
  436. newinet->inet_rcv_saddr = LOOPBACK4_IPV6;
  437. if (__inet_inherit_port(sk, newsk) < 0) {
  438. inet_csk_prepare_forced_close(newsk);
  439. dccp_done(newsk);
  440. goto out;
  441. }
  442. *own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash));
  443. /* Clone pktoptions received with SYN, if we own the req */
  444. if (*own_req && ireq->pktopts) {
  445. newnp->pktoptions = skb_clone(ireq->pktopts, GFP_ATOMIC);
  446. consume_skb(ireq->pktopts);
  447. ireq->pktopts = NULL;
  448. if (newnp->pktoptions)
  449. skb_set_owner_r(newnp->pktoptions, newsk);
  450. }
  451. return newsk;
  452. out_overflow:
  453. __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
  454. out_nonewsk:
  455. dst_release(dst);
  456. out:
  457. __NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENDROPS);
  458. return NULL;
  459. }
  460. /* The socket must have it's spinlock held when we get
  461. * here.
  462. *
  463. * We have a potential double-lock case here, so even when
  464. * doing backlog processing we use the BH locking scheme.
  465. * This is because we cannot sleep with the original spinlock
  466. * held.
  467. */
  468. static int dccp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
  469. {
  470. struct ipv6_pinfo *np = inet6_sk(sk);
  471. struct sk_buff *opt_skb = NULL;
  472. /* Imagine: socket is IPv6. IPv4 packet arrives,
  473. goes to IPv4 receive handler and backlogged.
  474. From backlog it always goes here. Kerboom...
  475. Fortunately, dccp_rcv_established and rcv_established
  476. handle them correctly, but it is not case with
  477. dccp_v6_hnd_req and dccp_v6_ctl_send_reset(). --ANK
  478. */
  479. if (skb->protocol == htons(ETH_P_IP))
  480. return dccp_v4_do_rcv(sk, skb);
  481. if (sk_filter(sk, skb))
  482. goto discard;
  483. /*
  484. * socket locking is here for SMP purposes as backlog rcv is currently
  485. * called with bh processing disabled.
  486. */
  487. /* Do Stevens' IPV6_PKTOPTIONS.
  488. Yes, guys, it is the only place in our code, where we
  489. may make it not affecting IPv4.
  490. The rest of code is protocol independent,
  491. and I do not like idea to uglify IPv4.
  492. Actually, all the idea behind IPV6_PKTOPTIONS
  493. looks not very well thought. For now we latch
  494. options, received in the last packet, enqueued
  495. by tcp. Feel free to propose better solution.
  496. --ANK (980728)
  497. */
  498. if (np->rxopt.all)
  499. /*
  500. * FIXME: Add handling of IPV6_PKTOPTIONS skb. See the comments below
  501. * (wrt ipv6_pktopions) and net/ipv6/tcp_ipv6.c for an example.
  502. */
  503. opt_skb = skb_clone(skb, GFP_ATOMIC);
  504. if (sk->sk_state == DCCP_OPEN) { /* Fast path */
  505. if (dccp_rcv_established(sk, skb, dccp_hdr(skb), skb->len))
  506. goto reset;
  507. if (opt_skb) {
  508. /* XXX This is where we would goto ipv6_pktoptions. */
  509. __kfree_skb(opt_skb);
  510. }
  511. return 0;
  512. }
  513. /*
  514. * Step 3: Process LISTEN state
  515. * If S.state == LISTEN,
  516. * If P.type == Request or P contains a valid Init Cookie option,
  517. * (* Must scan the packet's options to check for Init
  518. * Cookies. Only Init Cookies are processed here,
  519. * however; other options are processed in Step 8. This
  520. * scan need only be performed if the endpoint uses Init
  521. * Cookies *)
  522. * (* Generate a new socket and switch to that socket *)
  523. * Set S := new socket for this port pair
  524. * S.state = RESPOND
  525. * Choose S.ISS (initial seqno) or set from Init Cookies
  526. * Initialize S.GAR := S.ISS
  527. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookies
  528. * Continue with S.state == RESPOND
  529. * (* A Response packet will be generated in Step 11 *)
  530. * Otherwise,
  531. * Generate Reset(No Connection) unless P.type == Reset
  532. * Drop packet and return
  533. *
  534. * NOTE: the check for the packet types is done in
  535. * dccp_rcv_state_process
  536. */
  537. if (dccp_rcv_state_process(sk, skb, dccp_hdr(skb), skb->len))
  538. goto reset;
  539. if (opt_skb) {
  540. /* XXX This is where we would goto ipv6_pktoptions. */
  541. __kfree_skb(opt_skb);
  542. }
  543. return 0;
  544. reset:
  545. dccp_v6_ctl_send_reset(sk, skb);
  546. discard:
  547. if (opt_skb != NULL)
  548. __kfree_skb(opt_skb);
  549. kfree_skb(skb);
  550. return 0;
  551. }
  552. static int dccp_v6_rcv(struct sk_buff *skb)
  553. {
  554. const struct dccp_hdr *dh;
  555. bool refcounted;
  556. struct sock *sk;
  557. int min_cov;
  558. /* Step 1: Check header basics */
  559. if (dccp_invalid_packet(skb))
  560. goto discard_it;
  561. /* Step 1: If header checksum is incorrect, drop packet and return. */
  562. if (dccp_v6_csum_finish(skb, &ipv6_hdr(skb)->saddr,
  563. &ipv6_hdr(skb)->daddr)) {
  564. DCCP_WARN("dropped packet with invalid checksum\n");
  565. goto discard_it;
  566. }
  567. dh = dccp_hdr(skb);
  568. DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(dh);
  569. DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
  570. if (dccp_packet_without_ack(skb))
  571. DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
  572. else
  573. DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
  574. lookup:
  575. sk = __inet6_lookup_skb(&dccp_hashinfo, skb, __dccp_hdr_len(dh),
  576. dh->dccph_sport, dh->dccph_dport,
  577. inet6_iif(skb), &refcounted);
  578. if (!sk) {
  579. dccp_pr_debug("failed to look up flow ID in table and "
  580. "get corresponding socket\n");
  581. goto no_dccp_socket;
  582. }
  583. /*
  584. * Step 2:
  585. * ... or S.state == TIMEWAIT,
  586. * Generate Reset(No Connection) unless P.type == Reset
  587. * Drop packet and return
  588. */
  589. if (sk->sk_state == DCCP_TIME_WAIT) {
  590. dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: do_time_wait\n");
  591. inet_twsk_put(inet_twsk(sk));
  592. goto no_dccp_socket;
  593. }
  594. if (sk->sk_state == DCCP_NEW_SYN_RECV) {
  595. struct request_sock *req = inet_reqsk(sk);
  596. struct sock *nsk;
  597. sk = req->rsk_listener;
  598. if (unlikely(sk->sk_state != DCCP_LISTEN)) {
  599. inet_csk_reqsk_queue_drop_and_put(sk, req);
  600. goto lookup;
  601. }
  602. sock_hold(sk);
  603. refcounted = true;
  604. nsk = dccp_check_req(sk, skb, req);
  605. if (!nsk) {
  606. reqsk_put(req);
  607. goto discard_and_relse;
  608. }
  609. if (nsk == sk) {
  610. reqsk_put(req);
  611. } else if (dccp_child_process(sk, nsk, skb)) {
  612. dccp_v6_ctl_send_reset(sk, skb);
  613. goto discard_and_relse;
  614. } else {
  615. sock_put(sk);
  616. return 0;
  617. }
  618. }
  619. /*
  620. * RFC 4340, sec. 9.2.1: Minimum Checksum Coverage
  621. * o if MinCsCov = 0, only packets with CsCov = 0 are accepted
  622. * o if MinCsCov > 0, also accept packets with CsCov >= MinCsCov
  623. */
  624. min_cov = dccp_sk(sk)->dccps_pcrlen;
  625. if (dh->dccph_cscov && (min_cov == 0 || dh->dccph_cscov < min_cov)) {
  626. dccp_pr_debug("Packet CsCov %d does not satisfy MinCsCov %d\n",
  627. dh->dccph_cscov, min_cov);
  628. /* FIXME: send Data Dropped option (see also dccp_v4_rcv) */
  629. goto discard_and_relse;
  630. }
  631. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
  632. goto discard_and_relse;
  633. return __sk_receive_skb(sk, skb, 1, dh->dccph_doff * 4,
  634. refcounted) ? -1 : 0;
  635. no_dccp_socket:
  636. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  637. goto discard_it;
  638. /*
  639. * Step 2:
  640. * If no socket ...
  641. * Generate Reset(No Connection) unless P.type == Reset
  642. * Drop packet and return
  643. */
  644. if (dh->dccph_type != DCCP_PKT_RESET) {
  645. DCCP_SKB_CB(skb)->dccpd_reset_code =
  646. DCCP_RESET_CODE_NO_CONNECTION;
  647. dccp_v6_ctl_send_reset(sk, skb);
  648. }
  649. discard_it:
  650. kfree_skb(skb);
  651. return 0;
  652. discard_and_relse:
  653. if (refcounted)
  654. sock_put(sk);
  655. goto discard_it;
  656. }
  657. static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
  658. int addr_len)
  659. {
  660. struct sockaddr_in6 *usin = (struct sockaddr_in6 *)uaddr;
  661. struct inet_connection_sock *icsk = inet_csk(sk);
  662. struct inet_sock *inet = inet_sk(sk);
  663. struct ipv6_pinfo *np = inet6_sk(sk);
  664. struct dccp_sock *dp = dccp_sk(sk);
  665. struct in6_addr *saddr = NULL, *final_p, final;
  666. struct ipv6_txoptions *opt;
  667. struct flowi6 fl6;
  668. struct dst_entry *dst;
  669. int addr_type;
  670. int err;
  671. dp->dccps_role = DCCP_ROLE_CLIENT;
  672. if (addr_len < SIN6_LEN_RFC2133)
  673. return -EINVAL;
  674. if (usin->sin6_family != AF_INET6)
  675. return -EAFNOSUPPORT;
  676. memset(&fl6, 0, sizeof(fl6));
  677. if (np->sndflow) {
  678. fl6.flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
  679. IP6_ECN_flow_init(fl6.flowlabel);
  680. if (fl6.flowlabel & IPV6_FLOWLABEL_MASK) {
  681. struct ip6_flowlabel *flowlabel;
  682. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  683. if (flowlabel == NULL)
  684. return -EINVAL;
  685. fl6_sock_release(flowlabel);
  686. }
  687. }
  688. /*
  689. * connect() to INADDR_ANY means loopback (BSD'ism).
  690. */
  691. if (ipv6_addr_any(&usin->sin6_addr))
  692. usin->sin6_addr.s6_addr[15] = 1;
  693. addr_type = ipv6_addr_type(&usin->sin6_addr);
  694. if (addr_type & IPV6_ADDR_MULTICAST)
  695. return -ENETUNREACH;
  696. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  697. if (addr_len >= sizeof(struct sockaddr_in6) &&
  698. usin->sin6_scope_id) {
  699. /* If interface is set while binding, indices
  700. * must coincide.
  701. */
  702. if (sk->sk_bound_dev_if &&
  703. sk->sk_bound_dev_if != usin->sin6_scope_id)
  704. return -EINVAL;
  705. sk->sk_bound_dev_if = usin->sin6_scope_id;
  706. }
  707. /* Connect to link-local address requires an interface */
  708. if (!sk->sk_bound_dev_if)
  709. return -EINVAL;
  710. }
  711. sk->sk_v6_daddr = usin->sin6_addr;
  712. np->flow_label = fl6.flowlabel;
  713. /*
  714. * DCCP over IPv4
  715. */
  716. if (addr_type == IPV6_ADDR_MAPPED) {
  717. u32 exthdrlen = icsk->icsk_ext_hdr_len;
  718. struct sockaddr_in sin;
  719. SOCK_DEBUG(sk, "connect: ipv4 mapped\n");
  720. if (__ipv6_only_sock(sk))
  721. return -ENETUNREACH;
  722. sin.sin_family = AF_INET;
  723. sin.sin_port = usin->sin6_port;
  724. sin.sin_addr.s_addr = usin->sin6_addr.s6_addr32[3];
  725. icsk->icsk_af_ops = &dccp_ipv6_mapped;
  726. sk->sk_backlog_rcv = dccp_v4_do_rcv;
  727. err = dccp_v4_connect(sk, (struct sockaddr *)&sin, sizeof(sin));
  728. if (err) {
  729. icsk->icsk_ext_hdr_len = exthdrlen;
  730. icsk->icsk_af_ops = &dccp_ipv6_af_ops;
  731. sk->sk_backlog_rcv = dccp_v6_do_rcv;
  732. goto failure;
  733. }
  734. np->saddr = sk->sk_v6_rcv_saddr;
  735. return err;
  736. }
  737. if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr))
  738. saddr = &sk->sk_v6_rcv_saddr;
  739. fl6.flowi6_proto = IPPROTO_DCCP;
  740. fl6.daddr = sk->sk_v6_daddr;
  741. fl6.saddr = saddr ? *saddr : np->saddr;
  742. fl6.flowi6_oif = sk->sk_bound_dev_if;
  743. fl6.fl6_dport = usin->sin6_port;
  744. fl6.fl6_sport = inet->inet_sport;
  745. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  746. opt = rcu_dereference_protected(np->opt, lockdep_sock_is_held(sk));
  747. final_p = fl6_update_dst(&fl6, opt, &final);
  748. dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
  749. if (IS_ERR(dst)) {
  750. err = PTR_ERR(dst);
  751. goto failure;
  752. }
  753. if (saddr == NULL) {
  754. saddr = &fl6.saddr;
  755. sk->sk_v6_rcv_saddr = *saddr;
  756. }
  757. /* set the source address */
  758. np->saddr = *saddr;
  759. inet->inet_rcv_saddr = LOOPBACK4_IPV6;
  760. ip6_dst_store(sk, dst, NULL, NULL);
  761. icsk->icsk_ext_hdr_len = 0;
  762. if (opt)
  763. icsk->icsk_ext_hdr_len = opt->opt_flen + opt->opt_nflen;
  764. inet->inet_dport = usin->sin6_port;
  765. dccp_set_state(sk, DCCP_REQUESTING);
  766. err = inet6_hash_connect(&dccp_death_row, sk);
  767. if (err)
  768. goto late_failure;
  769. dp->dccps_iss = secure_dccpv6_sequence_number(np->saddr.s6_addr32,
  770. sk->sk_v6_daddr.s6_addr32,
  771. inet->inet_sport,
  772. inet->inet_dport);
  773. err = dccp_connect(sk);
  774. if (err)
  775. goto late_failure;
  776. return 0;
  777. late_failure:
  778. dccp_set_state(sk, DCCP_CLOSED);
  779. __sk_dst_reset(sk);
  780. failure:
  781. inet->inet_dport = 0;
  782. sk->sk_route_caps = 0;
  783. return err;
  784. }
  785. static const struct inet_connection_sock_af_ops dccp_ipv6_af_ops = {
  786. .queue_xmit = inet6_csk_xmit,
  787. .send_check = dccp_v6_send_check,
  788. .rebuild_header = inet6_sk_rebuild_header,
  789. .conn_request = dccp_v6_conn_request,
  790. .syn_recv_sock = dccp_v6_request_recv_sock,
  791. .net_header_len = sizeof(struct ipv6hdr),
  792. .setsockopt = ipv6_setsockopt,
  793. .getsockopt = ipv6_getsockopt,
  794. .addr2sockaddr = inet6_csk_addr2sockaddr,
  795. .sockaddr_len = sizeof(struct sockaddr_in6),
  796. .bind_conflict = inet6_csk_bind_conflict,
  797. #ifdef CONFIG_COMPAT
  798. .compat_setsockopt = compat_ipv6_setsockopt,
  799. .compat_getsockopt = compat_ipv6_getsockopt,
  800. #endif
  801. };
  802. /*
  803. * DCCP over IPv4 via INET6 API
  804. */
  805. static const struct inet_connection_sock_af_ops dccp_ipv6_mapped = {
  806. .queue_xmit = ip_queue_xmit,
  807. .send_check = dccp_v4_send_check,
  808. .rebuild_header = inet_sk_rebuild_header,
  809. .conn_request = dccp_v6_conn_request,
  810. .syn_recv_sock = dccp_v6_request_recv_sock,
  811. .net_header_len = sizeof(struct iphdr),
  812. .setsockopt = ipv6_setsockopt,
  813. .getsockopt = ipv6_getsockopt,
  814. .addr2sockaddr = inet6_csk_addr2sockaddr,
  815. .sockaddr_len = sizeof(struct sockaddr_in6),
  816. .bind_conflict = inet6_csk_bind_conflict,
  817. #ifdef CONFIG_COMPAT
  818. .compat_setsockopt = compat_ipv6_setsockopt,
  819. .compat_getsockopt = compat_ipv6_getsockopt,
  820. #endif
  821. };
  822. /* NOTE: A lot of things set to zero explicitly by call to
  823. * sk_alloc() so need not be done here.
  824. */
  825. static int dccp_v6_init_sock(struct sock *sk)
  826. {
  827. static __u8 dccp_v6_ctl_sock_initialized;
  828. int err = dccp_init_sock(sk, dccp_v6_ctl_sock_initialized);
  829. if (err == 0) {
  830. if (unlikely(!dccp_v6_ctl_sock_initialized))
  831. dccp_v6_ctl_sock_initialized = 1;
  832. inet_csk(sk)->icsk_af_ops = &dccp_ipv6_af_ops;
  833. }
  834. return err;
  835. }
  836. static void dccp_v6_destroy_sock(struct sock *sk)
  837. {
  838. dccp_destroy_sock(sk);
  839. inet6_destroy_sock(sk);
  840. }
  841. static struct timewait_sock_ops dccp6_timewait_sock_ops = {
  842. .twsk_obj_size = sizeof(struct dccp6_timewait_sock),
  843. };
  844. static struct proto dccp_v6_prot = {
  845. .name = "DCCPv6",
  846. .owner = THIS_MODULE,
  847. .close = dccp_close,
  848. .connect = dccp_v6_connect,
  849. .disconnect = dccp_disconnect,
  850. .ioctl = dccp_ioctl,
  851. .init = dccp_v6_init_sock,
  852. .setsockopt = dccp_setsockopt,
  853. .getsockopt = dccp_getsockopt,
  854. .sendmsg = dccp_sendmsg,
  855. .recvmsg = dccp_recvmsg,
  856. .backlog_rcv = dccp_v6_do_rcv,
  857. .hash = inet6_hash,
  858. .unhash = inet_unhash,
  859. .accept = inet_csk_accept,
  860. .get_port = inet_csk_get_port,
  861. .shutdown = dccp_shutdown,
  862. .destroy = dccp_v6_destroy_sock,
  863. .orphan_count = &dccp_orphan_count,
  864. .max_header = MAX_DCCP_HEADER,
  865. .obj_size = sizeof(struct dccp6_sock),
  866. .slab_flags = SLAB_DESTROY_BY_RCU,
  867. .rsk_prot = &dccp6_request_sock_ops,
  868. .twsk_prot = &dccp6_timewait_sock_ops,
  869. .h.hashinfo = &dccp_hashinfo,
  870. #ifdef CONFIG_COMPAT
  871. .compat_setsockopt = compat_dccp_setsockopt,
  872. .compat_getsockopt = compat_dccp_getsockopt,
  873. #endif
  874. };
  875. static const struct inet6_protocol dccp_v6_protocol = {
  876. .handler = dccp_v6_rcv,
  877. .err_handler = dccp_v6_err,
  878. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
  879. };
  880. static const struct proto_ops inet6_dccp_ops = {
  881. .family = PF_INET6,
  882. .owner = THIS_MODULE,
  883. .release = inet6_release,
  884. .bind = inet6_bind,
  885. .connect = inet_stream_connect,
  886. .socketpair = sock_no_socketpair,
  887. .accept = inet_accept,
  888. .getname = inet6_getname,
  889. .poll = dccp_poll,
  890. .ioctl = inet6_ioctl,
  891. .listen = inet_dccp_listen,
  892. .shutdown = inet_shutdown,
  893. .setsockopt = sock_common_setsockopt,
  894. .getsockopt = sock_common_getsockopt,
  895. .sendmsg = inet_sendmsg,
  896. .recvmsg = sock_common_recvmsg,
  897. .mmap = sock_no_mmap,
  898. .sendpage = sock_no_sendpage,
  899. #ifdef CONFIG_COMPAT
  900. .compat_setsockopt = compat_sock_common_setsockopt,
  901. .compat_getsockopt = compat_sock_common_getsockopt,
  902. #endif
  903. };
  904. static struct inet_protosw dccp_v6_protosw = {
  905. .type = SOCK_DCCP,
  906. .protocol = IPPROTO_DCCP,
  907. .prot = &dccp_v6_prot,
  908. .ops = &inet6_dccp_ops,
  909. .flags = INET_PROTOSW_ICSK,
  910. };
  911. static int __net_init dccp_v6_init_net(struct net *net)
  912. {
  913. if (dccp_hashinfo.bhash == NULL)
  914. return -ESOCKTNOSUPPORT;
  915. return inet_ctl_sock_create(&net->dccp.v6_ctl_sk, PF_INET6,
  916. SOCK_DCCP, IPPROTO_DCCP, net);
  917. }
  918. static void __net_exit dccp_v6_exit_net(struct net *net)
  919. {
  920. inet_ctl_sock_destroy(net->dccp.v6_ctl_sk);
  921. }
  922. static struct pernet_operations dccp_v6_ops = {
  923. .init = dccp_v6_init_net,
  924. .exit = dccp_v6_exit_net,
  925. };
  926. static int __init dccp_v6_init(void)
  927. {
  928. int err = proto_register(&dccp_v6_prot, 1);
  929. if (err != 0)
  930. goto out;
  931. err = inet6_add_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
  932. if (err != 0)
  933. goto out_unregister_proto;
  934. inet6_register_protosw(&dccp_v6_protosw);
  935. err = register_pernet_subsys(&dccp_v6_ops);
  936. if (err != 0)
  937. goto out_destroy_ctl_sock;
  938. out:
  939. return err;
  940. out_destroy_ctl_sock:
  941. inet6_del_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
  942. inet6_unregister_protosw(&dccp_v6_protosw);
  943. out_unregister_proto:
  944. proto_unregister(&dccp_v6_prot);
  945. goto out;
  946. }
  947. static void __exit dccp_v6_exit(void)
  948. {
  949. unregister_pernet_subsys(&dccp_v6_ops);
  950. inet6_del_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
  951. inet6_unregister_protosw(&dccp_v6_protosw);
  952. proto_unregister(&dccp_v6_prot);
  953. }
  954. module_init(dccp_v6_init);
  955. module_exit(dccp_v6_exit);
  956. /*
  957. * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
  958. * values directly, Also cover the case where the protocol is not specified,
  959. * i.e. net-pf-PF_INET6-proto-0-type-SOCK_DCCP
  960. */
  961. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET6, 33, 6);
  962. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_INET6, 0, 6);
  963. MODULE_LICENSE("GPL");
  964. MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@mandriva.com>");
  965. MODULE_DESCRIPTION("DCCPv6 - Datagram Congestion Controlled Protocol");