Merge with Linux 2.5.74.
[linux-2.6/linux-mips.git] / net / sctp / sm_make_chunk.c
blob2408465dda8a5009002701d6cd1400dd371e7d23
1 /* SCTP kernel reference Implementation
2 * Copyright (c) 1999-2000 Cisco, Inc.
3 * Copyright (c) 1999-2001 Motorola, Inc.
4 * Copyright (c) 2001-2002 Intel Corp.
5 * Copyright (c) 2001-2002 International Business Machines Corp.
7 * This file is part of the SCTP kernel reference Implementation
9 * This file includes part of the implementation of the add-IP extension,
10 * based on <draft-ietf-tsvwg-addip-sctp-02.txt> June 29, 2001,
11 * for the SCTP kernel reference Implementation.
13 * These functions work with the state functions in sctp_sm_statefuns.c
14 * to implement the state operations. These functions implement the
15 * steps which require modifying existing data structures.
17 * The SCTP reference implementation is free software;
18 * you can redistribute it and/or modify it under the terms of
19 * the GNU General Public License as published by
20 * the Free Software Foundation; either version 2, or (at your option)
21 * any later version.
23 * The SCTP reference implementation is distributed in the hope that it
24 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
25 * ************************
26 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 * See the GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with GNU CC; see the file COPYING. If not, write to
31 * the Free Software Foundation, 59 Temple Place - Suite 330,
32 * Boston, MA 02111-1307, USA.
34 * Please send any bug reports or fixes you make to the
35 * email address(es):
36 * lksctp developers <lksctp-developers@lists.sourceforge.net>
38 * Or submit a bug report through the following website:
39 * http://www.sf.net/projects/lksctp
41 * Written or modified by:
42 * La Monte H.P. Yarroll <piggy@acm.org>
43 * Karl Knutson <karl@athena.chicago.il.us>
44 * C. Robin <chris@hundredacre.ac.uk>
45 * Jon Grimm <jgrimm@us.ibm.com>
46 * Xingang Guo <xingang.guo@intel.com>
47 * Dajiang Zhang <dajiang.zhang@nokia.com>
48 * Sridhar Samudrala <sri@us.ibm.com>
49 * Daisy Chang <daisyc@us.ibm.com>
50 * Ardelle Fan <ardelle.fan@intel.com>
52 * Any bugs reported given to us we will try to fix... any fixes shared will
53 * be incorporated into the next SCTP release.
56 #include <linux/types.h>
57 #include <linux/kernel.h>
58 #include <linux/ip.h>
59 #include <linux/ipv6.h>
60 #include <linux/net.h>
61 #include <linux/inet.h>
62 #include <asm/scatterlist.h>
63 #include <linux/crypto.h>
64 #include <net/sock.h>
66 #include <linux/skbuff.h>
67 #include <linux/random.h> /* for get_random_bytes */
68 #include <net/sctp/sctp.h>
69 #include <net/sctp/sm.h>
71 extern kmem_cache_t *sctp_chunk_cachep;
73 /* What was the inbound interface for this chunk? */
74 int sctp_chunk_iif(const struct sctp_chunk *chunk)
76 struct sctp_af *af;
77 int iif = 0;
79 af = sctp_get_af_specific(ipver2af(chunk->skb->nh.iph->version));
80 if (af)
81 iif = af->skb_iif(chunk->skb);
83 return iif;
86 /* RFC 2960 3.3.2 Initiation (INIT) (1)
88 * Note 2: The ECN capable field is reserved for future use of
89 * Explicit Congestion Notification.
91 static const sctp_ecn_capable_param_t ecap_param = {
93 SCTP_PARAM_ECN_CAPABLE,
94 __constant_htons(sizeof(sctp_ecn_capable_param_t)),
98 /* A helper to initialize to initialize an op error inside a
99 * provided chunk, as most cause codes will be embedded inside an
100 * abort chunk.
102 void sctp_init_cause(struct sctp_chunk *chunk, __u16 cause_code,
103 const void *payload, size_t paylen)
105 sctp_errhdr_t err;
106 int padlen;
107 __u16 len;
109 /* Cause code constants are now defined in network order. */
110 err.cause = cause_code;
111 len = sizeof(sctp_errhdr_t) + paylen;
112 padlen = len % 4;
113 err.length = htons(len);
114 len += padlen;
115 sctp_addto_chunk(chunk, sizeof(sctp_errhdr_t), &err);
116 chunk->subh.err_hdr = sctp_addto_chunk(chunk, paylen, payload);
119 /* 3.3.2 Initiation (INIT) (1)
121 * This chunk is used to initiate a SCTP association between two
122 * endpoints. The format of the INIT chunk is shown below:
124 * 0 1 2 3
125 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
126 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
127 * | Type = 1 | Chunk Flags | Chunk Length |
128 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
129 * | Initiate Tag |
130 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
131 * | Advertised Receiver Window Credit (a_rwnd) |
132 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
133 * | Number of Outbound Streams | Number of Inbound Streams |
134 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135 * | Initial TSN |
136 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 * \ \
138 * / Optional/Variable-Length Parameters /
139 * \ \
140 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143 * The INIT chunk contains the following parameters. Unless otherwise
144 * noted, each parameter MUST only be included once in the INIT chunk.
146 * Fixed Parameters Status
147 * ----------------------------------------------
148 * Initiate Tag Mandatory
149 * Advertised Receiver Window Credit Mandatory
150 * Number of Outbound Streams Mandatory
151 * Number of Inbound Streams Mandatory
152 * Initial TSN Mandatory
154 * Variable Parameters Status Type Value
155 * -------------------------------------------------------------
156 * IPv4 Address (Note 1) Optional 5
157 * IPv6 Address (Note 1) Optional 6
158 * Cookie Preservative Optional 9
159 * Reserved for ECN Capable (Note 2) Optional 32768 (0x8000)
160 * Host Name Address (Note 3) Optional 11
161 * Supported Address Types (Note 4) Optional 12
163 struct sctp_chunk *sctp_make_init(const struct sctp_association *asoc,
164 const struct sctp_bind_addr *bp,
165 int gfp, int vparam_len)
167 sctp_inithdr_t init;
168 union sctp_params addrs;
169 size_t chunksize;
170 struct sctp_chunk *retval = NULL;
171 int num_types, addrs_len = 0;
172 struct sctp_opt *sp;
173 sctp_supported_addrs_param_t sat;
174 __u16 types[2];
176 /* RFC 2960 3.3.2 Initiation (INIT) (1)
178 * Note 1: The INIT chunks can contain multiple addresses that
179 * can be IPv4 and/or IPv6 in any combination.
181 retval = NULL;
183 /* Convert the provided bind address list to raw format. */
184 addrs = sctp_bind_addrs_to_raw(bp, &addrs_len, gfp);
186 init.init_tag = htonl(asoc->c.my_vtag);
187 init.a_rwnd = htonl(asoc->rwnd);
188 init.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
189 init.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
190 init.initial_tsn = htonl(asoc->c.initial_tsn);
192 /* How many address types are needed? */
193 sp = sctp_sk(asoc->base.sk);
194 num_types = sp->pf->supported_addrs(sp, types);
196 chunksize = sizeof(init) + addrs_len + SCTP_SAT_LEN(num_types);
197 chunksize += sizeof(ecap_param);
198 chunksize += vparam_len;
200 /* RFC 2960 3.3.2 Initiation (INIT) (1)
202 * Note 3: An INIT chunk MUST NOT contain more than one Host
203 * Name address parameter. Moreover, the sender of the INIT
204 * MUST NOT combine any other address types with the Host Name
205 * address in the INIT. The receiver of INIT MUST ignore any
206 * other address types if the Host Name address parameter is
207 * present in the received INIT chunk.
209 * PLEASE DO NOT FIXME [This version does not support Host Name.]
212 retval = sctp_make_chunk(asoc, SCTP_CID_INIT, 0, chunksize);
213 if (!retval)
214 goto nodata;
216 retval->subh.init_hdr =
217 sctp_addto_chunk(retval, sizeof(init), &init);
218 retval->param_hdr.v =
219 sctp_addto_chunk(retval, addrs_len, addrs.v);
221 /* RFC 2960 3.3.2 Initiation (INIT) (1)
223 * Note 4: This parameter, when present, specifies all the
224 * address types the sending endpoint can support. The absence
225 * of this parameter indicates that the sending endpoint can
226 * support any address type.
228 sat.param_hdr.type = SCTP_PARAM_SUPPORTED_ADDRESS_TYPES;
229 sat.param_hdr.length = htons(SCTP_SAT_LEN(num_types));
230 sctp_addto_chunk(retval, sizeof(sat), &sat);
231 sctp_addto_chunk(retval, num_types * sizeof(__u16), &types);
233 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
234 nodata:
235 if (addrs.v)
236 kfree(addrs.v);
237 return retval;
240 struct sctp_chunk *sctp_make_init_ack(const struct sctp_association *asoc,
241 const struct sctp_chunk *chunk,
242 int gfp, int unkparam_len)
244 sctp_inithdr_t initack;
245 struct sctp_chunk *retval;
246 union sctp_params addrs;
247 int addrs_len;
248 sctp_cookie_param_t *cookie;
249 int cookie_len;
250 size_t chunksize;
252 retval = NULL;
254 /* Note: there may be no addresses to embed. */
255 addrs = sctp_bind_addrs_to_raw(&asoc->base.bind_addr, &addrs_len, gfp);
257 initack.init_tag = htonl(asoc->c.my_vtag);
258 initack.a_rwnd = htonl(asoc->rwnd);
259 initack.num_outbound_streams = htons(asoc->c.sinit_num_ostreams);
260 initack.num_inbound_streams = htons(asoc->c.sinit_max_instreams);
261 initack.initial_tsn = htonl(asoc->c.initial_tsn);
263 /* FIXME: We really ought to build the cookie right
264 * into the packet instead of allocating more fresh memory.
266 cookie = sctp_pack_cookie(asoc->ep, asoc, chunk, &cookie_len,
267 addrs.v, addrs_len);
268 if (!cookie)
269 goto nomem_cookie;
271 /* Calculate the total size of allocation, include the reserved
272 * space for reporting unknown parameters if it is specified.
274 chunksize = sizeof(initack) + addrs_len + cookie_len + unkparam_len;
276 /* Tell peer that we'll do ECN only if peer advertised such cap. */
277 if (asoc->peer.ecn_capable)
278 chunksize += sizeof(ecap_param);
280 /* Now allocate and fill out the chunk. */
281 retval = sctp_make_chunk(asoc, SCTP_CID_INIT_ACK, 0, chunksize);
282 if (!retval)
283 goto nomem_chunk;
285 /* Per the advice in RFC 2960 6.4, send this reply to
286 * the source of the INIT packet.
288 retval->transport = chunk->transport;
289 retval->subh.init_hdr =
290 sctp_addto_chunk(retval, sizeof(initack), &initack);
291 retval->param_hdr.v = sctp_addto_chunk(retval, addrs_len, addrs.v);
292 sctp_addto_chunk(retval, cookie_len, cookie);
293 if (asoc->peer.ecn_capable)
294 sctp_addto_chunk(retval, sizeof(ecap_param), &ecap_param);
296 /* We need to remove the const qualifier at this point. */
297 retval->asoc = (struct sctp_association *) asoc;
299 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
301 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
302 * HEARTBEAT ACK, * etc.) to the same destination transport
303 * address from which it received the DATA or control chunk
304 * to which it is replying.
306 * [INIT ACK back to where the INIT came from.]
308 if (chunk)
309 retval->transport = chunk->transport;
311 nomem_chunk:
312 kfree(cookie);
313 nomem_cookie:
314 if (addrs.v)
315 kfree(addrs.v);
316 return retval;
319 /* 3.3.11 Cookie Echo (COOKIE ECHO) (10):
321 * This chunk is used only during the initialization of an association.
322 * It is sent by the initiator of an association to its peer to complete
323 * the initialization process. This chunk MUST precede any DATA chunk
324 * sent within the association, but MAY be bundled with one or more DATA
325 * chunks in the same packet.
327 * 0 1 2 3
328 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
329 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
330 * | Type = 10 |Chunk Flags | Length |
331 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
332 * / Cookie /
333 * \ \
334 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
336 * Chunk Flags: 8 bit
338 * Set to zero on transmit and ignored on receipt.
340 * Length: 16 bits (unsigned integer)
342 * Set to the size of the chunk in bytes, including the 4 bytes of
343 * the chunk header and the size of the Cookie.
345 * Cookie: variable size
347 * This field must contain the exact cookie received in the
348 * State Cookie parameter from the previous INIT ACK.
350 * An implementation SHOULD make the cookie as small as possible
351 * to insure interoperability.
353 struct sctp_chunk *sctp_make_cookie_echo(const struct sctp_association *asoc,
354 const struct sctp_chunk *chunk)
356 struct sctp_chunk *retval;
357 void *cookie;
358 int cookie_len;
360 cookie = asoc->peer.cookie;
361 cookie_len = asoc->peer.cookie_len;
363 /* Build a cookie echo chunk. */
364 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ECHO, 0, cookie_len);
365 if (!retval)
366 goto nodata;
367 retval->subh.cookie_hdr =
368 sctp_addto_chunk(retval, cookie_len, cookie);
370 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
372 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
373 * HEARTBEAT ACK, * etc.) to the same destination transport
374 * address from which it * received the DATA or control chunk
375 * to which it is replying.
377 * [COOKIE ECHO back to where the INIT ACK came from.]
379 if (chunk)
380 retval->transport = chunk->transport;
382 nodata:
383 return retval;
386 /* 3.3.12 Cookie Acknowledgement (COOKIE ACK) (11):
388 * This chunk is used only during the initialization of an
389 * association. It is used to acknowledge the receipt of a COOKIE
390 * ECHO chunk. This chunk MUST precede any DATA or SACK chunk sent
391 * within the association, but MAY be bundled with one or more DATA
392 * chunks or SACK chunk in the same SCTP packet.
394 * 0 1 2 3
395 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
396 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
397 * | Type = 11 |Chunk Flags | Length = 4 |
398 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
400 * Chunk Flags: 8 bits
402 * Set to zero on transmit and ignored on receipt.
404 struct sctp_chunk *sctp_make_cookie_ack(const struct sctp_association *asoc,
405 const struct sctp_chunk *chunk)
407 struct sctp_chunk *retval;
409 retval = sctp_make_chunk(asoc, SCTP_CID_COOKIE_ACK, 0, 0);
411 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
413 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
414 * HEARTBEAT ACK, * etc.) to the same destination transport
415 * address from which it * received the DATA or control chunk
416 * to which it is replying.
418 * [COOKIE ACK back to where the COOKIE ECHO came from.]
420 if (retval && chunk)
421 retval->transport = chunk->transport;
423 return retval;
427 * Appendix A: Explicit Congestion Notification:
428 * CWR:
430 * RFC 2481 details a specific bit for a sender to send in the header of
431 * its next outbound TCP segment to indicate to its peer that it has
432 * reduced its congestion window. This is termed the CWR bit. For
433 * SCTP the same indication is made by including the CWR chunk.
434 * This chunk contains one data element, i.e. the TSN number that
435 * was sent in the ECNE chunk. This element represents the lowest
436 * TSN number in the datagram that was originally marked with the
437 * CE bit.
439 * 0 1 2 3
440 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
441 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
442 * | Chunk Type=13 | Flags=00000000| Chunk Length = 8 |
443 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
444 * | Lowest TSN Number |
445 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
447 * Note: The CWR is considered a Control chunk.
449 struct sctp_chunk *sctp_make_cwr(const struct sctp_association *asoc,
450 const __u32 lowest_tsn,
451 const struct sctp_chunk *chunk)
453 struct sctp_chunk *retval;
454 sctp_cwrhdr_t cwr;
456 cwr.lowest_tsn = htonl(lowest_tsn);
457 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_CWR, 0,
458 sizeof(sctp_cwrhdr_t));
460 if (!retval)
461 goto nodata;
463 retval->subh.ecn_cwr_hdr =
464 sctp_addto_chunk(retval, sizeof(cwr), &cwr);
466 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
468 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
469 * HEARTBEAT ACK, * etc.) to the same destination transport
470 * address from which it * received the DATA or control chunk
471 * to which it is replying.
473 * [Report a reduced congestion window back to where the ECNE
474 * came from.]
476 if (chunk)
477 retval->transport = chunk->transport;
479 nodata:
480 return retval;
483 /* Make an ECNE chunk. This is a congestion experienced report. */
484 struct sctp_chunk *sctp_make_ecne(const struct sctp_association *asoc,
485 const __u32 lowest_tsn)
487 struct sctp_chunk *retval;
488 sctp_ecnehdr_t ecne;
490 ecne.lowest_tsn = htonl(lowest_tsn);
491 retval = sctp_make_chunk(asoc, SCTP_CID_ECN_ECNE, 0,
492 sizeof(sctp_ecnehdr_t));
493 if (!retval)
494 goto nodata;
495 retval->subh.ecne_hdr =
496 sctp_addto_chunk(retval, sizeof(ecne), &ecne);
498 nodata:
499 return retval;
502 /* Make a DATA chunk for the given association from the provided
503 * parameters. However, do not populate the data payload.
505 struct sctp_chunk *sctp_make_datafrag_empty(struct sctp_association *asoc,
506 const struct sctp_sndrcvinfo *sinfo,
507 int data_len, __u8 flags, __u16 ssn)
509 struct sctp_chunk *retval;
510 struct sctp_datahdr dp;
511 int chunk_len;
513 /* We assign the TSN as LATE as possible, not here when
514 * creating the chunk.
516 dp.tsn = 0;
517 dp.stream = htons(sinfo->sinfo_stream);
518 dp.ppid = htonl(sinfo->sinfo_ppid);
520 /* Set the flags for an unordered send. */
521 if (sinfo->sinfo_flags & MSG_UNORDERED) {
522 flags |= SCTP_DATA_UNORDERED;
523 dp.ssn = 0;
524 } else
525 dp.ssn = htons(ssn);
527 chunk_len = sizeof(dp) + data_len;
528 retval = sctp_make_chunk(asoc, SCTP_CID_DATA, flags, chunk_len);
529 if (!retval)
530 goto nodata;
532 retval->subh.data_hdr = sctp_addto_chunk(retval, sizeof(dp), &dp);
533 memcpy(&retval->sinfo, sinfo, sizeof(struct sctp_sndrcvinfo));
535 nodata:
536 return retval;
539 /* Make a DATA chunk for the given association. Populate the data
540 * payload.
542 struct sctp_chunk *sctp_make_datafrag(struct sctp_association *asoc,
543 const struct sctp_sndrcvinfo *sinfo,
544 int data_len, const __u8 *data,
545 __u8 flags, __u16 ssn)
547 struct sctp_chunk *retval;
549 retval = sctp_make_datafrag_empty(asoc, sinfo, data_len, flags, ssn);
550 if (retval)
551 sctp_addto_chunk(retval, data_len, data);
553 return retval;
556 /* Make a DATA chunk for the given association to ride on stream id
557 * 'stream', with a payload id of 'payload', and a body of 'data'.
559 struct sctp_chunk *sctp_make_data(struct sctp_association *asoc,
560 const struct sctp_sndrcvinfo *sinfo,
561 int data_len, const __u8 *data)
563 struct sctp_chunk *retval = NULL;
565 retval = sctp_make_data_empty(asoc, sinfo, data_len);
566 if (retval)
567 sctp_addto_chunk(retval, data_len, data);
568 return retval;
571 /* Make a DATA chunk for the given association to ride on stream id
572 * 'stream', with a payload id of 'payload', and a body big enough to
573 * hold 'data_len' octets of data. We use this version when we need
574 * to build the message AFTER allocating memory.
576 struct sctp_chunk *sctp_make_data_empty(struct sctp_association *asoc,
577 const struct sctp_sndrcvinfo *sinfo,
578 int data_len)
580 __u8 flags = SCTP_DATA_NOT_FRAG;
582 return sctp_make_datafrag_empty(asoc, sinfo, data_len, flags, 0);
585 /* Create a selective ackowledgement (SACK) for the given
586 * association. This reports on which TSN's we've seen to date,
587 * including duplicates and gaps.
589 struct sctp_chunk *sctp_make_sack(const struct sctp_association *asoc)
591 struct sctp_chunk *retval;
592 struct sctp_sackhdr sack;
593 int len;
594 __u32 ctsn;
595 __u16 num_gabs, num_dup_tsns;
596 struct sctp_tsnmap *map = (struct sctp_tsnmap *)&asoc->peer.tsn_map;
598 ctsn = sctp_tsnmap_get_ctsn(map);
599 SCTP_DEBUG_PRINTK("sackCTSNAck sent: 0x%x.\n", ctsn);
601 /* How much room is needed in the chunk? */
602 num_gabs = sctp_tsnmap_num_gabs(map);
603 num_dup_tsns = sctp_tsnmap_num_dups(map);
605 /* Initialize the SACK header. */
606 sack.cum_tsn_ack = htonl(ctsn);
607 sack.a_rwnd = htonl(asoc->a_rwnd);
608 sack.num_gap_ack_blocks = htons(num_gabs);
609 sack.num_dup_tsns = htons(num_dup_tsns);
611 len = sizeof(sack)
612 + sizeof(struct sctp_gap_ack_block) * num_gabs
613 + sizeof(__u32) * num_dup_tsns;
615 /* Create the chunk. */
616 retval = sctp_make_chunk(asoc, SCTP_CID_SACK, 0, len);
617 if (!retval)
618 goto nodata;
620 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
622 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
623 * HEARTBEAT ACK, etc.) to the same destination transport
624 * address from which it received the DATA or control chunk to
625 * which it is replying. This rule should also be followed if
626 * the endpoint is bundling DATA chunks together with the
627 * reply chunk.
629 * However, when acknowledging multiple DATA chunks received
630 * in packets from different source addresses in a single
631 * SACK, the SACK chunk may be transmitted to one of the
632 * destination transport addresses from which the DATA or
633 * control chunks being acknowledged were received.
635 * [BUG: We do not implement the following paragraph.
636 * Perhaps we should remember the last transport we used for a
637 * SACK and avoid that (if possible) if we have seen any
638 * duplicates. --piggy]
640 * When a receiver of a duplicate DATA chunk sends a SACK to a
641 * multi- homed endpoint it MAY be beneficial to vary the
642 * destination address and not use the source address of the
643 * DATA chunk. The reason being that receiving a duplicate
644 * from a multi-homed endpoint might indicate that the return
645 * path (as specified in the source address of the DATA chunk)
646 * for the SACK is broken.
648 * [Send to the address from which we last received a DATA chunk.]
650 retval->transport = asoc->peer.last_data_from;
652 retval->subh.sack_hdr =
653 sctp_addto_chunk(retval, sizeof(sack), &sack);
655 /* Add the gap ack block information. */
656 if (num_gabs)
657 sctp_addto_chunk(retval, sizeof(__u32) * num_gabs,
658 sctp_tsnmap_get_gabs(map));
660 /* Add the duplicate TSN information. */
661 if (num_dup_tsns)
662 sctp_addto_chunk(retval, sizeof(__u32) * num_dup_tsns,
663 sctp_tsnmap_get_dups(map));
665 nodata:
666 return retval;
669 /* Make a SHUTDOWN chunk. */
670 struct sctp_chunk *sctp_make_shutdown(const struct sctp_association *asoc)
672 struct sctp_chunk *retval;
673 sctp_shutdownhdr_t shut;
674 __u32 ctsn;
676 ctsn = sctp_tsnmap_get_ctsn(&asoc->peer.tsn_map);
677 shut.cum_tsn_ack = htonl(ctsn);
679 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN, 0,
680 sizeof(sctp_shutdownhdr_t));
681 if (!retval)
682 goto nodata;
684 retval->subh.shutdown_hdr =
685 sctp_addto_chunk(retval, sizeof(shut), &shut);
686 nodata:
687 return retval;
690 struct sctp_chunk *sctp_make_shutdown_ack(const struct sctp_association *asoc,
691 const struct sctp_chunk *chunk)
693 struct sctp_chunk *retval;
695 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_ACK, 0, 0);
697 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
699 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
700 * HEARTBEAT ACK, * etc.) to the same destination transport
701 * address from which it * received the DATA or control chunk
702 * to which it is replying.
704 * [ACK back to where the SHUTDOWN came from.]
706 if (retval && chunk)
707 retval->transport = chunk->transport;
709 return retval;
712 struct sctp_chunk *sctp_make_shutdown_complete(
713 const struct sctp_association *asoc,
714 const struct sctp_chunk *chunk)
716 struct sctp_chunk *retval;
717 __u8 flags = 0;
719 /* Maybe set the T-bit if we have no association. */
720 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
722 retval = sctp_make_chunk(asoc, SCTP_CID_SHUTDOWN_COMPLETE, flags, 0);
724 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
726 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
727 * HEARTBEAT ACK, * etc.) to the same destination transport
728 * address from which it * received the DATA or control chunk
729 * to which it is replying.
731 * [Report SHUTDOWN COMPLETE back to where the SHUTDOWN ACK
732 * came from.]
734 if (retval && chunk)
735 retval->transport = chunk->transport;
737 return retval;
740 /* Create an ABORT. Note that we set the T bit if we have no
741 * association.
743 struct sctp_chunk *sctp_make_abort(const struct sctp_association *asoc,
744 const struct sctp_chunk *chunk,
745 const size_t hint)
747 struct sctp_chunk *retval;
748 __u8 flags = 0;
750 /* Maybe set the T-bit if we have no association. */
751 flags |= asoc ? 0 : SCTP_CHUNK_FLAG_T;
753 retval = sctp_make_chunk(asoc, SCTP_CID_ABORT, flags, hint);
755 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
757 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
758 * HEARTBEAT ACK, * etc.) to the same destination transport
759 * address from which it * received the DATA or control chunk
760 * to which it is replying.
762 * [ABORT back to where the offender came from.]
764 if (retval && chunk)
765 retval->transport = chunk->transport;
767 return retval;
770 /* Helper to create ABORT with a NO_USER_DATA error. */
771 struct sctp_chunk *sctp_make_abort_no_data(
772 const struct sctp_association *asoc,
773 const struct sctp_chunk *chunk, __u32 tsn)
775 struct sctp_chunk *retval;
776 __u32 payload;
778 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t)
779 + sizeof(tsn));
781 if (!retval)
782 goto no_mem;
784 /* Put the tsn back into network byte order. */
785 payload = htonl(tsn);
786 sctp_init_cause(retval, SCTP_ERROR_NO_DATA, (const void *)&payload,
787 sizeof(payload));
789 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
791 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
792 * HEARTBEAT ACK, * etc.) to the same destination transport
793 * address from which it * received the DATA or control chunk
794 * to which it is replying.
796 * [ABORT back to where the offender came from.]
798 if (chunk)
799 retval->transport = chunk->transport;
801 no_mem:
802 return retval;
805 /* Helper to create ABORT with a SCTP_ERROR_USER_ABORT error. */
806 struct sctp_chunk *sctp_make_abort_user(const struct sctp_association *asoc,
807 const struct sctp_chunk *chunk,
808 const struct msghdr *msg)
810 struct sctp_chunk *retval;
811 void *payload = NULL, *payoff;
812 size_t paylen = 0;
813 struct iovec *iov = NULL;
814 int iovlen = 0;
816 if (msg) {
817 iov = msg->msg_iov;
818 iovlen = msg->msg_iovlen;
819 paylen = get_user_iov_size(iov, iovlen);
822 retval = sctp_make_abort(asoc, chunk, sizeof(sctp_errhdr_t) + paylen);
823 if (!retval)
824 goto err_chunk;
826 if (paylen) {
827 /* Put the msg_iov together into payload. */
828 payload = kmalloc(paylen, GFP_ATOMIC);
829 if (!payload)
830 goto err_payload;
831 payoff = payload;
833 for (; iovlen > 0; --iovlen) {
834 if (copy_from_user(payoff, iov->iov_base,iov->iov_len))
835 goto err_copy;
836 payoff += iov->iov_len;
837 iov++;
841 sctp_init_cause(retval, SCTP_ERROR_USER_ABORT, payload, paylen);
843 if (paylen)
844 kfree(payload);
846 return retval;
848 err_copy:
849 kfree(payload);
850 err_payload:
851 sctp_chunk_free(retval);
852 retval = NULL;
853 err_chunk:
854 return retval;
857 /* Make a HEARTBEAT chunk. */
858 struct sctp_chunk *sctp_make_heartbeat(const struct sctp_association *asoc,
859 const struct sctp_transport *transport,
860 const void *payload, const size_t paylen)
862 struct sctp_chunk *retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT,
863 0, paylen);
865 if (!retval)
866 goto nodata;
868 /* Cast away the 'const', as this is just telling the chunk
869 * what transport it belongs to.
871 retval->transport = (struct sctp_transport *) transport;
872 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
874 nodata:
875 return retval;
878 struct sctp_chunk *sctp_make_heartbeat_ack(const struct sctp_association *asoc,
879 const struct sctp_chunk *chunk,
880 const void *payload, const size_t paylen)
882 struct sctp_chunk *retval;
884 retval = sctp_make_chunk(asoc, SCTP_CID_HEARTBEAT_ACK, 0, paylen);
885 if (!retval)
886 goto nodata;
888 retval->subh.hbs_hdr = sctp_addto_chunk(retval, paylen, payload);
890 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
892 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
893 * HEARTBEAT ACK, * etc.) to the same destination transport
894 * address from which it * received the DATA or control chunk
895 * to which it is replying.
897 * [HBACK back to where the HEARTBEAT came from.]
899 if (chunk)
900 retval->transport = chunk->transport;
902 nodata:
903 return retval;
906 /* Create an Operation Error chunk with the specified space reserved.
907 * This routine can be used for containing multiple causes in the chunk.
909 struct sctp_chunk *sctp_make_op_error_space(
910 const struct sctp_association *asoc,
911 const struct sctp_chunk *chunk,
912 size_t size)
914 struct sctp_chunk *retval;
916 retval = sctp_make_chunk(asoc, SCTP_CID_ERROR, 0,
917 sizeof(sctp_errhdr_t) + size);
918 if (!retval)
919 goto nodata;
921 /* RFC 2960 6.4 Multi-homed SCTP Endpoints
923 * An endpoint SHOULD transmit reply chunks (e.g., SACK,
924 * HEARTBEAT ACK, etc.) to the same destination transport
925 * address from which it received the DATA or control chunk
926 * to which it is replying.
929 if (chunk)
930 retval->transport = chunk->transport;
932 nodata:
933 return retval;
936 /* Create an Operation Error chunk. */
937 struct sctp_chunk *sctp_make_op_error(const struct sctp_association *asoc,
938 const struct sctp_chunk *chunk,
939 __u16 cause_code, const void *payload,
940 size_t paylen)
942 struct sctp_chunk *retval;
944 retval = sctp_make_op_error_space(asoc, chunk, paylen);
945 if (!retval)
946 goto nodata;
948 sctp_init_cause(retval, cause_code, payload, paylen);
950 nodata:
951 return retval;
954 /********************************************************************
955 * 2nd Level Abstractions
956 ********************************************************************/
958 /* Turn an skb into a chunk.
959 * FIXME: Eventually move the structure directly inside the skb->cb[].
961 struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
962 const struct sctp_association *asoc,
963 struct sock *sk)
965 struct sctp_chunk *retval;
967 retval = kmem_cache_alloc(sctp_chunk_cachep, SLAB_ATOMIC);
969 if (!retval)
970 goto nodata;
971 memset(retval, 0, sizeof(struct sctp_chunk));
973 if (!sk) {
974 SCTP_DEBUG_PRINTK("chunkifying skb %p w/o an sk\n", skb);
977 retval->skb = skb;
978 retval->asoc = (struct sctp_association *)asoc;
979 retval->resent = 0;
980 retval->has_tsn = 0;
981 retval->has_ssn = 0;
982 retval->rtt_in_progress = 0;
983 retval->sent_at = 0;
984 retval->singleton = 1;
985 retval->end_of_packet = 0;
986 retval->ecn_ce_done = 0;
987 retval->pdiscard = 0;
989 /* sctpimpguide-05.txt Section 2.8.2
990 * M1) Each time a new DATA chunk is transmitted
991 * set the 'TSN.Missing.Report' count for that TSN to 0. The
992 * 'TSN.Missing.Report' count will be used to determine missing chunks
993 * and when to fast retransmit.
995 retval->tsn_missing_report = 0;
996 retval->tsn_gap_acked = 0;
997 retval->fast_retransmit = 0;
999 /* If this is a fragmented message, track all fragments
1000 * of the message (for SEND_FAILED).
1002 retval->msg = NULL;
1004 /* Polish the bead hole. */
1005 INIT_LIST_HEAD(&retval->transmitted_list);
1006 INIT_LIST_HEAD(&retval->frag_list);
1007 SCTP_DBG_OBJCNT_INC(chunk);
1008 atomic_set(&retval->refcnt, 1);
1011 nodata:
1012 return retval;
1015 /* Set chunk->source and dest based on the IP header in chunk->skb. */
1016 void sctp_init_addrs(struct sctp_chunk *chunk, union sctp_addr *src,
1017 union sctp_addr *dest)
1019 memcpy(&chunk->source, src, sizeof(union sctp_addr));
1020 memcpy(&chunk->dest, dest, sizeof(union sctp_addr));
1023 /* Extract the source address from a chunk. */
1024 const union sctp_addr *sctp_source(const struct sctp_chunk *chunk)
1026 /* If we have a known transport, use that. */
1027 if (chunk->transport) {
1028 return &chunk->transport->ipaddr;
1029 } else {
1030 /* Otherwise, extract it from the IP header. */
1031 return &chunk->source;
1035 /* Create a new chunk, setting the type and flags headers from the
1036 * arguments, reserving enough space for a 'paylen' byte payload.
1038 struct sctp_chunk *sctp_make_chunk(const struct sctp_association *asoc,
1039 __u8 type, __u8 flags, int paylen)
1041 struct sctp_chunk *retval;
1042 sctp_chunkhdr_t *chunk_hdr;
1043 struct sk_buff *skb;
1044 struct sock *sk;
1046 /* No need to allocate LL here, as this is only a chunk. */
1047 skb = alloc_skb(WORD_ROUND(sizeof(sctp_chunkhdr_t) + paylen),
1048 GFP_ATOMIC);
1049 if (!skb)
1050 goto nodata;
1052 /* Make room for the chunk header. */
1053 chunk_hdr = (sctp_chunkhdr_t *)skb_put(skb, sizeof(sctp_chunkhdr_t));
1054 chunk_hdr->type = type;
1055 chunk_hdr->flags = flags;
1056 chunk_hdr->length = htons(sizeof(sctp_chunkhdr_t));
1058 sk = asoc ? asoc->base.sk : NULL;
1059 retval = sctp_chunkify(skb, asoc, sk);
1060 if (!retval) {
1061 kfree_skb(skb);
1062 goto nodata;
1065 retval->chunk_hdr = chunk_hdr;
1066 retval->chunk_end = ((__u8 *)chunk_hdr) + sizeof(struct sctp_chunkhdr);
1068 /* Set the skb to the belonging sock for accounting. */
1069 skb->sk = sk;
1071 return retval;
1072 nodata:
1073 return NULL;
1077 /* Release the memory occupied by a chunk. */
1078 static void sctp_chunk_destroy(struct sctp_chunk *chunk)
1080 /* Free the chunk skb data and the SCTP_chunk stub itself. */
1081 dev_kfree_skb(chunk->skb);
1083 SCTP_DBG_OBJCNT_DEC(chunk);
1084 kmem_cache_free(sctp_chunk_cachep, chunk);
1087 /* Possibly, free the chunk. */
1088 void sctp_chunk_free(struct sctp_chunk *chunk)
1090 /* Make sure that we are not on any list. */
1091 skb_unlink((struct sk_buff *) chunk);
1092 list_del(&chunk->transmitted_list);
1094 /* Release our reference on the message tracker. */
1095 if (chunk->msg)
1096 sctp_datamsg_put(chunk->msg);
1098 sctp_chunk_put(chunk);
1101 /* Grab a reference to the chunk. */
1102 void sctp_chunk_hold(struct sctp_chunk *ch)
1104 atomic_inc(&ch->refcnt);
1107 /* Release a reference to the chunk. */
1108 void sctp_chunk_put(struct sctp_chunk *ch)
1110 if (atomic_dec_and_test(&ch->refcnt))
1111 sctp_chunk_destroy(ch);
1114 /* Append bytes to the end of a chunk. Will panic if chunk is not big
1115 * enough.
1117 void *sctp_addto_chunk(struct sctp_chunk *chunk, int len, const void *data)
1119 void *target;
1120 void *padding;
1121 int chunklen = ntohs(chunk->chunk_hdr->length);
1122 int padlen = chunklen % 4;
1124 padding = skb_put(chunk->skb, padlen);
1125 target = skb_put(chunk->skb, len);
1127 memset(padding, 0, padlen);
1128 memcpy(target, data, len);
1130 /* Adjust the chunk length field. */
1131 chunk->chunk_hdr->length = htons(chunklen + padlen + len);
1132 chunk->chunk_end = chunk->skb->tail;
1134 return target;
1137 /* Append bytes from user space to the end of a chunk. Will panic if
1138 * chunk is not big enough.
1139 * Returns a kernel err value.
1141 int sctp_user_addto_chunk(struct sctp_chunk *chunk, int off, int len,
1142 struct iovec *data)
1144 __u8 *target;
1145 int err = 0;
1147 /* Make room in chunk for data. */
1148 target = skb_put(chunk->skb, len);
1150 /* Copy data (whole iovec) into chunk */
1151 if ((err = memcpy_fromiovecend(target, data, off, len)))
1152 goto out;
1154 /* Adjust the chunk length field. */
1155 chunk->chunk_hdr->length =
1156 htons(ntohs(chunk->chunk_hdr->length) + len);
1157 chunk->chunk_end = chunk->skb->tail;
1159 out:
1160 return err;
1163 /* Helper function to assign a TSN if needed. This assumes that both
1164 * the data_hdr and association have already been assigned.
1166 void sctp_chunk_assign_ssn(struct sctp_chunk *chunk)
1168 __u16 ssn;
1169 __u16 sid;
1171 if (chunk->has_ssn)
1172 return;
1174 /* This is the last possible instant to assign a SSN. */
1175 if (chunk->chunk_hdr->flags & SCTP_DATA_UNORDERED) {
1176 ssn = 0;
1177 } else {
1178 sid = htons(chunk->subh.data_hdr->stream);
1179 if (chunk->chunk_hdr->flags & SCTP_DATA_LAST_FRAG)
1180 ssn = sctp_ssn_next(&chunk->asoc->ssnmap->out, sid);
1181 else
1182 ssn = sctp_ssn_peek(&chunk->asoc->ssnmap->out, sid);
1183 ssn = htons(ssn);
1186 chunk->subh.data_hdr->ssn = ssn;
1187 chunk->has_ssn = 1;
1190 /* Helper function to assign a TSN if needed. This assumes that both
1191 * the data_hdr and association have already been assigned.
1193 void sctp_chunk_assign_tsn(struct sctp_chunk *chunk)
1195 if (!chunk->has_tsn) {
1196 /* This is the last possible instant to
1197 * assign a TSN.
1199 chunk->subh.data_hdr->tsn =
1200 htonl(sctp_association_get_next_tsn(chunk->asoc));
1201 chunk->has_tsn = 1;
1205 /* Create a CLOSED association to use with an incoming packet. */
1206 struct sctp_association *sctp_make_temp_asoc(const struct sctp_endpoint *ep,
1207 struct sctp_chunk *chunk, int gfp)
1209 struct sctp_association *asoc;
1210 struct sk_buff *skb;
1211 sctp_scope_t scope;
1213 /* Create the bare association. */
1214 scope = sctp_scope(sctp_source(chunk));
1215 asoc = sctp_association_new(ep, ep->base.sk, scope, gfp);
1216 if (!asoc)
1217 goto nodata;
1218 asoc->temp = 1;
1219 skb = chunk->skb;
1220 /* Create an entry for the source address of the packet. */
1221 /* FIXME: Use the af specific helpers. */
1222 switch (skb->nh.iph->version) {
1223 case 4:
1224 asoc->c.peer_addr.v4.sin_family = AF_INET;
1225 asoc->c.peer_addr.v4.sin_port = ntohs(chunk->sctp_hdr->source);
1226 asoc->c.peer_addr.v4.sin_addr.s_addr = skb->nh.iph->saddr;
1227 break;
1229 case 6:
1230 asoc->c.peer_addr.v6.sin6_family = AF_INET6;
1231 asoc->c.peer_addr.v6.sin6_port
1232 = ntohs(chunk->sctp_hdr->source);
1233 asoc->c.peer_addr.v6.sin6_flowinfo = 0; /* BUG BUG BUG */
1234 asoc->c.peer_addr.v6.sin6_addr = skb->nh.ipv6h->saddr;
1235 asoc->c.peer_addr.v6.sin6_scope_id =
1236 ((struct inet6_skb_parm *)skb->cb)->iif;
1237 break;
1239 default:
1240 /* Yikes! I never heard of this kind of address. */
1241 goto fail;
1244 nodata:
1245 return asoc;
1247 fail:
1248 sctp_association_free(asoc);
1249 return NULL;
1252 /* Build a cookie representing asoc.
1253 * This INCLUDES the param header needed to put the cookie in the INIT ACK.
1255 sctp_cookie_param_t *sctp_pack_cookie(const struct sctp_endpoint *ep,
1256 const struct sctp_association *asoc,
1257 const struct sctp_chunk *init_chunk,
1258 int *cookie_len,
1259 const __u8 *raw_addrs, int addrs_len)
1261 sctp_cookie_param_t *retval;
1262 struct sctp_signed_cookie *cookie;
1263 struct scatterlist sg;
1264 int headersize, bodysize;
1265 unsigned int keylen;
1266 char *key;
1268 headersize = sizeof(sctp_paramhdr_t) + SCTP_SECRET_SIZE;
1269 bodysize = sizeof(struct sctp_cookie)
1270 + ntohs(init_chunk->chunk_hdr->length) + addrs_len;
1272 /* Pad out the cookie to a multiple to make the signature
1273 * functions simpler to write.
1275 if (bodysize % SCTP_COOKIE_MULTIPLE)
1276 bodysize += SCTP_COOKIE_MULTIPLE
1277 - (bodysize % SCTP_COOKIE_MULTIPLE);
1278 *cookie_len = headersize + bodysize;
1280 retval = (sctp_cookie_param_t *)kmalloc(*cookie_len, GFP_ATOMIC);
1282 if (!retval) {
1283 *cookie_len = 0;
1284 goto nodata;
1287 /* Clear this memory since we are sending this data structure
1288 * out on the network.
1290 memset(retval, 0x00, *cookie_len);
1291 cookie = (struct sctp_signed_cookie *) retval->body;
1293 /* Set up the parameter header. */
1294 retval->p.type = SCTP_PARAM_STATE_COOKIE;
1295 retval->p.length = htons(*cookie_len);
1297 /* Copy the cookie part of the association itself. */
1298 cookie->c = asoc->c;
1299 /* Save the raw address list length in the cookie. */
1300 cookie->c.raw_addr_list_len = addrs_len;
1302 /* Set an expiration time for the cookie. */
1303 do_gettimeofday(&cookie->c.expiration);
1304 tv_add(&asoc->cookie_life, &cookie->c.expiration);
1306 /* Copy the peer's init packet. */
1307 memcpy(&cookie->c.peer_init[0], init_chunk->chunk_hdr,
1308 ntohs(init_chunk->chunk_hdr->length));
1310 /* Copy the raw local address list of the association. */
1311 memcpy((__u8 *)&cookie->c.peer_init[0] +
1312 ntohs(init_chunk->chunk_hdr->length), raw_addrs, addrs_len);
1314 if (sctp_sk(ep->base.sk)->hmac) {
1315 /* Sign the message. */
1316 sg.page = virt_to_page(&cookie->c);
1317 sg.offset = (unsigned long)(&cookie->c) % PAGE_SIZE;
1318 sg.length = bodysize;
1319 keylen = SCTP_SECRET_SIZE;
1320 key = (char *)ep->secret_key[ep->current_key];
1322 sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen,
1323 &sg, 1, cookie->signature);
1326 nodata:
1327 return retval;
1330 /* Unpack the cookie from COOKIE ECHO chunk, recreating the association. */
1331 struct sctp_association *sctp_unpack_cookie(
1332 const struct sctp_endpoint *ep,
1333 const struct sctp_association *asoc,
1334 struct sctp_chunk *chunk, int gfp,
1335 int *error, struct sctp_chunk **errp)
1337 struct sctp_association *retval = NULL;
1338 struct sctp_signed_cookie *cookie;
1339 struct sctp_cookie *bear_cookie;
1340 int headersize, bodysize, fixed_size;
1341 __u8 digest[SCTP_SIGNATURE_SIZE];
1342 struct scatterlist sg;
1343 unsigned int keylen, len;
1344 char *key;
1345 sctp_scope_t scope;
1346 struct sk_buff *skb = chunk->skb;
1348 headersize = sizeof(sctp_chunkhdr_t) + SCTP_SECRET_SIZE;
1349 bodysize = ntohs(chunk->chunk_hdr->length) - headersize;
1350 fixed_size = headersize + sizeof(struct sctp_cookie);
1352 /* Verify that the chunk looks like it even has a cookie.
1353 * There must be enough room for our cookie and our peer's
1354 * INIT chunk.
1356 len = ntohs(chunk->chunk_hdr->length);
1357 if (len < fixed_size + sizeof(struct sctp_chunkhdr))
1358 goto malformed;
1360 /* Verify that the cookie has been padded out. */
1361 if (bodysize % SCTP_COOKIE_MULTIPLE)
1362 goto malformed;
1364 /* Process the cookie. */
1365 cookie = chunk->subh.cookie_hdr;
1366 bear_cookie = &cookie->c;
1368 if (!sctp_sk(ep->base.sk)->hmac)
1369 goto no_hmac;
1371 /* Check the signature. */
1372 keylen = SCTP_SECRET_SIZE;
1373 sg.page = virt_to_page(bear_cookie);
1374 sg.offset = (unsigned long)(bear_cookie) % PAGE_SIZE;
1375 sg.length = bodysize;
1376 key = (char *)ep->secret_key[ep->current_key];
1378 memset(digest, 0x00, sizeof(digest));
1379 sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen, &sg,
1380 1, digest);
1382 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1383 /* Try the previous key. */
1384 key = (char *)ep->secret_key[ep->last_key];
1385 memset(digest, 0x00, sizeof(digest));
1386 sctp_crypto_hmac(sctp_sk(ep->base.sk)->hmac, key, &keylen,
1387 &sg, 1, digest);
1389 if (memcmp(digest, cookie->signature, SCTP_SIGNATURE_SIZE)) {
1390 /* Yikes! Still bad signature! */
1391 *error = -SCTP_IERROR_BAD_SIG;
1392 goto fail;
1396 no_hmac:
1397 /* Check to see if the cookie is stale. If there is already
1398 * an association, there is no need to check cookie's expiration
1399 * for init collision case of lost COOKIE ACK.
1401 if (!asoc && tv_lt(bear_cookie->expiration, skb->stamp)) {
1402 __u16 len;
1404 * Section 3.3.10.3 Stale Cookie Error (3)
1406 * Cause of error
1407 * ---------------
1408 * Stale Cookie Error: Indicates the receipt of a valid State
1409 * Cookie that has expired.
1411 len = ntohs(chunk->chunk_hdr->length);
1412 *errp = sctp_make_op_error_space(asoc, chunk, len);
1413 if (*errp) {
1414 suseconds_t usecs = (skb->stamp.tv_sec -
1415 bear_cookie->expiration.tv_sec) * 1000000L +
1416 skb->stamp.tv_usec -
1417 bear_cookie->expiration.tv_usec;
1419 usecs = htonl(usecs);
1420 sctp_init_cause(*errp, SCTP_ERROR_STALE_COOKIE,
1421 &usecs, sizeof(usecs));
1422 *error = -SCTP_IERROR_STALE_COOKIE;
1423 } else
1424 *error = -SCTP_IERROR_NOMEM;
1426 goto fail;
1429 /* Make a new base association. */
1430 scope = sctp_scope(sctp_source(chunk));
1431 retval = sctp_association_new(ep, ep->base.sk, scope, gfp);
1432 if (!retval) {
1433 *error = -SCTP_IERROR_NOMEM;
1434 goto fail;
1437 /* Set up our peer's port number. */
1438 retval->peer.port = ntohs(chunk->sctp_hdr->source);
1440 /* Populate the association from the cookie. */
1441 memcpy(&retval->c, bear_cookie, sizeof(*bear_cookie));
1443 if (sctp_assoc_set_bind_addr_from_cookie(retval, bear_cookie,
1444 GFP_ATOMIC) < 0) {
1445 *error = -SCTP_IERROR_NOMEM;
1446 goto fail;
1449 /* Also, add the destination address. */
1450 if (list_empty(&retval->base.bind_addr.address_list)) {
1451 sctp_add_bind_addr(&retval->base.bind_addr, &chunk->dest,
1452 GFP_ATOMIC);
1455 retval->next_tsn = retval->c.initial_tsn;
1456 retval->ctsn_ack_point = retval->next_tsn - 1;
1458 /* The INIT stuff will be done by the side effects. */
1459 return retval;
1461 fail:
1462 if (retval)
1463 sctp_association_free(retval);
1465 return NULL;
1467 malformed:
1468 /* Yikes! The packet is either corrupt or deliberately
1469 * malformed.
1471 *error = -SCTP_IERROR_MALFORMED;
1472 goto fail;
1475 /********************************************************************
1476 * 3rd Level Abstractions
1477 ********************************************************************/
1479 struct __sctp_missing {
1480 __u32 num_missing;
1481 __u16 type;
1482 } __attribute__((packed));;
1485 * Report a missing mandatory parameter.
1487 static int sctp_process_missing_param(const struct sctp_association *asoc,
1488 sctp_param_t paramtype,
1489 struct sctp_chunk *chunk,
1490 struct sctp_chunk **errp)
1492 struct __sctp_missing report;
1493 __u16 len;
1495 len = WORD_ROUND(sizeof(report));
1497 /* Make an ERROR chunk, preparing enough room for
1498 * returning multiple unknown parameters.
1500 if (!*errp)
1501 *errp = sctp_make_op_error_space(asoc, chunk, len);
1503 if (*errp) {
1504 report.num_missing = htonl(1);
1505 report.type = paramtype;
1506 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM,
1507 &report, sizeof(report));
1510 /* Stop processing this chunk. */
1511 return 0;
1514 /* Report an Invalid Mandatory Parameter. */
1515 static int sctp_process_inv_mandatory(const struct sctp_association *asoc,
1516 struct sctp_chunk *chunk,
1517 struct sctp_chunk **errp)
1519 /* Invalid Mandatory Parameter Error has no payload. */
1521 if (!*errp)
1522 *errp = sctp_make_op_error_space(asoc, chunk, 0);
1524 if (*errp)
1525 sctp_init_cause(*errp, SCTP_ERROR_INV_PARAM, NULL, 0);
1527 /* Stop processing this chunk. */
1528 return 0;
1531 /* Do not attempt to handle the HOST_NAME parm. However, do
1532 * send back an indicator to the peer.
1534 static int sctp_process_hn_param(const struct sctp_association *asoc,
1535 union sctp_params param,
1536 struct sctp_chunk *chunk,
1537 struct sctp_chunk **errp)
1539 __u16 len = ntohs(param.p->length);
1541 /* Make an ERROR chunk. */
1542 if (!*errp)
1543 *errp = sctp_make_op_error_space(asoc, chunk, len);
1545 if (*errp)
1546 sctp_init_cause(*errp, SCTP_ERROR_DNS_FAILED,
1547 param.v, len);
1549 /* Stop processing this chunk. */
1550 return 0;
1553 /* RFC 3.2.1 & the Implementers Guide 2.2.
1555 * The Parameter Types are encoded such that the
1556 * highest-order two bits specify the action that must be
1557 * taken if the processing endpoint does not recognize the
1558 * Parameter Type.
1560 * 00 - Stop processing this SCTP chunk and discard it,
1561 * do not process any further chunks within it.
1563 * 01 - Stop processing this SCTP chunk and discard it,
1564 * do not process any further chunks within it, and report
1565 * the unrecognized parameter in an 'Unrecognized
1566 * Parameter Type' (in either an ERROR or in the INIT ACK).
1568 * 10 - Skip this parameter and continue processing.
1570 * 11 - Skip this parameter and continue processing but
1571 * report the unrecognized parameter in an
1572 * 'Unrecognized Parameter Type' (in either an ERROR or in
1573 * the INIT ACK).
1575 * Return value:
1576 * 0 - discard the chunk
1577 * 1 - continue with the chunk
1579 static int sctp_process_unk_param(const struct sctp_association *asoc,
1580 union sctp_params param,
1581 struct sctp_chunk *chunk,
1582 struct sctp_chunk **errp)
1584 int retval = 1;
1586 switch (param.p->type & SCTP_PARAM_ACTION_MASK) {
1587 case SCTP_PARAM_ACTION_DISCARD:
1588 retval = 0;
1589 break;
1590 case SCTP_PARAM_ACTION_DISCARD_ERR:
1591 retval = 0;
1592 /* Make an ERROR chunk, preparing enough room for
1593 * returning multiple unknown parameters.
1595 if (NULL == *errp)
1596 *errp = sctp_make_op_error_space(asoc, chunk,
1597 ntohs(chunk->chunk_hdr->length));
1599 if (*errp)
1600 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1601 param.v,
1602 WORD_ROUND(ntohs(param.p->length)));
1604 break;
1605 case SCTP_PARAM_ACTION_SKIP:
1606 break;
1607 case SCTP_PARAM_ACTION_SKIP_ERR:
1608 /* Make an ERROR chunk, preparing enough room for
1609 * returning multiple unknown parameters.
1611 if (NULL == *errp)
1612 *errp = sctp_make_op_error_space(asoc, chunk,
1613 ntohs(chunk->chunk_hdr->length));
1615 if (*errp) {
1616 sctp_init_cause(*errp, SCTP_ERROR_UNKNOWN_PARAM,
1617 param.v,
1618 WORD_ROUND(ntohs(param.p->length)));
1619 } else {
1620 /* If there is no memory for generating the ERROR
1621 * report as specified, an ABORT will be triggered
1622 * to the peer and the association won't be
1623 * established.
1625 retval = 0;
1628 break;
1629 default:
1630 break;
1633 return retval;
1636 /* Find unrecognized parameters in the chunk.
1637 * Return values:
1638 * 0 - discard the chunk
1639 * 1 - continue with the chunk
1641 static int sctp_verify_param(const struct sctp_association *asoc,
1642 union sctp_params param,
1643 sctp_cid_t cid,
1644 struct sctp_chunk *chunk,
1645 struct sctp_chunk **err_chunk)
1647 int retval = 1;
1649 /* FIXME - This routine is not looking at each parameter per the
1650 * chunk type, i.e., unrecognized parameters should be further
1651 * identified based on the chunk id.
1654 switch (param.p->type) {
1655 case SCTP_PARAM_IPV4_ADDRESS:
1656 case SCTP_PARAM_IPV6_ADDRESS:
1657 case SCTP_PARAM_COOKIE_PRESERVATIVE:
1658 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1659 case SCTP_PARAM_STATE_COOKIE:
1660 case SCTP_PARAM_HEARTBEAT_INFO:
1661 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1662 case SCTP_PARAM_ECN_CAPABLE:
1663 break;
1665 case SCTP_PARAM_HOST_NAME_ADDRESS:
1666 /* Tell the peer, we won't support this param. */
1667 return sctp_process_hn_param(asoc, param, chunk, err_chunk);
1668 default:
1669 SCTP_DEBUG_PRINTK("Unrecognized param: %d for chunk %d.\n",
1670 ntohs(param.p->type), cid);
1671 return sctp_process_unk_param(asoc, param, chunk, err_chunk);
1673 break;
1675 return retval;
1678 /* Verify the INIT packet before we process it. */
1679 int sctp_verify_init(const struct sctp_association *asoc,
1680 sctp_cid_t cid,
1681 sctp_init_chunk_t *peer_init,
1682 struct sctp_chunk *chunk,
1683 struct sctp_chunk **errp)
1685 union sctp_params param;
1686 int has_cookie = 0;
1688 /* Verify stream values are non-zero. */
1689 if ((0 == peer_init->init_hdr.num_outbound_streams) ||
1690 (0 == peer_init->init_hdr.num_inbound_streams)) {
1692 sctp_process_inv_mandatory(asoc, chunk, errp);
1693 return 0;
1696 /* Check for missing mandatory parameters. */
1697 sctp_walk_params(param, peer_init, init_hdr.params) {
1699 if (SCTP_PARAM_STATE_COOKIE == param.p->type)
1700 has_cookie = 1;
1702 } /* for (loop through all parameters) */
1704 /* The only missing mandatory param possible today is
1705 * the state cookie for an INIT-ACK chunk.
1707 if ((SCTP_CID_INIT_ACK == cid) && !has_cookie) {
1708 sctp_process_missing_param(asoc, SCTP_PARAM_STATE_COOKIE,
1709 chunk, errp);
1710 return 0;
1713 /* Find unrecognized parameters. */
1715 sctp_walk_params(param, peer_init, init_hdr.params) {
1717 if (!sctp_verify_param(asoc, param, cid, chunk, errp)) {
1718 if (SCTP_PARAM_HOST_NAME_ADDRESS == param.p->type)
1719 return 0;
1720 else
1721 return 1;
1724 } /* for (loop through all parameters) */
1726 return 1;
1729 /* Unpack the parameters in an INIT packet into an association.
1730 * Returns 0 on failure, else success.
1731 * FIXME: This is an association method.
1733 int sctp_process_init(struct sctp_association *asoc, sctp_cid_t cid,
1734 const union sctp_addr *peer_addr,
1735 sctp_init_chunk_t *peer_init, int gfp)
1737 union sctp_params param;
1738 struct sctp_transport *transport;
1739 struct list_head *pos, *temp;
1740 char *cookie;
1742 /* We must include the address that the INIT packet came from.
1743 * This is the only address that matters for an INIT packet.
1744 * When processing a COOKIE ECHO, we retrieve the from address
1745 * of the INIT from the cookie.
1748 /* This implementation defaults to making the first transport
1749 * added as the primary transport. The source address seems to
1750 * be a a better choice than any of the embedded addresses.
1752 if (peer_addr)
1753 if(!sctp_assoc_add_peer(asoc, peer_addr, gfp))
1754 goto nomem;
1756 /* Process the initialization parameters. */
1758 sctp_walk_params(param, peer_init, init_hdr.params) {
1760 if (!sctp_process_param(asoc, param, peer_addr, gfp))
1761 goto clean_up;
1764 /* The fixed INIT headers are always in network byte
1765 * order.
1767 asoc->peer.i.init_tag =
1768 ntohl(peer_init->init_hdr.init_tag);
1769 asoc->peer.i.a_rwnd =
1770 ntohl(peer_init->init_hdr.a_rwnd);
1771 asoc->peer.i.num_outbound_streams =
1772 ntohs(peer_init->init_hdr.num_outbound_streams);
1773 asoc->peer.i.num_inbound_streams =
1774 ntohs(peer_init->init_hdr.num_inbound_streams);
1775 asoc->peer.i.initial_tsn =
1776 ntohl(peer_init->init_hdr.initial_tsn);
1778 /* Apply the upper bounds for output streams based on peer's
1779 * number of inbound streams.
1781 if (asoc->c.sinit_num_ostreams >
1782 ntohs(peer_init->init_hdr.num_inbound_streams)) {
1783 asoc->c.sinit_num_ostreams =
1784 ntohs(peer_init->init_hdr.num_inbound_streams);
1787 if (asoc->c.sinit_max_instreams >
1788 ntohs(peer_init->init_hdr.num_outbound_streams)) {
1789 asoc->c.sinit_max_instreams =
1790 ntohs(peer_init->init_hdr.num_outbound_streams);
1793 /* Copy Initiation tag from INIT to VT_peer in cookie. */
1794 asoc->c.peer_vtag = asoc->peer.i.init_tag;
1796 /* Peer Rwnd : Current calculated value of the peer's rwnd. */
1797 asoc->peer.rwnd = asoc->peer.i.a_rwnd;
1799 /* Copy cookie in case we need to resend COOKIE-ECHO. */
1800 cookie = asoc->peer.cookie;
1801 if (cookie) {
1802 asoc->peer.cookie = kmalloc(asoc->peer.cookie_len, gfp);
1803 if (!asoc->peer.cookie)
1804 goto clean_up;
1805 memcpy(asoc->peer.cookie, cookie, asoc->peer.cookie_len);
1808 /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily
1809 * high (for example, implementations MAY use the size of the receiver
1810 * advertised window).
1812 list_for_each(pos, &asoc->peer.transport_addr_list) {
1813 transport = list_entry(pos, struct sctp_transport, transports);
1814 transport->ssthresh = asoc->peer.i.a_rwnd;
1817 /* Set up the TSN tracking pieces. */
1818 sctp_tsnmap_init(&asoc->peer.tsn_map, SCTP_TSN_MAP_SIZE,
1819 asoc->peer.i.initial_tsn);
1821 /* RFC 2960 6.5 Stream Identifier and Stream Sequence Number
1823 * The stream sequence number in all the streams shall start
1824 * from 0 when the association is established. Also, when the
1825 * stream sequence number reaches the value 65535 the next
1826 * stream sequence number shall be set to 0.
1829 /* Allocate storage for the negotiated streams. */
1830 asoc->ssnmap = sctp_ssnmap_new(asoc->peer.i.num_outbound_streams,
1831 asoc->c.sinit_num_ostreams, gfp);
1832 if (!asoc->ssnmap)
1833 goto nomem_ssnmap;
1835 /* ADDIP Section 4.1 ASCONF Chunk Procedures
1837 * When an endpoint has an ASCONF signaled change to be sent to the
1838 * remote endpoint it should do the following:
1839 * ...
1840 * A2) A serial number should be assigned to the Chunk. The serial
1841 * number should be a monotonically increasing number. All serial
1842 * numbers are defined to be initialized at the start of the
1843 * association to the same value as the Initial TSN.
1845 asoc->peer.addip_serial = asoc->peer.i.initial_tsn - 1;
1846 return 1;
1848 nomem_ssnmap:
1849 clean_up:
1850 /* Release the transport structures. */
1851 list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) {
1852 transport = list_entry(pos, struct sctp_transport, transports);
1853 list_del(pos);
1854 sctp_transport_free(transport);
1856 nomem:
1857 return 0;
1861 /* Update asoc with the option described in param.
1863 * RFC2960 3.3.2.1 Optional/Variable Length Parameters in INIT
1865 * asoc is the association to update.
1866 * param is the variable length parameter to use for update.
1867 * cid tells us if this is an INIT, INIT ACK or COOKIE ECHO.
1868 * If the current packet is an INIT we want to minimize the amount of
1869 * work we do. In particular, we should not build transport
1870 * structures for the addresses.
1872 int sctp_process_param(struct sctp_association *asoc, union sctp_params param,
1873 const union sctp_addr *peer_addr, int gfp)
1875 union sctp_addr addr;
1876 int i;
1877 __u16 sat;
1878 int retval = 1;
1879 sctp_scope_t scope;
1880 time_t stale;
1882 /* We maintain all INIT parameters in network byte order all the
1883 * time. This allows us to not worry about whether the parameters
1884 * came from a fresh INIT, and INIT ACK, or were stored in a cookie.
1886 switch (param.p->type) {
1887 case SCTP_PARAM_IPV6_ADDRESS:
1888 if (PF_INET6 != asoc->base.sk->sk_family)
1889 break;
1890 /* Fall through. */
1891 case SCTP_PARAM_IPV4_ADDRESS:
1892 sctp_param2sockaddr(&addr, param.addr, asoc->peer.port, 0);
1893 scope = sctp_scope(peer_addr);
1894 if (sctp_in_scope(&addr, scope))
1895 if (!sctp_assoc_add_peer(asoc, &addr, gfp))
1896 return 0;
1897 break;
1899 case SCTP_PARAM_COOKIE_PRESERVATIVE:
1900 if (!sctp_cookie_preserve_enable)
1901 break;
1903 stale = ntohl(param.life->lifespan_increment);
1905 /* Suggested Cookie Life span increment's unit is msec,
1906 * (1/1000sec).
1908 asoc->cookie_life.tv_sec += stale / 1000;
1909 asoc->cookie_life.tv_usec += (stale % 1000) * 1000;
1910 break;
1912 case SCTP_PARAM_HOST_NAME_ADDRESS:
1913 SCTP_DEBUG_PRINTK("unimplemented SCTP_HOST_NAME_ADDRESS\n");
1914 break;
1916 case SCTP_PARAM_SUPPORTED_ADDRESS_TYPES:
1917 /* Turn off the default values first so we'll know which
1918 * ones are really set by the peer.
1920 asoc->peer.ipv4_address = 0;
1921 asoc->peer.ipv6_address = 0;
1923 /* Cycle through address types; avoid divide by 0. */
1924 sat = ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1925 if (sat)
1926 sat /= sizeof(__u16);
1928 for (i = 0; i < sat; ++i) {
1929 switch (param.sat->types[i]) {
1930 case SCTP_PARAM_IPV4_ADDRESS:
1931 asoc->peer.ipv4_address = 1;
1932 break;
1934 case SCTP_PARAM_IPV6_ADDRESS:
1935 asoc->peer.ipv6_address = 1;
1936 break;
1938 case SCTP_PARAM_HOST_NAME_ADDRESS:
1939 asoc->peer.hostname_address = 1;
1940 break;
1942 default: /* Just ignore anything else. */
1943 break;
1946 break;
1948 case SCTP_PARAM_STATE_COOKIE:
1949 asoc->peer.cookie_len =
1950 ntohs(param.p->length) - sizeof(sctp_paramhdr_t);
1951 asoc->peer.cookie = param.cookie->body;
1952 break;
1954 case SCTP_PARAM_HEARTBEAT_INFO:
1955 /* Would be odd to receive, but it causes no problems. */
1956 break;
1958 case SCTP_PARAM_UNRECOGNIZED_PARAMETERS:
1959 /* Rejected during verify stage. */
1960 break;
1962 case SCTP_PARAM_ECN_CAPABLE:
1963 asoc->peer.ecn_capable = 1;
1964 break;
1966 default:
1967 /* Any unrecognized parameters should have been caught
1968 * and handled by sctp_verify_param() which should be
1969 * called prior to this routine. Simply log the error
1970 * here.
1972 SCTP_DEBUG_PRINTK("Ignoring param: %d for association %p.\n",
1973 ntohs(param.p->type), asoc);
1974 break;
1977 return retval;
1980 /* Select a new verification tag. */
1981 __u32 sctp_generate_tag(const struct sctp_endpoint *ep)
1983 /* I believe that this random number generator complies with RFC1750.
1984 * A tag of 0 is reserved for special cases (e.g. INIT).
1986 __u32 x;
1988 do {
1989 get_random_bytes(&x, sizeof(__u32));
1990 } while (x == 0);
1992 return x;
1995 /* Select an initial TSN to send during startup. */
1996 __u32 sctp_generate_tsn(const struct sctp_endpoint *ep)
1998 __u32 retval;
2000 get_random_bytes(&retval, sizeof(__u32));
2001 return retval;
2004 /********************************************************************
2005 * 4th Level Abstractions
2006 ********************************************************************/
2008 /* Convert from an SCTP IP parameter to a union sctp_addr. */
2009 void sctp_param2sockaddr(union sctp_addr *addr, union sctp_addr_param *param,
2010 __u16 port, int iif)
2012 switch(param->v4.param_hdr.type) {
2013 case SCTP_PARAM_IPV4_ADDRESS:
2014 addr->v4.sin_family = AF_INET;
2015 addr->v4.sin_port = port;
2016 addr->v4.sin_addr.s_addr = param->v4.addr.s_addr;
2017 break;
2019 case SCTP_PARAM_IPV6_ADDRESS:
2020 addr->v6.sin6_family = AF_INET6;
2021 addr->v6.sin6_port = port;
2022 addr->v6.sin6_flowinfo = 0; /* BUG */
2023 addr->v6.sin6_addr = param->v6.addr;
2024 addr->v6.sin6_scope_id = iif;
2025 break;
2027 default:
2028 SCTP_DEBUG_PRINTK("Illegal address type %d\n",
2029 ntohs(param->v4.param_hdr.type));
2030 break;
2034 /* Convert an IP address in an SCTP param into a sockaddr_in. */
2035 /* Returns true if a valid conversion was possible. */
2036 int sctp_addr2sockaddr(union sctp_params p, union sctp_addr *sa)
2038 switch (p.p->type) {
2039 case SCTP_PARAM_IPV4_ADDRESS:
2040 sa->v4.sin_addr = *((struct in_addr *)&p.v4->addr);
2041 sa->v4.sin_family = AF_INET;
2042 break;
2044 case SCTP_PARAM_IPV6_ADDRESS:
2045 *((struct in6_addr *)&sa->v4.sin_addr)
2046 = p.v6->addr;
2047 sa->v4.sin_family = AF_INET6;
2048 break;
2050 default:
2051 return 0;
2054 return 1;
2057 /* Convert a sockaddr_in to an IP address in an SCTP param.
2058 * Returns len if a valid conversion was possible.
2060 int sockaddr2sctp_addr(const union sctp_addr *sa, union sctp_addr_param *p)
2062 int len = 0;
2064 switch (sa->v4.sin_family) {
2065 case AF_INET:
2066 p->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS;
2067 p->v4.param_hdr.length = ntohs(sizeof(sctp_ipv4addr_param_t));
2068 len = sizeof(sctp_ipv4addr_param_t);
2069 p->v4.addr.s_addr = sa->v4.sin_addr.s_addr;
2070 break;
2072 case AF_INET6:
2073 p->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
2074 p->v6.param_hdr.length = ntohs(sizeof(sctp_ipv6addr_param_t));
2075 len = sizeof(sctp_ipv6addr_param_t);
2076 p->v6.addr = *(&sa->v6.sin6_addr);
2077 break;
2079 default:
2080 printk(KERN_WARNING "sockaddr2sctp_addr: Illegal family %d.\n",
2081 sa->v4.sin_family);
2082 return 0;
2085 return len;