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
),
36 EXPORT_SYMBOL_GPL(dccp_hashinfo
);
38 static int dccp_v4_get_port(struct sock
*sk
, const unsigned short snum
)
40 return inet_csk_get_port(&dccp_hashinfo
, sk
, snum
,
41 inet_csk_bind_conflict
);
44 static void dccp_v4_hash(struct sock
*sk
)
46 inet_hash(&dccp_hashinfo
, sk
);
49 static void dccp_v4_unhash(struct sock
*sk
)
51 inet_unhash(&dccp_hashinfo
, sk
);
54 /* called with local bh disabled */
55 static int __dccp_v4_check_established(struct sock
*sk
, const __u16 lport
,
56 struct inet_timewait_sock
**twp
)
58 struct inet_sock
*inet
= inet_sk(sk
);
59 const u32 daddr
= inet
->rcv_saddr
;
60 const u32 saddr
= inet
->daddr
;
61 const int dif
= sk
->sk_bound_dev_if
;
62 INET_ADDR_COOKIE(acookie
, saddr
, daddr
)
63 const __u32 ports
= INET_COMBINED_PORTS(inet
->dport
, lport
);
64 unsigned int hash
= inet_ehashfn(daddr
, lport
, saddr
, inet
->dport
);
65 struct inet_ehash_bucket
*head
= inet_ehash_bucket(&dccp_hashinfo
, hash
);
66 const struct sock
*sk2
;
67 const struct hlist_node
*node
;
68 struct inet_timewait_sock
*tw
;
70 prefetch(head
->chain
.first
);
71 write_lock(&head
->lock
);
73 /* Check TIME-WAIT sockets first. */
74 sk_for_each(sk2
, node
, &(head
+ dccp_hashinfo
.ehash_size
)->chain
) {
77 if (INET_TW_MATCH(sk2
, hash
, acookie
, saddr
, daddr
, ports
, dif
))
82 /* And established part... */
83 sk_for_each(sk2
, node
, &head
->chain
) {
84 if (INET_MATCH(sk2
, hash
, acookie
, saddr
, daddr
, ports
, dif
))
88 /* Must record num and sport now. Otherwise we will see
89 * in hash table socket with a funny identity. */
91 inet
->sport
= htons(lport
);
93 BUG_TRAP(sk_unhashed(sk
));
94 __sk_add_node(sk
, &head
->chain
);
95 sock_prot_inc_use(sk
->sk_prot
);
96 write_unlock(&head
->lock
);
100 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED
);
101 } else if (tw
!= NULL
) {
102 /* Silly. Should hash-dance instead... */
103 inet_twsk_deschedule(tw
, &dccp_death_row
);
104 NET_INC_STATS_BH(LINUX_MIB_TIMEWAITRECYCLED
);
112 write_unlock(&head
->lock
);
113 return -EADDRNOTAVAIL
;
117 * Bind a port for a connect operation and hash it.
119 static int dccp_v4_hash_connect(struct sock
*sk
)
121 const unsigned short snum
= inet_sk(sk
)->num
;
122 struct inet_bind_hashbucket
*head
;
123 struct inet_bind_bucket
*tb
;
127 int low
= sysctl_local_port_range
[0];
128 int high
= sysctl_local_port_range
[1];
129 int remaining
= (high
- low
) + 1;
130 int rover
= net_random() % (high
- low
) + low
;
131 struct hlist_node
*node
;
132 struct inet_timewait_sock
*tw
= NULL
;
136 head
= &dccp_hashinfo
.bhash
[inet_bhashfn(rover
,
137 dccp_hashinfo
.bhash_size
)];
138 spin_lock(&head
->lock
);
140 /* Does not bother with rcv_saddr checks,
141 * because the established check is already
144 inet_bind_bucket_for_each(tb
, node
, &head
->chain
) {
145 if (tb
->port
== rover
) {
146 BUG_TRAP(!hlist_empty(&tb
->owners
));
147 if (tb
->fastreuse
>= 0)
149 if (!__dccp_v4_check_established(sk
,
157 tb
= inet_bind_bucket_create(dccp_hashinfo
.bind_bucket_cachep
,
160 spin_unlock(&head
->lock
);
167 spin_unlock(&head
->lock
);
170 } while (--remaining
> 0);
174 return -EADDRNOTAVAIL
;
177 /* All locks still held and bhs disabled */
178 inet_bind_hash(sk
, tb
, rover
);
179 if (sk_unhashed(sk
)) {
180 inet_sk(sk
)->sport
= htons(rover
);
181 __inet_hash(&dccp_hashinfo
, sk
, 0);
183 spin_unlock(&head
->lock
);
186 inet_twsk_deschedule(tw
, &dccp_death_row
);
194 head
= &dccp_hashinfo
.bhash
[inet_bhashfn(snum
,
195 dccp_hashinfo
.bhash_size
)];
196 tb
= inet_csk(sk
)->icsk_bind_hash
;
197 spin_lock_bh(&head
->lock
);
198 if (sk_head(&tb
->owners
) == sk
&& sk
->sk_bind_node
.next
== NULL
) {
199 __inet_hash(&dccp_hashinfo
, sk
, 0);
200 spin_unlock_bh(&head
->lock
);
203 spin_unlock(&head
->lock
);
204 /* No definite answer... Walk to established hash table */
205 ret
= __dccp_v4_check_established(sk
, snum
, NULL
);
212 static int dccp_v4_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
215 struct inet_sock
*inet
= inet_sk(sk
);
216 struct dccp_sock
*dp
= dccp_sk(sk
);
217 const struct sockaddr_in
*usin
= (struct sockaddr_in
*)uaddr
;
223 dp
->dccps_role
= DCCP_ROLE_CLIENT
;
225 if (dccp_service_not_initialized(sk
))
228 if (addr_len
< sizeof(struct sockaddr_in
))
231 if (usin
->sin_family
!= AF_INET
)
232 return -EAFNOSUPPORT
;
234 nexthop
= daddr
= usin
->sin_addr
.s_addr
;
235 if (inet
->opt
!= NULL
&& inet
->opt
->srr
) {
238 nexthop
= inet
->opt
->faddr
;
241 tmp
= ip_route_connect(&rt
, nexthop
, inet
->saddr
,
242 RT_CONN_FLAGS(sk
), sk
->sk_bound_dev_if
,
244 inet
->sport
, usin
->sin_port
, sk
);
248 if (rt
->rt_flags
& (RTCF_MULTICAST
| RTCF_BROADCAST
)) {
253 if (inet
->opt
== NULL
|| !inet
->opt
->srr
)
256 if (inet
->saddr
== 0)
257 inet
->saddr
= rt
->rt_src
;
258 inet
->rcv_saddr
= inet
->saddr
;
260 inet
->dport
= usin
->sin_port
;
263 dp
->dccps_ext_header_len
= 0;
264 if (inet
->opt
!= NULL
)
265 dp
->dccps_ext_header_len
= inet
->opt
->optlen
;
267 * Socket identity is still unknown (sport may be zero).
268 * However we set state to DCCP_REQUESTING and not releasing socket
269 * lock select source port, enter ourselves into the hash tables and
270 * complete initialization after this.
272 dccp_set_state(sk
, DCCP_REQUESTING
);
273 err
= dccp_v4_hash_connect(sk
);
277 err
= ip_route_newports(&rt
, inet
->sport
, inet
->dport
, sk
);
281 /* OK, now commit destination to socket. */
282 sk_setup_caps(sk
, &rt
->u
.dst
);
285 dp
->dccps_iss
= secure_dccp_sequence_number(inet
->saddr
,
289 dccp_update_gss(sk
, dp
->dccps_iss
);
292 * SWL and AWL are initially adjusted so that they are not less than
293 * the initial Sequence Numbers received and sent, respectively:
294 * SWL := max(GSR + 1 - floor(W/4), ISR),
295 * AWL := max(GSS - W' + 1, ISS).
296 * These adjustments MUST be applied only at the beginning of the
299 dccp_set_seqno(&dp
->dccps_awl
, max48(dp
->dccps_awl
, dp
->dccps_iss
));
301 inet
->id
= dp
->dccps_iss
^ jiffies
;
303 err
= dccp_connect(sk
);
311 * This unhashes the socket and releases the local port, if necessary.
313 dccp_set_state(sk
, DCCP_CLOSED
);
315 sk
->sk_route_caps
= 0;
321 * This routine does path mtu discovery as defined in RFC1191.
323 static inline void dccp_do_pmtu_discovery(struct sock
*sk
,
324 const struct iphdr
*iph
,
327 struct dst_entry
*dst
;
328 const struct inet_sock
*inet
= inet_sk(sk
);
329 const struct dccp_sock
*dp
= dccp_sk(sk
);
331 /* We are not interested in DCCP_LISTEN and request_socks (RESPONSEs
332 * send out by Linux are always < 576bytes so they should go through
335 if (sk
->sk_state
== DCCP_LISTEN
)
338 /* We don't check in the destentry if pmtu discovery is forbidden
339 * on this route. We just assume that no packet_to_big packets
340 * are send back when pmtu discovery is not active.
341 * There is a small race when the user changes this flag in the
342 * route, but I think that's acceptable.
344 if ((dst
= __sk_dst_check(sk
, 0)) == NULL
)
347 dst
->ops
->update_pmtu(dst
, mtu
);
349 /* Something is about to be wrong... Remember soft error
350 * for the case, if this connection will not able to recover.
352 if (mtu
< dst_mtu(dst
) && ip_dont_fragment(sk
, dst
))
353 sk
->sk_err_soft
= EMSGSIZE
;
357 if (inet
->pmtudisc
!= IP_PMTUDISC_DONT
&&
358 dp
->dccps_pmtu_cookie
> mtu
) {
359 dccp_sync_mss(sk
, mtu
);
362 * From: draft-ietf-dccp-spec-11.txt
364 * DCCP-Sync packets are the best choice for upward
365 * probing, since DCCP-Sync probes do not risk application
368 dccp_send_sync(sk
, dp
->dccps_gsr
, DCCP_PKT_SYNC
);
369 } /* else let the usual retransmit timer handle it */
372 static void dccp_v4_ctl_send_ack(struct sk_buff
*rxskb
)
375 struct dccp_hdr
*rxdh
= dccp_hdr(rxskb
), *dh
;
376 const int dccp_hdr_ack_len
= sizeof(struct dccp_hdr
) +
377 sizeof(struct dccp_hdr_ext
) +
378 sizeof(struct dccp_hdr_ack_bits
);
381 if (((struct rtable
*)rxskb
->dst
)->rt_type
!= RTN_LOCAL
)
384 skb
= alloc_skb(MAX_DCCP_HEADER
+ 15, GFP_ATOMIC
);
388 /* Reserve space for headers. */
389 skb_reserve(skb
, MAX_DCCP_HEADER
);
391 skb
->dst
= dst_clone(rxskb
->dst
);
393 skb
->h
.raw
= skb_push(skb
, dccp_hdr_ack_len
);
395 memset(dh
, 0, dccp_hdr_ack_len
);
397 /* Build DCCP header and checksum it. */
398 dh
->dccph_type
= DCCP_PKT_ACK
;
399 dh
->dccph_sport
= rxdh
->dccph_dport
;
400 dh
->dccph_dport
= rxdh
->dccph_sport
;
401 dh
->dccph_doff
= dccp_hdr_ack_len
/ 4;
404 dccp_hdr_set_seq(dh
, DCCP_SKB_CB(rxskb
)->dccpd_ack_seq
);
405 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb
),
406 DCCP_SKB_CB(rxskb
)->dccpd_seq
);
408 bh_lock_sock(dccp_ctl_socket
->sk
);
409 err
= ip_build_and_send_pkt(skb
, dccp_ctl_socket
->sk
,
410 rxskb
->nh
.iph
->daddr
,
411 rxskb
->nh
.iph
->saddr
, NULL
);
412 bh_unlock_sock(dccp_ctl_socket
->sk
);
414 if (err
== NET_XMIT_CN
|| err
== 0) {
415 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS
);
416 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS
);
420 static void dccp_v4_reqsk_send_ack(struct sk_buff
*skb
,
421 struct request_sock
*req
)
423 dccp_v4_ctl_send_ack(skb
);
426 static int dccp_v4_send_response(struct sock
*sk
, struct request_sock
*req
,
427 struct dst_entry
*dst
)
432 /* First, grab a route. */
434 if (dst
== NULL
&& (dst
= inet_csk_route_req(sk
, req
)) == NULL
)
437 skb
= dccp_make_response(sk
, dst
, req
);
439 const struct inet_request_sock
*ireq
= inet_rsk(req
);
441 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
442 err
= ip_build_and_send_pkt(skb
, sk
, ireq
->loc_addr
,
445 if (err
== NET_XMIT_CN
)
455 * This routine is called by the ICMP module when it gets some sort of error
456 * condition. If err < 0 then the socket should be closed and the error
457 * returned to the user. If err > 0 it's just the icmp type << 8 | icmp code.
458 * After adjustment header points to the first 8 bytes of the tcp header. We
459 * need to find the appropriate port.
461 * The locking strategy used here is very "optimistic". When someone else
462 * accesses the socket the ICMP is just dropped and for some paths there is no
463 * check at all. A more general error queue to queue errors for later handling
464 * is probably better.
466 void dccp_v4_err(struct sk_buff
*skb
, u32 info
)
468 const struct iphdr
*iph
= (struct iphdr
*)skb
->data
;
469 const struct dccp_hdr
*dh
= (struct dccp_hdr
*)(skb
->data
+
471 struct dccp_sock
*dp
;
472 struct inet_sock
*inet
;
473 const int type
= skb
->h
.icmph
->type
;
474 const int code
= skb
->h
.icmph
->code
;
479 if (skb
->len
< (iph
->ihl
<< 2) + 8) {
480 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS
);
484 sk
= inet_lookup(&dccp_hashinfo
, iph
->daddr
, dh
->dccph_dport
,
485 iph
->saddr
, dh
->dccph_sport
, inet_iif(skb
));
487 ICMP_INC_STATS_BH(ICMP_MIB_INERRORS
);
491 if (sk
->sk_state
== DCCP_TIME_WAIT
) {
492 inet_twsk_put((struct inet_timewait_sock
*)sk
);
497 /* If too many ICMPs get dropped on busy
498 * servers this needs to be solved differently.
500 if (sock_owned_by_user(sk
))
501 NET_INC_STATS_BH(LINUX_MIB_LOCKDROPPEDICMPS
);
503 if (sk
->sk_state
== DCCP_CLOSED
)
507 seq
= dccp_hdr_seq(skb
);
508 if (sk
->sk_state
!= DCCP_LISTEN
&&
509 !between48(seq
, dp
->dccps_swl
, dp
->dccps_swh
)) {
510 NET_INC_STATS(LINUX_MIB_OUTOFWINDOWICMPS
);
515 case ICMP_SOURCE_QUENCH
:
516 /* Just silently ignore these. */
518 case ICMP_PARAMETERPROB
:
521 case ICMP_DEST_UNREACH
:
522 if (code
> NR_ICMP_UNREACH
)
525 if (code
== ICMP_FRAG_NEEDED
) { /* PMTU discovery (RFC1191) */
526 if (!sock_owned_by_user(sk
))
527 dccp_do_pmtu_discovery(sk
, iph
, info
);
531 err
= icmp_err_convert
[code
].errno
;
533 case ICMP_TIME_EXCEEDED
:
540 switch (sk
->sk_state
) {
541 struct request_sock
*req
, **prev
;
543 if (sock_owned_by_user(sk
))
545 req
= inet_csk_search_req(sk
, &prev
, dh
->dccph_dport
,
546 iph
->daddr
, iph
->saddr
);
551 * ICMPs are not backlogged, hence we cannot get an established
556 if (seq
!= dccp_rsk(req
)->dreq_iss
) {
557 NET_INC_STATS_BH(LINUX_MIB_OUTOFWINDOWICMPS
);
561 * Still in RESPOND, just remove it silently.
562 * There is no good way to pass the error to the newly
563 * created socket, and POSIX does not want network
564 * errors returned from accept().
566 inet_csk_reqsk_queue_drop(sk
, req
, prev
);
569 case DCCP_REQUESTING
:
571 if (!sock_owned_by_user(sk
)) {
572 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS
);
575 sk
->sk_error_report(sk
);
579 sk
->sk_err_soft
= err
;
583 /* If we've already connected we will keep trying
584 * until we time out, or the user gives up.
586 * rfc1122 4.2.3.9 allows to consider as hard errors
587 * only PROTO_UNREACH and PORT_UNREACH (well, FRAG_FAILED too,
588 * but it is obsoleted by pmtu discovery).
590 * Note, that in modern internet, where routing is unreliable
591 * and in each dark corner broken firewalls sit, sending random
592 * errors ordered by their masters even this two messages finally lose
593 * their original sense (even Linux sends invalid PORT_UNREACHs)
595 * Now we are in compliance with RFCs.
600 if (!sock_owned_by_user(sk
) && inet
->recverr
) {
602 sk
->sk_error_report(sk
);
603 } else /* Only an error on timeout */
604 sk
->sk_err_soft
= err
;
610 /* This routine computes an IPv4 DCCP checksum. */
611 static void dccp_v4_send_check(struct sock
*sk
, int len
, struct sk_buff
*skb
)
613 const struct inet_sock
*inet
= inet_sk(sk
);
614 struct dccp_hdr
*dh
= dccp_hdr(skb
);
616 dh
->dccph_checksum
= dccp_v4_checksum(skb
, inet
->saddr
, inet
->daddr
);
619 int dccp_v4_send_reset(struct sock
*sk
, enum dccp_reset_codes code
)
623 * FIXME: what if rebuild_header fails?
624 * Should we be doing a rebuild_header here?
626 int err
= inet_sk_rebuild_header(sk
);
631 skb
= dccp_make_reset(sk
, sk
->sk_dst_cache
, code
);
633 const struct inet_sock
*inet
= inet_sk(sk
);
635 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
636 err
= ip_build_and_send_pkt(skb
, sk
,
637 inet
->saddr
, inet
->daddr
, NULL
);
638 if (err
== NET_XMIT_CN
)
645 static inline u64
dccp_v4_init_sequence(const struct sock
*sk
,
646 const struct sk_buff
*skb
)
648 return secure_dccp_sequence_number(skb
->nh
.iph
->daddr
,
650 dccp_hdr(skb
)->dccph_dport
,
651 dccp_hdr(skb
)->dccph_sport
);
654 static inline int dccp_bad_service_code(const struct sock
*sk
,
657 const struct dccp_sock
*dp
= dccp_sk(sk
);
659 if (dp
->dccps_service
== service
)
661 return !dccp_list_has_service(dp
->dccps_service_list
, service
);
664 int dccp_v4_conn_request(struct sock
*sk
, struct sk_buff
*skb
)
666 struct inet_request_sock
*ireq
;
668 struct request_sock
*req
;
669 struct dccp_request_sock
*dreq
;
670 const __u32 saddr
= skb
->nh
.iph
->saddr
;
671 const __u32 daddr
= skb
->nh
.iph
->daddr
;
672 const __u32 service
= dccp_hdr_request(skb
)->dccph_req_service
;
673 struct dccp_skb_cb
*dcb
= DCCP_SKB_CB(skb
);
674 __u8 reset_code
= DCCP_RESET_CODE_TOO_BUSY
;
675 struct dst_entry
*dst
= NULL
;
677 /* Never answer to DCCP_PKT_REQUESTs send to broadcast or multicast */
678 if (((struct rtable
*)skb
->dst
)->rt_flags
&
679 (RTCF_BROADCAST
| RTCF_MULTICAST
)) {
680 reset_code
= DCCP_RESET_CODE_NO_CONNECTION
;
684 if (dccp_bad_service_code(sk
, service
)) {
685 reset_code
= DCCP_RESET_CODE_BAD_SERVICE_CODE
;
689 * TW buckets are converted to open requests without
690 * limitations, they conserve resources and peer is
691 * evidently real one.
693 if (inet_csk_reqsk_queue_is_full(sk
))
697 * Accept backlog is full. If we have already queued enough
698 * of warm entries in syn queue, drop request. It is better than
699 * clogging syn queue with openreqs with exponentially increasing
702 if (sk_acceptq_is_full(sk
) && inet_csk_reqsk_queue_young(sk
) > 1)
705 req
= reqsk_alloc(sk
->sk_prot
->rsk_prot
);
709 /* FIXME: process options */
711 dccp_openreq_init(req
, &dp
, skb
);
713 ireq
= inet_rsk(req
);
714 ireq
->loc_addr
= daddr
;
715 ireq
->rmt_addr
= saddr
;
716 /* FIXME: Merge Aristeu's option parsing code when ready */
717 req
->rcv_wnd
= 100; /* Fake, option parsing will get the
722 * Step 3: Process LISTEN state
724 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
726 * In fact we defer setting S.GSR, S.SWL, S.SWH to
727 * dccp_create_openreq_child.
729 dreq
= dccp_rsk(req
);
730 dreq
->dreq_isr
= dcb
->dccpd_seq
;
731 dreq
->dreq_iss
= dccp_v4_init_sequence(sk
, skb
);
732 dreq
->dreq_service
= service
;
734 if (dccp_v4_send_response(sk
, req
, dst
))
737 inet_csk_reqsk_queue_hash_add(sk
, req
, DCCP_TIMEOUT_INIT
);
742 * FIXME: should be reqsk_free after implementing req->rsk_ops
746 DCCP_INC_STATS_BH(DCCP_MIB_ATTEMPTFAILS
);
747 dcb
->dccpd_reset_code
= reset_code
;
752 * The three way handshake has completed - we got a valid ACK or DATAACK -
753 * now create the new socket.
755 * This is the equivalent of TCP's tcp_v4_syn_recv_sock
757 struct sock
*dccp_v4_request_recv_sock(struct sock
*sk
, struct sk_buff
*skb
,
758 struct request_sock
*req
,
759 struct dst_entry
*dst
)
761 struct inet_request_sock
*ireq
;
762 struct inet_sock
*newinet
;
763 struct dccp_sock
*newdp
;
766 if (sk_acceptq_is_full(sk
))
769 if (dst
== NULL
&& (dst
= inet_csk_route_req(sk
, req
)) == NULL
)
772 newsk
= dccp_create_openreq_child(sk
, req
, skb
);
776 sk_setup_caps(newsk
, dst
);
778 newdp
= dccp_sk(newsk
);
779 newinet
= inet_sk(newsk
);
780 ireq
= inet_rsk(req
);
781 newinet
->daddr
= ireq
->rmt_addr
;
782 newinet
->rcv_saddr
= ireq
->loc_addr
;
783 newinet
->saddr
= ireq
->loc_addr
;
784 newinet
->opt
= ireq
->opt
;
786 newinet
->mc_index
= inet_iif(skb
);
787 newinet
->mc_ttl
= skb
->nh
.iph
->ttl
;
788 newinet
->id
= jiffies
;
790 dccp_sync_mss(newsk
, dst_mtu(dst
));
792 __inet_hash(&dccp_hashinfo
, newsk
, 0);
793 __inet_inherit_port(&dccp_hashinfo
, sk
, newsk
);
798 NET_INC_STATS_BH(LINUX_MIB_LISTENOVERFLOWS
);
800 NET_INC_STATS_BH(LINUX_MIB_LISTENDROPS
);
805 static struct sock
*dccp_v4_hnd_req(struct sock
*sk
, struct sk_buff
*skb
)
807 const struct dccp_hdr
*dh
= dccp_hdr(skb
);
808 const struct iphdr
*iph
= skb
->nh
.iph
;
810 struct request_sock
**prev
;
811 /* Find possible connection requests. */
812 struct request_sock
*req
= inet_csk_search_req(sk
, &prev
,
814 iph
->saddr
, iph
->daddr
);
816 return dccp_check_req(sk
, skb
, req
, prev
);
818 nsk
= __inet_lookup_established(&dccp_hashinfo
,
819 iph
->saddr
, dh
->dccph_sport
,
820 iph
->daddr
, ntohs(dh
->dccph_dport
),
823 if (nsk
->sk_state
!= DCCP_TIME_WAIT
) {
827 inet_twsk_put((struct inet_timewait_sock
*)nsk
);
834 int dccp_v4_checksum(const struct sk_buff
*skb
, const u32 saddr
,
837 const struct dccp_hdr
* dh
= dccp_hdr(skb
);
841 if (dh
->dccph_cscov
== 0)
842 checksum_len
= skb
->len
;
844 checksum_len
= (dh
->dccph_cscov
+ dh
->dccph_x
) * sizeof(u32
);
845 checksum_len
= checksum_len
< skb
->len
? checksum_len
:
849 tmp
= csum_partial((unsigned char *)dh
, checksum_len
, 0);
850 return csum_tcpudp_magic(saddr
, daddr
, checksum_len
,
854 static int dccp_v4_verify_checksum(struct sk_buff
*skb
,
855 const u32 saddr
, const u32 daddr
)
857 struct dccp_hdr
*dh
= dccp_hdr(skb
);
861 if (dh
->dccph_cscov
== 0)
862 checksum_len
= skb
->len
;
864 checksum_len
= (dh
->dccph_cscov
+ dh
->dccph_x
) * sizeof(u32
);
865 checksum_len
= checksum_len
< skb
->len
? checksum_len
:
868 tmp
= csum_partial((unsigned char *)dh
, checksum_len
, 0);
869 return csum_tcpudp_magic(saddr
, daddr
, checksum_len
,
870 IPPROTO_DCCP
, tmp
) == 0 ? 0 : -1;
873 static struct dst_entry
* dccp_v4_route_skb(struct sock
*sk
,
877 struct flowi fl
= { .oif
= ((struct rtable
*)skb
->dst
)->rt_iif
,
879 { .daddr
= skb
->nh
.iph
->saddr
,
880 .saddr
= skb
->nh
.iph
->daddr
,
881 .tos
= RT_CONN_FLAGS(sk
) } },
882 .proto
= sk
->sk_protocol
,
884 { .sport
= dccp_hdr(skb
)->dccph_dport
,
885 .dport
= dccp_hdr(skb
)->dccph_sport
}
889 if (ip_route_output_flow(&rt
, &fl
, sk
, 0)) {
890 IP_INC_STATS_BH(IPSTATS_MIB_OUTNOROUTES
);
897 static void dccp_v4_ctl_send_reset(struct sk_buff
*rxskb
)
900 struct dccp_hdr
*rxdh
= dccp_hdr(rxskb
), *dh
;
901 const int dccp_hdr_reset_len
= sizeof(struct dccp_hdr
) +
902 sizeof(struct dccp_hdr_ext
) +
903 sizeof(struct dccp_hdr_reset
);
905 struct dst_entry
*dst
;
908 /* Never send a reset in response to a reset. */
909 if (rxdh
->dccph_type
== DCCP_PKT_RESET
)
912 if (((struct rtable
*)rxskb
->dst
)->rt_type
!= RTN_LOCAL
)
915 dst
= dccp_v4_route_skb(dccp_ctl_socket
->sk
, rxskb
);
919 skb
= alloc_skb(MAX_DCCP_HEADER
+ 15, GFP_ATOMIC
);
923 /* Reserve space for headers. */
924 skb_reserve(skb
, MAX_DCCP_HEADER
);
925 skb
->dst
= dst_clone(dst
);
927 skb
->h
.raw
= skb_push(skb
, dccp_hdr_reset_len
);
929 memset(dh
, 0, dccp_hdr_reset_len
);
931 /* Build DCCP header and checksum it. */
932 dh
->dccph_type
= DCCP_PKT_RESET
;
933 dh
->dccph_sport
= rxdh
->dccph_dport
;
934 dh
->dccph_dport
= rxdh
->dccph_sport
;
935 dh
->dccph_doff
= dccp_hdr_reset_len
/ 4;
937 dccp_hdr_reset(skb
)->dccph_reset_code
=
938 DCCP_SKB_CB(rxskb
)->dccpd_reset_code
;
940 /* See "8.3.1. Abnormal Termination" in draft-ietf-dccp-spec-11 */
942 if (DCCP_SKB_CB(rxskb
)->dccpd_ack_seq
!= DCCP_PKT_WITHOUT_ACK_SEQ
)
943 dccp_set_seqno(&seqno
, DCCP_SKB_CB(rxskb
)->dccpd_ack_seq
+ 1);
945 dccp_hdr_set_seq(dh
, seqno
);
946 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb
),
947 DCCP_SKB_CB(rxskb
)->dccpd_seq
);
949 dh
->dccph_checksum
= dccp_v4_checksum(skb
, rxskb
->nh
.iph
->saddr
,
950 rxskb
->nh
.iph
->daddr
);
952 bh_lock_sock(dccp_ctl_socket
->sk
);
953 err
= ip_build_and_send_pkt(skb
, dccp_ctl_socket
->sk
,
954 rxskb
->nh
.iph
->daddr
,
955 rxskb
->nh
.iph
->saddr
, NULL
);
956 bh_unlock_sock(dccp_ctl_socket
->sk
);
958 if (err
== NET_XMIT_CN
|| err
== 0) {
959 DCCP_INC_STATS_BH(DCCP_MIB_OUTSEGS
);
960 DCCP_INC_STATS_BH(DCCP_MIB_OUTRSTS
);
966 int dccp_v4_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
968 struct dccp_hdr
*dh
= dccp_hdr(skb
);
970 if (sk
->sk_state
== DCCP_OPEN
) { /* Fast path */
971 if (dccp_rcv_established(sk
, skb
, dh
, skb
->len
))
977 * Step 3: Process LISTEN state
978 * If S.state == LISTEN,
979 * If P.type == Request or P contains a valid Init Cookie
981 * * Must scan the packet's options to check for an Init
982 * Cookie. Only the Init Cookie is processed here,
983 * however; other options are processed in Step 8. This
984 * scan need only be performed if the endpoint uses Init
986 * * Generate a new socket and switch to that socket *
987 * Set S := new socket for this port pair
989 * Choose S.ISS (initial seqno) or set from Init Cookie
990 * Set S.ISR, S.GSR, S.SWL, S.SWH from packet or Init Cookie
991 * Continue with S.state == RESPOND
992 * * A Response packet will be generated in Step 11 *
994 * Generate Reset(No Connection) unless P.type == Reset
995 * Drop packet and return
997 * NOTE: the check for the packet types is done in
998 * dccp_rcv_state_process
1000 if (sk
->sk_state
== DCCP_LISTEN
) {
1001 struct sock
*nsk
= dccp_v4_hnd_req(sk
, skb
);
1007 if (dccp_child_process(sk
, nsk
, skb
))
1013 if (dccp_rcv_state_process(sk
, skb
, dh
, skb
->len
))
1018 dccp_v4_ctl_send_reset(skb
);
1024 static inline int dccp_invalid_packet(struct sk_buff
*skb
)
1026 const struct dccp_hdr
*dh
;
1028 if (skb
->pkt_type
!= PACKET_HOST
)
1031 if (!pskb_may_pull(skb
, sizeof(struct dccp_hdr
))) {
1032 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: pskb_may_pull failed\n");
1038 /* If the packet type is not understood, drop packet and return */
1039 if (dh
->dccph_type
>= DCCP_PKT_INVALID
) {
1040 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: invalid packet type\n");
1045 * If P.Data Offset is too small for packet type, or too large for
1046 * packet, drop packet and return
1048 if (dh
->dccph_doff
< dccp_hdr_len(skb
) / sizeof(u32
)) {
1049 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: P.Data Offset(%u) "
1055 if (!pskb_may_pull(skb
, dh
->dccph_doff
* sizeof(u32
))) {
1056 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: P.Data Offset(%u) "
1065 * If P.type is not Data, Ack, or DataAck and P.X == 0 (the packet
1066 * has short sequence numbers), drop packet and return
1068 if (dh
->dccph_x
== 0 &&
1069 dh
->dccph_type
!= DCCP_PKT_DATA
&&
1070 dh
->dccph_type
!= DCCP_PKT_ACK
&&
1071 dh
->dccph_type
!= DCCP_PKT_DATAACK
) {
1072 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: P.type (%s) not Data, Ack "
1073 "nor DataAck and P.X == 0\n",
1074 dccp_packet_name(dh
->dccph_type
));
1078 /* If the header checksum is incorrect, drop packet and return */
1079 if (dccp_v4_verify_checksum(skb
, skb
->nh
.iph
->saddr
,
1080 skb
->nh
.iph
->daddr
) < 0) {
1081 LIMIT_NETDEBUG(KERN_WARNING
"DCCP: header checksum is "
1089 /* this is called when real data arrives */
1090 int dccp_v4_rcv(struct sk_buff
*skb
)
1092 const struct dccp_hdr
*dh
;
1096 /* Step 1: Check header basics: */
1098 if (dccp_invalid_packet(skb
))
1103 DCCP_SKB_CB(skb
)->dccpd_seq
= dccp_hdr_seq(skb
);
1104 DCCP_SKB_CB(skb
)->dccpd_type
= dh
->dccph_type
;
1106 dccp_pr_debug("%8.8s "
1107 "src=%u.%u.%u.%u@%-5d "
1108 "dst=%u.%u.%u.%u@%-5d seq=%llu",
1109 dccp_packet_name(dh
->dccph_type
),
1110 NIPQUAD(skb
->nh
.iph
->saddr
), ntohs(dh
->dccph_sport
),
1111 NIPQUAD(skb
->nh
.iph
->daddr
), ntohs(dh
->dccph_dport
),
1112 (unsigned long long) DCCP_SKB_CB(skb
)->dccpd_seq
);
1114 if (dccp_packet_without_ack(skb
)) {
1115 DCCP_SKB_CB(skb
)->dccpd_ack_seq
= DCCP_PKT_WITHOUT_ACK_SEQ
;
1116 dccp_pr_debug_cat("\n");
1118 DCCP_SKB_CB(skb
)->dccpd_ack_seq
= dccp_hdr_ack_seq(skb
);
1119 dccp_pr_debug_cat(", ack=%llu\n",
1120 (unsigned long long)
1121 DCCP_SKB_CB(skb
)->dccpd_ack_seq
);
1125 * Look up flow ID in table and get corresponding socket */
1126 sk
= __inet_lookup(&dccp_hashinfo
,
1127 skb
->nh
.iph
->saddr
, dh
->dccph_sport
,
1128 skb
->nh
.iph
->daddr
, ntohs(dh
->dccph_dport
),
1134 * Generate Reset(No Connection) unless P.type == Reset
1135 * Drop packet and return
1138 dccp_pr_debug("failed to look up flow ID in table and "
1139 "get corresponding socket\n");
1140 goto no_dccp_socket
;
1145 * ... or S.state == TIMEWAIT,
1146 * Generate Reset(No Connection) unless P.type == Reset
1147 * Drop packet and return
1150 if (sk
->sk_state
== DCCP_TIME_WAIT
) {
1151 dccp_pr_debug("sk->sk_state == DCCP_TIME_WAIT: "
1156 if (!xfrm4_policy_check(sk
, XFRM_POLICY_IN
, skb
)) {
1157 dccp_pr_debug("xfrm4_policy_check failed\n");
1158 goto discard_and_relse
;
1161 if (sk_filter(sk
, skb
, 0)) {
1162 dccp_pr_debug("sk_filter failed\n");
1163 goto discard_and_relse
;
1170 if (!sock_owned_by_user(sk
))
1171 rc
= dccp_v4_do_rcv(sk
, skb
);
1173 sk_add_backlog(sk
, skb
);
1180 if (!xfrm4_policy_check(NULL
, XFRM_POLICY_IN
, skb
))
1184 * Generate Reset(No Connection) unless P.type == Reset
1185 * Drop packet and return
1187 if (dh
->dccph_type
!= DCCP_PKT_RESET
) {
1188 DCCP_SKB_CB(skb
)->dccpd_reset_code
=
1189 DCCP_RESET_CODE_NO_CONNECTION
;
1190 dccp_v4_ctl_send_reset(skb
);
1194 /* Discard frame. */
1203 inet_twsk_put((struct inet_timewait_sock
*)sk
);
1204 goto no_dccp_socket
;
1207 struct inet_connection_sock_af_ops dccp_ipv4_af_ops
= {
1208 .queue_xmit
= ip_queue_xmit
,
1209 .send_check
= dccp_v4_send_check
,
1210 .rebuild_header
= inet_sk_rebuild_header
,
1211 .conn_request
= dccp_v4_conn_request
,
1212 .syn_recv_sock
= dccp_v4_request_recv_sock
,
1213 .net_header_len
= sizeof(struct iphdr
),
1214 .setsockopt
= ip_setsockopt
,
1215 .getsockopt
= ip_getsockopt
,
1216 .addr2sockaddr
= inet_csk_addr2sockaddr
,
1217 .sockaddr_len
= sizeof(struct sockaddr_in
),
1220 static int dccp_v4_init_sock(struct sock
*sk
)
1222 struct dccp_sock
*dp
= dccp_sk(sk
);
1223 static int dccp_ctl_socket_init
= 1;
1225 dccp_options_init(&dp
->dccps_options
);
1226 do_gettimeofday(&dp
->dccps_epoch
);
1228 if (dp
->dccps_options
.dccpo_send_ack_vector
) {
1229 dp
->dccps_hc_rx_ackvec
= dccp_ackvec_alloc(DCCP_MAX_ACKVEC_LEN
,
1231 if (dp
->dccps_hc_rx_ackvec
== NULL
)
1236 * FIXME: We're hardcoding the CCID, and doing this at this point makes
1237 * the listening (master) sock get CCID control blocks, which is not
1238 * necessary, but for now, to not mess with the test userspace apps,
1239 * lets leave it here, later the real solution is to do this in a
1240 * setsockopt(CCIDs-I-want/accept). -acme
1242 if (likely(!dccp_ctl_socket_init
)) {
1243 dp
->dccps_hc_rx_ccid
= ccid_init(dp
->dccps_options
.dccpo_rx_ccid
,
1245 dp
->dccps_hc_tx_ccid
= ccid_init(dp
->dccps_options
.dccpo_tx_ccid
,
1247 if (dp
->dccps_hc_rx_ccid
== NULL
||
1248 dp
->dccps_hc_tx_ccid
== NULL
) {
1249 ccid_exit(dp
->dccps_hc_rx_ccid
, sk
);
1250 ccid_exit(dp
->dccps_hc_tx_ccid
, sk
);
1251 if (dp
->dccps_options
.dccpo_send_ack_vector
) {
1252 dccp_ackvec_free(dp
->dccps_hc_rx_ackvec
);
1253 dp
->dccps_hc_rx_ackvec
= NULL
;
1255 dp
->dccps_hc_rx_ccid
= dp
->dccps_hc_tx_ccid
= NULL
;
1259 dccp_ctl_socket_init
= 0;
1261 dccp_init_xmit_timers(sk
);
1262 inet_csk(sk
)->icsk_rto
= DCCP_TIMEOUT_INIT
;
1263 sk
->sk_state
= DCCP_CLOSED
;
1264 sk
->sk_write_space
= dccp_write_space
;
1265 inet_csk(sk
)->icsk_af_ops
= &dccp_ipv4_af_ops
;
1266 dp
->dccps_mss_cache
= 536;
1267 dp
->dccps_role
= DCCP_ROLE_UNDEFINED
;
1268 dp
->dccps_service
= DCCP_SERVICE_INVALID_VALUE
;
1273 static int dccp_v4_destroy_sock(struct sock
*sk
)
1275 struct dccp_sock
*dp
= dccp_sk(sk
);
1278 * DCCP doesn't use sk_write_queue, just sk_send_head
1279 * for retransmissions
1281 if (sk
->sk_send_head
!= NULL
) {
1282 kfree_skb(sk
->sk_send_head
);
1283 sk
->sk_send_head
= NULL
;
1286 /* Clean up a referenced DCCP bind bucket. */
1287 if (inet_csk(sk
)->icsk_bind_hash
!= NULL
)
1288 inet_put_port(&dccp_hashinfo
, sk
);
1290 kfree(dp
->dccps_service_list
);
1291 dp
->dccps_service_list
= NULL
;
1293 ccid_hc_rx_exit(dp
->dccps_hc_rx_ccid
, sk
);
1294 ccid_hc_tx_exit(dp
->dccps_hc_tx_ccid
, sk
);
1295 if (dp
->dccps_options
.dccpo_send_ack_vector
) {
1296 dccp_ackvec_free(dp
->dccps_hc_rx_ackvec
);
1297 dp
->dccps_hc_rx_ackvec
= NULL
;
1299 ccid_exit(dp
->dccps_hc_rx_ccid
, sk
);
1300 ccid_exit(dp
->dccps_hc_tx_ccid
, sk
);
1301 dp
->dccps_hc_rx_ccid
= dp
->dccps_hc_tx_ccid
= NULL
;
1306 static void dccp_v4_reqsk_destructor(struct request_sock
*req
)
1308 kfree(inet_rsk(req
)->opt
);
1311 static struct request_sock_ops dccp_request_sock_ops
= {
1313 .obj_size
= sizeof(struct dccp_request_sock
),
1314 .rtx_syn_ack
= dccp_v4_send_response
,
1315 .send_ack
= dccp_v4_reqsk_send_ack
,
1316 .destructor
= dccp_v4_reqsk_destructor
,
1317 .send_reset
= dccp_v4_ctl_send_reset
,
1320 struct proto dccp_v4_prot
= {
1322 .owner
= THIS_MODULE
,
1323 .close
= dccp_close
,
1324 .connect
= dccp_v4_connect
,
1325 .disconnect
= dccp_disconnect
,
1326 .ioctl
= dccp_ioctl
,
1327 .init
= dccp_v4_init_sock
,
1328 .setsockopt
= dccp_setsockopt
,
1329 .getsockopt
= dccp_getsockopt
,
1330 .sendmsg
= dccp_sendmsg
,
1331 .recvmsg
= dccp_recvmsg
,
1332 .backlog_rcv
= dccp_v4_do_rcv
,
1333 .hash
= dccp_v4_hash
,
1334 .unhash
= dccp_v4_unhash
,
1335 .accept
= inet_csk_accept
,
1336 .get_port
= dccp_v4_get_port
,
1337 .shutdown
= dccp_shutdown
,
1338 .destroy
= dccp_v4_destroy_sock
,
1339 .orphan_count
= &dccp_orphan_count
,
1340 .max_header
= MAX_DCCP_HEADER
,
1341 .obj_size
= sizeof(struct dccp_sock
),
1342 .rsk_prot
= &dccp_request_sock_ops
,
1343 .twsk_obj_size
= sizeof(struct inet_timewait_sock
),