ipv6.c 29 KB

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