ipv6.c 29 KB

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