]>
Commit | Line | Data |
---|---|---|
60c778b2 | 1 | /* SCTP kernel implementation |
1da177e4 LT |
2 | * (C) Copyright IBM Corp. 2001, 2004 |
3 | * Copyright (c) 1999-2000 Cisco, Inc. | |
4 | * Copyright (c) 1999-2001 Motorola, Inc. | |
5 | * | |
60c778b2 | 6 | * This file is part of the SCTP kernel implementation |
1da177e4 LT |
7 | * |
8 | * These functions handle output processing. | |
9 | * | |
60c778b2 | 10 | * This SCTP implementation is free software; |
1da177e4 LT |
11 | * you can redistribute it and/or modify it under the terms of |
12 | * the GNU General Public License as published by | |
13 | * the Free Software Foundation; either version 2, or (at your option) | |
14 | * any later version. | |
15 | * | |
60c778b2 | 16 | * This SCTP implementation is distributed in the hope that it |
1da177e4 LT |
17 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied |
18 | * ************************ | |
19 | * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
20 | * See the GNU General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License | |
4b2f13a2 JK |
23 | * along with GNU CC; see the file COPYING. If not, see |
24 | * <http://www.gnu.org/licenses/>. | |
1da177e4 LT |
25 | * |
26 | * Please send any bug reports or fixes you make to the | |
27 | * email address(es): | |
91705c61 | 28 | * lksctp developers <linux-sctp@vger.kernel.org> |
1da177e4 | 29 | * |
1da177e4 LT |
30 | * Written or modified by: |
31 | * La Monte H.P. Yarroll <piggy@acm.org> | |
32 | * Karl Knutson <karl@athena.chicago.il.us> | |
33 | * Jon Grimm <jgrimm@austin.ibm.com> | |
34 | * Sridhar Samudrala <sri@us.ibm.com> | |
1da177e4 LT |
35 | */ |
36 | ||
145ce502 JP |
37 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
38 | ||
1da177e4 LT |
39 | #include <linux/types.h> |
40 | #include <linux/kernel.h> | |
41 | #include <linux/wait.h> | |
42 | #include <linux/time.h> | |
43 | #include <linux/ip.h> | |
44 | #include <linux/ipv6.h> | |
45 | #include <linux/init.h> | |
5a0e3ad6 | 46 | #include <linux/slab.h> |
1da177e4 | 47 | #include <net/inet_ecn.h> |
8d2f9e81 | 48 | #include <net/ip.h> |
1da177e4 | 49 | #include <net/icmp.h> |
7c73a6fa | 50 | #include <net/net_namespace.h> |
1da177e4 | 51 | |
1da177e4 LT |
52 | #include <linux/socket.h> /* for sa_family_t */ |
53 | #include <net/sock.h> | |
54 | ||
55 | #include <net/sctp/sctp.h> | |
56 | #include <net/sctp/sm.h> | |
9ad0977f | 57 | #include <net/sctp/checksum.h> |
1da177e4 LT |
58 | |
59 | /* Forward declarations for private helpers. */ | |
ed106277 NH |
60 | static sctp_xmit_t __sctp_packet_append_chunk(struct sctp_packet *packet, |
61 | struct sctp_chunk *chunk); | |
e83963b7 | 62 | static sctp_xmit_t sctp_packet_can_append_data(struct sctp_packet *packet, |
1da177e4 | 63 | struct sctp_chunk *chunk); |
e83963b7 VY |
64 | static void sctp_packet_append_data(struct sctp_packet *packet, |
65 | struct sctp_chunk *chunk); | |
66 | static sctp_xmit_t sctp_packet_will_fit(struct sctp_packet *packet, | |
67 | struct sctp_chunk *chunk, | |
68 | u16 chunk_len); | |
1da177e4 | 69 | |
be297143 WY |
70 | static void sctp_packet_reset(struct sctp_packet *packet) |
71 | { | |
72 | packet->size = packet->overhead; | |
73 | packet->has_cookie_echo = 0; | |
74 | packet->has_sack = 0; | |
75 | packet->has_data = 0; | |
76 | packet->has_auth = 0; | |
77 | packet->ipfragok = 0; | |
78 | packet->auth = NULL; | |
79 | } | |
80 | ||
1da177e4 LT |
81 | /* Config a packet. |
82 | * This appears to be a followup set of initializations. | |
83 | */ | |
84 | struct sctp_packet *sctp_packet_config(struct sctp_packet *packet, | |
85 | __u32 vtag, int ecn_capable) | |
86 | { | |
90017acc MRL |
87 | struct sctp_transport *tp = packet->transport; |
88 | struct sctp_association *asoc = tp->asoc; | |
1da177e4 | 89 | |
bb33381d | 90 | pr_debug("%s: packet:%p vtag:0x%x\n", __func__, packet, vtag); |
1da177e4 LT |
91 | |
92 | packet->vtag = vtag; | |
1da177e4 | 93 | |
90017acc MRL |
94 | if (asoc && tp->dst) { |
95 | struct sock *sk = asoc->base.sk; | |
96 | ||
97 | rcu_read_lock(); | |
98 | if (__sk_dst_get(sk) != tp->dst) { | |
99 | dst_hold(tp->dst); | |
100 | sk_setup_caps(sk, tp->dst); | |
101 | } | |
102 | ||
103 | if (sk_can_gso(sk)) { | |
104 | struct net_device *dev = tp->dst->dev; | |
105 | ||
106 | packet->max_size = dev->gso_max_size; | |
107 | } else { | |
108 | packet->max_size = asoc->pathmtu; | |
109 | } | |
110 | rcu_read_unlock(); | |
111 | ||
112 | } else { | |
113 | packet->max_size = tp->pathmtu; | |
114 | } | |
115 | ||
1da177e4 | 116 | if (ecn_capable && sctp_packet_empty(packet)) { |
90017acc | 117 | struct sctp_chunk *chunk; |
1da177e4 LT |
118 | |
119 | /* If there a is a prepend chunk stick it on the list before | |
d808ad9a YH |
120 | * any other chunks get appended. |
121 | */ | |
90017acc | 122 | chunk = sctp_get_ecne_prepend(asoc); |
1da177e4 LT |
123 | if (chunk) |
124 | sctp_packet_append_chunk(packet, chunk); | |
125 | } | |
126 | ||
127 | return packet; | |
128 | } | |
129 | ||
130 | /* Initialize the packet structure. */ | |
131 | struct sctp_packet *sctp_packet_init(struct sctp_packet *packet, | |
132 | struct sctp_transport *transport, | |
133 | __u16 sport, __u16 dport) | |
134 | { | |
135 | struct sctp_association *asoc = transport->asoc; | |
136 | size_t overhead; | |
137 | ||
bb33381d | 138 | pr_debug("%s: packet:%p transport:%p\n", __func__, packet, transport); |
1da177e4 LT |
139 | |
140 | packet->transport = transport; | |
141 | packet->source_port = sport; | |
142 | packet->destination_port = dport; | |
79af02c2 | 143 | INIT_LIST_HEAD(&packet->chunk_list); |
1da177e4 | 144 | if (asoc) { |
d808ad9a YH |
145 | struct sctp_sock *sp = sctp_sk(asoc->base.sk); |
146 | overhead = sp->pf->af->net_header_len; | |
1da177e4 LT |
147 | } else { |
148 | overhead = sizeof(struct ipv6hdr); | |
149 | } | |
150 | overhead += sizeof(struct sctphdr); | |
151 | packet->overhead = overhead; | |
be297143 | 152 | sctp_packet_reset(packet); |
1da177e4 | 153 | packet->vtag = 0; |
3e3251b3 | 154 | |
1da177e4 LT |
155 | return packet; |
156 | } | |
157 | ||
158 | /* Free a packet. */ | |
159 | void sctp_packet_free(struct sctp_packet *packet) | |
160 | { | |
79af02c2 | 161 | struct sctp_chunk *chunk, *tmp; |
1da177e4 | 162 | |
bb33381d | 163 | pr_debug("%s: packet:%p\n", __func__, packet); |
1da177e4 | 164 | |
79af02c2 DM |
165 | list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) { |
166 | list_del_init(&chunk->list); | |
1da177e4 | 167 | sctp_chunk_free(chunk); |
79af02c2 | 168 | } |
1da177e4 LT |
169 | } |
170 | ||
171 | /* This routine tries to append the chunk to the offered packet. If adding | |
172 | * the chunk causes the packet to exceed the path MTU and COOKIE_ECHO chunk | |
173 | * is not present in the packet, it transmits the input packet. | |
174 | * Data can be bundled with a packet containing a COOKIE_ECHO chunk as long | |
175 | * as it can fit in the packet, but any more data that does not fit in this | |
176 | * packet can be sent only after receiving the COOKIE_ACK. | |
177 | */ | |
178 | sctp_xmit_t sctp_packet_transmit_chunk(struct sctp_packet *packet, | |
2e3216cd | 179 | struct sctp_chunk *chunk, |
cea8768f | 180 | int one_packet, gfp_t gfp) |
1da177e4 LT |
181 | { |
182 | sctp_xmit_t retval; | |
183 | int error = 0; | |
184 | ||
bb33381d | 185 | pr_debug("%s: packet:%p chunk:%p\n", __func__, packet, chunk); |
1da177e4 LT |
186 | |
187 | switch ((retval = (sctp_packet_append_chunk(packet, chunk)))) { | |
188 | case SCTP_XMIT_PMTU_FULL: | |
189 | if (!packet->has_cookie_echo) { | |
cea8768f | 190 | error = sctp_packet_transmit(packet, gfp); |
1da177e4 LT |
191 | if (error < 0) |
192 | chunk->skb->sk->sk_err = -error; | |
193 | ||
194 | /* If we have an empty packet, then we can NOT ever | |
195 | * return PMTU_FULL. | |
196 | */ | |
2e3216cd VY |
197 | if (!one_packet) |
198 | retval = sctp_packet_append_chunk(packet, | |
199 | chunk); | |
1da177e4 LT |
200 | } |
201 | break; | |
202 | ||
203 | case SCTP_XMIT_RWND_FULL: | |
204 | case SCTP_XMIT_OK: | |
526cbef7 | 205 | case SCTP_XMIT_DELAY: |
1da177e4 | 206 | break; |
3ff50b79 | 207 | } |
1da177e4 LT |
208 | |
209 | return retval; | |
210 | } | |
211 | ||
4cd57c80 VY |
212 | /* Try to bundle an auth chunk into the packet. */ |
213 | static sctp_xmit_t sctp_packet_bundle_auth(struct sctp_packet *pkt, | |
214 | struct sctp_chunk *chunk) | |
215 | { | |
216 | struct sctp_association *asoc = pkt->transport->asoc; | |
217 | struct sctp_chunk *auth; | |
218 | sctp_xmit_t retval = SCTP_XMIT_OK; | |
219 | ||
220 | /* if we don't have an association, we can't do authentication */ | |
221 | if (!asoc) | |
222 | return retval; | |
223 | ||
224 | /* See if this is an auth chunk we are bundling or if | |
225 | * auth is already bundled. | |
226 | */ | |
4007cc88 | 227 | if (chunk->chunk_hdr->type == SCTP_CID_AUTH || pkt->has_auth) |
4cd57c80 VY |
228 | return retval; |
229 | ||
230 | /* if the peer did not request this chunk to be authenticated, | |
231 | * don't do it | |
232 | */ | |
233 | if (!chunk->auth) | |
234 | return retval; | |
235 | ||
236 | auth = sctp_make_auth(asoc); | |
237 | if (!auth) | |
238 | return retval; | |
239 | ||
ed106277 NH |
240 | retval = __sctp_packet_append_chunk(pkt, auth); |
241 | ||
242 | if (retval != SCTP_XMIT_OK) | |
243 | sctp_chunk_free(auth); | |
4cd57c80 VY |
244 | |
245 | return retval; | |
246 | } | |
247 | ||
1da177e4 LT |
248 | /* Try to bundle a SACK with the packet. */ |
249 | static sctp_xmit_t sctp_packet_bundle_sack(struct sctp_packet *pkt, | |
250 | struct sctp_chunk *chunk) | |
251 | { | |
252 | sctp_xmit_t retval = SCTP_XMIT_OK; | |
253 | ||
254 | /* If sending DATA and haven't aleady bundled a SACK, try to | |
255 | * bundle one in to the packet. | |
256 | */ | |
257 | if (sctp_chunk_is_data(chunk) && !pkt->has_sack && | |
258 | !pkt->has_cookie_echo) { | |
259 | struct sctp_association *asoc; | |
af87b823 | 260 | struct timer_list *timer; |
1da177e4 | 261 | asoc = pkt->transport->asoc; |
af87b823 | 262 | timer = &asoc->timers[SCTP_EVENT_TIMEOUT_SACK]; |
1da177e4 | 263 | |
af87b823 DG |
264 | /* If the SACK timer is running, we have a pending SACK */ |
265 | if (timer_pending(timer)) { | |
1da177e4 | 266 | struct sctp_chunk *sack; |
4244854d NH |
267 | |
268 | if (pkt->transport->sack_generation != | |
269 | pkt->transport->asoc->peer.sack_generation) | |
270 | return retval; | |
271 | ||
1da177e4 LT |
272 | asoc->a_rwnd = asoc->rwnd; |
273 | sack = sctp_make_sack(asoc); | |
274 | if (sack) { | |
ed106277 NH |
275 | retval = __sctp_packet_append_chunk(pkt, sack); |
276 | if (retval != SCTP_XMIT_OK) { | |
277 | sctp_chunk_free(sack); | |
278 | goto out; | |
279 | } | |
1da177e4 | 280 | asoc->peer.sack_needed = 0; |
af87b823 | 281 | if (del_timer(timer)) |
1da177e4 LT |
282 | sctp_association_put(asoc); |
283 | } | |
284 | } | |
285 | } | |
ed106277 | 286 | out: |
1da177e4 LT |
287 | return retval; |
288 | } | |
289 | ||
ed106277 | 290 | |
1da177e4 LT |
291 | /* Append a chunk to the offered packet reporting back any inability to do |
292 | * so. | |
293 | */ | |
ed106277 NH |
294 | static sctp_xmit_t __sctp_packet_append_chunk(struct sctp_packet *packet, |
295 | struct sctp_chunk *chunk) | |
1da177e4 LT |
296 | { |
297 | sctp_xmit_t retval = SCTP_XMIT_OK; | |
298 | __u16 chunk_len = WORD_ROUND(ntohs(chunk->chunk_hdr->length)); | |
1da177e4 | 299 | |
e83963b7 VY |
300 | /* Check to see if this chunk will fit into the packet */ |
301 | retval = sctp_packet_will_fit(packet, chunk, chunk_len); | |
302 | if (retval != SCTP_XMIT_OK) | |
303 | goto finish; | |
1da177e4 | 304 | |
e83963b7 | 305 | /* We believe that this chunk is OK to add to the packet */ |
4cd57c80 | 306 | switch (chunk->chunk_hdr->type) { |
f7010e61 | 307 | case SCTP_CID_DATA: |
e83963b7 VY |
308 | /* Account for the data being in the packet */ |
309 | sctp_packet_append_data(packet, chunk); | |
1da177e4 LT |
310 | /* Disallow SACK bundling after DATA. */ |
311 | packet->has_sack = 1; | |
4cd57c80 VY |
312 | /* Disallow AUTH bundling after DATA */ |
313 | packet->has_auth = 1; | |
314 | /* Let it be knows that packet has DATA in it */ | |
315 | packet->has_data = 1; | |
759af00e VY |
316 | /* timestamp the chunk for rtx purposes */ |
317 | chunk->sent_at = jiffies; | |
4cd57c80 | 318 | break; |
f7010e61 | 319 | case SCTP_CID_COOKIE_ECHO: |
1da177e4 | 320 | packet->has_cookie_echo = 1; |
4cd57c80 VY |
321 | break; |
322 | ||
f7010e61 | 323 | case SCTP_CID_SACK: |
1da177e4 | 324 | packet->has_sack = 1; |
196d6759 MB |
325 | if (chunk->asoc) |
326 | chunk->asoc->stats.osacks++; | |
4cd57c80 VY |
327 | break; |
328 | ||
f7010e61 | 329 | case SCTP_CID_AUTH: |
4cd57c80 VY |
330 | packet->has_auth = 1; |
331 | packet->auth = chunk; | |
332 | break; | |
333 | } | |
1da177e4 LT |
334 | |
335 | /* It is OK to send this chunk. */ | |
79af02c2 | 336 | list_add_tail(&chunk->list, &packet->chunk_list); |
1da177e4 LT |
337 | packet->size += chunk_len; |
338 | chunk->transport = packet->transport; | |
339 | finish: | |
340 | return retval; | |
341 | } | |
342 | ||
ed106277 NH |
343 | /* Append a chunk to the offered packet reporting back any inability to do |
344 | * so. | |
345 | */ | |
346 | sctp_xmit_t sctp_packet_append_chunk(struct sctp_packet *packet, | |
347 | struct sctp_chunk *chunk) | |
348 | { | |
349 | sctp_xmit_t retval = SCTP_XMIT_OK; | |
350 | ||
bb33381d | 351 | pr_debug("%s: packet:%p chunk:%p\n", __func__, packet, chunk); |
ed106277 NH |
352 | |
353 | /* Data chunks are special. Before seeing what else we can | |
354 | * bundle into this packet, check to see if we are allowed to | |
355 | * send this DATA. | |
356 | */ | |
357 | if (sctp_chunk_is_data(chunk)) { | |
358 | retval = sctp_packet_can_append_data(packet, chunk); | |
359 | if (retval != SCTP_XMIT_OK) | |
360 | goto finish; | |
361 | } | |
362 | ||
363 | /* Try to bundle AUTH chunk */ | |
364 | retval = sctp_packet_bundle_auth(packet, chunk); | |
365 | if (retval != SCTP_XMIT_OK) | |
366 | goto finish; | |
367 | ||
368 | /* Try to bundle SACK chunk */ | |
369 | retval = sctp_packet_bundle_sack(packet, chunk); | |
370 | if (retval != SCTP_XMIT_OK) | |
371 | goto finish; | |
372 | ||
373 | retval = __sctp_packet_append_chunk(packet, chunk); | |
374 | ||
375 | finish: | |
376 | return retval; | |
377 | } | |
378 | ||
4c3a5bda TG |
379 | static void sctp_packet_release_owner(struct sk_buff *skb) |
380 | { | |
381 | sk_free(skb->sk); | |
382 | } | |
383 | ||
384 | static void sctp_packet_set_owner_w(struct sk_buff *skb, struct sock *sk) | |
385 | { | |
386 | skb_orphan(skb); | |
387 | skb->sk = sk; | |
388 | skb->destructor = sctp_packet_release_owner; | |
389 | ||
390 | /* | |
391 | * The data chunks have already been accounted for in sctp_sendmsg(), | |
392 | * therefore only reserve a single byte to keep socket around until | |
393 | * the packet has been transmitted. | |
394 | */ | |
395 | atomic_inc(&sk->sk_wmem_alloc); | |
396 | } | |
397 | ||
1da177e4 LT |
398 | /* All packets are sent to the network through this function from |
399 | * sctp_outq_tail(). | |
400 | * | |
401 | * The return value is a normal kernel error return value. | |
402 | */ | |
cea8768f | 403 | int sctp_packet_transmit(struct sctp_packet *packet, gfp_t gfp) |
1da177e4 LT |
404 | { |
405 | struct sctp_transport *tp = packet->transport; | |
406 | struct sctp_association *asoc = tp->asoc; | |
407 | struct sctphdr *sh; | |
90017acc | 408 | struct sk_buff *nskb = NULL, *head = NULL; |
79af02c2 | 409 | struct sctp_chunk *chunk, *tmp; |
1da177e4 LT |
410 | struct sock *sk; |
411 | int err = 0; | |
412 | int padding; /* How much padding do we need? */ | |
90017acc | 413 | int pkt_size; |
1da177e4 | 414 | __u8 has_data = 0; |
90017acc MRL |
415 | int gso = 0; |
416 | int pktcount = 0; | |
0438816e | 417 | struct dst_entry *dst; |
4cd57c80 | 418 | unsigned char *auth = NULL; /* pointer to auth in skb data */ |
1da177e4 | 419 | |
bb33381d | 420 | pr_debug("%s: packet:%p\n", __func__, packet); |
1da177e4 LT |
421 | |
422 | /* Do NOT generate a chunkless packet. */ | |
79af02c2 | 423 | if (list_empty(&packet->chunk_list)) |
1da177e4 LT |
424 | return err; |
425 | ||
426 | /* Set up convenience variables... */ | |
79af02c2 | 427 | chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list); |
1da177e4 LT |
428 | sk = chunk->skb->sk; |
429 | ||
90017acc MRL |
430 | /* Allocate the head skb, or main one if not in GSO */ |
431 | if (packet->size > tp->pathmtu && !packet->ipfragok) { | |
432 | if (sk_can_gso(sk)) { | |
433 | gso = 1; | |
434 | pkt_size = packet->overhead; | |
435 | } else { | |
436 | /* If this happens, we trash this packet and try | |
437 | * to build a new one, hopefully correct this | |
438 | * time. Application may notice this error. | |
439 | */ | |
440 | pr_err_once("Trying to GSO but underlying device doesn't support it."); | |
441 | goto nomem; | |
442 | } | |
443 | } else { | |
444 | pkt_size = packet->size; | |
445 | } | |
446 | head = alloc_skb(pkt_size + MAX_HEADER, gfp); | |
447 | if (!head) | |
1da177e4 | 448 | goto nomem; |
90017acc MRL |
449 | if (gso) { |
450 | NAPI_GRO_CB(head)->last = head; | |
451 | skb_shinfo(head)->gso_type = sk->sk_gso_type; | |
452 | } | |
1da177e4 LT |
453 | |
454 | /* Make sure the outbound skb has enough header room reserved. */ | |
90017acc | 455 | skb_reserve(head, packet->overhead + MAX_HEADER); |
1da177e4 LT |
456 | |
457 | /* Set the owning socket so that we know where to get the | |
458 | * destination IP address. | |
459 | */ | |
90017acc | 460 | sctp_packet_set_owner_w(head, sk); |
1da177e4 | 461 | |
e0268868 | 462 | if (!sctp_transport_dst_check(tp)) { |
503b55fd SS |
463 | sctp_transport_route(tp, NULL, sctp_sk(sk)); |
464 | if (asoc && (asoc->param_flags & SPP_PMTUD_ENABLE)) { | |
02f3d4ce | 465 | sctp_assoc_sync_pmtu(sk, asoc); |
503b55fd SS |
466 | } |
467 | } | |
adf30907 | 468 | dst = dst_clone(tp->dst); |
ff0ac74a | 469 | if (!dst) |
503b55fd | 470 | goto no_route; |
90017acc | 471 | skb_dst_set(head, dst); |
503b55fd | 472 | |
1da177e4 | 473 | /* Build the SCTP header. */ |
90017acc MRL |
474 | sh = (struct sctphdr *)skb_push(head, sizeof(struct sctphdr)); |
475 | skb_reset_transport_header(head); | |
1da177e4 LT |
476 | sh->source = htons(packet->source_port); |
477 | sh->dest = htons(packet->destination_port); | |
478 | ||
479 | /* From 6.8 Adler-32 Checksum Calculation: | |
480 | * After the packet is constructed (containing the SCTP common | |
481 | * header and one or more control or DATA chunks), the | |
482 | * transmitter shall: | |
483 | * | |
484 | * 1) Fill in the proper Verification Tag in the SCTP common | |
485 | * header and initialize the checksum field to 0's. | |
486 | */ | |
487 | sh->vtag = htonl(packet->vtag); | |
488 | sh->checksum = 0; | |
489 | ||
bb33381d DB |
490 | pr_debug("***sctp_transmit_packet***\n"); |
491 | ||
90017acc MRL |
492 | do { |
493 | /* Set up convenience variables... */ | |
494 | chunk = list_entry(packet->chunk_list.next, struct sctp_chunk, list); | |
495 | pktcount++; | |
1da177e4 | 496 | |
90017acc MRL |
497 | /* Calculate packet size, so it fits in PMTU. Leave |
498 | * other chunks for the next packets. | |
499 | */ | |
500 | if (gso) { | |
501 | pkt_size = packet->overhead; | |
502 | list_for_each_entry(chunk, &packet->chunk_list, list) { | |
503 | int padded = WORD_ROUND(chunk->skb->len); | |
504 | ||
505 | if (pkt_size + padded > tp->pathmtu) | |
506 | break; | |
507 | pkt_size += padded; | |
d8dd1578 | 508 | } |
6eabca54 | 509 | |
90017acc MRL |
510 | /* Allocate a new skb. */ |
511 | nskb = alloc_skb(pkt_size + MAX_HEADER, gfp); | |
512 | if (!nskb) | |
513 | goto nomem; | |
1da177e4 | 514 | |
90017acc MRL |
515 | /* Make sure the outbound skb has enough header |
516 | * room reserved. | |
517 | */ | |
518 | skb_reserve(nskb, packet->overhead + MAX_HEADER); | |
519 | } else { | |
520 | nskb = head; | |
521 | } | |
1da177e4 | 522 | |
90017acc MRL |
523 | /** |
524 | * 3.2 Chunk Field Descriptions | |
525 | * | |
526 | * The total length of a chunk (including Type, Length and | |
527 | * Value fields) MUST be a multiple of 4 bytes. If the length | |
528 | * of the chunk is not a multiple of 4 bytes, the sender MUST | |
529 | * pad the chunk with all zero bytes and this padding is not | |
530 | * included in the chunk length field. The sender should | |
531 | * never pad with more than 3 bytes. | |
532 | * | |
533 | * [This whole comment explains WORD_ROUND() below.] | |
4cd57c80 | 534 | */ |
4cd57c80 | 535 | |
90017acc MRL |
536 | pkt_size -= packet->overhead; |
537 | list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) { | |
538 | list_del_init(&chunk->list); | |
539 | if (sctp_chunk_is_data(chunk)) { | |
540 | /* 6.3.1 C4) When data is in flight and when allowed | |
541 | * by rule C5, a new RTT measurement MUST be made each | |
542 | * round trip. Furthermore, new RTT measurements | |
543 | * SHOULD be made no more than once per round-trip | |
544 | * for a given destination transport address. | |
545 | */ | |
546 | ||
547 | if (!chunk->resent && !tp->rto_pending) { | |
548 | chunk->rtt_in_progress = 1; | |
549 | tp->rto_pending = 1; | |
550 | } | |
551 | ||
552 | has_data = 1; | |
553 | } | |
554 | ||
555 | padding = WORD_ROUND(chunk->skb->len) - chunk->skb->len; | |
556 | if (padding) | |
557 | memset(skb_put(chunk->skb, padding), 0, padding); | |
558 | ||
559 | /* if this is the auth chunk that we are adding, | |
560 | * store pointer where it will be added and put | |
561 | * the auth into the packet. | |
562 | */ | |
563 | if (chunk == packet->auth) | |
564 | auth = skb_tail_pointer(nskb); | |
565 | ||
566 | memcpy(skb_put(nskb, chunk->skb->len), | |
503b55fd | 567 | chunk->skb->data, chunk->skb->len); |
1da177e4 | 568 | |
90017acc MRL |
569 | pr_debug("*** Chunk:%p[%s] %s 0x%x, length:%d, chunk->skb->len:%d, rtt_in_progress:%d\n", |
570 | chunk, | |
571 | sctp_cname(SCTP_ST_CHUNK(chunk->chunk_hdr->type)), | |
572 | chunk->has_tsn ? "TSN" : "No TSN", | |
573 | chunk->has_tsn ? ntohl(chunk->subh.data_hdr->tsn) : 0, | |
574 | ntohs(chunk->chunk_hdr->length), chunk->skb->len, | |
575 | chunk->rtt_in_progress); | |
576 | ||
577 | /* If this is a control chunk, this is our last | |
578 | * reference. Free data chunks after they've been | |
579 | * acknowledged or have failed. | |
580 | * Re-queue auth chunks if needed. | |
581 | */ | |
582 | pkt_size -= WORD_ROUND(chunk->skb->len); | |
1da177e4 | 583 | |
90017acc MRL |
584 | if (chunk == packet->auth && !list_empty(&packet->chunk_list)) |
585 | list_add(&chunk->list, &packet->chunk_list); | |
586 | else if (!sctp_chunk_is_data(chunk)) | |
587 | sctp_chunk_free(chunk); | |
588 | ||
589 | if (!pkt_size) | |
590 | break; | |
591 | } | |
592 | ||
593 | /* SCTP-AUTH, Section 6.2 | |
594 | * The sender MUST calculate the MAC as described in RFC2104 [2] | |
595 | * using the hash function H as described by the MAC Identifier and | |
596 | * the shared association key K based on the endpoint pair shared key | |
597 | * described by the shared key identifier. The 'data' used for the | |
598 | * computation of the AUTH-chunk is given by the AUTH chunk with its | |
599 | * HMAC field set to zero (as shown in Figure 6) followed by all | |
600 | * chunks that are placed after the AUTH chunk in the SCTP packet. | |
601 | */ | |
602 | if (auth) | |
603 | sctp_auth_calculate_hmac(asoc, nskb, | |
604 | (struct sctp_auth_chunk *)auth, | |
605 | gfp); | |
606 | ||
607 | if (!gso) | |
608 | break; | |
609 | ||
610 | if (skb_gro_receive(&head, nskb)) | |
611 | goto nomem; | |
612 | nskb = NULL; | |
613 | if (WARN_ON_ONCE(skb_shinfo(head)->gso_segs >= | |
614 | sk->sk_gso_max_segs)) | |
615 | goto nomem; | |
616 | } while (!list_empty(&packet->chunk_list)); | |
4cd57c80 VY |
617 | |
618 | /* 2) Calculate the Adler-32 checksum of the whole packet, | |
619 | * including the SCTP common header and all the | |
620 | * chunks. | |
621 | * | |
622 | * Note: Adler-32 is no longer applicable, as has been replaced | |
623 | * by CRC32-C as described in <draft-ietf-tsvwg-sctpcsum-02.txt>. | |
90017acc MRL |
624 | * |
625 | * If it's a GSO packet, it's postponed to sctp_skb_segment. | |
4cd57c80 | 626 | */ |
90017acc MRL |
627 | if (!sctp_checksum_disable || gso) { |
628 | if (!gso && (!(dst->dev->features & NETIF_F_SCTP_CRC) || | |
629 | dst_xfrm(dst) || packet->ipfragok)) { | |
630 | sh->checksum = sctp_compute_cksum(head, 0); | |
b73c43f8 | 631 | } else { |
25985edc | 632 | /* no need to seed pseudo checksum for SCTP */ |
90017acc MRL |
633 | head->ip_summed = CHECKSUM_PARTIAL; |
634 | head->csum_start = skb_transport_header(head) - head->head; | |
635 | head->csum_offset = offsetof(struct sctphdr, checksum); | |
8dc92f7e JB |
636 | } |
637 | } | |
1da177e4 | 638 | |
1da177e4 LT |
639 | /* IP layer ECN support |
640 | * From RFC 2481 | |
641 | * "The ECN-Capable Transport (ECT) bit would be set by the | |
642 | * data sender to indicate that the end-points of the | |
643 | * transport protocol are ECN-capable." | |
644 | * | |
645 | * Now setting the ECT bit all the time, as it should not cause | |
646 | * any problems protocol-wise even if our peer ignores it. | |
647 | * | |
648 | * Note: The works for IPv6 layer checks this bit too later | |
649 | * in transmission. See IP6_ECN_flow_xmit(). | |
650 | */ | |
90017acc | 651 | tp->af_specific->ecn_capable(sk); |
1da177e4 LT |
652 | |
653 | /* Set up the IP options. */ | |
654 | /* BUG: not implemented | |
655 | * For v4 this all lives somewhere in sk->sk_opt... | |
656 | */ | |
657 | ||
658 | /* Dump that on IP! */ | |
196d6759 | 659 | if (asoc) { |
90017acc | 660 | asoc->stats.opackets += pktcount; |
196d6759 MB |
661 | if (asoc->peer.last_sent_to != tp) |
662 | /* Considering the multiple CPU scenario, this is a | |
663 | * "correcter" place for last_sent_to. --xguo | |
664 | */ | |
665 | asoc->peer.last_sent_to = tp; | |
1da177e4 LT |
666 | } |
667 | ||
668 | if (has_data) { | |
669 | struct timer_list *timer; | |
670 | unsigned long timeout; | |
671 | ||
1da177e4 | 672 | /* Restart the AUTOCLOSE timer when sending data. */ |
9f70f46b NH |
673 | if (sctp_state(asoc, ESTABLISHED) && |
674 | asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) { | |
1da177e4 LT |
675 | timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE]; |
676 | timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]; | |
677 | ||
678 | if (!mod_timer(timer, jiffies + timeout)) | |
679 | sctp_association_hold(asoc); | |
680 | } | |
681 | } | |
682 | ||
90017acc MRL |
683 | pr_debug("***sctp_transmit_packet*** skb->len:%d\n", head->len); |
684 | ||
685 | if (gso) { | |
686 | /* Cleanup our debris for IP stacks */ | |
687 | memset(head->cb, 0, max(sizeof(struct inet_skb_parm), | |
688 | sizeof(struct inet6_skb_parm))); | |
1da177e4 | 689 | |
90017acc MRL |
690 | skb_shinfo(head)->gso_segs = pktcount; |
691 | skb_shinfo(head)->gso_size = GSO_BY_FRAGS; | |
692 | ||
693 | /* We have to refresh this in case we are xmiting to | |
694 | * more than one transport at a time | |
695 | */ | |
696 | rcu_read_lock(); | |
697 | if (__sk_dst_get(sk) != tp->dst) { | |
698 | dst_hold(tp->dst); | |
699 | sk_setup_caps(sk, tp->dst); | |
700 | } | |
701 | rcu_read_unlock(); | |
702 | } | |
703 | head->ignore_df = packet->ipfragok; | |
704 | tp->af_specific->sctp_xmit(head, tp); | |
1da177e4 LT |
705 | |
706 | out: | |
d521c08f | 707 | sctp_packet_reset(packet); |
1da177e4 LT |
708 | return err; |
709 | no_route: | |
90017acc MRL |
710 | kfree_skb(head); |
711 | if (nskb != head) | |
712 | kfree_skb(nskb); | |
29c4afc4 AS |
713 | |
714 | if (asoc) | |
715 | IP_INC_STATS(sock_net(asoc->base.sk), IPSTATS_MIB_OUTNOROUTES); | |
1da177e4 LT |
716 | |
717 | /* FIXME: Returning the 'err' will effect all the associations | |
718 | * associated with a socket, although only one of the paths of the | |
719 | * association is unreachable. | |
720 | * The real failure of a transport or association can be passed on | |
721 | * to the user via notifications. So setting this error may not be | |
722 | * required. | |
723 | */ | |
724 | /* err = -EHOSTUNREACH; */ | |
725 | err: | |
726 | /* Control chunks are unreliable so just drop them. DATA chunks | |
727 | * will get resent or dropped later. | |
728 | */ | |
729 | ||
79af02c2 DM |
730 | list_for_each_entry_safe(chunk, tmp, &packet->chunk_list, list) { |
731 | list_del_init(&chunk->list); | |
1da177e4 | 732 | if (!sctp_chunk_is_data(chunk)) |
d808ad9a | 733 | sctp_chunk_free(chunk); |
1da177e4 LT |
734 | } |
735 | goto out; | |
736 | nomem: | |
737 | err = -ENOMEM; | |
738 | goto err; | |
739 | } | |
740 | ||
741 | /******************************************************************** | |
742 | * 2nd Level Abstractions | |
743 | ********************************************************************/ | |
744 | ||
e83963b7 VY |
745 | /* This private function check to see if a chunk can be added */ |
746 | static sctp_xmit_t sctp_packet_can_append_data(struct sctp_packet *packet, | |
1da177e4 LT |
747 | struct sctp_chunk *chunk) |
748 | { | |
e83963b7 | 749 | size_t datasize, rwnd, inflight, flight_size; |
1da177e4 | 750 | struct sctp_transport *transport = packet->transport; |
1da177e4 | 751 | struct sctp_association *asoc = transport->asoc; |
1da177e4 LT |
752 | struct sctp_outq *q = &asoc->outqueue; |
753 | ||
754 | /* RFC 2960 6.1 Transmission of DATA Chunks | |
755 | * | |
756 | * A) At any given time, the data sender MUST NOT transmit new data to | |
757 | * any destination transport address if its peer's rwnd indicates | |
758 | * that the peer has no buffer space (i.e. rwnd is 0, see Section | |
759 | * 6.2.1). However, regardless of the value of rwnd (including if it | |
760 | * is 0), the data sender can always have one DATA chunk in flight to | |
761 | * the receiver if allowed by cwnd (see rule B below). This rule | |
762 | * allows the sender to probe for a change in rwnd that the sender | |
763 | * missed due to the SACK having been lost in transit from the data | |
764 | * receiver to the data sender. | |
765 | */ | |
766 | ||
767 | rwnd = asoc->peer.rwnd; | |
e83963b7 VY |
768 | inflight = q->outstanding_bytes; |
769 | flight_size = transport->flight_size; | |
1da177e4 LT |
770 | |
771 | datasize = sctp_data_size(chunk); | |
772 | ||
723189fa DL |
773 | if (datasize > rwnd && inflight > 0) |
774 | /* We have (at least) one data chunk in flight, | |
775 | * so we can't fall back to rule 6.1 B). | |
776 | */ | |
777 | return SCTP_XMIT_RWND_FULL; | |
1da177e4 | 778 | |
1da177e4 LT |
779 | /* RFC 2960 6.1 Transmission of DATA Chunks |
780 | * | |
781 | * B) At any given time, the sender MUST NOT transmit new data | |
782 | * to a given transport address if it has cwnd or more bytes | |
783 | * of data outstanding to that transport address. | |
784 | */ | |
785 | /* RFC 7.2.4 & the Implementers Guide 2.8. | |
786 | * | |
787 | * 3) ... | |
788 | * When a Fast Retransmit is being performed the sender SHOULD | |
789 | * ignore the value of cwnd and SHOULD NOT delay retransmission. | |
790 | */ | |
723189fa DL |
791 | if (chunk->fast_retransmit != SCTP_NEED_FRTX && |
792 | flight_size >= transport->cwnd) | |
793 | return SCTP_XMIT_RWND_FULL; | |
1da177e4 LT |
794 | |
795 | /* Nagle's algorithm to solve small-packet problem: | |
796 | * Inhibit the sending of new chunks when new outgoing data arrives | |
797 | * if any previously transmitted data on the connection remains | |
798 | * unacknowledged. | |
799 | */ | |
1da177e4 | 800 | |
723189fa DL |
801 | if (sctp_sk(asoc->base.sk)->nodelay) |
802 | /* Nagle disabled */ | |
803 | return SCTP_XMIT_OK; | |
804 | ||
805 | if (!sctp_packet_empty(packet)) | |
806 | /* Append to packet */ | |
807 | return SCTP_XMIT_OK; | |
808 | ||
809 | if (inflight == 0) | |
810 | /* Nothing unacked */ | |
811 | return SCTP_XMIT_OK; | |
812 | ||
813 | if (!sctp_state(asoc, ESTABLISHED)) | |
814 | return SCTP_XMIT_OK; | |
815 | ||
816 | /* Check whether this chunk and all the rest of pending data will fit | |
817 | * or delay in hopes of bundling a full sized packet. | |
818 | */ | |
e43569e6 MRL |
819 | if (chunk->skb->len + q->out_qlen > |
820 | transport->pathmtu - packet->overhead - sizeof(sctp_data_chunk_t) - 4) | |
723189fa DL |
821 | /* Enough data queued to fill a packet */ |
822 | return SCTP_XMIT_OK; | |
823 | ||
824 | /* Don't delay large message writes that may have been fragmented */ | |
825 | if (!chunk->msg->can_delay) | |
826 | return SCTP_XMIT_OK; | |
827 | ||
828 | /* Defer until all data acked or packet full */ | |
526cbef7 | 829 | return SCTP_XMIT_DELAY; |
e83963b7 VY |
830 | } |
831 | ||
832 | /* This private function does management things when adding DATA chunk */ | |
833 | static void sctp_packet_append_data(struct sctp_packet *packet, | |
834 | struct sctp_chunk *chunk) | |
835 | { | |
836 | struct sctp_transport *transport = packet->transport; | |
837 | size_t datasize = sctp_data_size(chunk); | |
838 | struct sctp_association *asoc = transport->asoc; | |
839 | u32 rwnd = asoc->peer.rwnd; | |
840 | ||
1da177e4 LT |
841 | /* Keep track of how many bytes are in flight over this transport. */ |
842 | transport->flight_size += datasize; | |
843 | ||
844 | /* Keep track of how many bytes are in flight to the receiver. */ | |
845 | asoc->outqueue.outstanding_bytes += datasize; | |
846 | ||
a76c0adf | 847 | /* Update our view of the receiver's rwnd. */ |
1da177e4 LT |
848 | if (datasize < rwnd) |
849 | rwnd -= datasize; | |
850 | else | |
851 | rwnd = 0; | |
852 | ||
853 | asoc->peer.rwnd = rwnd; | |
854 | /* Has been accepted for transmission. */ | |
855 | if (!asoc->peer.prsctp_capable) | |
856 | chunk->msg->can_abandon = 0; | |
d8dd1578 NH |
857 | sctp_chunk_assign_tsn(chunk); |
858 | sctp_chunk_assign_ssn(chunk); | |
e83963b7 VY |
859 | } |
860 | ||
861 | static sctp_xmit_t sctp_packet_will_fit(struct sctp_packet *packet, | |
862 | struct sctp_chunk *chunk, | |
863 | u16 chunk_len) | |
864 | { | |
90017acc | 865 | size_t psize, pmtu; |
e83963b7 VY |
866 | sctp_xmit_t retval = SCTP_XMIT_OK; |
867 | ||
868 | psize = packet->size; | |
90017acc MRL |
869 | if (packet->transport->asoc) |
870 | pmtu = packet->transport->asoc->pathmtu; | |
871 | else | |
872 | pmtu = packet->transport->pathmtu; | |
e83963b7 VY |
873 | |
874 | /* Decide if we need to fragment or resubmit later. */ | |
90017acc MRL |
875 | if (psize + chunk_len > pmtu) { |
876 | /* It's OK to fragment at IP level if any one of the following | |
e83963b7 | 877 | * is true: |
90017acc MRL |
878 | * 1. The packet is empty (meaning this chunk is greater |
879 | * the MTU) | |
880 | * 2. The packet doesn't have any data in it yet and data | |
881 | * requires authentication. | |
e83963b7 | 882 | */ |
90017acc | 883 | if (sctp_packet_empty(packet) || |
e83963b7 VY |
884 | (!packet->has_data && chunk->auth)) { |
885 | /* We no longer do re-fragmentation. | |
886 | * Just fragment at the IP layer, if we | |
887 | * actually hit this condition | |
888 | */ | |
889 | packet->ipfragok = 1; | |
90017acc | 890 | goto out; |
e83963b7 | 891 | } |
90017acc MRL |
892 | |
893 | /* It is also okay to fragment if the chunk we are | |
894 | * adding is a control chunk, but only if current packet | |
895 | * is not a GSO one otherwise it causes fragmentation of | |
896 | * a large frame. So in this case we allow the | |
897 | * fragmentation by forcing it to be in a new packet. | |
898 | */ | |
899 | if (!sctp_chunk_is_data(chunk) && packet->has_data) | |
900 | retval = SCTP_XMIT_PMTU_FULL; | |
901 | ||
902 | if (psize + chunk_len > packet->max_size) | |
903 | /* Hit GSO/PMTU limit, gotta flush */ | |
904 | retval = SCTP_XMIT_PMTU_FULL; | |
905 | ||
906 | if (!packet->transport->burst_limited && | |
907 | psize + chunk_len > (packet->transport->cwnd >> 1)) | |
908 | /* Do not allow a single GSO packet to use more | |
909 | * than half of cwnd. | |
910 | */ | |
911 | retval = SCTP_XMIT_PMTU_FULL; | |
912 | ||
913 | if (packet->transport->burst_limited && | |
914 | psize + chunk_len > (packet->transport->burst_limited >> 1)) | |
915 | /* Do not allow a single GSO packet to use more | |
916 | * than half of original cwnd. | |
917 | */ | |
918 | retval = SCTP_XMIT_PMTU_FULL; | |
919 | /* Otherwise it will fit in the GSO packet */ | |
e83963b7 | 920 | } |
1da177e4 | 921 | |
90017acc | 922 | out: |
1da177e4 LT |
923 | return retval; |
924 | } |