ipv6.c 30 KB

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