ipv6.c 30 KB

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