]>
git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - net/dccp/output.c
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
);
98 if (dcb
->dccpd_type
== DCCP_PKT_ACK
||
99 dcb
->dccpd_type
== DCCP_PKT_DATAACK
)
100 dccp_event_ack_sent(sk
);
102 DCCP_INC_STATS(DCCP_MIB_OUTSEGS
);
104 err
= ip_queue_xmit(skb
, 0);
108 /* NET_XMIT_CN is special. It does not guarantee,
109 * that this packet is lost. It tells that device
110 * is about to start to drop packets or already
111 * drops some packets of the same priority and
112 * invokes us to send less aggressively.
114 return err
== NET_XMIT_CN
? 0 : err
;
119 unsigned int dccp_sync_mss(struct sock
*sk
, u32 pmtu
)
121 struct dccp_sock
*dp
= dccp_sk(sk
);
125 * FIXME: we really should be using the af_specific thing to support IPv6.
126 * mss_now = pmtu - tp->af_specific->net_header_len - sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext);
128 mss_now
= pmtu
- sizeof(struct iphdr
) - sizeof(struct dccp_hdr
) - sizeof(struct dccp_hdr_ext
);
130 /* Now subtract optional transport overhead */
131 mss_now
-= dp
->dccps_ext_header_len
;
134 * FIXME: this should come from the CCID infrastructure, where, say,
135 * TFRC will say it wants TIMESTAMPS, ELAPSED time, etc, for now lets
136 * put a rough estimate for NDP + TIMESTAMP + TIMESTAMP_ECHO + ELAPSED
137 * TIME + TFRC_OPT_LOSS_EVENT_RATE + TFRC_OPT_RECEIVE_RATE + padding to
138 * make it a multiple of 4
141 mss_now
-= ((5 + 6 + 10 + 6 + 6 + 6 + 3) / 4) * 4;
143 /* And store cached results */
144 dp
->dccps_pmtu_cookie
= pmtu
;
145 dp
->dccps_mss_cache
= mss_now
;
150 int dccp_retransmit_skb(struct sock
*sk
, struct sk_buff
*skb
)
152 if (inet_sk_rebuild_header(sk
) != 0)
153 return -EHOSTUNREACH
; /* Routing failure or similar. */
155 return dccp_transmit_skb(sk
, (skb_cloned(skb
) ?
156 pskb_copy(skb
, GFP_ATOMIC
):
157 skb_clone(skb
, GFP_ATOMIC
)));
160 struct sk_buff
*dccp_make_response(struct sock
*sk
, struct dst_entry
*dst
,
161 struct request_sock
*req
)
164 const int dccp_header_size
= sizeof(struct dccp_hdr
) +
165 sizeof(struct dccp_hdr_ext
) +
166 sizeof(struct dccp_hdr_response
);
167 struct sk_buff
*skb
= sock_wmalloc(sk
, MAX_HEADER
+ DCCP_MAX_OPT_LEN
+
173 /* Reserve space for headers. */
174 skb_reserve(skb
, MAX_HEADER
+ DCCP_MAX_OPT_LEN
+ dccp_header_size
);
176 skb
->dst
= dst_clone(dst
);
179 DCCP_SKB_CB(skb
)->dccpd_type
= DCCP_PKT_RESPONSE
;
180 DCCP_SKB_CB(skb
)->dccpd_seq
= dccp_rsk(req
)->dreq_iss
;
181 dccp_insert_options(sk
, skb
);
183 skb
->h
.raw
= skb_push(skb
, dccp_header_size
);
186 memset(dh
, 0, dccp_header_size
);
188 dh
->dccph_sport
= inet_sk(sk
)->sport
;
189 dh
->dccph_dport
= inet_rsk(req
)->rmt_port
;
190 dh
->dccph_doff
= (dccp_header_size
+ DCCP_SKB_CB(skb
)->dccpd_opt_len
) / 4;
191 dh
->dccph_type
= DCCP_PKT_RESPONSE
;
193 dccp_hdr_set_seq(dh
, dccp_rsk(req
)->dreq_iss
);
194 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb
), dccp_rsk(req
)->dreq_isr
);
196 dh
->dccph_checksum
= dccp_v4_checksum(skb
);
198 DCCP_INC_STATS(DCCP_MIB_OUTSEGS
);
202 struct sk_buff
*dccp_make_reset(struct sock
*sk
, struct dst_entry
*dst
,
203 const enum dccp_reset_codes code
)
207 struct dccp_sock
*dp
= dccp_sk(sk
);
208 const int dccp_header_size
= sizeof(struct dccp_hdr
) +
209 sizeof(struct dccp_hdr_ext
) +
210 sizeof(struct dccp_hdr_reset
);
211 struct sk_buff
*skb
= sock_wmalloc(sk
, MAX_HEADER
+ DCCP_MAX_OPT_LEN
+
217 /* Reserve space for headers. */
218 skb_reserve(skb
, MAX_HEADER
+ DCCP_MAX_OPT_LEN
+ dccp_header_size
);
220 skb
->dst
= dst_clone(dst
);
223 dccp_inc_seqno(&dp
->dccps_gss
);
225 DCCP_SKB_CB(skb
)->dccpd_reset_code
= code
;
226 DCCP_SKB_CB(skb
)->dccpd_type
= DCCP_PKT_RESET
;
227 DCCP_SKB_CB(skb
)->dccpd_seq
= dp
->dccps_gss
;
228 dccp_insert_options(sk
, skb
);
230 skb
->h
.raw
= skb_push(skb
, dccp_header_size
);
233 memset(dh
, 0, dccp_header_size
);
235 dh
->dccph_sport
= inet_sk(sk
)->sport
;
236 dh
->dccph_dport
= inet_sk(sk
)->dport
;
237 dh
->dccph_doff
= (dccp_header_size
+ DCCP_SKB_CB(skb
)->dccpd_opt_len
) / 4;
238 dh
->dccph_type
= DCCP_PKT_RESET
;
240 dccp_hdr_set_seq(dh
, dp
->dccps_gss
);
241 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb
), dp
->dccps_gsr
);
243 dccp_hdr_reset(skb
)->dccph_reset_code
= code
;
245 dh
->dccph_checksum
= dccp_v4_checksum(skb
);
247 DCCP_INC_STATS(DCCP_MIB_OUTSEGS
);
252 * Do all connect socket setups that can be done AF independent.
254 static inline void dccp_connect_init(struct sock
*sk
)
256 struct dst_entry
*dst
= __sk_dst_get(sk
);
257 struct inet_connection_sock
*icsk
= inet_csk(sk
);
260 sock_reset_flag(sk
, SOCK_DONE
);
262 dccp_sync_mss(sk
, dst_mtu(dst
));
265 * FIXME: set dp->{dccps_swh,dccps_swl}, with
266 * something like dccp_inc_seq
269 icsk
->icsk_retransmits
= 0;
272 int dccp_connect(struct sock
*sk
)
275 struct inet_connection_sock
*icsk
= inet_csk(sk
);
277 dccp_connect_init(sk
);
279 skb
= alloc_skb(MAX_DCCP_HEADER
+ 15, sk
->sk_allocation
);
280 if (unlikely(skb
== NULL
))
283 /* Reserve space for headers. */
284 skb_reserve(skb
, MAX_DCCP_HEADER
);
286 DCCP_SKB_CB(skb
)->dccpd_type
= DCCP_PKT_REQUEST
;
287 /* FIXME: set service to something meaningful, coming
289 DCCP_SKB_CB(skb
)->dccpd_service
= 0;
291 skb_set_owner_w(skb
, sk
);
293 BUG_TRAP(sk
->sk_send_head
== NULL
);
294 sk
->sk_send_head
= skb
;
295 dccp_transmit_skb(sk
, skb_clone(skb
, GFP_KERNEL
));
296 DCCP_INC_STATS(DCCP_MIB_ACTIVEOPENS
);
298 /* Timer for repeating the REQUEST until an answer. */
299 inet_csk_reset_xmit_timer(sk
, ICSK_TIME_RETRANS
, icsk
->icsk_rto
, TCP_RTO_MAX
);
303 void dccp_send_ack(struct sock
*sk
)
305 /* If we have been reset, we may not send again. */
306 if (sk
->sk_state
!= DCCP_CLOSED
) {
307 struct sk_buff
*skb
= alloc_skb(MAX_DCCP_HEADER
, GFP_ATOMIC
);
310 inet_csk_schedule_ack(sk
);
311 inet_csk(sk
)->icsk_ack
.ato
= TCP_ATO_MIN
;
312 inet_csk_reset_xmit_timer(sk
, ICSK_TIME_DACK
, TCP_DELACK_MAX
, TCP_RTO_MAX
);
316 /* Reserve space for headers */
317 skb_reserve(skb
, MAX_DCCP_HEADER
);
319 DCCP_SKB_CB(skb
)->dccpd_type
= DCCP_PKT_ACK
;
320 skb_set_owner_w(skb
, sk
);
321 dccp_transmit_skb(sk
, skb
);
325 EXPORT_SYMBOL_GPL(dccp_send_ack
);
327 void dccp_send_delayed_ack(struct sock
*sk
)
329 struct inet_connection_sock
*icsk
= inet_csk(sk
);
331 * FIXME: tune this timer. elapsed time fixes the skew, so no problem
332 * with using 2s, and active senders also piggyback the ACK into a
333 * DATAACK packet, so this is really for quiescent senders.
335 unsigned long timeout
= jiffies
+ 2 * HZ
;
337 /* Use new timeout only if there wasn't a older one earlier. */
338 if (icsk
->icsk_ack
.pending
& ICSK_ACK_TIMER
) {
339 /* If delack timer was blocked or is about to expire,
342 * FIXME: check the "about to expire" part
344 if (icsk
->icsk_ack
.blocked
) {
349 if (!time_before(timeout
, icsk
->icsk_ack
.timeout
))
350 timeout
= icsk
->icsk_ack
.timeout
;
352 icsk
->icsk_ack
.pending
|= ICSK_ACK_SCHED
| ICSK_ACK_TIMER
;
353 icsk
->icsk_ack
.timeout
= timeout
;
354 sk_reset_timer(sk
, &icsk
->icsk_delack_timer
, timeout
);
357 void dccp_send_sync(struct sock
*sk
, u64 seq
)
360 * We are not putting this on the write queue, so
361 * dccp_transmit_skb() will set the ownership to this
364 struct sk_buff
*skb
= alloc_skb(MAX_DCCP_HEADER
, GFP_ATOMIC
);
367 /* FIXME: how to make sure the sync is sent? */
370 /* Reserve space for headers and prepare control bits. */
371 skb_reserve(skb
, MAX_DCCP_HEADER
);
373 DCCP_SKB_CB(skb
)->dccpd_type
= DCCP_PKT_SYNC
;
374 DCCP_SKB_CB(skb
)->dccpd_seq
= seq
;
376 skb_set_owner_w(skb
, sk
);
377 dccp_transmit_skb(sk
, skb
);
380 /* Send a DCCP_PKT_CLOSE/CLOSEREQ. The caller locks the socket for us. This cannot be
381 * allowed to fail queueing a DCCP_PKT_CLOSE/CLOSEREQ frame under any circumstances.
383 void dccp_send_close(struct sock
*sk
)
385 struct dccp_sock
*dp
= dccp_sk(sk
);
388 /* Socket is locked, keep trying until memory is available. */
390 skb
= alloc_skb(sk
->sk_prot
->max_header
, GFP_KERNEL
);
396 /* Reserve space for headers and prepare control bits. */
397 skb_reserve(skb
, sk
->sk_prot
->max_header
);
399 DCCP_SKB_CB(skb
)->dccpd_type
= dp
->dccps_role
== DCCP_ROLE_CLIENT
? DCCP_PKT_CLOSE
: DCCP_PKT_CLOSEREQ
;
401 skb_set_owner_w(skb
, sk
);
402 dccp_transmit_skb(sk
, skb
);
404 ccid_hc_rx_exit(dp
->dccps_hc_rx_ccid
, sk
);
405 ccid_hc_tx_exit(dp
->dccps_hc_tx_ccid
, sk
);