4 * An implementation of the DCCP protocol
5 * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
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.
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>
21 #include <net/inet_hashtables.h>
23 #include <net/tcp_states.h>
30 struct inet_hashinfo __cacheline_aligned dccp_hashinfo
= {
31 .lhash_lock
= RW_LOCK_UNLOCKED
,
32 .lhash_users
= ATOMIC_INIT(0),
33 .lhash_wait
= __WAIT_QUEUE_HEAD_INITIALIZER(dccp_hashinfo
.lhash_wait
),
34 .portalloc_lock
= SPIN_LOCK_UNLOCKED
,
35 .port_rover
= 1024 - 1,
38 EXPORT_SYMBOL_GPL(dccp_hashinfo
);
40 static int dccp_v4_get_port(struct sock
*sk
, const unsigned short snum
)
42 return inet_csk_get_port(&dccp_hashinfo
, sk
, snum
);
45 static void dccp_v4_hash(struct sock
*sk
)
47 inet_hash(&dccp_hashinfo
, sk
);
50 static void dccp_v4_unhash(struct sock
*sk
)
52 inet_unhash(&dccp_hashinfo
, sk
);
55 /* called with local bh disabled */
56 static int __dccp_v4_check_established(struct sock
*sk
, const __u16 lport
,
57 struct inet_timewait_sock
**twp
)
59 struct inet_sock
*inet
= inet_sk(sk
);
60 const u32 daddr
= inet
->rcv_saddr
;
61 const u32 saddr
= inet
->daddr
;
62 const int dif
= sk
->sk_bound_dev_if
;
63 INET_ADDR_COOKIE(acookie
, saddr
, daddr
)
64 const __u32 ports
= INET_COMBINED_PORTS(inet
->dport
, lport
);
65 unsigned int hash
= inet_ehashfn(daddr
, lport
, saddr
, inet
->dport
);
66 struct inet_ehash_bucket
*head
= inet_ehash_bucket(&dccp_hashinfo
, hash
);
67 const struct sock
*sk2
;
68 const struct hlist_node
*node
;
69 struct inet_timewait_sock
*tw
;
71 prefetch(head
->chain
.first
);
72 write_lock(&head
->lock
);
74 /* Check TIME-WAIT sockets first. */
75 sk_for_each(sk2
, node
, &(head
+ dccp_hashinfo
.ehash_size
)->chain
) {
78 if (INET_TW_MATCH(sk2
, hash
, acookie
, saddr
, daddr
, ports
, dif
))
83 /* And established part... */
84 sk_for_each(sk2
, node
, &head
->chain
) {
85 if (INET_MATCH(sk2
, hash
, acookie
, saddr
, daddr
, ports
, dif
))
89 /* Must record num and sport now. Otherwise we will see
90 * in hash table socket with a funny identity. */
92 inet
->sport
= htons(lport
);
94 BUG_TRAP(sk_unhashed(sk
));
95 __sk_add_node(sk
, &head
->chain
);
96 sock_prot_inc_use(sk
->sk_prot
);
97 write_unlock(&head
->lock
);
101 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED
);
102 } else if (tw
!= NULL
) {
103 /* Silly. Should hash-dance instead... */
104 inet_twsk_deschedule(tw
, &dccp_death_row
);
105 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED
);
113 write_unlock(&head
->lock
);
114 return -EADDRNOTAVAIL
;
118 * Bind a port for a connect operation and hash it.
120 static int dccp_v4_hash_connect(struct sock
*sk
)
122 const unsigned short snum
= inet_sk(sk
)->num
;
123 struct inet_bind_hashbucket
*head
;
124 struct inet_bind_bucket
*tb
;
129 int low
= sysctl_local_port_range
[0];
130 int high
= sysctl_local_port_range
[1];
131 int remaining
= (high
- low
) + 1;
132 struct hlist_node
*node
;
133 struct inet_timewait_sock
*tw
= NULL
;
137 /* TODO. Actually it is not so bad idea to remove
138 * dccp_hashinfo.portalloc_lock before next submission to
140 * As soon as we touch this place at all it is time to think.
142 * Now it protects single _advisory_ variable
143 * dccp_hashinfo.port_rover, hence it is mostly useless.
144 * Code will work nicely if we just delete it, but
145 * I am afraid in contented case it will work not better or
146 * even worse: another cpu just will hit the same bucket
148 * So some cpu salt could remove both contention and
149 * memory pingpong. Any ideas how to do this in a nice way?
151 spin_lock(&dccp_hashinfo
.portalloc_lock
);
152 rover
= dccp_hashinfo
.port_rover
;
156 if ((rover
< low
) || (rover
> high
))
158 head
= &dccp_hashinfo
.bhash
[inet_bhashfn(rover
,
159 dccp_hashinfo
.bhash_size
)];
160 spin_lock(&head
->lock
);
162 /* Does not bother with rcv_saddr checks,
163 * because the established check is already
166 inet_bind_bucket_for_each(tb
, node
, &head
->chain
) {
167 if (tb
->port
== rover
) {
168 BUG_TRAP(!hlist_empty(&tb
->owners
));
169 if (tb
->fastreuse
>= 0)
171 if (!__dccp_v4_check_established(sk
,
179 tb
= inet_bind_bucket_create(dccp_hashinfo
.bind_bucket_cachep
,
182 spin_unlock(&head
->lock
);
189 spin_unlock(&head
->lock
);
190 } while (--remaining
> 0);
191 dccp_hashinfo
.port_rover
= rover
;
192 spin_unlock(&dccp_hashinfo
.portalloc_lock
);
196 return -EADDRNOTAVAIL
;
199 /* All locks still held and bhs disabled */
200 dccp_hashinfo
.port_rover
= rover
;
201 spin_unlock(&dccp_hashinfo
.portalloc_lock
);
203 inet_bind_hash(sk
, tb
, rover
);
204 if (sk_unhashed(sk
)) {
205 inet_sk(sk
)->sport
= htons(rover
);
206 __inet_hash(&dccp_hashinfo
, sk
, 0);
208 spin_unlock(&head
->lock
);
211 inet_twsk_deschedule(tw
, &dccp_death_row
);
219 head
= &dccp_hashinfo
.bhash
[inet_bhashfn(snum
,
220 dccp_hashinfo
.bhash_size
)];
221 tb
= inet_csk(sk
)->icsk_bind_hash
;
222 spin_lock_bh(&head
->lock
);
223 if (sk_head(&tb
->owners
) == sk
&& sk
->sk_bind_node
.next
== NULL
) {
224 __inet_hash(&dccp_hashinfo
, sk
, 0);
225 spin_unlock_bh(&head
->lock
);
228 spin_unlock(&head
->lock
);
229 /* No definite answer... Walk to established hash table */
230 ret
= __dccp_v4_check_established(sk
, snum
, NULL
);
237 static int dccp_v4_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
240 struct inet_sock
*inet
= inet_sk(sk
);
241 struct dccp_sock
*dp
= dccp_sk(sk
);
242 const struct sockaddr_in
*usin
= (struct sockaddr_in
*)uaddr
;
248 dp
->dccps_role
= DCCP_ROLE_CLIENT
;
250 if (dccp_service_not_initialized(sk
))
253 if (addr_len
< sizeof(struct sockaddr_in
))
256 if (usin
->sin_family
!= AF_INET
)
257 return -EAFNOSUPPORT
;
259 nexthop
= daddr
= usin
->sin_addr
.s_addr
;
260 if (inet
->opt
!= NULL
&& inet
->opt
->srr
) {
263 nexthop
= inet
->opt
->faddr
;
266 tmp
= ip_route_connect(&rt
, nexthop
, inet
->saddr
,
267 RT_CONN_FLAGS(sk
), sk
->sk_bound_dev_if
,
269 inet
->sport
, usin
->sin_port
, sk
);
273 if (rt
->rt_flags
& (RTCF_MULTICAST
| RTCF_BROADCAST
)) {
278 if (inet
->opt
== NULL
|| !inet
->opt
->srr
)
281 if (inet
->saddr
== 0)
282 inet
->saddr
= rt
->rt_src
;
283 inet
->rcv_saddr
= inet
->saddr
;
285 inet
->dport
= usin
->sin_port
;
288 dp
->dccps_ext_header_len
= 0;
289 if (inet
->opt
!= NULL
)
290 dp
->dccps_ext_header_len
= inet
->opt
->optlen
;
292 * Socket identity is still unknown (sport may be zero).
293 * However we set state to DCCP_REQUESTING and not releasing socket
294 * lock select source port, enter ourselves into the hash tables and
295 * complete initialization after this.
297 dccp_set_state(sk
, DCCP_REQUESTING
);
298 err
= dccp_v4_hash_connect(sk
);
302 err
= ip_route_newports(&rt
, inet
->sport
, inet
->dport
, sk
);
306 /* OK, now commit destination to socket. */
307 sk_setup_caps(sk
, &rt
->u
.dst
);
310 dp
->dccps_iss
= secure_dccp_sequence_number(inet
->saddr
,
314 dccp_update_gss(sk
, dp
->dccps_iss
);
317 * SWL and AWL are initially adjusted so that they are not less than
318 * the initial Sequence Numbers received and sent, respectively:
319 * SWL := max(GSR + 1 - floor(W/4), ISR),
320 * AWL := max(GSS - W' + 1, ISS).
321 * These adjustments MUST be applied only at the beginning of the
324 dccp_set_seqno(&dp
->dccps_awl
, max48(dp
->dccps_awl
, dp
->dccps_iss
));
326 inet
->id
= dp
->dccps_iss
^ jiffies
;
328 err
= dccp_connect(sk
);
336 * This unhashes the socket and releases the local port, if necessary.
338 dccp_set_state(sk
, DCCP_CLOSED
);
340 sk
->sk_route_caps
= 0;
346 * This routine does path mtu discovery as defined in RFC1191.
348 static inline void dccp_do_pmtu_discovery(struct sock
*sk
,
349 const struct iphdr
*iph
,
352 struct dst_entry
*dst
;
353 const struct inet_sock
*inet
= inet_sk(sk
);
354 const struct dccp_sock
*dp
= dccp_sk(sk
);
356 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
357 * send out by Linux are always < 576bytes so they should go through
360 if (sk
->sk_state
== DCCP_LISTEN
)
363 /* We don't check in the destentry if pmtu discovery is forbidden
364 * on this route. We just assume that no packet_to_big packets
365 * are send back when pmtu discovery is not active.
366 * There is a small race when the user changes this flag in the
367 * route, but I think that's acceptable.
369 if ((dst
= __sk_dst_check(sk
, 0)) == NULL
)
372 dst
->ops
->update_pmtu(dst
, mtu
);
374 /* Something is about to be wrong... Remember soft error
375 * for the case, if this connection will not able to recover.
377 if (mtu
< dst_mtu(dst
) && ip_dont_fragment(sk
, dst
))
378 sk
->sk_err_soft
= EMSGSIZE
;
382 if (inet
->pmtudisc
!= IP_PMTUDISC_DONT
&&
383 dp
->dccps_pmtu_cookie
> mtu
) {
384 dccp_sync_mss(sk
, mtu
);
387 * From: draft-ietf-dccp-spec-11.txt
389 * DCCP-Sync packets are the best choice for upward
390 * probing, since DCCP-Sync probes do not risk application
393 dccp_send_sync(sk
, dp
->dccps_gsr
, DCCP_PKT_SYNC
);
394 } /* else let the usual retransmit timer handle it */
397 static void dccp_v4_ctl_send_ack(struct sk_buff
*rxskb
)
400 struct dccp_hdr
*rxdh
= dccp_hdr(rxskb
), *dh
;
401 const int dccp_hdr_ack_len
= sizeof(struct dccp_hdr
) +
402 sizeof(struct dccp_hdr_ext
) +
403 sizeof(struct dccp_hdr_ack_bits
);
406 if (((struct rtable
*)rxskb
->dst
)->rt_type
!= RTN_LOCAL
)
409 skb
= alloc_skb(MAX_DCCP_HEADER
+ 15, GFP_ATOMIC
);
413 /* Reserve space for headers. */
414 skb_reserve(skb
, MAX_DCCP_HEADER
);
416 skb
->dst
= dst_clone(rxskb
->dst
);
418 skb
->h
.raw
= skb_push(skb
, dccp_hdr_ack_len
);
420 memset(dh
, 0, dccp_hdr_ack_len
);
422 /* Build DCCP header and checksum it. */
423 dh
->dccph_type
= DCCP_PKT_ACK
;
424 dh
->dccph_sport
= rxdh
->dccph_dport
;
425 dh
->dccph_dport
= rxdh
->dccph_sport
;
426 dh
->dccph_doff
= dccp_hdr_ack_len
/ 4;
429 dccp_hdr_set_seq(dh
, DCCP_SKB_CB(rxskb
)->dccpd_ack_seq
);
430 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb
),
431 DCCP_SKB_CB(rxskb
)->dccpd_seq
);
433 bh_lock_sock(dccp_ctl_socket
->sk
);
434 err
= ip_build_and_send_pkt(skb
, dccp_ctl_socket
->sk
,
435 rxskb
->nh
.iph
->daddr
,
436 rxskb
->nh
.iph
->saddr
, NULL
);
437 bh_unlock_sock(dccp_ctl_socket
->sk
);
439 if (err
== NET_XMIT_CN
|| err
== 0) {
440 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS
);
441 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS
);
445 static void dccp_v4_reqsk_send_ack(struct sk_buff
*skb
,
446 struct request_sock
*req
)
448 dccp_v4_ctl_send_ack(skb
);
451 static int dccp_v4_send_response(struct sock
*sk
, struct request_sock
*req
,
452 struct dst_entry
*dst
)
457 /* First, grab a route. */
459 if (dst
== NULL
&& (dst
= inet_csk_route_req(sk
, req
)) == NULL
)
462 skb
= dccp_make_response(sk
, dst
, req
);
464 const struct inet_request_sock
*ireq
= inet_rsk(req
);
466 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
467 err
= ip_build_and_send_pkt(skb
, sk
, ireq
->loc_addr
,
470 if (err
== NET_XMIT_CN
)
480 * This routine is called by the ICMP module when it gets some sort of error
481 * condition. If err < 0 then the socket should be closed and the error
482 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
483 * After adjustment header points to the first 8 bytes of the tcp header. We
484 * need to find the appropriate port.
486 * The locking strategy used here is very "optimistic". When someone else
487 * accesses the socket the ICMP is just dropped and for some paths there is no
488 * check at all. A more general error queue to queue errors for later handling
489 * is probably better.
491 void dccp_v4_err(struct sk_buff
*skb
, u32 info
)
493 const struct iphdr
*iph
= (struct iphdr
*)skb
->data
;
494 const struct dccp_hdr
*dh
= (struct dccp_hdr
*)(skb
->data
+
496 struct dccp_sock
*dp
;
497 struct inet_sock
*inet
;
498 const int type
= skb
->h
.icmph
->type
;
499 const int code
= skb
->h
.icmph
->code
;
504 if (skb
->len
< (iph
->ihl
<< 2) + 8) {
505 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS
);
509 sk
= inet_lookup(&dccp_hashinfo
, iph
->daddr
, dh
->dccph_dport
,
510 iph
->saddr
, dh
->dccph_sport
, inet_iif(skb
));
512 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS
);
516 if (sk
->sk_state
== DCCP_TIME_WAIT
) {
517 inet_twsk_put((struct inet_timewait_sock
*)sk
);
522 /* If too many ICMPs get dropped on busy
523 * servers this needs to be solved differently.
525 if (sock_owned_by_user(sk
))
526 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS
);
528 if (sk
->sk_state
== DCCP_CLOSED
)
532 seq
= dccp_hdr_seq(skb
);
533 if (sk
->sk_state
!= DCCP_LISTEN
&&
534 !between48(seq
, dp
->dccps_swl
, dp
->dccps_swh
)) {
535 NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS
);
540 case ICMP_SOURCE_QUENCH
:
541 /* Just silently ignore these. */
543 case ICMP_PARAMETERPROB
:
546 case ICMP_DEST_UNREACH
:
547 if (code
> NR_ICMP_UNREACH
)
550 if (code
== ICMP_FRAG_NEEDED
) { /* PMTU discovery (RFC1191) */
551 if (!sock_owned_by_user(sk
))
552 dccp_do_pmtu_discovery(sk
, iph
, info
);
556 err
= icmp_err_convert
[code
].errno
;
558 case ICMP_TIME_EXCEEDED
:
565 switch (sk
->sk_state
) {
566 struct request_sock
*req
, **prev
;
568 if (sock_owned_by_user(sk
))
570 req
= inet_csk_search_req(sk
, &prev
, dh
->dccph_dport
,
571 iph
->daddr
, iph
->saddr
);
576 * ICMPs are not backlogged, hence we cannot get an established
581 if (seq
!= dccp_rsk(req
)->dreq_iss
) {
582 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS
);
586 * Still in RESPOND, just remove it silently.
587 * There is no good way to pass the error to the newly
588 * created socket, and POSIX does not want network
589 * errors returned from accept().
591 inet_csk_reqsk_queue_drop(sk
, req
, prev
);
594 case DCCP_REQUESTING
:
596 if (!sock_owned_by_user(sk
)) {
597 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS
);
600 sk
->sk_error_report(sk
);
604 sk
->sk_err_soft
= err
;
608 /* If we've already connected we will keep trying
609 * until we time out, or the user gives up.
611 * rfc1122 4.2.3.9 allows to consider as hard errors
612 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
613 * but it is obsoleted by pmtu discovery).
615 * Note, that in modern internet, where routing is unreliable
616 * and in each dark corner broken firewalls sit, sending random
617 * errors ordered by their masters even this two messages finally lose
618 * their original sense (even Linux sends invalid PORT_UNREACHs)
620 * Now we are in compliance with RFCs.
625 if (!sock_owned_by_user(sk
) && inet
->recverr
) {
627 sk
->sk_error_report(sk
);
628 } else /* Only an error on timeout */
629 sk
->sk_err_soft
= err
;
635 int dccp_v4_send_reset(struct sock
*sk
, enum dccp_reset_codes code
)
639 * FIXME: what if rebuild_header fails?
640 * Should we be doing a rebuild_header here?
642 int err
= inet_sk_rebuild_header(sk
);
647 skb
= dccp_make_reset(sk
, sk
->sk_dst_cache
, code
);
649 const struct inet_sock
*inet
= inet_sk(sk
);
651 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
652 err
= ip_build_and_send_pkt(skb
, sk
,
653 inet
->saddr
, inet
->daddr
, NULL
);
654 if (err
== NET_XMIT_CN
)
661 static inline u64
dccp_v4_init_sequence(const struct sock
*sk
,
662 const struct sk_buff
*skb
)
664 return secure_dccp_sequence_number(skb
->nh
.iph
->daddr
,
666 dccp_hdr(skb
)->dccph_dport
,
667 dccp_hdr(skb
)->dccph_sport
);
670 static inline int dccp_bad_service_code(const struct sock
*sk
,
673 const struct dccp_sock
*dp
= dccp_sk(sk
);
675 if (dp
->dccps_service
== service
)
677 return !dccp_list_has_service(dp
->dccps_service_list
, service
);
680 int dccp_v4_conn_request(struct sock
*sk
, struct sk_buff
*skb
)
682 struct inet_request_sock
*ireq
;
684 struct request_sock
*req
;
685 struct dccp_request_sock
*dreq
;
686 const __u32 saddr
= skb
->nh
.iph
->saddr
;
687 const __u32 daddr
= skb
->nh
.iph
->daddr
;
688 const __u32 service
= dccp_hdr_request(skb
)->dccph_req_service
;
689 struct dccp_skb_cb
*dcb
= DCCP_SKB_CB(skb
);
690 __u8 reset_code
= DCCP_RESET_CODE_TOO_BUSY
;
691 struct dst_entry
*dst
= NULL
;
693 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
694 if (((struct rtable
*)skb
->dst
)->rt_flags
&
695 (RTCF_BROADCAST
| RTCF_MULTICAST
)) {
696 reset_code
= DCCP_RESET_CODE_NO_CONNECTION
;
700 if (dccp_bad_service_code(sk
, service
)) {
701 reset_code
= DCCP_RESET_CODE_BAD_SERVICE_CODE
;
705 * TW buckets are converted to open requests without
706 * limitations, they conserve resources and peer is
707 * evidently real one.
709 if (inet_csk_reqsk_queue_is_full(sk
))
713 * Accept backlog is full. If we have already queued enough
714 * of warm entries in syn queue, drop request. It is better than
715 * clogging syn queue with openreqs with exponentially increasing
718 if (sk_acceptq_is_full(sk
) && inet_csk_reqsk_queue_young(sk
) > 1)
721 req
= reqsk_alloc(sk
->sk_prot
->rsk_prot
);
725 /* FIXME: process options */
727 dccp_openreq_init(req
, &dp
, skb
);
729 ireq
= inet_rsk(req
);
730 ireq
->loc_addr
= daddr
;
731 ireq
->rmt_addr
= saddr
;
732 /* FIXME: Merge Aristeu's option parsing code when ready */
733 req
->rcv_wnd
= 100; /* Fake, option parsing will get the
738 * Step 3: Process LISTEN state
740 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
742 * In fact we defer setting S.GSR, S.SWL, S.SWH to
743 * dccp_create_openreq_child.
745 dreq
= dccp_rsk(req
);
746 dreq
->dreq_isr
= dcb
->dccpd_seq
;
747 dreq
->dreq_iss
= dccp_v4_init_sequence(sk
, skb
);
748 dreq
->dreq_service
= service
;
750 if (dccp_v4_send_response(sk
, req
, dst
))
753 inet_csk_reqsk_queue_hash_add(sk
, req
, DCCP_TIMEOUT_INIT
);
758 * FIXME: should be reqsk_free after implementing req->rsk_ops
762 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS
);
763 dcb
->dccpd_reset_code
= reset_code
;
768 * The three way handshake has completed - we got a valid ACK or DATAACK -
769 * now create the new socket.
771 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
773 struct sock
*dccp_v4_request_recv_sock(struct sock
*sk
, struct sk_buff
*skb
,
774 struct request_sock
*req
,
775 struct dst_entry
*dst
)
777 struct inet_request_sock
*ireq
;
778 struct inet_sock
*newinet
;
779 struct dccp_sock
*newdp
;
782 if (sk_acceptq_is_full(sk
))
785 if (dst
== NULL
&& (dst
= inet_csk_route_req(sk
, req
)) == NULL
)
788 newsk
= dccp_create_openreq_child(sk
, req
, skb
);
792 sk_setup_caps(newsk
, dst
);
794 newdp
= dccp_sk(newsk
);
795 newinet
= inet_sk(newsk
);
796 ireq
= inet_rsk(req
);
797 newinet
->daddr
= ireq
->rmt_addr
;
798 newinet
->rcv_saddr
= ireq
->loc_addr
;
799 newinet
->saddr
= ireq
->loc_addr
;
800 newinet
->opt
= ireq
->opt
;
802 newinet
->mc_index
= inet_iif(skb
);
803 newinet
->mc_ttl
= skb
->nh
.iph
->ttl
;
804 newinet
->id
= jiffies
;
806 dccp_sync_mss(newsk
, dst_mtu(dst
));
808 __inet_hash(&dccp_hashinfo
, newsk
, 0);
809 __inet_inherit_port(&dccp_hashinfo
, sk
, newsk
);
814 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS
);
816 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS
);
821 static struct sock
*dccp_v4_hnd_req(struct sock
*sk
, struct sk_buff
*skb
)
823 const struct dccp_hdr
*dh
= dccp_hdr(skb
);
824 const struct iphdr
*iph
= skb
->nh
.iph
;
826 struct request_sock
**prev
;
827 /* Find possible connection requests. */
828 struct request_sock
*req
= inet_csk_search_req(sk
, &prev
,
830 iph
->saddr
, iph
->daddr
);
832 return dccp_check_req(sk
, skb
, req
, prev
);
834 nsk
= __inet_lookup_established(&dccp_hashinfo
,
835 iph
->saddr
, dh
->dccph_sport
,
836 iph
->daddr
, ntohs(dh
->dccph_dport
),
839 if (nsk
->sk_state
!= DCCP_TIME_WAIT
) {
843 inet_twsk_put((struct inet_timewait_sock
*)nsk
);
850 int dccp_v4_checksum(const struct sk_buff
*skb
, const u32 saddr
,
853 const struct dccp_hdr
* dh
= dccp_hdr(skb
);
857 if (dh
->dccph_cscov
== 0)
858 checksum_len
= skb
->len
;
860 checksum_len
= (dh
->dccph_cscov
+ dh
->dccph_x
) * sizeof(u32
);
861 checksum_len
= checksum_len
< skb
->len
? checksum_len
:
865 tmp
= csum_partial((unsigned char *)dh
, checksum_len
, 0);
866 return csum_tcpudp_magic(saddr
, daddr
, checksum_len
,
870 static int dccp_v4_verify_checksum(struct sk_buff
*skb
,
871 const u32 saddr
, const u32 daddr
)
873 struct dccp_hdr
*dh
= dccp_hdr(skb
);
877 if (dh
->dccph_cscov
== 0)
878 checksum_len
= skb
->len
;
880 checksum_len
= (dh
->dccph_cscov
+ dh
->dccph_x
) * sizeof(u32
);
881 checksum_len
= checksum_len
< skb
->len
? checksum_len
:
884 tmp
= csum_partial((unsigned char *)dh
, checksum_len
, 0);
885 return csum_tcpudp_magic(saddr
, daddr
, checksum_len
,
886 IPPROTO_DCCP
, tmp
) == 0 ? 0 : -1;
889 static struct dst_entry
* dccp_v4_route_skb(struct sock
*sk
,
893 struct flowi fl
= { .oif
= ((struct rtable
*)skb
->dst
)->rt_iif
,
895 { .daddr
= skb
->nh
.iph
->saddr
,
896 .saddr
= skb
->nh
.iph
->daddr
,
897 .tos
= RT_CONN_FLAGS(sk
) } },
898 .proto
= sk
->sk_protocol
,
900 { .sport
= dccp_hdr(skb
)->dccph_dport
,
901 .dport
= dccp_hdr(skb
)->dccph_sport
}
905 if (ip_route_output_flow(&rt
, &fl
, sk
, 0)) {
906 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES
);
913 static void dccp_v4_ctl_send_reset(struct sk_buff
*rxskb
)
916 struct dccp_hdr
*rxdh
= dccp_hdr(rxskb
), *dh
;
917 const int dccp_hdr_reset_len
= sizeof(struct dccp_hdr
) +
918 sizeof(struct dccp_hdr_ext
) +
919 sizeof(struct dccp_hdr_reset
);
921 struct dst_entry
*dst
;
924 /* Never send a reset in response to a reset. */
925 if (rxdh
->dccph_type
== DCCP_PKT_RESET
)
928 if (((struct rtable
*)rxskb
->dst
)->rt_type
!= RTN_LOCAL
)
931 dst
= dccp_v4_route_skb(dccp_ctl_socket
->sk
, rxskb
);
935 skb
= alloc_skb(MAX_DCCP_HEADER
+ 15, GFP_ATOMIC
);
939 /* Reserve space for headers. */
940 skb_reserve(skb
, MAX_DCCP_HEADER
);
941 skb
->dst
= dst_clone(dst
);
943 skb
->h
.raw
= skb_push(skb
, dccp_hdr_reset_len
);
945 memset(dh
, 0, dccp_hdr_reset_len
);
947 /* Build DCCP header and checksum it. */
948 dh
->dccph_type
= DCCP_PKT_RESET
;
949 dh
->dccph_sport
= rxdh
->dccph_dport
;
950 dh
->dccph_dport
= rxdh
->dccph_sport
;
951 dh
->dccph_doff
= dccp_hdr_reset_len
/ 4;
953 dccp_hdr_reset(skb
)->dccph_reset_code
=
954 DCCP_SKB_CB(rxskb
)->dccpd_reset_code
;
956 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
958 if (DCCP_SKB_CB(rxskb
)->dccpd_ack_seq
!= DCCP_PKT_WITHOUT_ACK_SEQ
)
959 dccp_set_seqno(&seqno
, DCCP_SKB_CB(rxskb
)->dccpd_ack_seq
+ 1);
961 dccp_hdr_set_seq(dh
, seqno
);
962 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb
),
963 DCCP_SKB_CB(rxskb
)->dccpd_seq
);
965 dh
->dccph_checksum
= dccp_v4_checksum(skb
, rxskb
->nh
.iph
->saddr
,
966 rxskb
->nh
.iph
->daddr
);
968 bh_lock_sock(dccp_ctl_socket
->sk
);
969 err
= ip_build_and_send_pkt(skb
, dccp_ctl_socket
->sk
,
970 rxskb
->nh
.iph
->daddr
,
971 rxskb
->nh
.iph
->saddr
, NULL
);
972 bh_unlock_sock(dccp_ctl_socket
->sk
);
974 if (err
== NET_XMIT_CN
|| err
== 0) {
975 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS
);
976 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS
);
982 int dccp_v4_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
984 struct dccp_hdr
*dh
= dccp_hdr(skb
);
986 if (sk
->sk_state
== DCCP_OPEN
) { /* Fast path */
987 if (dccp_rcv_established(sk
, skb
, dh
, skb
->len
))
993 * Step 3: Process LISTEN state
994 * If S.state == LISTEN,
995 * If P.type == Request or P contains a valid Init Cookie
997 * * Must scan the packet's options to check for an Init
998 * Cookie. Only the Init Cookie is processed here,
999 * however; other options are processed in Step 8. This
1000 * scan need only be performed if the endpoint uses Init
1002 * * Generate a new socket and switch to that socket *
1003 * Set S := new socket for this port pair
1005 * Choose S.ISS (initial seqno) or set from Init Cookie
1006 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
1007 * Continue with S.state == RESPOND
1008 * * A Response packet will be generated in Step 11 *
1010 * Generate Reset(No Connection) unless P.type == Reset
1011 * Drop packet and return
1013 * NOTE: the check for the packet types is done in
1014 * dccp_rcv_state_process
1016 if (sk
->sk_state
== DCCP_LISTEN
) {
1017 struct sock
*nsk
= dccp_v4_hnd_req(sk
, skb
);
1023 if (dccp_child_process(sk
, nsk
, skb
))
1029 if (dccp_rcv_state_process(sk
, skb
, dh
, skb
->len
))
1034 dccp_v4_ctl_send_reset(skb
);
1040 static inline int dccp_invalid_packet(struct sk_buff
*skb
)
1042 const struct dccp_hdr
*dh
;
1044 if (skb
->pkt_type
!= PACKET_HOST
)
1047 if (!pskb_may_pull(skb
, sizeof(struct dccp_hdr
))) {
1048 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: pskb_may_pull failed\n");
1054 /* If the packet type is not understood, drop packet and return */
1055 if (dh
->dccph_type
>= DCCP_PKT_INVALID
) {
1056 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: invalid packet type\n");
1061 * If P.Data Offset is too small for packet type, or too large for
1062 * packet, drop packet and return
1064 if (dh
->dccph_doff
< dccp_hdr_len(skb
) / sizeof(u32
)) {
1065 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: P.Data Offset(%u) "
1071 if (!pskb_may_pull(skb
, dh
->dccph_doff
* sizeof(u32
))) {
1072 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: P.Data Offset(%u) "
1081 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
1082 * has short sequence numbers), drop packet and return
1084 if (dh
->dccph_x
== 0 &&
1085 dh
->dccph_type
!= DCCP_PKT_DATA
&&
1086 dh
->dccph_type
!= DCCP_PKT_ACK
&&
1087 dh
->dccph_type
!= DCCP_PKT_DATAACK
) {
1088 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: P.type (%s) not Data, Ack "
1089 "nor DataAck and P.X == 0\n",
1090 dccp_packet_name(dh
->dccph_type
));
1094 /* If the header checksum is incorrect, drop packet and return */
1095 if (dccp_v4_verify_checksum(skb
, skb
->nh
.iph
->saddr
,
1096 skb
->nh
.iph
->daddr
) < 0) {
1097 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: header checksum is "
1105 /* this is called when real data arrives */
1106 int dccp_v4_rcv(struct sk_buff
*skb
)
1108 const struct dccp_hdr
*dh
;
1112 /* Step 1: Check header basics: */
1114 if (dccp_invalid_packet(skb
))
1119 DCCP_SKB_CB(skb
)->dccpd_seq
= dccp_hdr_seq(skb
);
1120 DCCP_SKB_CB(skb
)->dccpd_type
= dh
->dccph_type
;
1122 dccp_pr_debug("%8.8s "
1123 "src=%u.%u.%u.%u@%-5d "
1124 "dst=%u.%u.%u.%u@%-5d seq=%llu",
1125 dccp_packet_name(dh
->dccph_type
),
1126 NIPQUAD(skb
->nh
.iph
->saddr
), ntohs(dh
->dccph_sport
),
1127 NIPQUAD(skb
->nh
.iph
->daddr
), ntohs(dh
->dccph_dport
),
1128 (unsigned long long) DCCP_SKB_CB(skb
)->dccpd_seq
);
1130 if (dccp_packet_without_ack(skb
)) {
1131 DCCP_SKB_CB(skb
)->dccpd_ack_seq
= DCCP_PKT_WITHOUT_ACK_SEQ
;
1132 dccp_pr_debug_cat("\n");
1134 DCCP_SKB_CB(skb
)->dccpd_ack_seq
= dccp_hdr_ack_seq(skb
);
1135 dccp_pr_debug_cat(", ack=%llu\n",
1136 (unsigned long long)
1137 DCCP_SKB_CB(skb
)->dccpd_ack_seq
);
1141 * Look up flow ID in table and get corresponding socket */
1142 sk
= __inet_lookup(&dccp_hashinfo
,
1143 skb
->nh
.iph
->saddr
, dh
->dccph_sport
,
1144 skb
->nh
.iph
->daddr
, ntohs(dh
->dccph_dport
),
1150 * Generate Reset(No Connection) unless P.type == Reset
1151 * Drop packet and return
1154 dccp_pr_debug("failed to look up flow ID in table and "
1155 "get corresponding socket\n");
1156 goto no_dccp_socket
;
1161 * ... or S.state == TIMEWAIT,
1162 * Generate Reset(No Connection) unless P.type == Reset
1163 * Drop packet and return
1166 if (sk
->sk_state
== DCCP_TIME_WAIT
) {
1167 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
1172 if (!xfrm4_policy_check(sk
, XFRM_POLICY_IN
, skb
)) {
1173 dccp_pr_debug("xfrm4_policy_check failed\n");
1174 goto discard_and_relse
;
1177 if (sk_filter(sk
, skb
, 0)) {
1178 dccp_pr_debug("sk_filter failed\n");
1179 goto discard_and_relse
;
1186 if (!sock_owned_by_user(sk
))
1187 rc
= dccp_v4_do_rcv(sk
, skb
);
1189 sk_add_backlog(sk
, skb
);
1196 if (!xfrm4_policy_check(NULL
, XFRM_POLICY_IN
, skb
))
1200 * Generate Reset(No Connection) unless P.type == Reset
1201 * Drop packet and return
1203 if (dh
->dccph_type
!= DCCP_PKT_RESET
) {
1204 DCCP_SKB_CB(skb
)->dccpd_reset_code
=
1205 DCCP_RESET_CODE_NO_CONNECTION
;
1206 dccp_v4_ctl_send_reset(skb
);
1210 /* Discard frame. */
1219 inet_twsk_put((struct inet_timewait_sock
*)sk
);
1220 goto no_dccp_socket
;
1223 static int dccp_v4_init_sock(struct sock
*sk
)
1225 struct dccp_sock
*dp
= dccp_sk(sk
);
1226 static int dccp_ctl_socket_init
= 1;
1228 dccp_options_init(&dp
->dccps_options
);
1229 do_gettimeofday(&dp
->dccps_epoch
);
1231 if (dp
->dccps_options
.dccpo_send_ack_vector
) {
1232 dp
->dccps_hc_rx_ackvec
= dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN
,
1234 if (dp
->dccps_hc_rx_ackvec
== NULL
)
1239 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1240 * the listening (master) sock get CCID control blocks, which is not
1241 * necessary, but for now, to not mess with the test userspace apps,
1242 * lets leave it here, later the real solution is to do this in a
1243 * setsockopt(CCIDs-I-want/accept). -acme
1245 if (likely(!dccp_ctl_socket_init
)) {
1246 dp
->dccps_hc_rx_ccid
= ccid_init(dp
->dccps_options
.dccpo_rx_ccid
,
1248 dp
->dccps_hc_tx_ccid
= ccid_init(dp
->dccps_options
.dccpo_tx_ccid
,
1250 if (dp
->dccps_hc_rx_ccid
== NULL
||
1251 dp
->dccps_hc_tx_ccid
== NULL
) {
1252 ccid_exit(dp
->dccps_hc_rx_ccid
, sk
);
1253 ccid_exit(dp
->dccps_hc_tx_ccid
, sk
);
1254 if (dp
->dccps_options
.dccpo_send_ack_vector
) {
1255 dccp_ackvec_free(dp
->dccps_hc_rx_ackvec
);
1256 dp
->dccps_hc_rx_ackvec
= NULL
;
1258 dp
->dccps_hc_rx_ccid
= dp
->dccps_hc_tx_ccid
= NULL
;
1262 dccp_ctl_socket_init
= 0;
1264 dccp_init_xmit_timers(sk
);
1265 inet_csk(sk
)->icsk_rto
= DCCP_TIMEOUT_INIT
;
1266 sk
->sk_state
= DCCP_CLOSED
;
1267 sk
->sk_write_space
= dccp_write_space
;
1268 dp
->dccps_mss_cache
= 536;
1269 dp
->dccps_role
= DCCP_ROLE_UNDEFINED
;
1270 dp
->dccps_service
= DCCP_SERVICE_INVALID_VALUE
;
1275 static int dccp_v4_destroy_sock(struct sock
*sk
)
1277 struct dccp_sock
*dp
= dccp_sk(sk
);
1280 * DCCP doesn't use sk_qrite_queue, just sk_send_head
1281 * for retransmissions
1283 if (sk
->sk_send_head
!= NULL
) {
1284 kfree_skb(sk
->sk_send_head
);
1285 sk
->sk_send_head
= NULL
;
1288 /* Clean up a referenced DCCP bind bucket. */
1289 if (inet_csk(sk
)->icsk_bind_hash
!= NULL
)
1290 inet_put_port(&dccp_hashinfo
, sk
);
1292 if (dp
->dccps_service_list
!= NULL
) {
1293 kfree(dp
->dccps_service_list
);
1294 dp
->dccps_service_list
= NULL
;
1297 ccid_hc_rx_exit(dp
->dccps_hc_rx_ccid
, sk
);
1298 ccid_hc_tx_exit(dp
->dccps_hc_tx_ccid
, sk
);
1299 if (dp
->dccps_options
.dccpo_send_ack_vector
) {
1300 dccp_ackvec_free(dp
->dccps_hc_rx_ackvec
);
1301 dp
->dccps_hc_rx_ackvec
= NULL
;
1303 ccid_exit(dp
->dccps_hc_rx_ccid
, sk
);
1304 ccid_exit(dp
->dccps_hc_tx_ccid
, sk
);
1305 dp
->dccps_hc_rx_ccid
= dp
->dccps_hc_tx_ccid
= NULL
;
1310 static void dccp_v4_reqsk_destructor(struct request_sock
*req
)
1312 kfree(inet_rsk(req
)->opt
);
1315 static struct request_sock_ops dccp_request_sock_ops
= {
1317 .obj_size
= sizeof(struct dccp_request_sock
),
1318 .rtx_syn_ack
= dccp_v4_send_response
,
1319 .send_ack
= dccp_v4_reqsk_send_ack
,
1320 .destructor
= dccp_v4_reqsk_destructor
,
1321 .send_reset
= dccp_v4_ctl_send_reset
,
1324 struct proto dccp_v4_prot
= {
1326 .owner
= THIS_MODULE
,
1327 .close
= dccp_close
,
1328 .connect
= dccp_v4_connect
,
1329 .disconnect
= dccp_disconnect
,
1330 .ioctl
= dccp_ioctl
,
1331 .init
= dccp_v4_init_sock
,
1332 .setsockopt
= dccp_setsockopt
,
1333 .getsockopt
= dccp_getsockopt
,
1334 .sendmsg
= dccp_sendmsg
,
1335 .recvmsg
= dccp_recvmsg
,
1336 .backlog_rcv
= dccp_v4_do_rcv
,
1337 .hash
= dccp_v4_hash
,
1338 .unhash
= dccp_v4_unhash
,
1339 .accept
= inet_csk_accept
,
1340 .get_port
= dccp_v4_get_port
,
1341 .shutdown
= dccp_shutdown
,
1342 .destroy
= dccp_v4_destroy_sock
,
1343 .orphan_count
= &dccp_orphan_count
,
1344 .max_header
= MAX_DCCP_HEADER
,
1345 .obj_size
= sizeof(struct dccp_sock
),
1346 .rsk_prot
= &dccp_request_sock_ops
,
1347 .twsk_obj_size
= sizeof(struct inet_timewait_sock
),