]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - net/dccp/output.c
[DCCP]: Initial implementation
[mirror_ubuntu-hirsute-kernel.git] / net / dccp / output.c
1 /*
2 * net/dccp/output.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/skbuff.h>
16
17 #include <net/sock.h>
18
19 #include "ccid.h"
20 #include "dccp.h"
21
22 static inline void dccp_event_ack_sent(struct sock *sk)
23 {
24 inet_csk_clear_xmit_timer(sk, ICSK_TIME_DACK);
25 }
26
27 /*
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
31 * device.
32 */
33 int dccp_transmit_skb(struct sock *sk, struct sk_buff *skb)
34 {
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);
39 struct dccp_hdr *dh;
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);
44 int err, set_ack = 1;
45 u64 ackno = dp->dccps_gsr;
46
47 /*
48 * FIXME: study DCCP_PKT_SYNC[ACK] to see what is the right thing
49 * to do here...
50 */
51 dccp_inc_seqno(&dp->dccps_gss);
52
53 dcb->dccpd_seq = dp->dccps_gss;
54 dccp_insert_options(sk, skb);
55
56 switch (dcb->dccpd_type) {
57 case DCCP_PKT_DATA:
58 set_ack = 0;
59 break;
60 case DCCP_PKT_SYNC:
61 case DCCP_PKT_SYNCACK:
62 ackno = dcb->dccpd_seq;
63 break;
64 }
65
66 skb->h.raw = skb_push(skb, dccp_header_size);
67 dh = dccp_hdr(skb);
68 /* Data packets are not cloned as they are never retransmitted */
69 if (skb_cloned(skb))
70 skb_set_owner_w(skb, sk);
71
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 */
80 dh->dccph_x = 1;
81
82 dp->dccps_awh = dp->dccps_gss;
83 dccp_hdr_set_seq(dh, dp->dccps_gss);
84 if (set_ack)
85 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), ackno);
86
87 switch (dcb->dccpd_type) {
88 case DCCP_PKT_REQUEST:
89 dccp_hdr_request(skb)->dccph_req_service = dcb->dccpd_service;
90 break;
91 case DCCP_PKT_RESET:
92 dccp_hdr_reset(skb)->dccph_reset_code = dcb->dccpd_reset_code;
93 break;
94 }
95
96 dh->dccph_checksum = dccp_v4_checksum(skb);
97
98 if (dcb->dccpd_type == DCCP_PKT_ACK ||
99 dcb->dccpd_type == DCCP_PKT_DATAACK)
100 dccp_event_ack_sent(sk);
101
102 DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
103
104 err = ip_queue_xmit(skb, 0);
105 if (err <= 0)
106 return err;
107
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.
113 */
114 return err == NET_XMIT_CN ? 0 : err;
115 }
116 return -ENOBUFS;
117 }
118
119 unsigned int dccp_sync_mss(struct sock *sk, u32 pmtu)
120 {
121 struct dccp_sock *dp = dccp_sk(sk);
122 int mss_now;
123
124 /*
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);
127 */
128 mss_now = pmtu - sizeof(struct iphdr) - sizeof(struct dccp_hdr) - sizeof(struct dccp_hdr_ext);
129
130 /* Now subtract optional transport overhead */
131 mss_now -= dp->dccps_ext_header_len;
132
133 /*
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
139 */
140
141 mss_now -= ((5 + 6 + 10 + 6 + 6 + 6 + 3) / 4) * 4;
142
143 /* And store cached results */
144 dp->dccps_pmtu_cookie = pmtu;
145 dp->dccps_mss_cache = mss_now;
146
147 return mss_now;
148 }
149
150 int dccp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
151 {
152 if (inet_sk_rebuild_header(sk) != 0)
153 return -EHOSTUNREACH; /* Routing failure or similar. */
154
155 return dccp_transmit_skb(sk, (skb_cloned(skb) ?
156 pskb_copy(skb, GFP_ATOMIC):
157 skb_clone(skb, GFP_ATOMIC)));
158 }
159
160 struct sk_buff *dccp_make_response(struct sock *sk, struct dst_entry *dst,
161 struct request_sock *req)
162 {
163 struct dccp_hdr *dh;
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 +
168 dccp_header_size, 1,
169 GFP_ATOMIC);
170 if (skb == NULL)
171 return NULL;
172
173 /* Reserve space for headers. */
174 skb_reserve(skb, MAX_HEADER + DCCP_MAX_OPT_LEN + dccp_header_size);
175
176 skb->dst = dst_clone(dst);
177 skb->csum = 0;
178
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);
182
183 skb->h.raw = skb_push(skb, dccp_header_size);
184
185 dh = dccp_hdr(skb);
186 memset(dh, 0, dccp_header_size);
187
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;
192 dh->dccph_x = 1;
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);
195
196 dh->dccph_checksum = dccp_v4_checksum(skb);
197
198 DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
199 return skb;
200 }
201
202 struct sk_buff *dccp_make_reset(struct sock *sk, struct dst_entry *dst,
203 const enum dccp_reset_codes code)
204
205 {
206 struct dccp_hdr *dh;
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 +
212 dccp_header_size, 1,
213 GFP_ATOMIC);
214 if (skb == NULL)
215 return NULL;
216
217 /* Reserve space for headers. */
218 skb_reserve(skb, MAX_HEADER + DCCP_MAX_OPT_LEN + dccp_header_size);
219
220 skb->dst = dst_clone(dst);
221 skb->csum = 0;
222
223 dccp_inc_seqno(&dp->dccps_gss);
224
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);
229
230 skb->h.raw = skb_push(skb, dccp_header_size);
231
232 dh = dccp_hdr(skb);
233 memset(dh, 0, dccp_header_size);
234
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;
239 dh->dccph_x = 1;
240 dccp_hdr_set_seq(dh, dp->dccps_gss);
241 dccp_hdr_set_ack(dccp_hdr_ack_bits(skb), dp->dccps_gsr);
242
243 dccp_hdr_reset(skb)->dccph_reset_code = code;
244
245 dh->dccph_checksum = dccp_v4_checksum(skb);
246
247 DCCP_INC_STATS(DCCP_MIB_OUTSEGS);
248 return skb;
249 }
250
251 /*
252 * Do all connect socket setups that can be done AF independent.
253 */
254 static inline void dccp_connect_init(struct sock *sk)
255 {
256 struct dst_entry *dst = __sk_dst_get(sk);
257 struct inet_connection_sock *icsk = inet_csk(sk);
258
259 sk->sk_err = 0;
260 sock_reset_flag(sk, SOCK_DONE);
261
262 dccp_sync_mss(sk, dst_mtu(dst));
263
264 /*
265 * FIXME: set dp->{dccps_swh,dccps_swl}, with
266 * something like dccp_inc_seq
267 */
268
269 icsk->icsk_retransmits = 0;
270 }
271
272 int dccp_connect(struct sock *sk)
273 {
274 struct sk_buff *skb;
275 struct inet_connection_sock *icsk = inet_csk(sk);
276
277 dccp_connect_init(sk);
278
279 skb = alloc_skb(MAX_DCCP_HEADER + 15, sk->sk_allocation);
280 if (unlikely(skb == NULL))
281 return -ENOBUFS;
282
283 /* Reserve space for headers. */
284 skb_reserve(skb, MAX_DCCP_HEADER);
285
286 DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_REQUEST;
287 /* FIXME: set service to something meaningful, coming
288 * from userspace*/
289 DCCP_SKB_CB(skb)->dccpd_service = 0;
290 skb->csum = 0;
291 skb_set_owner_w(skb, sk);
292
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);
297
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);
300 return 0;
301 }
302
303 void dccp_send_ack(struct sock *sk)
304 {
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);
308
309 if (skb == NULL) {
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);
313 return;
314 }
315
316 /* Reserve space for headers */
317 skb_reserve(skb, MAX_DCCP_HEADER);
318 skb->csum = 0;
319 DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_ACK;
320 skb_set_owner_w(skb, sk);
321 dccp_transmit_skb(sk, skb);
322 }
323 }
324
325 EXPORT_SYMBOL_GPL(dccp_send_ack);
326
327 void dccp_send_delayed_ack(struct sock *sk)
328 {
329 struct inet_connection_sock *icsk = inet_csk(sk);
330 /*
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.
334 */
335 unsigned long timeout = jiffies + 2 * HZ;
336
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,
340 * send ACK now.
341 *
342 * FIXME: check the "about to expire" part
343 */
344 if (icsk->icsk_ack.blocked) {
345 dccp_send_ack(sk);
346 return;
347 }
348
349 if (!time_before(timeout, icsk->icsk_ack.timeout))
350 timeout = icsk->icsk_ack.timeout;
351 }
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);
355 }
356
357 void dccp_send_sync(struct sock *sk, u64 seq)
358 {
359 /*
360 * We are not putting this on the write queue, so
361 * dccp_transmit_skb() will set the ownership to this
362 * sock.
363 */
364 struct sk_buff *skb = alloc_skb(MAX_DCCP_HEADER, GFP_ATOMIC);
365
366 if (skb == NULL)
367 /* FIXME: how to make sure the sync is sent? */
368 return;
369
370 /* Reserve space for headers and prepare control bits. */
371 skb_reserve(skb, MAX_DCCP_HEADER);
372 skb->csum = 0;
373 DCCP_SKB_CB(skb)->dccpd_type = DCCP_PKT_SYNC;
374 DCCP_SKB_CB(skb)->dccpd_seq = seq;
375
376 skb_set_owner_w(skb, sk);
377 dccp_transmit_skb(sk, skb);
378 }
379
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.
382 */
383 void dccp_send_close(struct sock *sk)
384 {
385 struct dccp_sock *dp = dccp_sk(sk);
386 struct sk_buff *skb;
387
388 /* Socket is locked, keep trying until memory is available. */
389 for (;;) {
390 skb = alloc_skb(sk->sk_prot->max_header, GFP_KERNEL);
391 if (skb != NULL)
392 break;
393 yield();
394 }
395
396 /* Reserve space for headers and prepare control bits. */
397 skb_reserve(skb, sk->sk_prot->max_header);
398 skb->csum = 0;
399 DCCP_SKB_CB(skb)->dccpd_type = dp->dccps_role == DCCP_ROLE_CLIENT ? DCCP_PKT_CLOSE : DCCP_PKT_CLOSEREQ;
400
401 skb_set_owner_w(skb, sk);
402 dccp_transmit_skb(sk, skb);
403
404 ccid_hc_rx_exit(dp->dccps_hc_rx_ccid, sk);
405 ccid_hc_tx_exit(dp->dccps_hc_tx_ccid, sk);
406 }