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/skbuff.h>
22 static inline void dccp_event_ack_sent(struct sock *sk)
24 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
28 * All SKB's seen here are completely headerless. It is our
29 * job to build the DCCP header, and pass the packet down to
30 * IP so it can do the same plus pass the packet off to the
33 int dccp_transmit_skb(struct sock *sk, struct sk_buff *skb)
35 if (likely(skb != NULL)) {
36 const struct inet_sock *inet = inet_sk(sk);
37 struct dccp_sock *dp = dccp_sk(sk);
38 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
40 /* XXX For now we're using only 48 bits sequence numbers */
41 const int dccp_header_size = sizeof(*dh) +
42 sizeof(struct dccp_hdr_ext) +
43 dccp_packet_hdr_len(dcb->dccpd_type);
45 u64 ackno = dp->dccps_gsr;
48 * FIXME: study DCCP_PKT_SYNC[ACK] to see what is the right thing
51 dccp_inc_seqno(&dp->dccps_gss);
53 dcb->dccpd_seq = dp->dccps_gss;
54 dccp_insert_options(sk, skb);
56 switch (dcb->dccpd_type) {
61 case DCCP_PKT_SYNCACK:
62 ackno = dcb->dccpd_seq;
66 skb->h.raw = skb_push(skb, dccp_header_size);
68 /* Data packets are not cloned as they are never retransmitted */
70 skb_set_owner_w(skb, sk);
72 /* Build DCCP header and checksum it. */
73 memset(dh, 0, dccp_header_size);
74 dh->dccph_type = dcb->dccpd_type;
75 dh->dccph_sport = inet->sport;
76 dh->dccph_dport = inet->dport;
77 dh->dccph_doff = (dccp_header_size + dcb->dccpd_opt_len) / 4;
78 dh->dccph_ccval = dcb->dccpd_ccval;
79 /* XXX For now we're using only 48 bits sequence numbers */
82 dp->dccps_awh = dp->dccps_gss;
83 dccp_hdr_set_seq(dh, dp->dccps_gss);
85 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), ackno);
87 switch (dcb->dccpd_type) {
88 case DCCP_PKT_REQUEST:
89 dccp_hdr_request(skb)->dccph_req_service = dcb->dccpd_service;
92 dccp_hdr_reset(skb)->dccph_reset_code = dcb->dccpd_reset_code;
96 dh->dccph_checksum = dccp_v4_checksum(skb, inet->saddr,
99 if (dcb->dccpd_type == DCCP_PKT_ACK ||
100 dcb->dccpd_type == DCCP_PKT_DATAACK)
101 dccp_event_ack_sent(sk);
103 DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
105 err = ip_queue_xmit(skb, 0);
109 /* NET_XMIT_CN is special. It does not guarantee,
110 * that this packet is lost. It tells that device
111 * is about to start to drop packets or already
112 * drops some packets of the same priority and
113 * invokes us to send less aggressively.
115 return err == NET_XMIT_CN ? 0 : err;
120 unsigned int dccp_sync_mss(struct sock *sk, u32 pmtu)
122 struct dccp_sock *dp = dccp_sk(sk);
126 * FIXME: we really should be using the af_specific thing to support IPv6.
127 * mss_now = pmtu - tp->af_specific->net_header_len - sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext);
129 mss_now = pmtu - sizeof(struct iphdr) - sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext);
131 /* Now subtract optional transport overhead */
132 mss_now -= dp->dccps_ext_header_len;
135 * FIXME: this should come from the CCID infrastructure, where, say,
136 * TFRC will say it wants TIMESTAMPS, ELAPSED time, etc, for now lets
137 * put a rough estimate for NDP + TIMESTAMP + TIMESTAMP_ECHO + ELAPSED
138 * TIME + TFRC_OPT_LOSS_EVENT_RATE + TFRC_OPT_RECEIVE_RATE + padding to
139 * make it a multiple of 4
142 mss_now -= ((5 + 6 + 10 + 6 + 6 + 6 + 3) / 4) * 4;
144 /* And store cached results */
145 dp->dccps_pmtu_cookie = pmtu;
146 dp->dccps_mss_cache = mss_now;
151 int dccp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
153 if (inet_sk_rebuild_header(sk) != 0)
154 return -EHOSTUNREACH; /* Routing failure or similar. */
156 return dccp_transmit_skb(sk, (skb_cloned(skb) ?
157 pskb_copy(skb, GFP_ATOMIC):
158 skb_clone(skb, GFP_ATOMIC)));
161 struct sk_buff *dccp_make_response(struct sock *sk, struct dst_entry *dst,
162 struct request_sock *req)
165 const int dccp_header_size = sizeof(struct dccp_hdr) +
166 sizeof(struct dccp_hdr_ext) +
167 sizeof(struct dccp_hdr_response);
168 struct sk_buff *skb = sock_wmalloc(sk, MAX_HEADER + DCCP_MAX_OPT_LEN +
174 /* Reserve space for headers. */
175 skb_reserve(skb, MAX_HEADER + DCCP_MAX_OPT_LEN + dccp_header_size);
177 skb->dst = dst_clone(dst);
180 DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_RESPONSE;
181 DCCP_SKB_CB(skb)->dccpd_seq = dccp_rsk(req)->dreq_iss;
182 dccp_insert_options(sk, skb);
184 skb->h.raw = skb_push(skb, dccp_header_size);
187 memset(dh, 0, dccp_header_size);
189 dh->dccph_sport = inet_sk(sk)->sport;
190 dh->dccph_dport = inet_rsk(req)->rmt_port;
191 dh->dccph_doff = (dccp_header_size + DCCP_SKB_CB(skb)->dccpd_opt_len) / 4;
192 dh->dccph_type = DCCP_PKT_RESPONSE;
194 dccp_hdr_set_seq(dh, dccp_rsk(req)->dreq_iss);
195 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), dccp_rsk(req)->dreq_isr);
197 dh->dccph_checksum = dccp_v4_checksum(skb, inet_rsk(req)->loc_addr,
198 inet_rsk(req)->rmt_addr);
200 DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
204 struct sk_buff *dccp_make_reset(struct sock *sk, struct dst_entry *dst,
205 const enum dccp_reset_codes code)
209 struct dccp_sock *dp = dccp_sk(sk);
210 const int dccp_header_size = sizeof(struct dccp_hdr) +
211 sizeof(struct dccp_hdr_ext) +
212 sizeof(struct dccp_hdr_reset);
213 struct sk_buff *skb = sock_wmalloc(sk, MAX_HEADER + DCCP_MAX_OPT_LEN +
219 /* Reserve space for headers. */
220 skb_reserve(skb, MAX_HEADER + DCCP_MAX_OPT_LEN + dccp_header_size);
222 skb->dst = dst_clone(dst);
225 dccp_inc_seqno(&dp->dccps_gss);
227 DCCP_SKB_CB(skb)->dccpd_reset_code = code;
228 DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_RESET;
229 DCCP_SKB_CB(skb)->dccpd_seq = dp->dccps_gss;
230 dccp_insert_options(sk, skb);
232 skb->h.raw = skb_push(skb, dccp_header_size);
235 memset(dh, 0, dccp_header_size);
237 dh->dccph_sport = inet_sk(sk)->sport;
238 dh->dccph_dport = inet_sk(sk)->dport;
239 dh->dccph_doff = (dccp_header_size + DCCP_SKB_CB(skb)->dccpd_opt_len) / 4;
240 dh->dccph_type = DCCP_PKT_RESET;
242 dccp_hdr_set_seq(dh, dp->dccps_gss);
243 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), dp->dccps_gsr);
245 dccp_hdr_reset(skb)->dccph_reset_code = code;
247 dh->dccph_checksum = dccp_v4_checksum(skb, inet_sk(sk)->saddr,
250 DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
255 * Do all connect socket setups that can be done AF independent.
257 static inline void dccp_connect_init(struct sock *sk)
259 struct dst_entry *dst = __sk_dst_get(sk);
260 struct inet_connection_sock *icsk = inet_csk(sk);
263 sock_reset_flag(sk, SOCK_DONE);
265 dccp_sync_mss(sk, dst_mtu(dst));
268 * FIXME: set dp->{dccps_swh,dccps_swl}, with
269 * something like dccp_inc_seq
272 icsk->icsk_retransmits = 0;
275 int dccp_connect(struct sock *sk)
278 struct inet_connection_sock *icsk = inet_csk(sk);
280 dccp_connect_init(sk);
282 skb = alloc_skb(MAX_DCCP_HEADER + 15, sk->sk_allocation);
283 if (unlikely(skb == NULL))
286 /* Reserve space for headers. */
287 skb_reserve(skb, MAX_DCCP_HEADER);
289 DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_REQUEST;
290 /* FIXME: set service to something meaningful, coming
292 DCCP_SKB_CB(skb)->dccpd_service = 0;
294 skb_set_owner_w(skb, sk);
296 BUG_TRAP(sk->sk_send_head == NULL);
297 sk->sk_send_head = skb;
298 dccp_transmit_skb(sk, skb_clone(skb, GFP_KERNEL));
299 DCCP_INC_STATS(DCCP_MIB_ACTIVEOPENS);
301 /* Timer for repeating the REQUEST until an answer. */
302 inet_csk_reset_xmit_timer(sk, ICSK_TIME_RETRANS, icsk->icsk_rto, TCP_RTO_MAX);
306 void dccp_send_ack(struct sock *sk)
308 /* If we have been reset, we may not send again. */
309 if (sk->sk_state != DCCP_CLOSED) {
310 struct sk_buff *skb = alloc_skb(MAX_DCCP_HEADER, GFP_ATOMIC);
313 inet_csk_schedule_ack(sk);
314 inet_csk(sk)->icsk_ack.ato = TCP_ATO_MIN;
315 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, TCP_DELACK_MAX, TCP_RTO_MAX);
319 /* Reserve space for headers */
320 skb_reserve(skb, MAX_DCCP_HEADER);
322 DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_ACK;
323 skb_set_owner_w(skb, sk);
324 dccp_transmit_skb(sk, skb);
328 EXPORT_SYMBOL_GPL(dccp_send_ack);
330 void dccp_send_delayed_ack(struct sock *sk)
332 struct inet_connection_sock *icsk = inet_csk(sk);
334 * FIXME: tune this timer. elapsed time fixes the skew, so no problem
335 * with using 2s, and active senders also piggyback the ACK into a
336 * DATAACK packet, so this is really for quiescent senders.
338 unsigned long timeout = jiffies + 2 * HZ;
340 /* Use new timeout only if there wasn't a older one earlier. */
341 if (icsk->icsk_ack.pending & ICSK_ACK_TIMER) {
342 /* If delack timer was blocked or is about to expire,
345 * FIXME: check the "about to expire" part
347 if (icsk->icsk_ack.blocked) {
352 if (!time_before(timeout, icsk->icsk_ack.timeout))
353 timeout = icsk->icsk_ack.timeout;
355 icsk->icsk_ack.pending |= ICSK_ACK_SCHED | ICSK_ACK_TIMER;
356 icsk->icsk_ack.timeout = timeout;
357 sk_reset_timer(sk, &icsk->icsk_delack_timer, timeout);
360 void dccp_send_sync(struct sock *sk, u64 seq)
363 * We are not putting this on the write queue, so
364 * dccp_transmit_skb() will set the ownership to this
367 struct sk_buff *skb = alloc_skb(MAX_DCCP_HEADER, GFP_ATOMIC);
370 /* FIXME: how to make sure the sync is sent? */
373 /* Reserve space for headers and prepare control bits. */
374 skb_reserve(skb, MAX_DCCP_HEADER);
376 DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_SYNC;
377 DCCP_SKB_CB(skb)->dccpd_seq = seq;
379 skb_set_owner_w(skb, sk);
380 dccp_transmit_skb(sk, skb);
383 /* Send a DCCP_PKT_CLOSE/CLOSEREQ. The caller locks the socket for us. This cannot be
384 * allowed to fail queueing a DCCP_PKT_CLOSE/CLOSEREQ frame under any circumstances.
386 void dccp_send_close(struct sock *sk)
388 struct dccp_sock *dp = dccp_sk(sk);
391 /* Socket is locked, keep trying until memory is available. */
393 skb = alloc_skb(sk->sk_prot->max_header, GFP_KERNEL);
399 /* Reserve space for headers and prepare control bits. */
400 skb_reserve(skb, sk->sk_prot->max_header);
402 DCCP_SKB_CB(skb)->dccpd_type = dp->dccps_role == DCCP_ROLE_CLIENT ? DCCP_PKT_CLOSE : DCCP_PKT_CLOSEREQ;
404 skb_set_owner_w(skb, sk);
405 dccp_transmit_skb(sk, skb);
407 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
408 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);