ipv6.c 29 KB

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