ipv6.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  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/config.h>
  15. #include <linux/module.h>
  16. #include <linux/random.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 "dccp.h"
  31. #include "ipv6.h"
  32. /* Socket used for sending RSTs and ACKs */
  33. static struct socket *dccp_v6_ctl_socket;
  34. static void dccp_v6_ctl_send_reset(struct sk_buff *skb);
  35. static void dccp_v6_reqsk_send_ack(struct sk_buff *skb,
  36. struct request_sock *req);
  37. static void dccp_v6_send_check(struct sock *sk, int len, struct sk_buff *skb);
  38. static int dccp_v6_do_rcv(struct sock *sk, struct sk_buff *skb);
  39. static struct inet_connection_sock_af_ops dccp_ipv6_mapped;
  40. static struct inet_connection_sock_af_ops dccp_ipv6_af_ops;
  41. static int dccp_v6_get_port(struct sock *sk, unsigned short snum)
  42. {
  43. return inet_csk_get_port(&dccp_hashinfo, sk, snum,
  44. inet6_csk_bind_conflict);
  45. }
  46. static void dccp_v6_hash(struct sock *sk)
  47. {
  48. if (sk->sk_state != DCCP_CLOSED) {
  49. if (inet_csk(sk)->icsk_af_ops == &dccp_ipv6_mapped) {
  50. dccp_hash(sk);
  51. return;
  52. }
  53. local_bh_disable();
  54. __inet6_hash(&dccp_hashinfo, sk);
  55. local_bh_enable();
  56. }
  57. }
  58. static inline u16 dccp_v6_check(struct dccp_hdr *dh, int len,
  59. struct in6_addr *saddr,
  60. struct in6_addr *daddr,
  61. unsigned long base)
  62. {
  63. return csum_ipv6_magic(saddr, daddr, len, IPPROTO_DCCP, base);
  64. }
  65. static __u32 dccp_v6_init_sequence(struct sock *sk, struct sk_buff *skb)
  66. {
  67. const struct dccp_hdr *dh = dccp_hdr(skb);
  68. if (skb->protocol == htons(ETH_P_IPV6))
  69. return secure_tcpv6_sequence_number(skb->nh.ipv6h->daddr.s6_addr32,
  70. skb->nh.ipv6h->saddr.s6_addr32,
  71. dh->dccph_dport,
  72. dh->dccph_sport);
  73. else
  74. return secure_dccp_sequence_number(skb->nh.iph->daddr,
  75. skb->nh.iph->saddr,
  76. dh->dccph_dport,
  77. dh->dccph_sport);
  78. }
  79. static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
  80. int addr_len)
  81. {
  82. struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
  83. struct inet_connection_sock *icsk = inet_csk(sk);
  84. struct inet_sock *inet = inet_sk(sk);
  85. struct ipv6_pinfo *np = inet6_sk(sk);
  86. struct dccp_sock *dp = dccp_sk(sk);
  87. struct in6_addr *saddr = NULL, *final_p = NULL, final;
  88. struct flowi fl;
  89. struct dst_entry *dst;
  90. int addr_type;
  91. int err;
  92. dp->dccps_role = DCCP_ROLE_CLIENT;
  93. if (addr_len < SIN6_LEN_RFC2133)
  94. return -EINVAL;
  95. if (usin->sin6_family != AF_INET6)
  96. return -EAFNOSUPPORT;
  97. memset(&fl, 0, sizeof(fl));
  98. if (np->sndflow) {
  99. fl.fl6_flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
  100. IP6_ECN_flow_init(fl.fl6_flowlabel);
  101. if (fl.fl6_flowlabel & IPV6_FLOWLABEL_MASK) {
  102. struct ip6_flowlabel *flowlabel;
  103. flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
  104. if (flowlabel == NULL)
  105. return -EINVAL;
  106. ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst);
  107. fl6_sock_release(flowlabel);
  108. }
  109. }
  110. /*
  111. * connect() to INADDR_ANY means loopback (BSD'ism).
  112. */
  113. if (ipv6_addr_any(&usin->sin6_addr))
  114. usin->sin6_addr.s6_addr[15] = 0x1;
  115. addr_type = ipv6_addr_type(&usin->sin6_addr);
  116. if(addr_type & IPV6_ADDR_MULTICAST)
  117. return -ENETUNREACH;
  118. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  119. if (addr_len >= sizeof(struct sockaddr_in6) &&
  120. usin->sin6_scope_id) {
  121. /* If interface is set while binding, indices
  122. * must coincide.
  123. */
  124. if (sk->sk_bound_dev_if &&
  125. sk->sk_bound_dev_if != usin->sin6_scope_id)
  126. return -EINVAL;
  127. sk->sk_bound_dev_if = usin->sin6_scope_id;
  128. }
  129. /* Connect to link-local address requires an interface */
  130. if (!sk->sk_bound_dev_if)
  131. return -EINVAL;
  132. }
  133. ipv6_addr_copy(&np->daddr, &usin->sin6_addr);
  134. np->flow_label = fl.fl6_flowlabel;
  135. /*
  136. * DCCP over IPv4
  137. */
  138. if (addr_type == IPV6_ADDR_MAPPED) {
  139. u32 exthdrlen = icsk->icsk_ext_hdr_len;
  140. struct sockaddr_in sin;
  141. SOCK_DEBUG(sk, "connect: ipv4 mapped\n");
  142. if (__ipv6_only_sock(sk))
  143. return -ENETUNREACH;
  144. sin.sin_family = AF_INET;
  145. sin.sin_port = usin->sin6_port;
  146. sin.sin_addr.s_addr = usin->sin6_addr.s6_addr32[3];
  147. icsk->icsk_af_ops = &dccp_ipv6_mapped;
  148. sk->sk_backlog_rcv = dccp_v4_do_rcv;
  149. err = dccp_v4_connect(sk, (struct sockaddr *)&sin, sizeof(sin));
  150. if (err) {
  151. icsk->icsk_ext_hdr_len = exthdrlen;
  152. icsk->icsk_af_ops = &dccp_ipv6_af_ops;
  153. sk->sk_backlog_rcv = dccp_v6_do_rcv;
  154. goto failure;
  155. } else {
  156. ipv6_addr_set(&np->saddr, 0, 0, htonl(0x0000FFFF),
  157. inet->saddr);
  158. ipv6_addr_set(&np->rcv_saddr, 0, 0, htonl(0x0000FFFF),
  159. inet->rcv_saddr);
  160. }
  161. return err;
  162. }
  163. if (!ipv6_addr_any(&np->rcv_saddr))
  164. saddr = &np->rcv_saddr;
  165. fl.proto = IPPROTO_DCCP;
  166. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  167. ipv6_addr_copy(&fl.fl6_src, saddr ? saddr : &np->saddr);
  168. fl.oif = sk->sk_bound_dev_if;
  169. fl.fl_ip_dport = usin->sin6_port;
  170. fl.fl_ip_sport = inet->sport;
  171. if (np->opt && np->opt->srcrt) {
  172. struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
  173. ipv6_addr_copy(&final, &fl.fl6_dst);
  174. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  175. final_p = &final;
  176. }
  177. err = ip6_dst_lookup(sk, &dst, &fl);
  178. if (err)
  179. goto failure;
  180. if (final_p)
  181. ipv6_addr_copy(&fl.fl6_dst, final_p);
  182. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0)
  183. goto failure;
  184. if (saddr == NULL) {
  185. saddr = &fl.fl6_src;
  186. ipv6_addr_copy(&np->rcv_saddr, saddr);
  187. }
  188. /* set the source address */
  189. ipv6_addr_copy(&np->saddr, saddr);
  190. inet->rcv_saddr = LOOPBACK4_IPV6;
  191. ip6_dst_store(sk, dst, NULL);
  192. icsk->icsk_ext_hdr_len = 0;
  193. if (np->opt)
  194. icsk->icsk_ext_hdr_len = (np->opt->opt_flen +
  195. np->opt->opt_nflen);
  196. inet->dport = usin->sin6_port;
  197. dccp_set_state(sk, DCCP_REQUESTING);
  198. err = inet6_hash_connect(&dccp_death_row, sk);
  199. if (err)
  200. goto late_failure;
  201. /* FIXME */
  202. #if 0
  203. dp->dccps_gar = secure_dccp_v6_sequence_number(np->saddr.s6_addr32,
  204. np->daddr.s6_addr32,
  205. inet->sport,
  206. inet->dport);
  207. #endif
  208. err = dccp_connect(sk);
  209. if (err)
  210. goto late_failure;
  211. return 0;
  212. late_failure:
  213. dccp_set_state(sk, DCCP_CLOSED);
  214. __sk_dst_reset(sk);
  215. failure:
  216. inet->dport = 0;
  217. sk->sk_route_caps = 0;
  218. return err;
  219. }
  220. static void dccp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  221. int type, int code, int offset, __be32 info)
  222. {
  223. struct ipv6hdr *hdr = (struct ipv6hdr *)skb->data;
  224. const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data + offset);
  225. struct ipv6_pinfo *np;
  226. struct sock *sk;
  227. int err;
  228. __u64 seq;
  229. sk = inet6_lookup(&dccp_hashinfo, &hdr->daddr, dh->dccph_dport,
  230. &hdr->saddr, dh->dccph_sport, skb->dev->ifindex);
  231. if (sk == NULL) {
  232. ICMP6_INC_STATS_BH(__in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
  233. return;
  234. }
  235. if (sk->sk_state == DCCP_TIME_WAIT) {
  236. inet_twsk_put((struct inet_timewait_sock *)sk);
  237. return;
  238. }
  239. bh_lock_sock(sk);
  240. if (sock_owned_by_user(sk))
  241. NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
  242. if (sk->sk_state == DCCP_CLOSED)
  243. goto out;
  244. np = inet6_sk(sk);
  245. if (type == ICMPV6_PKT_TOOBIG) {
  246. struct dst_entry *dst = NULL;
  247. if (sock_owned_by_user(sk))
  248. goto out;
  249. if ((1 << sk->sk_state) & (DCCPF_LISTEN | DCCPF_CLOSED))
  250. goto out;
  251. /* icmp should have updated the destination cache entry */
  252. dst = __sk_dst_check(sk, np->dst_cookie);
  253. if (dst == NULL) {
  254. struct inet_sock *inet = inet_sk(sk);
  255. struct flowi fl;
  256. /* BUGGG_FUTURE: Again, it is not clear how
  257. to handle rthdr case. Ignore this complexity
  258. for now.
  259. */
  260. memset(&fl, 0, sizeof(fl));
  261. fl.proto = IPPROTO_DCCP;
  262. ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
  263. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  264. fl.oif = sk->sk_bound_dev_if;
  265. fl.fl_ip_dport = inet->dport;
  266. fl.fl_ip_sport = inet->sport;
  267. if ((err = ip6_dst_lookup(sk, &dst, &fl))) {
  268. sk->sk_err_soft = -err;
  269. goto out;
  270. }
  271. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0) {
  272. sk->sk_err_soft = -err;
  273. goto out;
  274. }
  275. } else
  276. dst_hold(dst);
  277. if (inet_csk(sk)->icsk_pmtu_cookie > dst_mtu(dst)) {
  278. dccp_sync_mss(sk, dst_mtu(dst));
  279. } /* else let the usual retransmit timer handle it */
  280. dst_release(dst);
  281. goto out;
  282. }
  283. icmpv6_err_convert(type, code, &err);
  284. seq = DCCP_SKB_CB(skb)->dccpd_seq;
  285. /* Might be for an request_sock */
  286. switch (sk->sk_state) {
  287. struct request_sock *req, **prev;
  288. case DCCP_LISTEN:
  289. if (sock_owned_by_user(sk))
  290. goto out;
  291. req = inet6_csk_search_req(sk, &prev, dh->dccph_dport,
  292. &hdr->daddr, &hdr->saddr,
  293. inet6_iif(skb));
  294. if (!req)
  295. goto out;
  296. /* ICMPs are not backlogged, hence we cannot get
  297. * an established socket here.
  298. */
  299. BUG_TRAP(req->sk == NULL);
  300. if (seq != dccp_rsk(req)->dreq_iss) {
  301. NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
  302. goto out;
  303. }
  304. inet_csk_reqsk_queue_drop(sk, req, prev);
  305. goto out;
  306. case DCCP_REQUESTING:
  307. case DCCP_RESPOND: /* Cannot happen.
  308. It can, it SYNs are crossed. --ANK */
  309. if (!sock_owned_by_user(sk)) {
  310. DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
  311. sk->sk_err = err;
  312. /*
  313. * Wake people up to see the error
  314. * (see connect in sock.c)
  315. */
  316. sk->sk_error_report(sk);
  317. dccp_done(sk);
  318. } else
  319. sk->sk_err_soft = err;
  320. goto out;
  321. }
  322. if (!sock_owned_by_user(sk) && np->recverr) {
  323. sk->sk_err = err;
  324. sk->sk_error_report(sk);
  325. } else
  326. sk->sk_err_soft = err;
  327. out:
  328. bh_unlock_sock(sk);
  329. sock_put(sk);
  330. }
  331. static int dccp_v6_send_response(struct sock *sk, struct request_sock *req,
  332. struct dst_entry *dst)
  333. {
  334. struct inet6_request_sock *ireq6 = inet6_rsk(req);
  335. struct ipv6_pinfo *np = inet6_sk(sk);
  336. struct sk_buff *skb;
  337. struct ipv6_txoptions *opt = NULL;
  338. struct in6_addr *final_p = NULL, final;
  339. struct flowi fl;
  340. int err = -1;
  341. memset(&fl, 0, sizeof(fl));
  342. fl.proto = IPPROTO_DCCP;
  343. ipv6_addr_copy(&fl.fl6_dst, &ireq6->rmt_addr);
  344. ipv6_addr_copy(&fl.fl6_src, &ireq6->loc_addr);
  345. fl.fl6_flowlabel = 0;
  346. fl.oif = ireq6->iif;
  347. fl.fl_ip_dport = inet_rsk(req)->rmt_port;
  348. fl.fl_ip_sport = inet_sk(sk)->sport;
  349. if (dst == NULL) {
  350. opt = np->opt;
  351. if (opt == NULL &&
  352. np->rxopt.bits.osrcrt == 2 &&
  353. ireq6->pktopts) {
  354. struct sk_buff *pktopts = ireq6->pktopts;
  355. struct inet6_skb_parm *rxopt = IP6CB(pktopts);
  356. if (rxopt->srcrt)
  357. opt = ipv6_invert_rthdr(sk,
  358. (struct ipv6_rt_hdr *)(pktopts->nh.raw +
  359. rxopt->srcrt));
  360. }
  361. if (opt && opt->srcrt) {
  362. struct rt0_hdr *rt0 = (struct rt0_hdr *)opt->srcrt;
  363. ipv6_addr_copy(&final, &fl.fl6_dst);
  364. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  365. final_p = &final;
  366. }
  367. err = ip6_dst_lookup(sk, &dst, &fl);
  368. if (err)
  369. goto done;
  370. if (final_p)
  371. ipv6_addr_copy(&fl.fl6_dst, final_p);
  372. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0)
  373. goto done;
  374. }
  375. skb = dccp_make_response(sk, dst, req);
  376. if (skb != NULL) {
  377. struct dccp_hdr *dh = dccp_hdr(skb);
  378. dh->dccph_checksum = dccp_v6_check(dh, skb->len,
  379. &ireq6->loc_addr,
  380. &ireq6->rmt_addr,
  381. csum_partial((char *)dh,
  382. skb->len,
  383. skb->csum));
  384. ipv6_addr_copy(&fl.fl6_dst, &ireq6->rmt_addr);
  385. err = ip6_xmit(sk, skb, &fl, opt, 0);
  386. if (err == NET_XMIT_CN)
  387. err = 0;
  388. }
  389. done:
  390. if (opt && opt != np->opt)
  391. sock_kfree_s(sk, opt, opt->tot_len);
  392. dst_release(dst);
  393. return err;
  394. }
  395. static void dccp_v6_reqsk_destructor(struct request_sock *req)
  396. {
  397. if (inet6_rsk(req)->pktopts != NULL)
  398. kfree_skb(inet6_rsk(req)->pktopts);
  399. }
  400. static struct request_sock_ops dccp6_request_sock_ops = {
  401. .family = AF_INET6,
  402. .obj_size = sizeof(struct dccp6_request_sock),
  403. .rtx_syn_ack = dccp_v6_send_response,
  404. .send_ack = dccp_v6_reqsk_send_ack,
  405. .destructor = dccp_v6_reqsk_destructor,
  406. .send_reset = dccp_v6_ctl_send_reset,
  407. };
  408. static struct timewait_sock_ops dccp6_timewait_sock_ops = {
  409. .twsk_obj_size = sizeof(struct dccp6_timewait_sock),
  410. };
  411. static void dccp_v6_send_check(struct sock *sk, int len, struct sk_buff *skb)
  412. {
  413. struct ipv6_pinfo *np = inet6_sk(sk);
  414. struct dccp_hdr *dh = dccp_hdr(skb);
  415. dh->dccph_checksum = csum_ipv6_magic(&np->saddr, &np->daddr,
  416. len, IPPROTO_DCCP,
  417. csum_partial((char *)dh,
  418. dh->dccph_doff << 2,
  419. skb->csum));
  420. }
  421. static void dccp_v6_ctl_send_reset(struct sk_buff *rxskb)
  422. {
  423. struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
  424. const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
  425. sizeof(struct dccp_hdr_ext) +
  426. sizeof(struct dccp_hdr_reset);
  427. struct sk_buff *skb;
  428. struct flowi fl;
  429. u64 seqno;
  430. if (rxdh->dccph_type == DCCP_PKT_RESET)
  431. return;
  432. if (!ipv6_unicast_destination(rxskb))
  433. return;
  434. /*
  435. * We need to grab some memory, and put together an RST,
  436. * and then put it into the queue to be sent.
  437. */
  438. skb = alloc_skb(MAX_HEADER + sizeof(struct ipv6hdr) +
  439. dccp_hdr_reset_len, GFP_ATOMIC);
  440. if (skb == NULL)
  441. return;
  442. skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr) +
  443. dccp_hdr_reset_len);
  444. skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
  445. dh = dccp_hdr(skb);
  446. memset(dh, 0, dccp_hdr_reset_len);
  447. /* Swap the send and the receive. */
  448. dh->dccph_type = DCCP_PKT_RESET;
  449. dh->dccph_sport = rxdh->dccph_dport;
  450. dh->dccph_dport = rxdh->dccph_sport;
  451. dh->dccph_doff = dccp_hdr_reset_len / 4;
  452. dh->dccph_x = 1;
  453. dccp_hdr_reset(skb)->dccph_reset_code =
  454. DCCP_SKB_CB(rxskb)->dccpd_reset_code;
  455. /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
  456. seqno = 0;
  457. if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
  458. dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
  459. dccp_hdr_set_seq(dh, seqno);
  460. dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
  461. DCCP_SKB_CB(rxskb)->dccpd_seq);
  462. memset(&fl, 0, sizeof(fl));
  463. ipv6_addr_copy(&fl.fl6_dst, &rxskb->nh.ipv6h->saddr);
  464. ipv6_addr_copy(&fl.fl6_src, &rxskb->nh.ipv6h->daddr);
  465. dh->dccph_checksum = csum_ipv6_magic(&fl.fl6_src, &fl.fl6_dst,
  466. sizeof(*dh), IPPROTO_DCCP,
  467. skb->csum);
  468. fl.proto = IPPROTO_DCCP;
  469. fl.oif = inet6_iif(rxskb);
  470. fl.fl_ip_dport = dh->dccph_dport;
  471. fl.fl_ip_sport = dh->dccph_sport;
  472. /* sk = NULL, but it is safe for now. RST socket required. */
  473. if (!ip6_dst_lookup(NULL, &skb->dst, &fl)) {
  474. if (xfrm_lookup(&skb->dst, &fl, NULL, 0) >= 0) {
  475. ip6_xmit(dccp_v6_ctl_socket->sk, skb, &fl, NULL, 0);
  476. DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
  477. DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
  478. return;
  479. }
  480. }
  481. kfree_skb(skb);
  482. }
  483. static void dccp_v6_ctl_send_ack(struct sk_buff *rxskb)
  484. {
  485. struct flowi fl;
  486. struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
  487. const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
  488. sizeof(struct dccp_hdr_ext) +
  489. sizeof(struct dccp_hdr_ack_bits);
  490. struct sk_buff *skb;
  491. skb = alloc_skb(MAX_HEADER + sizeof(struct ipv6hdr) +
  492. dccp_hdr_ack_len, GFP_ATOMIC);
  493. if (skb == NULL)
  494. return;
  495. skb_reserve(skb, MAX_HEADER + sizeof(struct ipv6hdr) +
  496. dccp_hdr_ack_len);
  497. skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
  498. dh = dccp_hdr(skb);
  499. memset(dh, 0, dccp_hdr_ack_len);
  500. /* Build DCCP header and checksum it. */
  501. dh->dccph_type = DCCP_PKT_ACK;
  502. dh->dccph_sport = rxdh->dccph_dport;
  503. dh->dccph_dport = rxdh->dccph_sport;
  504. dh->dccph_doff = dccp_hdr_ack_len / 4;
  505. dh->dccph_x = 1;
  506. dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
  507. dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
  508. DCCP_SKB_CB(rxskb)->dccpd_seq);
  509. memset(&fl, 0, sizeof(fl));
  510. ipv6_addr_copy(&fl.fl6_dst, &rxskb->nh.ipv6h->saddr);
  511. ipv6_addr_copy(&fl.fl6_src, &rxskb->nh.ipv6h->daddr);
  512. /* FIXME: calculate checksum, IPv4 also should... */
  513. fl.proto = IPPROTO_DCCP;
  514. fl.oif = inet6_iif(rxskb);
  515. fl.fl_ip_dport = dh->dccph_dport;
  516. fl.fl_ip_sport = dh->dccph_sport;
  517. if (!ip6_dst_lookup(NULL, &skb->dst, &fl)) {
  518. if (xfrm_lookup(&skb->dst, &fl, NULL, 0) >= 0) {
  519. ip6_xmit(dccp_v6_ctl_socket->sk, skb, &fl, NULL, 0);
  520. DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
  521. return;
  522. }
  523. }
  524. kfree_skb(skb);
  525. }
  526. static void dccp_v6_reqsk_send_ack(struct sk_buff *skb,
  527. struct request_sock *req)
  528. {
  529. dccp_v6_ctl_send_ack(skb);
  530. }
  531. static struct sock *dccp_v6_hnd_req(struct sock *sk,struct sk_buff *skb)
  532. {
  533. const struct dccp_hdr *dh = dccp_hdr(skb);
  534. const struct ipv6hdr *iph = skb->nh.ipv6h;
  535. struct sock *nsk;
  536. struct request_sock **prev;
  537. /* Find possible connection requests. */
  538. struct request_sock *req = inet6_csk_search_req(sk, &prev,
  539. dh->dccph_sport,
  540. &iph->saddr,
  541. &iph->daddr,
  542. inet6_iif(skb));
  543. if (req != NULL)
  544. return dccp_check_req(sk, skb, req, prev);
  545. nsk = __inet6_lookup_established(&dccp_hashinfo,
  546. &iph->saddr, dh->dccph_sport,
  547. &iph->daddr, ntohs(dh->dccph_dport),
  548. inet6_iif(skb));
  549. if (nsk != NULL) {
  550. if (nsk->sk_state != DCCP_TIME_WAIT) {
  551. bh_lock_sock(nsk);
  552. return nsk;
  553. }
  554. inet_twsk_put((struct inet_timewait_sock *)nsk);
  555. return NULL;
  556. }
  557. return sk;
  558. }
  559. static int dccp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
  560. {
  561. struct inet_request_sock *ireq;
  562. struct dccp_sock dp;
  563. struct request_sock *req;
  564. struct dccp_request_sock *dreq;
  565. struct inet6_request_sock *ireq6;
  566. struct ipv6_pinfo *np = inet6_sk(sk);
  567. const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
  568. struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
  569. __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
  570. if (skb->protocol == htons(ETH_P_IP))
  571. return dccp_v4_conn_request(sk, skb);
  572. if (!ipv6_unicast_destination(skb))
  573. goto drop;
  574. if (dccp_bad_service_code(sk, service)) {
  575. reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
  576. goto drop;
  577. }
  578. /*
  579. * There are no SYN attacks on IPv6, yet...
  580. */
  581. if (inet_csk_reqsk_queue_is_full(sk))
  582. goto drop;
  583. if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
  584. goto drop;
  585. req = inet6_reqsk_alloc(sk->sk_prot->rsk_prot);
  586. if (req == NULL)
  587. goto drop;
  588. /* FIXME: process options */
  589. dccp_openreq_init(req, &dp, skb);
  590. ireq6 = inet6_rsk(req);
  591. ireq = inet_rsk(req);
  592. ipv6_addr_copy(&ireq6->rmt_addr, &skb->nh.ipv6h->saddr);
  593. ipv6_addr_copy(&ireq6->loc_addr, &skb->nh.ipv6h->daddr);
  594. req->rcv_wnd = 100; /* Fake, option parsing will get the
  595. right value */
  596. ireq6->pktopts = NULL;
  597. if (ipv6_opt_accepted(sk, skb) ||
  598. np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
  599. np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
  600. atomic_inc(&skb->users);
  601. ireq6->pktopts = skb;
  602. }
  603. ireq6->iif = sk->sk_bound_dev_if;
  604. /* So that link locals have meaning */
  605. if (!sk->sk_bound_dev_if &&
  606. ipv6_addr_type(&ireq6->rmt_addr) & IPV6_ADDR_LINKLOCAL)
  607. ireq6->iif = inet6_iif(skb);
  608. /*
  609. * Step 3: Process LISTEN state
  610. *
  611. * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
  612. *
  613. * In fact we defer setting S.GSR, S.SWL, S.SWH to
  614. * dccp_create_openreq_child.
  615. */
  616. dreq = dccp_rsk(req);
  617. dreq->dreq_isr = dcb->dccpd_seq;
  618. dreq->dreq_iss = dccp_v6_init_sequence(sk, skb);
  619. dreq->dreq_service = service;
  620. if (dccp_v6_send_response(sk, req, NULL))
  621. goto drop_and_free;
  622. inet6_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
  623. return 0;
  624. drop_and_free:
  625. reqsk_free(req);
  626. drop:
  627. DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
  628. dcb->dccpd_reset_code = reset_code;
  629. return -1;
  630. }
  631. static struct sock *dccp_v6_request_recv_sock(struct sock *sk,
  632. struct sk_buff *skb,
  633. struct request_sock *req,
  634. struct dst_entry *dst)
  635. {
  636. struct inet6_request_sock *ireq6 = inet6_rsk(req);
  637. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  638. struct inet_sock *newinet;
  639. struct dccp_sock *newdp;
  640. struct dccp6_sock *newdp6;
  641. struct sock *newsk;
  642. struct ipv6_txoptions *opt;
  643. if (skb->protocol == htons(ETH_P_IP)) {
  644. /*
  645. * v6 mapped
  646. */
  647. newsk = dccp_v4_request_recv_sock(sk, skb, req, dst);
  648. if (newsk == NULL)
  649. return NULL;
  650. newdp6 = (struct dccp6_sock *)newsk;
  651. newdp = dccp_sk(newsk);
  652. newinet = inet_sk(newsk);
  653. newinet->pinet6 = &newdp6->inet6;
  654. newnp = inet6_sk(newsk);
  655. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  656. ipv6_addr_set(&newnp->daddr, 0, 0, htonl(0x0000FFFF),
  657. newinet->daddr);
  658. ipv6_addr_set(&newnp->saddr, 0, 0, htonl(0x0000FFFF),
  659. newinet->saddr);
  660. ipv6_addr_copy(&newnp->rcv_saddr, &newnp->saddr);
  661. inet_csk(newsk)->icsk_af_ops = &dccp_ipv6_mapped;
  662. newsk->sk_backlog_rcv = dccp_v4_do_rcv;
  663. newnp->pktoptions = NULL;
  664. newnp->opt = NULL;
  665. newnp->mcast_oif = inet6_iif(skb);
  666. newnp->mcast_hops = skb->nh.ipv6h->hop_limit;
  667. /*
  668. * No need to charge this sock to the relevant IPv6 refcnt debug socks count
  669. * here, dccp_create_openreq_child now does this for us, see the comment in
  670. * that function for the gory details. -acme
  671. */
  672. /* It is tricky place. Until this moment IPv4 tcp
  673. worked with IPv6 icsk.icsk_af_ops.
  674. Sync it now.
  675. */
  676. dccp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
  677. return newsk;
  678. }
  679. opt = np->opt;
  680. if (sk_acceptq_is_full(sk))
  681. goto out_overflow;
  682. if (np->rxopt.bits.osrcrt == 2 &&
  683. opt == NULL && ireq6->pktopts) {
  684. struct inet6_skb_parm *rxopt = IP6CB(ireq6->pktopts);
  685. if (rxopt->srcrt)
  686. opt = ipv6_invert_rthdr(sk,
  687. (struct ipv6_rt_hdr *)(ireq6->pktopts->nh.raw +
  688. rxopt->srcrt));
  689. }
  690. if (dst == NULL) {
  691. struct in6_addr *final_p = NULL, final;
  692. struct flowi fl;
  693. memset(&fl, 0, sizeof(fl));
  694. fl.proto = IPPROTO_DCCP;
  695. ipv6_addr_copy(&fl.fl6_dst, &ireq6->rmt_addr);
  696. if (opt && opt->srcrt) {
  697. struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
  698. ipv6_addr_copy(&final, &fl.fl6_dst);
  699. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  700. final_p = &final;
  701. }
  702. ipv6_addr_copy(&fl.fl6_src, &ireq6->loc_addr);
  703. fl.oif = sk->sk_bound_dev_if;
  704. fl.fl_ip_dport = inet_rsk(req)->rmt_port;
  705. fl.fl_ip_sport = inet_sk(sk)->sport;
  706. if (ip6_dst_lookup(sk, &dst, &fl))
  707. goto out;
  708. if (final_p)
  709. ipv6_addr_copy(&fl.fl6_dst, final_p);
  710. if ((xfrm_lookup(&dst, &fl, sk, 0)) < 0)
  711. goto out;
  712. }
  713. newsk = dccp_create_openreq_child(sk, req, skb);
  714. if (newsk == NULL)
  715. goto out;
  716. /*
  717. * No need to charge this sock to the relevant IPv6 refcnt debug socks
  718. * count here, dccp_create_openreq_child now does this for us, see the
  719. * comment in that function for the gory details. -acme
  720. */
  721. ip6_dst_store(newsk, dst, NULL);
  722. newsk->sk_route_caps = dst->dev->features &
  723. ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
  724. newdp6 = (struct dccp6_sock *)newsk;
  725. newinet = inet_sk(newsk);
  726. newinet->pinet6 = &newdp6->inet6;
  727. newdp = dccp_sk(newsk);
  728. newnp = inet6_sk(newsk);
  729. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  730. ipv6_addr_copy(&newnp->daddr, &ireq6->rmt_addr);
  731. ipv6_addr_copy(&newnp->saddr, &ireq6->loc_addr);
  732. ipv6_addr_copy(&newnp->rcv_saddr, &ireq6->loc_addr);
  733. newsk->sk_bound_dev_if = ireq6->iif;
  734. /* Now IPv6 options...
  735. First: no IPv4 options.
  736. */
  737. newinet->opt = NULL;
  738. /* Clone RX bits */
  739. newnp->rxopt.all = np->rxopt.all;
  740. /* Clone pktoptions received with SYN */
  741. newnp->pktoptions = NULL;
  742. if (ireq6->pktopts != NULL) {
  743. newnp->pktoptions = skb_clone(ireq6->pktopts, GFP_ATOMIC);
  744. kfree_skb(ireq6->pktopts);
  745. ireq6->pktopts = NULL;
  746. if (newnp->pktoptions)
  747. skb_set_owner_r(newnp->pktoptions, newsk);
  748. }
  749. newnp->opt = NULL;
  750. newnp->mcast_oif = inet6_iif(skb);
  751. newnp->mcast_hops = skb->nh.ipv6h->hop_limit;
  752. /* Clone native IPv6 options from listening socket (if any)
  753. Yes, keeping reference count would be much more clever,
  754. but we make one more one thing there: reattach optmem
  755. to newsk.
  756. */
  757. if (opt) {
  758. newnp->opt = ipv6_dup_options(newsk, opt);
  759. if (opt != np->opt)
  760. sock_kfree_s(sk, opt, opt->tot_len);
  761. }
  762. inet_csk(newsk)->icsk_ext_hdr_len = 0;
  763. if (newnp->opt)
  764. inet_csk(newsk)->icsk_ext_hdr_len = (newnp->opt->opt_nflen +
  765. newnp->opt->opt_flen);
  766. dccp_sync_mss(newsk, dst_mtu(dst));
  767. newinet->daddr = newinet->saddr = newinet->rcv_saddr = LOOPBACK4_IPV6;
  768. __inet6_hash(&dccp_hashinfo, newsk);
  769. inet_inherit_port(&dccp_hashinfo, sk, newsk);
  770. return newsk;
  771. out_overflow:
  772. NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
  773. out:
  774. NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
  775. if (opt && opt != np->opt)
  776. sock_kfree_s(sk, opt, opt->tot_len);
  777. dst_release(dst);
  778. return NULL;
  779. }
  780. /* The socket must have it's spinlock held when we get
  781. * here.
  782. *
  783. * We have a potential double-lock case here, so even when
  784. * doing backlog processing we use the BH locking scheme.
  785. * This is because we cannot sleep with the original spinlock
  786. * held.
  787. */
  788. static int dccp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
  789. {
  790. struct ipv6_pinfo *np = inet6_sk(sk);
  791. struct sk_buff *opt_skb = NULL;
  792. /* Imagine: socket is IPv6. IPv4 packet arrives,
  793. goes to IPv4 receive handler and backlogged.
  794. From backlog it always goes here. Kerboom...
  795. Fortunately, dccp_rcv_established and rcv_established
  796. handle them correctly, but it is not case with
  797. dccp_v6_hnd_req and dccp_v6_ctl_send_reset(). --ANK
  798. */
  799. if (skb->protocol == htons(ETH_P_IP))
  800. return dccp_v4_do_rcv(sk, skb);
  801. if (sk_filter(sk, skb, 0))
  802. goto discard;
  803. /*
  804. * socket locking is here for SMP purposes as backlog rcv
  805. * is currently called with bh processing disabled.
  806. */
  807. /* Do Stevens' IPV6_PKTOPTIONS.
  808. Yes, guys, it is the only place in our code, where we
  809. may make it not affecting IPv4.
  810. The rest of code is protocol independent,
  811. and I do not like idea to uglify IPv4.
  812. Actually, all the idea behind IPV6_PKTOPTIONS
  813. looks not very well thought. For now we latch
  814. options, received in the last packet, enqueued
  815. by tcp. Feel free to propose better solution.
  816. --ANK (980728)
  817. */
  818. if (np->rxopt.all)
  819. opt_skb = skb_clone(skb, GFP_ATOMIC);
  820. if (sk->sk_state == DCCP_OPEN) { /* Fast path */
  821. if (dccp_rcv_established(sk, skb, dccp_hdr(skb), skb->len))
  822. goto reset;
  823. return 0;
  824. }
  825. if (sk->sk_state == DCCP_LISTEN) {
  826. struct sock *nsk = dccp_v6_hnd_req(sk, skb);
  827. if (!nsk)
  828. goto discard;
  829. /*
  830. * Queue it on the new socket if the new socket is active,
  831. * otherwise we just shortcircuit this and continue with
  832. * the new socket..
  833. */
  834. if(nsk != sk) {
  835. if (dccp_child_process(sk, nsk, skb))
  836. goto reset;
  837. if (opt_skb)
  838. __kfree_skb(opt_skb);
  839. return 0;
  840. }
  841. }
  842. if (dccp_rcv_state_process(sk, skb, dccp_hdr(skb), skb->len))
  843. goto reset;
  844. return 0;
  845. reset:
  846. dccp_v6_ctl_send_reset(skb);
  847. discard:
  848. if (opt_skb)
  849. __kfree_skb(opt_skb);
  850. kfree_skb(skb);
  851. return 0;
  852. }
  853. static int dccp_v6_rcv(struct sk_buff **pskb)
  854. {
  855. const struct dccp_hdr *dh;
  856. struct sk_buff *skb = *pskb;
  857. struct sock *sk;
  858. /* Step 1: Check header basics: */
  859. if (dccp_invalid_packet(skb))
  860. goto discard_it;
  861. dh = dccp_hdr(skb);
  862. DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
  863. DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
  864. if (dccp_packet_without_ack(skb))
  865. DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
  866. else
  867. DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
  868. /* Step 2:
  869. * Look up flow ID in table and get corresponding socket */
  870. sk = __inet6_lookup(&dccp_hashinfo, &skb->nh.ipv6h->saddr,
  871. dh->dccph_sport,
  872. &skb->nh.ipv6h->daddr, ntohs(dh->dccph_dport),
  873. inet6_iif(skb));
  874. /*
  875. * Step 2:
  876. * If no socket ...
  877. * Generate Reset(No Connection) unless P.type == Reset
  878. * Drop packet and return
  879. */
  880. if (sk == NULL)
  881. goto no_dccp_socket;
  882. /*
  883. * Step 2:
  884. * ... or S.state == TIMEWAIT,
  885. * Generate Reset(No Connection) unless P.type == Reset
  886. * Drop packet and return
  887. */
  888. if (sk->sk_state == DCCP_TIME_WAIT)
  889. goto do_time_wait;
  890. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
  891. goto discard_and_relse;
  892. return sk_receive_skb(sk, skb) ? -1 : 0;
  893. no_dccp_socket:
  894. if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
  895. goto discard_it;
  896. /*
  897. * Step 2:
  898. * Generate Reset(No Connection) unless P.type == Reset
  899. * Drop packet and return
  900. */
  901. if (dh->dccph_type != DCCP_PKT_RESET) {
  902. DCCP_SKB_CB(skb)->dccpd_reset_code =
  903. DCCP_RESET_CODE_NO_CONNECTION;
  904. dccp_v6_ctl_send_reset(skb);
  905. }
  906. discard_it:
  907. /*
  908. * Discard frame
  909. */
  910. kfree_skb(skb);
  911. return 0;
  912. discard_and_relse:
  913. sock_put(sk);
  914. goto discard_it;
  915. do_time_wait:
  916. inet_twsk_put((struct inet_timewait_sock *)sk);
  917. goto no_dccp_socket;
  918. }
  919. static struct inet_connection_sock_af_ops dccp_ipv6_af_ops = {
  920. .queue_xmit = inet6_csk_xmit,
  921. .send_check = dccp_v6_send_check,
  922. .rebuild_header = inet6_sk_rebuild_header,
  923. .conn_request = dccp_v6_conn_request,
  924. .syn_recv_sock = dccp_v6_request_recv_sock,
  925. .net_header_len = sizeof(struct ipv6hdr),
  926. .setsockopt = ipv6_setsockopt,
  927. .getsockopt = ipv6_getsockopt,
  928. .addr2sockaddr = inet6_csk_addr2sockaddr,
  929. .sockaddr_len = sizeof(struct sockaddr_in6)
  930. };
  931. /*
  932. * DCCP over IPv4 via INET6 API
  933. */
  934. static struct inet_connection_sock_af_ops dccp_ipv6_mapped = {
  935. .queue_xmit = ip_queue_xmit,
  936. .send_check = dccp_v4_send_check,
  937. .rebuild_header = inet_sk_rebuild_header,
  938. .conn_request = dccp_v6_conn_request,
  939. .syn_recv_sock = dccp_v6_request_recv_sock,
  940. .net_header_len = sizeof(struct iphdr),
  941. .setsockopt = ipv6_setsockopt,
  942. .getsockopt = ipv6_getsockopt,
  943. .addr2sockaddr = inet6_csk_addr2sockaddr,
  944. .sockaddr_len = sizeof(struct sockaddr_in6)
  945. };
  946. /* NOTE: A lot of things set to zero explicitly by call to
  947. * sk_alloc() so need not be done here.
  948. */
  949. static int dccp_v6_init_sock(struct sock *sk)
  950. {
  951. static __u8 dccp_v6_ctl_sock_initialized;
  952. int err = dccp_init_sock(sk, dccp_v6_ctl_sock_initialized);
  953. if (err == 0) {
  954. if (unlikely(!dccp_v6_ctl_sock_initialized))
  955. dccp_v6_ctl_sock_initialized = 1;
  956. inet_csk(sk)->icsk_af_ops = &dccp_ipv6_af_ops;
  957. }
  958. return err;
  959. }
  960. static int dccp_v6_destroy_sock(struct sock *sk)
  961. {
  962. dccp_destroy_sock(sk);
  963. return inet6_destroy_sock(sk);
  964. }
  965. static struct proto dccp_v6_prot = {
  966. .name = "DCCPv6",
  967. .owner = THIS_MODULE,
  968. .close = dccp_close,
  969. .connect = dccp_v6_connect,
  970. .disconnect = dccp_disconnect,
  971. .ioctl = dccp_ioctl,
  972. .init = dccp_v6_init_sock,
  973. .setsockopt = dccp_setsockopt,
  974. .getsockopt = dccp_getsockopt,
  975. .sendmsg = dccp_sendmsg,
  976. .recvmsg = dccp_recvmsg,
  977. .backlog_rcv = dccp_v6_do_rcv,
  978. .hash = dccp_v6_hash,
  979. .unhash = dccp_unhash,
  980. .accept = inet_csk_accept,
  981. .get_port = dccp_v6_get_port,
  982. .shutdown = dccp_shutdown,
  983. .destroy = dccp_v6_destroy_sock,
  984. .orphan_count = &dccp_orphan_count,
  985. .max_header = MAX_DCCP_HEADER,
  986. .obj_size = sizeof(struct dccp6_sock),
  987. .rsk_prot = &dccp6_request_sock_ops,
  988. .twsk_prot = &dccp6_timewait_sock_ops,
  989. };
  990. static struct inet6_protocol dccp_v6_protocol = {
  991. .handler = dccp_v6_rcv,
  992. .err_handler = dccp_v6_err,
  993. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
  994. };
  995. static struct proto_ops inet6_dccp_ops = {
  996. .family = PF_INET6,
  997. .owner = THIS_MODULE,
  998. .release = inet6_release,
  999. .bind = inet6_bind,
  1000. .connect = inet_stream_connect,
  1001. .socketpair = sock_no_socketpair,
  1002. .accept = inet_accept,
  1003. .getname = inet6_getname,
  1004. .poll = dccp_poll,
  1005. .ioctl = inet6_ioctl,
  1006. .listen = inet_dccp_listen,
  1007. .shutdown = inet_shutdown,
  1008. .setsockopt = sock_common_setsockopt,
  1009. .getsockopt = sock_common_getsockopt,
  1010. .sendmsg = inet_sendmsg,
  1011. .recvmsg = sock_common_recvmsg,
  1012. .mmap = sock_no_mmap,
  1013. .sendpage = sock_no_sendpage,
  1014. };
  1015. static struct inet_protosw dccp_v6_protosw = {
  1016. .type = SOCK_DCCP,
  1017. .protocol = IPPROTO_DCCP,
  1018. .prot = &dccp_v6_prot,
  1019. .ops = &inet6_dccp_ops,
  1020. .capability = -1,
  1021. .flags = INET_PROTOSW_ICSK,
  1022. };
  1023. static char dccp_v6_ctl_socket_err_msg[] __initdata =
  1024. KERN_ERR "DCCP: Failed to create the control socket.\n";
  1025. static int __init dccp_v6_ctl_sock_init(void)
  1026. {
  1027. int rc = sock_create_kern(PF_INET6, SOCK_DCCP, IPPROTO_DCCP,
  1028. &dccp_v6_ctl_socket);
  1029. if (rc < 0)
  1030. printk(dccp_v6_ctl_socket_err_msg);
  1031. else {
  1032. dccp_v6_ctl_socket->sk->sk_allocation = GFP_ATOMIC;
  1033. inet_sk(dccp_v6_ctl_socket->sk)->uc_ttl = -1;
  1034. /* Unhash it so that IP input processing does not even
  1035. * see it, we do not wish this socket to see incoming
  1036. * packets.
  1037. */
  1038. dccp_v6_ctl_socket->sk->sk_prot->unhash(dccp_v6_ctl_socket->sk);
  1039. }
  1040. return rc;
  1041. }
  1042. static int __init dccp_v6_init(void)
  1043. {
  1044. int err = proto_register(&dccp_v6_prot, 1);
  1045. if (err != 0)
  1046. goto out;
  1047. err = inet6_add_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
  1048. if (err != 0)
  1049. goto out_unregister_proto;
  1050. inet6_register_protosw(&dccp_v6_protosw);
  1051. if (dccp_v6_ctl_sock_init() != 0)
  1052. goto out_unregister_protosw;
  1053. out:
  1054. return err;
  1055. out_unregister_protosw:
  1056. inet6_del_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
  1057. inet6_unregister_protosw(&dccp_v6_protosw);
  1058. out_unregister_proto:
  1059. proto_unregister(&dccp_v6_prot);
  1060. goto out;
  1061. }
  1062. static void __exit dccp_v6_exit(void)
  1063. {
  1064. inet6_del_protocol(&dccp_v6_protocol, IPPROTO_DCCP);
  1065. inet6_unregister_protosw(&dccp_v6_protosw);
  1066. proto_unregister(&dccp_v6_prot);
  1067. }
  1068. module_init(dccp_v6_init);
  1069. module_exit(dccp_v6_exit);
  1070. /*
  1071. * __stringify doesn't likes enums, so use SOCK_DCCP (6) and IPPROTO_DCCP (33)
  1072. * values directly, Also cover the case where the protocol is not specified,
  1073. * i.e. net-pf-PF_INET6-proto-0-type-SOCK_DCCP
  1074. */
  1075. MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-33-type-6");
  1076. MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-0-type-6");
  1077. MODULE_LICENSE("GPL");
  1078. MODULE_AUTHOR("Arnaldo Carvalho de Melo <acme@mandriva.com>");
  1079. MODULE_DESCRIPTION("DCCPv6 - Datagram Congestion Controlled Protocol");