]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/dccp/ipv4.c
[DCCP]: Use reqsk_free in dccp_v4_conn_request
[net-next-2.6.git] / net / dccp / ipv4.c
CommitLineData
7c657876
ACM
1/*
2 * net/dccp/ipv4.c
3 *
4 * An implementation of the DCCP protocol
5 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13#include <linux/config.h>
14#include <linux/dccp.h>
15#include <linux/icmp.h>
16#include <linux/module.h>
17#include <linux/skbuff.h>
18#include <linux/random.h>
19
20#include <net/icmp.h>
21#include <net/inet_hashtables.h>
22#include <net/sock.h>
23#include <net/tcp_states.h>
24#include <net/xfrm.h>
25
ae31c339 26#include "ackvec.h"
7c657876
ACM
27#include "ccid.h"
28#include "dccp.h"
29
30struct inet_hashinfo __cacheline_aligned dccp_hashinfo = {
31 .lhash_lock = RW_LOCK_UNLOCKED,
32 .lhash_users = ATOMIC_INIT(0),
7690af3f 33 .lhash_wait = __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo.lhash_wait),
7c657876
ACM
34};
35
540722ff
ACM
36EXPORT_SYMBOL_GPL(dccp_hashinfo);
37
7c657876
ACM
38static int dccp_v4_get_port(struct sock *sk, const unsigned short snum)
39{
971af18b
ACM
40 return inet_csk_get_port(&dccp_hashinfo, sk, snum,
41 inet_csk_bind_conflict);
7c657876
ACM
42}
43
44static void dccp_v4_hash(struct sock *sk)
45{
46 inet_hash(&dccp_hashinfo, sk);
47}
48
f21e68ca 49void dccp_unhash(struct sock *sk)
7c657876
ACM
50{
51 inet_unhash(&dccp_hashinfo, sk);
52}
53
f21e68ca
ACM
54EXPORT_SYMBOL_GPL(dccp_unhash);
55
7c657876
ACM
56/* called with local bh disabled */
57static int __dccp_v4_check_established(struct sock *sk, const __u16 lport,
58 struct inet_timewait_sock **twp)
59{
60 struct inet_sock *inet = inet_sk(sk);
61 const u32 daddr = inet->rcv_saddr;
62 const u32 saddr = inet->daddr;
63 const int dif = sk->sk_bound_dev_if;
64 INET_ADDR_COOKIE(acookie, saddr, daddr)
65 const __u32 ports = INET_COMBINED_PORTS(inet->dport, lport);
81c3d547
ED
66 unsigned int hash = inet_ehashfn(daddr, lport, saddr, inet->dport);
67 struct inet_ehash_bucket *head = inet_ehash_bucket(&dccp_hashinfo, hash);
7c657876
ACM
68 const struct sock *sk2;
69 const struct hlist_node *node;
70 struct inet_timewait_sock *tw;
71
81c3d547 72 prefetch(head->chain.first);
7c657876
ACM
73 write_lock(&head->lock);
74
75 /* Check TIME-WAIT sockets first. */
76 sk_for_each(sk2, node, &(head + dccp_hashinfo.ehash_size)->chain) {
77 tw = inet_twsk(sk2);
78
81c3d547 79 if (INET_TW_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif))
7c657876
ACM
80 goto not_unique;
81 }
82 tw = NULL;
83
84 /* And established part... */
85 sk_for_each(sk2, node, &head->chain) {
81c3d547 86 if (INET_MATCH(sk2, hash, acookie, saddr, daddr, ports, dif))
7c657876
ACM
87 goto not_unique;
88 }
89
90 /* Must record num and sport now. Otherwise we will see
91 * in hash table socket with a funny identity. */
92 inet->num = lport;
93 inet->sport = htons(lport);
81c3d547 94 sk->sk_hash = hash;
7c657876
ACM
95 BUG_TRAP(sk_unhashed(sk));
96 __sk_add_node(sk, &head->chain);
97 sock_prot_inc_use(sk->sk_prot);
98 write_unlock(&head->lock);
99
100 if (twp != NULL) {
101 *twp = tw;
102 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
103 } else if (tw != NULL) {
104 /* Silly. Should hash-dance instead... */
64cf1e5d 105 inet_twsk_deschedule(tw, &dccp_death_row);
7c657876
ACM
106 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED);
107
108 inet_twsk_put(tw);
109 }
110
111 return 0;
112
113not_unique:
114 write_unlock(&head->lock);
115 return -EADDRNOTAVAIL;
116}
117
118/*
119 * Bind a port for a connect operation and hash it.
120 */
121static int dccp_v4_hash_connect(struct sock *sk)
122{
123 const unsigned short snum = inet_sk(sk)->num;
124 struct inet_bind_hashbucket *head;
125 struct inet_bind_bucket *tb;
126 int ret;
127
128 if (snum == 0) {
7c657876
ACM
129 int low = sysctl_local_port_range[0];
130 int high = sysctl_local_port_range[1];
131 int remaining = (high - low) + 1;
6df71634 132 int rover = net_random() % (high - low) + low;
7c657876
ACM
133 struct hlist_node *node;
134 struct inet_timewait_sock *tw = NULL;
135
136 local_bh_disable();
7c657876 137 do {
7690af3f
ACM
138 head = &dccp_hashinfo.bhash[inet_bhashfn(rover,
139 dccp_hashinfo.bhash_size)];
7c657876
ACM
140 spin_lock(&head->lock);
141
142 /* Does not bother with rcv_saddr checks,
143 * because the established check is already
144 * unique enough.
145 */
146 inet_bind_bucket_for_each(tb, node, &head->chain) {
147 if (tb->port == rover) {
148 BUG_TRAP(!hlist_empty(&tb->owners));
149 if (tb->fastreuse >= 0)
150 goto next_port;
151 if (!__dccp_v4_check_established(sk,
152 rover,
153 &tw))
154 goto ok;
155 goto next_port;
156 }
157 }
158
7690af3f
ACM
159 tb = inet_bind_bucket_create(dccp_hashinfo.bind_bucket_cachep,
160 head, rover);
7c657876
ACM
161 if (tb == NULL) {
162 spin_unlock(&head->lock);
163 break;
164 }
165 tb->fastreuse = -1;
166 goto ok;
167
168 next_port:
169 spin_unlock(&head->lock);
6df71634
SH
170 if (++rover > high)
171 rover = low;
7c657876 172 } while (--remaining > 0);
7c657876
ACM
173
174 local_bh_enable();
175
176 return -EADDRNOTAVAIL;
177
178ok:
179 /* All locks still held and bhs disabled */
7c657876
ACM
180 inet_bind_hash(sk, tb, rover);
181 if (sk_unhashed(sk)) {
182 inet_sk(sk)->sport = htons(rover);
183 __inet_hash(&dccp_hashinfo, sk, 0);
184 }
185 spin_unlock(&head->lock);
186
187 if (tw != NULL) {
64cf1e5d 188 inet_twsk_deschedule(tw, &dccp_death_row);
7c657876
ACM
189 inet_twsk_put(tw);
190 }
191
192 ret = 0;
193 goto out;
194 }
195
7690af3f
ACM
196 head = &dccp_hashinfo.bhash[inet_bhashfn(snum,
197 dccp_hashinfo.bhash_size)];
7c657876
ACM
198 tb = inet_csk(sk)->icsk_bind_hash;
199 spin_lock_bh(&head->lock);
200 if (sk_head(&tb->owners) == sk && sk->sk_bind_node.next == NULL) {
201 __inet_hash(&dccp_hashinfo, sk, 0);
202 spin_unlock_bh(&head->lock);
203 return 0;
204 } else {
205 spin_unlock(&head->lock);
206 /* No definite answer... Walk to established hash table */
207 ret = __dccp_v4_check_established(sk, snum, NULL);
208out:
209 local_bh_enable();
210 return ret;
211 }
212}
213
f21e68ca 214int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
7c657876
ACM
215{
216 struct inet_sock *inet = inet_sk(sk);
217 struct dccp_sock *dp = dccp_sk(sk);
218 const struct sockaddr_in *usin = (struct sockaddr_in *)uaddr;
219 struct rtable *rt;
220 u32 daddr, nexthop;
221 int tmp;
222 int err;
223
224 dp->dccps_role = DCCP_ROLE_CLIENT;
225
67e6b629
ACM
226 if (dccp_service_not_initialized(sk))
227 return -EPROTO;
228
7c657876
ACM
229 if (addr_len < sizeof(struct sockaddr_in))
230 return -EINVAL;
231
232 if (usin->sin_family != AF_INET)
233 return -EAFNOSUPPORT;
234
235 nexthop = daddr = usin->sin_addr.s_addr;
236 if (inet->opt != NULL && inet->opt->srr) {
237 if (daddr == 0)
238 return -EINVAL;
239 nexthop = inet->opt->faddr;
240 }
241
242 tmp = ip_route_connect(&rt, nexthop, inet->saddr,
243 RT_CONN_FLAGS(sk), sk->sk_bound_dev_if,
244 IPPROTO_DCCP,
245 inet->sport, usin->sin_port, sk);
246 if (tmp < 0)
247 return tmp;
248
249 if (rt->rt_flags & (RTCF_MULTICAST | RTCF_BROADCAST)) {
250 ip_rt_put(rt);
251 return -ENETUNREACH;
252 }
253
254 if (inet->opt == NULL || !inet->opt->srr)
255 daddr = rt->rt_dst;
256
257 if (inet->saddr == 0)
258 inet->saddr = rt->rt_src;
259 inet->rcv_saddr = inet->saddr;
260
261 inet->dport = usin->sin_port;
262 inet->daddr = daddr;
263
264 dp->dccps_ext_header_len = 0;
265 if (inet->opt != NULL)
266 dp->dccps_ext_header_len = inet->opt->optlen;
267 /*
268 * Socket identity is still unknown (sport may be zero).
269 * However we set state to DCCP_REQUESTING and not releasing socket
270 * lock select source port, enter ourselves into the hash tables and
271 * complete initialization after this.
272 */
273 dccp_set_state(sk, DCCP_REQUESTING);
274 err = dccp_v4_hash_connect(sk);
275 if (err != 0)
276 goto failure;
277
278 err = ip_route_newports(&rt, inet->sport, inet->dport, sk);
279 if (err != 0)
280 goto failure;
281
282 /* OK, now commit destination to socket. */
283 sk_setup_caps(sk, &rt->u.dst);
284
285 dp->dccps_gar =
286 dp->dccps_iss = secure_dccp_sequence_number(inet->saddr,
287 inet->daddr,
288 inet->sport,
289 usin->sin_port);
290 dccp_update_gss(sk, dp->dccps_iss);
291
292 inet->id = dp->dccps_iss ^ jiffies;
293
294 err = dccp_connect(sk);
295 rt = NULL;
296 if (err != 0)
297 goto failure;
298out:
299 return err;
300failure:
7690af3f
ACM
301 /*
302 * This unhashes the socket and releases the local port, if necessary.
303 */
7c657876
ACM
304 dccp_set_state(sk, DCCP_CLOSED);
305 ip_rt_put(rt);
306 sk->sk_route_caps = 0;
307 inet->dport = 0;
308 goto out;
309}
310
f21e68ca
ACM
311EXPORT_SYMBOL_GPL(dccp_v4_connect);
312
7c657876
ACM
313/*
314 * This routine does path mtu discovery as defined in RFC1191.
315 */
316static inline void dccp_do_pmtu_discovery(struct sock *sk,
317 const struct iphdr *iph,
318 u32 mtu)
319{
320 struct dst_entry *dst;
321 const struct inet_sock *inet = inet_sk(sk);
322 const struct dccp_sock *dp = dccp_sk(sk);
323
324 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
325 * send out by Linux are always < 576bytes so they should go through
326 * unfragmented).
327 */
328 if (sk->sk_state == DCCP_LISTEN)
329 return;
330
331 /* We don't check in the destentry if pmtu discovery is forbidden
332 * on this route. We just assume that no packet_to_big packets
333 * are send back when pmtu discovery is not active.
334 * There is a small race when the user changes this flag in the
335 * route, but I think that's acceptable.
336 */
337 if ((dst = __sk_dst_check(sk, 0)) == NULL)
338 return;
339
340 dst->ops->update_pmtu(dst, mtu);
341
342 /* Something is about to be wrong... Remember soft error
343 * for the case, if this connection will not able to recover.
344 */
345 if (mtu < dst_mtu(dst) && ip_dont_fragment(sk, dst))
346 sk->sk_err_soft = EMSGSIZE;
347
348 mtu = dst_mtu(dst);
349
350 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
351 dp->dccps_pmtu_cookie > mtu) {
352 dccp_sync_mss(sk, mtu);
353
354 /*
355 * From: draft-ietf-dccp-spec-11.txt
356 *
7690af3f
ACM
357 * DCCP-Sync packets are the best choice for upward
358 * probing, since DCCP-Sync probes do not risk application
359 * data loss.
7c657876 360 */
e92ae93a 361 dccp_send_sync(sk, dp->dccps_gsr, DCCP_PKT_SYNC);
7c657876
ACM
362 } /* else let the usual retransmit timer handle it */
363}
364
365static void dccp_v4_ctl_send_ack(struct sk_buff *rxskb)
366{
367 int err;
368 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
369 const int dccp_hdr_ack_len = sizeof(struct dccp_hdr) +
370 sizeof(struct dccp_hdr_ext) +
371 sizeof(struct dccp_hdr_ack_bits);
372 struct sk_buff *skb;
373
374 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
375 return;
376
377 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
378 if (skb == NULL)
379 return;
380
381 /* Reserve space for headers. */
382 skb_reserve(skb, MAX_DCCP_HEADER);
383
384 skb->dst = dst_clone(rxskb->dst);
385
386 skb->h.raw = skb_push(skb, dccp_hdr_ack_len);
387 dh = dccp_hdr(skb);
388 memset(dh, 0, dccp_hdr_ack_len);
389
390 /* Build DCCP header and checksum it. */
391 dh->dccph_type = DCCP_PKT_ACK;
392 dh->dccph_sport = rxdh->dccph_dport;
393 dh->dccph_dport = rxdh->dccph_sport;
394 dh->dccph_doff = dccp_hdr_ack_len / 4;
395 dh->dccph_x = 1;
396
397 dccp_hdr_set_seq(dh, DCCP_SKB_CB(rxskb)->dccpd_ack_seq);
7690af3f
ACM
398 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
399 DCCP_SKB_CB(rxskb)->dccpd_seq);
7c657876
ACM
400
401 bh_lock_sock(dccp_ctl_socket->sk);
402 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
7690af3f
ACM
403 rxskb->nh.iph->daddr,
404 rxskb->nh.iph->saddr, NULL);
7c657876
ACM
405 bh_unlock_sock(dccp_ctl_socket->sk);
406
407 if (err == NET_XMIT_CN || err == 0) {
408 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
409 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
410 }
411}
412
7690af3f
ACM
413static void dccp_v4_reqsk_send_ack(struct sk_buff *skb,
414 struct request_sock *req)
7c657876
ACM
415{
416 dccp_v4_ctl_send_ack(skb);
417}
418
419static int dccp_v4_send_response(struct sock *sk, struct request_sock *req,
420 struct dst_entry *dst)
421{
422 int err = -1;
423 struct sk_buff *skb;
424
425 /* First, grab a route. */
426
427 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
428 goto out;
429
430 skb = dccp_make_response(sk, dst, req);
431 if (skb != NULL) {
432 const struct inet_request_sock *ireq = inet_rsk(req);
433
49c5bfaf 434 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
7c657876
ACM
435 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr,
436 ireq->rmt_addr,
437 ireq->opt);
438 if (err == NET_XMIT_CN)
439 err = 0;
440 }
441
442out:
443 dst_release(dst);
444 return err;
445}
446
447/*
448 * This routine is called by the ICMP module when it gets some sort of error
449 * condition. If err < 0 then the socket should be closed and the error
450 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
451 * After adjustment header points to the first 8 bytes of the tcp header. We
452 * need to find the appropriate port.
453 *
454 * The locking strategy used here is very "optimistic". When someone else
455 * accesses the socket the ICMP is just dropped and for some paths there is no
456 * check at all. A more general error queue to queue errors for later handling
457 * is probably better.
458 */
459void dccp_v4_err(struct sk_buff *skb, u32 info)
460{
461 const struct iphdr *iph = (struct iphdr *)skb->data;
7690af3f
ACM
462 const struct dccp_hdr *dh = (struct dccp_hdr *)(skb->data +
463 (iph->ihl << 2));
7c657876
ACM
464 struct dccp_sock *dp;
465 struct inet_sock *inet;
466 const int type = skb->h.icmph->type;
467 const int code = skb->h.icmph->code;
468 struct sock *sk;
469 __u64 seq;
470 int err;
471
472 if (skb->len < (iph->ihl << 2) + 8) {
473 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
474 return;
475 }
476
477 sk = inet_lookup(&dccp_hashinfo, iph->daddr, dh->dccph_dport,
478 iph->saddr, dh->dccph_sport, inet_iif(skb));
479 if (sk == NULL) {
480 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS);
481 return;
482 }
483
484 if (sk->sk_state == DCCP_TIME_WAIT) {
485 inet_twsk_put((struct inet_timewait_sock *)sk);
486 return;
487 }
488
489 bh_lock_sock(sk);
490 /* If too many ICMPs get dropped on busy
491 * servers this needs to be solved differently.
492 */
493 if (sock_owned_by_user(sk))
494 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS);
495
496 if (sk->sk_state == DCCP_CLOSED)
497 goto out;
498
499 dp = dccp_sk(sk);
500 seq = dccp_hdr_seq(skb);
501 if (sk->sk_state != DCCP_LISTEN &&
502 !between48(seq, dp->dccps_swl, dp->dccps_swh)) {
503 NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS);
504 goto out;
505 }
506
507 switch (type) {
508 case ICMP_SOURCE_QUENCH:
509 /* Just silently ignore these. */
510 goto out;
511 case ICMP_PARAMETERPROB:
512 err = EPROTO;
513 break;
514 case ICMP_DEST_UNREACH:
515 if (code > NR_ICMP_UNREACH)
516 goto out;
517
518 if (code == ICMP_FRAG_NEEDED) { /* PMTU discovery (RFC1191) */
519 if (!sock_owned_by_user(sk))
520 dccp_do_pmtu_discovery(sk, iph, info);
521 goto out;
522 }
523
524 err = icmp_err_convert[code].errno;
525 break;
526 case ICMP_TIME_EXCEEDED:
527 err = EHOSTUNREACH;
528 break;
529 default:
530 goto out;
531 }
532
533 switch (sk->sk_state) {
534 struct request_sock *req , **prev;
535 case DCCP_LISTEN:
536 if (sock_owned_by_user(sk))
537 goto out;
538 req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
539 iph->daddr, iph->saddr);
540 if (!req)
541 goto out;
542
543 /*
544 * ICMPs are not backlogged, hence we cannot get an established
545 * socket here.
546 */
547 BUG_TRAP(!req->sk);
548
549 if (seq != dccp_rsk(req)->dreq_iss) {
550 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS);
551 goto out;
552 }
553 /*
554 * Still in RESPOND, just remove it silently.
555 * There is no good way to pass the error to the newly
556 * created socket, and POSIX does not want network
557 * errors returned from accept().
558 */
559 inet_csk_reqsk_queue_drop(sk, req, prev);
560 goto out;
561
562 case DCCP_REQUESTING:
563 case DCCP_RESPOND:
564 if (!sock_owned_by_user(sk)) {
565 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
566 sk->sk_err = err;
567
568 sk->sk_error_report(sk);
569
570 dccp_done(sk);
571 } else
572 sk->sk_err_soft = err;
573 goto out;
574 }
575
576 /* If we've already connected we will keep trying
577 * until we time out, or the user gives up.
578 *
579 * rfc1122 4.2.3.9 allows to consider as hard errors
580 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
581 * but it is obsoleted by pmtu discovery).
582 *
583 * Note, that in modern internet, where routing is unreliable
584 * and in each dark corner broken firewalls sit, sending random
585 * errors ordered by their masters even this two messages finally lose
586 * their original sense (even Linux sends invalid PORT_UNREACHs)
587 *
588 * Now we are in compliance with RFCs.
589 * --ANK (980905)
590 */
591
592 inet = inet_sk(sk);
593 if (!sock_owned_by_user(sk) && inet->recverr) {
594 sk->sk_err = err;
595 sk->sk_error_report(sk);
596 } else /* Only an error on timeout */
597 sk->sk_err_soft = err;
598out:
599 bh_unlock_sock(sk);
600 sock_put(sk);
601}
602
57cca05a 603/* This routine computes an IPv4 DCCP checksum. */
f21e68ca 604void dccp_v4_send_check(struct sock *sk, int len, struct sk_buff *skb)
57cca05a
ACM
605{
606 const struct inet_sock *inet = inet_sk(sk);
607 struct dccp_hdr *dh = dccp_hdr(skb);
608
609 dh->dccph_checksum = dccp_v4_checksum(skb, inet->saddr, inet->daddr);
610}
611
f21e68ca
ACM
612EXPORT_SYMBOL_GPL(dccp_v4_send_check);
613
7c657876
ACM
614int dccp_v4_send_reset(struct sock *sk, enum dccp_reset_codes code)
615{
616 struct sk_buff *skb;
617 /*
618 * FIXME: what if rebuild_header fails?
619 * Should we be doing a rebuild_header here?
620 */
621 int err = inet_sk_rebuild_header(sk);
622
623 if (err != 0)
624 return err;
625
626 skb = dccp_make_reset(sk, sk->sk_dst_cache, code);
627 if (skb != NULL) {
7c657876
ACM
628 const struct inet_sock *inet = inet_sk(sk);
629
49c5bfaf 630 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
7c657876
ACM
631 err = ip_build_and_send_pkt(skb, sk,
632 inet->saddr, inet->daddr, NULL);
633 if (err == NET_XMIT_CN)
634 err = 0;
7c657876
ACM
635 }
636
637 return err;
638}
639
640static inline u64 dccp_v4_init_sequence(const struct sock *sk,
641 const struct sk_buff *skb)
642{
643 return secure_dccp_sequence_number(skb->nh.iph->daddr,
644 skb->nh.iph->saddr,
645 dccp_hdr(skb)->dccph_dport,
646 dccp_hdr(skb)->dccph_sport);
647}
648
649int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
650{
651 struct inet_request_sock *ireq;
652 struct dccp_sock dp;
653 struct request_sock *req;
654 struct dccp_request_sock *dreq;
655 const __u32 saddr = skb->nh.iph->saddr;
656 const __u32 daddr = skb->nh.iph->daddr;
67e6b629 657 const __u32 service = dccp_hdr_request(skb)->dccph_req_service;
0c10c5d9
ACM
658 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
659 __u8 reset_code = DCCP_RESET_CODE_TOO_BUSY;
7c657876
ACM
660
661 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
662 if (((struct rtable *)skb->dst)->rt_flags &
0c10c5d9
ACM
663 (RTCF_BROADCAST | RTCF_MULTICAST)) {
664 reset_code = DCCP_RESET_CODE_NO_CONNECTION;
7c657876 665 goto drop;
0c10c5d9 666 }
7c657876 667
67e6b629
ACM
668 if (dccp_bad_service_code(sk, service)) {
669 reset_code = DCCP_RESET_CODE_BAD_SERVICE_CODE;
670 goto drop;
671 }
7c657876
ACM
672 /*
673 * TW buckets are converted to open requests without
674 * limitations, they conserve resources and peer is
675 * evidently real one.
676 */
677 if (inet_csk_reqsk_queue_is_full(sk))
678 goto drop;
679
680 /*
681 * Accept backlog is full. If we have already queued enough
682 * of warm entries in syn queue, drop request. It is better than
683 * clogging syn queue with openreqs with exponentially increasing
684 * timeout.
685 */
686 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
687 goto drop;
688
689 req = reqsk_alloc(sk->sk_prot->rsk_prot);
690 if (req == NULL)
691 goto drop;
692
693 /* FIXME: process options */
694
695 dccp_openreq_init(req, &dp, skb);
696
697 ireq = inet_rsk(req);
698 ireq->loc_addr = daddr;
699 ireq->rmt_addr = saddr;
7690af3f
ACM
700 req->rcv_wnd = 100; /* Fake, option parsing will get the
701 right value */
7c657876
ACM
702 ireq->opt = NULL;
703
704 /*
705 * Step 3: Process LISTEN state
706 *
707 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
708 *
709 * In fact we defer setting S.GSR, S.SWL, S.SWH to
710 * dccp_create_openreq_child.
711 */
712 dreq = dccp_rsk(req);
67e6b629
ACM
713 dreq->dreq_isr = dcb->dccpd_seq;
714 dreq->dreq_iss = dccp_v4_init_sequence(sk, skb);
715 dreq->dreq_service = service;
7c657876 716
f21e68ca 717 if (dccp_v4_send_response(sk, req, NULL))
7c657876
ACM
718 goto drop_and_free;
719
720 inet_csk_reqsk_queue_hash_add(sk, req, DCCP_TIMEOUT_INIT);
721 return 0;
722
723drop_and_free:
fc44b980 724 reqsk_free(req);
7c657876
ACM
725drop:
726 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS);
0c10c5d9 727 dcb->dccpd_reset_code = reset_code;
7c657876
ACM
728 return -1;
729}
730
f21e68ca
ACM
731EXPORT_SYMBOL_GPL(dccp_v4_conn_request);
732
7c657876
ACM
733/*
734 * The three way handshake has completed - we got a valid ACK or DATAACK -
735 * now create the new socket.
736 *
737 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
738 */
739struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
740 struct request_sock *req,
741 struct dst_entry *dst)
742{
743 struct inet_request_sock *ireq;
744 struct inet_sock *newinet;
745 struct dccp_sock *newdp;
746 struct sock *newsk;
747
748 if (sk_acceptq_is_full(sk))
749 goto exit_overflow;
750
751 if (dst == NULL && (dst = inet_csk_route_req(sk, req)) == NULL)
752 goto exit;
753
754 newsk = dccp_create_openreq_child(sk, req, skb);
755 if (newsk == NULL)
756 goto exit;
757
758 sk_setup_caps(newsk, dst);
759
760 newdp = dccp_sk(newsk);
761 newinet = inet_sk(newsk);
762 ireq = inet_rsk(req);
763 newinet->daddr = ireq->rmt_addr;
764 newinet->rcv_saddr = ireq->loc_addr;
765 newinet->saddr = ireq->loc_addr;
766 newinet->opt = ireq->opt;
767 ireq->opt = NULL;
768 newinet->mc_index = inet_iif(skb);
769 newinet->mc_ttl = skb->nh.iph->ttl;
770 newinet->id = jiffies;
771
772 dccp_sync_mss(newsk, dst_mtu(dst));
773
774 __inet_hash(&dccp_hashinfo, newsk, 0);
775 __inet_inherit_port(&dccp_hashinfo, sk, newsk);
776
777 return newsk;
778
779exit_overflow:
780 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS);
781exit:
782 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS);
783 dst_release(dst);
784 return NULL;
785}
786
f21e68ca
ACM
787EXPORT_SYMBOL_GPL(dccp_v4_request_recv_sock);
788
7c657876
ACM
789static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
790{
791 const struct dccp_hdr *dh = dccp_hdr(skb);
792 const struct iphdr *iph = skb->nh.iph;
793 struct sock *nsk;
794 struct request_sock **prev;
795 /* Find possible connection requests. */
796 struct request_sock *req = inet_csk_search_req(sk, &prev,
797 dh->dccph_sport,
798 iph->saddr, iph->daddr);
799 if (req != NULL)
800 return dccp_check_req(sk, skb, req, prev);
801
802 nsk = __inet_lookup_established(&dccp_hashinfo,
803 iph->saddr, dh->dccph_sport,
804 iph->daddr, ntohs(dh->dccph_dport),
805 inet_iif(skb));
806 if (nsk != NULL) {
807 if (nsk->sk_state != DCCP_TIME_WAIT) {
808 bh_lock_sock(nsk);
809 return nsk;
810 }
811 inet_twsk_put((struct inet_timewait_sock *)nsk);
812 return NULL;
813 }
814
815 return sk;
816}
817
7690af3f
ACM
818int dccp_v4_checksum(const struct sk_buff *skb, const u32 saddr,
819 const u32 daddr)
7c657876 820{
95b81ef7 821 const struct dccp_hdr* dh = dccp_hdr(skb);
7c657876
ACM
822 int checksum_len;
823 u32 tmp;
824
825 if (dh->dccph_cscov == 0)
826 checksum_len = skb->len;
827 else {
828 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
7690af3f
ACM
829 checksum_len = checksum_len < skb->len ? checksum_len :
830 skb->len;
7c657876
ACM
831 }
832
833 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
7690af3f
ACM
834 return csum_tcpudp_magic(saddr, daddr, checksum_len,
835 IPPROTO_DCCP, tmp);
7c657876
ACM
836}
837
95b81ef7
YN
838static int dccp_v4_verify_checksum(struct sk_buff *skb,
839 const u32 saddr, const u32 daddr)
7c657876 840{
95b81ef7
YN
841 struct dccp_hdr *dh = dccp_hdr(skb);
842 int checksum_len;
843 u32 tmp;
7c657876 844
95b81ef7
YN
845 if (dh->dccph_cscov == 0)
846 checksum_len = skb->len;
847 else {
848 checksum_len = (dh->dccph_cscov + dh->dccph_x) * sizeof(u32);
7690af3f
ACM
849 checksum_len = checksum_len < skb->len ? checksum_len :
850 skb->len;
95b81ef7
YN
851 }
852 tmp = csum_partial((unsigned char *)dh, checksum_len, 0);
7690af3f
ACM
853 return csum_tcpudp_magic(saddr, daddr, checksum_len,
854 IPPROTO_DCCP, tmp) == 0 ? 0 : -1;
7c657876
ACM
855}
856
857static struct dst_entry* dccp_v4_route_skb(struct sock *sk,
858 struct sk_buff *skb)
859{
860 struct rtable *rt;
861 struct flowi fl = { .oif = ((struct rtable *)skb->dst)->rt_iif,
862 .nl_u = { .ip4_u =
863 { .daddr = skb->nh.iph->saddr,
864 .saddr = skb->nh.iph->daddr,
865 .tos = RT_CONN_FLAGS(sk) } },
866 .proto = sk->sk_protocol,
867 .uli_u = { .ports =
868 { .sport = dccp_hdr(skb)->dccph_dport,
7690af3f
ACM
869 .dport = dccp_hdr(skb)->dccph_sport }
870 }
871 };
7c657876
ACM
872
873 if (ip_route_output_flow(&rt, &fl, sk, 0)) {
874 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES);
875 return NULL;
876 }
877
878 return &rt->u.dst;
879}
880
a1d3a355 881static void dccp_v4_ctl_send_reset(struct sk_buff *rxskb)
7c657876
ACM
882{
883 int err;
884 struct dccp_hdr *rxdh = dccp_hdr(rxskb), *dh;
885 const int dccp_hdr_reset_len = sizeof(struct dccp_hdr) +
886 sizeof(struct dccp_hdr_ext) +
887 sizeof(struct dccp_hdr_reset);
888 struct sk_buff *skb;
889 struct dst_entry *dst;
2807d4ff 890 u64 seqno;
7c657876
ACM
891
892 /* Never send a reset in response to a reset. */
893 if (rxdh->dccph_type == DCCP_PKT_RESET)
894 return;
895
896 if (((struct rtable *)rxskb->dst)->rt_type != RTN_LOCAL)
897 return;
898
899 dst = dccp_v4_route_skb(dccp_ctl_socket->sk, rxskb);
900 if (dst == NULL)
901 return;
902
903 skb = alloc_skb(MAX_DCCP_HEADER + 15, GFP_ATOMIC);
904 if (skb == NULL)
905 goto out;
906
907 /* Reserve space for headers. */
908 skb_reserve(skb, MAX_DCCP_HEADER);
909 skb->dst = dst_clone(dst);
910
911 skb->h.raw = skb_push(skb, dccp_hdr_reset_len);
912 dh = dccp_hdr(skb);
913 memset(dh, 0, dccp_hdr_reset_len);
914
915 /* Build DCCP header and checksum it. */
916 dh->dccph_type = DCCP_PKT_RESET;
917 dh->dccph_sport = rxdh->dccph_dport;
918 dh->dccph_dport = rxdh->dccph_sport;
919 dh->dccph_doff = dccp_hdr_reset_len / 4;
920 dh->dccph_x = 1;
7690af3f
ACM
921 dccp_hdr_reset(skb)->dccph_reset_code =
922 DCCP_SKB_CB(rxskb)->dccpd_reset_code;
7c657876 923
2807d4ff
ACM
924 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
925 seqno = 0;
926 if (DCCP_SKB_CB(rxskb)->dccpd_ack_seq != DCCP_PKT_WITHOUT_ACK_SEQ)
927 dccp_set_seqno(&seqno, DCCP_SKB_CB(rxskb)->dccpd_ack_seq + 1);
928
929 dccp_hdr_set_seq(dh, seqno);
7690af3f
ACM
930 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb),
931 DCCP_SKB_CB(rxskb)->dccpd_seq);
7c657876 932
95b81ef7
YN
933 dh->dccph_checksum = dccp_v4_checksum(skb, rxskb->nh.iph->saddr,
934 rxskb->nh.iph->daddr);
7c657876
ACM
935
936 bh_lock_sock(dccp_ctl_socket->sk);
937 err = ip_build_and_send_pkt(skb, dccp_ctl_socket->sk,
7690af3f
ACM
938 rxskb->nh.iph->daddr,
939 rxskb->nh.iph->saddr, NULL);
7c657876
ACM
940 bh_unlock_sock(dccp_ctl_socket->sk);
941
942 if (err == NET_XMIT_CN || err == 0) {
943 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS);
944 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS);
945 }
946out:
947 dst_release(dst);
948}
949
950int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
951{
952 struct dccp_hdr *dh = dccp_hdr(skb);
953
954 if (sk->sk_state == DCCP_OPEN) { /* Fast path */
955 if (dccp_rcv_established(sk, skb, dh, skb->len))
956 goto reset;
957 return 0;
958 }
959
960 /*
961 * Step 3: Process LISTEN state
962 * If S.state == LISTEN,
7690af3f
ACM
963 * If P.type == Request or P contains a valid Init Cookie
964 * option,
7c657876
ACM
965 * * Must scan the packet's options to check for an Init
966 * Cookie. Only the Init Cookie is processed here,
967 * however; other options are processed in Step 8. This
968 * scan need only be performed if the endpoint uses Init
969 * Cookies *
970 * * Generate a new socket and switch to that socket *
971 * Set S := new socket for this port pair
972 * S.state = RESPOND
973 * Choose S.ISS (initial seqno) or set from Init Cookie
974 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
975 * Continue with S.state == RESPOND
976 * * A Response packet will be generated in Step 11 *
977 * Otherwise,
978 * Generate Reset(No Connection) unless P.type == Reset
979 * Drop packet and return
980 *
7690af3f
ACM
981 * NOTE: the check for the packet types is done in
982 * dccp_rcv_state_process
7c657876
ACM
983 */
984 if (sk->sk_state == DCCP_LISTEN) {
985 struct sock *nsk = dccp_v4_hnd_req(sk, skb);
986
987 if (nsk == NULL)
988 goto discard;
989
990 if (nsk != sk) {
991 if (dccp_child_process(sk, nsk, skb))
992 goto reset;
993 return 0;
994 }
995 }
996
997 if (dccp_rcv_state_process(sk, skb, dh, skb->len))
998 goto reset;
999 return 0;
1000
1001reset:
7c657876
ACM
1002 dccp_v4_ctl_send_reset(skb);
1003discard:
1004 kfree_skb(skb);
1005 return 0;
1006}
1007
f21e68ca
ACM
1008EXPORT_SYMBOL_GPL(dccp_v4_do_rcv);
1009
1010int dccp_invalid_packet(struct sk_buff *skb)
7c657876
ACM
1011{
1012 const struct dccp_hdr *dh;
1013
1014 if (skb->pkt_type != PACKET_HOST)
1015 return 1;
1016
1017 if (!pskb_may_pull(skb, sizeof(struct dccp_hdr))) {
c59eab46 1018 LIMIT_NETDEBUG(KERN_WARNING "DCCP: pskb_may_pull failed\n");
7c657876
ACM
1019 return 1;
1020 }
1021
1022 dh = dccp_hdr(skb);
1023
1024 /* If the packet type is not understood, drop packet and return */
1025 if (dh->dccph_type >= DCCP_PKT_INVALID) {
c59eab46 1026 LIMIT_NETDEBUG(KERN_WARNING "DCCP: invalid packet type\n");
7c657876
ACM
1027 return 1;
1028 }
1029
1030 /*
1031 * If P.Data Offset is too small for packet type, or too large for
1032 * packet, drop packet and return
1033 */
1034 if (dh->dccph_doff < dccp_hdr_len(skb) / sizeof(u32)) {
c59eab46
ACM
1035 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1036 "too small 1\n",
1037 dh->dccph_doff);
7c657876
ACM
1038 return 1;
1039 }
1040
1041 if (!pskb_may_pull(skb, dh->dccph_doff * sizeof(u32))) {
c59eab46
ACM
1042 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.Data Offset(%u) "
1043 "too small 2\n",
1044 dh->dccph_doff);
7c657876
ACM
1045 return 1;
1046 }
1047
1048 dh = dccp_hdr(skb);
1049
1050 /*
1051 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
1052 * has short sequence numbers), drop packet and return
1053 */
1054 if (dh->dccph_x == 0 &&
1055 dh->dccph_type != DCCP_PKT_DATA &&
1056 dh->dccph_type != DCCP_PKT_ACK &&
1057 dh->dccph_type != DCCP_PKT_DATAACK) {
c59eab46
ACM
1058 LIMIT_NETDEBUG(KERN_WARNING "DCCP: P.type (%s) not Data, Ack "
1059 "nor DataAck and P.X == 0\n",
1060 dccp_packet_name(dh->dccph_type));
7c657876
ACM
1061 return 1;
1062 }
1063
7c657876
ACM
1064 return 0;
1065}
1066
f21e68ca
ACM
1067EXPORT_SYMBOL_GPL(dccp_invalid_packet);
1068
7c657876
ACM
1069/* this is called when real data arrives */
1070int dccp_v4_rcv(struct sk_buff *skb)
1071{
1072 const struct dccp_hdr *dh;
1073 struct sock *sk;
1074 int rc;
1075
1076 /* Step 1: Check header basics: */
1077
1078 if (dccp_invalid_packet(skb))
1079 goto discard_it;
1080
f21e68ca
ACM
1081 /* If the header checksum is incorrect, drop packet and return */
1082 if (dccp_v4_verify_checksum(skb, skb->nh.iph->saddr,
1083 skb->nh.iph->daddr) < 0) {
1084 LIMIT_NETDEBUG(KERN_WARNING "%s: incorrect header checksum\n",
1085 __FUNCTION__);
1086 goto discard_it;
1087 }
1088
7c657876 1089 dh = dccp_hdr(skb);
7c657876 1090
7c657876
ACM
1091 DCCP_SKB_CB(skb)->dccpd_seq = dccp_hdr_seq(skb);
1092 DCCP_SKB_CB(skb)->dccpd_type = dh->dccph_type;
1093
1094 dccp_pr_debug("%8.8s "
1095 "src=%u.%u.%u.%u@%-5d "
1096 "dst=%u.%u.%u.%u@%-5d seq=%llu",
1097 dccp_packet_name(dh->dccph_type),
1098 NIPQUAD(skb->nh.iph->saddr), ntohs(dh->dccph_sport),
1099 NIPQUAD(skb->nh.iph->daddr), ntohs(dh->dccph_dport),
f6ccf554 1100 (unsigned long long) DCCP_SKB_CB(skb)->dccpd_seq);
7c657876
ACM
1101
1102 if (dccp_packet_without_ack(skb)) {
1103 DCCP_SKB_CB(skb)->dccpd_ack_seq = DCCP_PKT_WITHOUT_ACK_SEQ;
1104 dccp_pr_debug_cat("\n");
1105 } else {
1106 DCCP_SKB_CB(skb)->dccpd_ack_seq = dccp_hdr_ack_seq(skb);
f6ccf554
DM
1107 dccp_pr_debug_cat(", ack=%llu\n",
1108 (unsigned long long)
1109 DCCP_SKB_CB(skb)->dccpd_ack_seq);
7c657876
ACM
1110 }
1111
1112 /* Step 2:
1113 * Look up flow ID in table and get corresponding socket */
1114 sk = __inet_lookup(&dccp_hashinfo,
1115 skb->nh.iph->saddr, dh->dccph_sport,
1116 skb->nh.iph->daddr, ntohs(dh->dccph_dport),
1117 inet_iif(skb));
1118
1119 /*
1120 * Step 2:
1121 * If no socket ...
1122 * Generate Reset(No Connection) unless P.type == Reset
1123 * Drop packet and return
1124 */
1125 if (sk == NULL) {
1126 dccp_pr_debug("failed to look up flow ID in table and "
1127 "get corresponding socket\n");
1128 goto no_dccp_socket;
1129 }
1130
1131 /*
1132 * Step 2:
1133 * ... or S.state == TIMEWAIT,
1134 * Generate Reset(No Connection) unless P.type == Reset
1135 * Drop packet and return
1136 */
1137
1138 if (sk->sk_state == DCCP_TIME_WAIT) {
64cf1e5d
ACM
1139 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
1140 "do_time_wait\n");
1141 goto do_time_wait;
7c657876
ACM
1142 }
1143
1144 if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
1145 dccp_pr_debug("xfrm4_policy_check failed\n");
1146 goto discard_and_relse;
1147 }
1148
1149 if (sk_filter(sk, skb, 0)) {
1150 dccp_pr_debug("sk_filter failed\n");
1151 goto discard_and_relse;
1152 }
1153
1154 skb->dev = NULL;
1155
1156 bh_lock_sock(sk);
1157 rc = 0;
1158 if (!sock_owned_by_user(sk))
1159 rc = dccp_v4_do_rcv(sk, skb);
1160 else
1161 sk_add_backlog(sk, skb);
1162 bh_unlock_sock(sk);
1163
1164 sock_put(sk);
1165 return rc;
1166
1167no_dccp_socket:
1168 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
1169 goto discard_it;
1170 /*
1171 * Step 2:
1172 * Generate Reset(No Connection) unless P.type == Reset
1173 * Drop packet and return
1174 */
1175 if (dh->dccph_type != DCCP_PKT_RESET) {
7690af3f
ACM
1176 DCCP_SKB_CB(skb)->dccpd_reset_code =
1177 DCCP_RESET_CODE_NO_CONNECTION;
7c657876
ACM
1178 dccp_v4_ctl_send_reset(skb);
1179 }
1180
1181discard_it:
1182 /* Discard frame. */
1183 kfree_skb(skb);
1184 return 0;
1185
1186discard_and_relse:
1187 sock_put(sk);
1188 goto discard_it;
64cf1e5d
ACM
1189
1190do_time_wait:
1191 inet_twsk_put((struct inet_timewait_sock *)sk);
1192 goto no_dccp_socket;
7c657876
ACM
1193}
1194
57cca05a
ACM
1195struct inet_connection_sock_af_ops dccp_ipv4_af_ops = {
1196 .queue_xmit = ip_queue_xmit,
1197 .send_check = dccp_v4_send_check,
1198 .rebuild_header = inet_sk_rebuild_header,
1199 .conn_request = dccp_v4_conn_request,
1200 .syn_recv_sock = dccp_v4_request_recv_sock,
1201 .net_header_len = sizeof(struct iphdr),
1202 .setsockopt = ip_setsockopt,
1203 .getsockopt = ip_getsockopt,
1204 .addr2sockaddr = inet_csk_addr2sockaddr,
1205 .sockaddr_len = sizeof(struct sockaddr_in),
1206};
1207
f21e68ca 1208int dccp_v4_init_sock(struct sock *sk)
7c657876
ACM
1209{
1210 struct dccp_sock *dp = dccp_sk(sk);
1211 static int dccp_ctl_socket_init = 1;
1212
1213 dccp_options_init(&dp->dccps_options);
b0e56780 1214 do_gettimeofday(&dp->dccps_epoch);
7c657876
ACM
1215
1216 if (dp->dccps_options.dccpo_send_ack_vector) {
ae31c339
ACM
1217 dp->dccps_hc_rx_ackvec = dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN,
1218 GFP_KERNEL);
1219 if (dp->dccps_hc_rx_ackvec == NULL)
7c657876
ACM
1220 return -ENOMEM;
1221 }
1222
1223 /*
1224 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1225 * the listening (master) sock get CCID control blocks, which is not
1226 * necessary, but for now, to not mess with the test userspace apps,
1227 * lets leave it here, later the real solution is to do this in a
1228 * setsockopt(CCIDs-I-want/accept). -acme
1229 */
1230 if (likely(!dccp_ctl_socket_init)) {
561713cf 1231 dp->dccps_hc_rx_ccid = ccid_init(dp->dccps_options.dccpo_rx_ccid,
7690af3f 1232 sk);
561713cf 1233 dp->dccps_hc_tx_ccid = ccid_init(dp->dccps_options.dccpo_tx_ccid,
7690af3f 1234 sk);
7c657876
ACM
1235 if (dp->dccps_hc_rx_ccid == NULL ||
1236 dp->dccps_hc_tx_ccid == NULL) {
1237 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1238 ccid_exit(dp->dccps_hc_tx_ccid, sk);
ae31c339
ACM
1239 if (dp->dccps_options.dccpo_send_ack_vector) {
1240 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1241 dp->dccps_hc_rx_ackvec = NULL;
1242 }
7c657876
ACM
1243 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1244 return -ENOMEM;
1245 }
1246 } else
1247 dccp_ctl_socket_init = 0;
1248
1249 dccp_init_xmit_timers(sk);
0b4e03bf 1250 inet_csk(sk)->icsk_rto = DCCP_TIMEOUT_INIT;
7c657876 1251 sk->sk_state = DCCP_CLOSED;
c530cfb1 1252 sk->sk_write_space = dccp_write_space;
57cca05a 1253 inet_csk(sk)->icsk_af_ops = &dccp_ipv4_af_ops;
7c657876
ACM
1254 dp->dccps_mss_cache = 536;
1255 dp->dccps_role = DCCP_ROLE_UNDEFINED;
67e6b629 1256 dp->dccps_service = DCCP_SERVICE_INVALID_VALUE;
7c657876
ACM
1257
1258 return 0;
1259}
1260
f21e68ca
ACM
1261EXPORT_SYMBOL_GPL(dccp_v4_init_sock);
1262
1263int dccp_v4_destroy_sock(struct sock *sk)
7c657876
ACM
1264{
1265 struct dccp_sock *dp = dccp_sk(sk);
1266
1267 /*
4c7e6895 1268 * DCCP doesn't use sk_write_queue, just sk_send_head
7c657876
ACM
1269 * for retransmissions
1270 */
1271 if (sk->sk_send_head != NULL) {
1272 kfree_skb(sk->sk_send_head);
1273 sk->sk_send_head = NULL;
1274 }
1275
1276 /* Clean up a referenced DCCP bind bucket. */
1277 if (inet_csk(sk)->icsk_bind_hash != NULL)
1278 inet_put_port(&dccp_hashinfo, sk);
1279
a51482bd
JJ
1280 kfree(dp->dccps_service_list);
1281 dp->dccps_service_list = NULL;
67e6b629 1282
8efa544f
ACM
1283 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
1284 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
ae31c339
ACM
1285 if (dp->dccps_options.dccpo_send_ack_vector) {
1286 dccp_ackvec_free(dp->dccps_hc_rx_ackvec);
1287 dp->dccps_hc_rx_ackvec = NULL;
1288 }
7c657876
ACM
1289 ccid_exit(dp->dccps_hc_rx_ccid, sk);
1290 ccid_exit(dp->dccps_hc_tx_ccid, sk);
1291 dp->dccps_hc_rx_ccid = dp->dccps_hc_tx_ccid = NULL;
1292
1293 return 0;
1294}
1295
f21e68ca
ACM
1296EXPORT_SYMBOL_GPL(dccp_v4_destroy_sock);
1297
7c657876
ACM
1298static void dccp_v4_reqsk_destructor(struct request_sock *req)
1299{
1300 kfree(inet_rsk(req)->opt);
1301}
1302
1303static struct request_sock_ops dccp_request_sock_ops = {
1304 .family = PF_INET,
1305 .obj_size = sizeof(struct dccp_request_sock),
1306 .rtx_syn_ack = dccp_v4_send_response,
1307 .send_ack = dccp_v4_reqsk_send_ack,
1308 .destructor = dccp_v4_reqsk_destructor,
1309 .send_reset = dccp_v4_ctl_send_reset,
1310};
1311
34ca6860 1312struct proto dccp_prot = {
7c657876
ACM
1313 .name = "DCCP",
1314 .owner = THIS_MODULE,
1315 .close = dccp_close,
1316 .connect = dccp_v4_connect,
1317 .disconnect = dccp_disconnect,
1318 .ioctl = dccp_ioctl,
1319 .init = dccp_v4_init_sock,
1320 .setsockopt = dccp_setsockopt,
1321 .getsockopt = dccp_getsockopt,
1322 .sendmsg = dccp_sendmsg,
1323 .recvmsg = dccp_recvmsg,
1324 .backlog_rcv = dccp_v4_do_rcv,
1325 .hash = dccp_v4_hash,
f21e68ca 1326 .unhash = dccp_unhash,
7c657876
ACM
1327 .accept = inet_csk_accept,
1328 .get_port = dccp_v4_get_port,
1329 .shutdown = dccp_shutdown,
1330 .destroy = dccp_v4_destroy_sock,
1331 .orphan_count = &dccp_orphan_count,
1332 .max_header = MAX_DCCP_HEADER,
1333 .obj_size = sizeof(struct dccp_sock),
1334 .rsk_prot = &dccp_request_sock_ops,
64cf1e5d 1335 .twsk_obj_size = sizeof(struct inet_timewait_sock),
7c657876 1336};