]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - net/sctp/endpointola.c
[PKT_SCHED]: vlan tag match
[mirror_ubuntu-zesty-kernel.git] / net / sctp / endpointola.c
CommitLineData
1da177e4
LT
1/* SCTP kernel reference Implementation
2 * Copyright (c) 1999-2000 Cisco, Inc.
3 * Copyright (c) 1999-2001 Motorola, Inc.
4 * Copyright (c) 2001-2002 International Business Machines, Corp.
5 * Copyright (c) 2001 Intel Corp.
6 * Copyright (c) 2001 Nokia, Inc.
7 * Copyright (c) 2001 La Monte H.P. Yarroll
8 *
9 * This file is part of the SCTP kernel reference Implementation
10 *
11 * This abstraction represents an SCTP endpoint.
12 *
13 * This file is part of the implementation of the add-IP extension,
14 * based on <draft-ietf-tsvwg-addip-sctp-02.txt> June 29, 2001,
15 * for the SCTP kernel reference Implementation.
16 *
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.
22 *
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.
28 *
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.
33 *
34 * Please send any bug reports or fixes you make to the
35 * email address(es):
36 * lksctp developers <lksctp-developers@lists.sourceforge.net>
37 *
38 * Or submit a bug report through the following website:
39 * http://www.sf.net/projects/lksctp
40 *
41 * Written or modified by:
42 * La Monte H.P. Yarroll <piggy@acm.org>
43 * Karl Knutson <karl@athena.chicago.il.us>
44 * Jon Grimm <jgrimm@austin.ibm.com>
45 * Daisy Chang <daisyc@us.ibm.com>
46 * Dajiang Zhang <dajiang.zhang@nokia.com>
47 *
48 * Any bugs reported given to us we will try to fix... any fixes shared will
49 * be incorporated into the next SCTP release.
50 */
51
52#include <linux/types.h>
1da177e4
LT
53#include <linux/slab.h>
54#include <linux/in.h>
55#include <linux/random.h> /* get_random_bytes() */
56#include <linux/crypto.h>
57#include <net/sock.h>
58#include <net/ipv6.h>
59#include <net/sctp/sctp.h>
60#include <net/sctp/sm.h>
61
62/* Forward declarations for internal helpers. */
c4028958 63static void sctp_endpoint_bh_rcv(struct work_struct *work);
1da177e4
LT
64
65/*
66 * Initialize the base fields of the endpoint structure.
67 */
68static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
3182cd84 69 struct sock *sk,
dd0fc66f 70 gfp_t gfp)
1da177e4 71{
a29a5bd4
VY
72 struct sctp_hmac_algo_param *auth_hmacs = NULL;
73 struct sctp_chunks_param *auth_chunks = NULL;
74 struct sctp_shared_key *null_key;
75 int err;
76
1da177e4
LT
77 memset(ep, 0, sizeof(struct sctp_endpoint));
78
b68dbcab
VY
79 ep->digest = kzalloc(SCTP_SIGNATURE_SIZE, gfp);
80 if (!ep->digest)
81 return NULL;
82
a29a5bd4
VY
83 if (sctp_auth_enable) {
84 /* Allocate space for HMACS and CHUNKS authentication
85 * variables. There are arrays that we encode directly
86 * into parameters to make the rest of the operations easier.
87 */
88 auth_hmacs = kzalloc(sizeof(sctp_hmac_algo_param_t) +
89 sizeof(__u16) * SCTP_AUTH_NUM_HMACS, gfp);
90 if (!auth_hmacs)
91 goto nomem;
92
93 auth_chunks = kzalloc(sizeof(sctp_chunks_param_t) +
94 SCTP_NUM_CHUNK_TYPES, gfp);
95 if (!auth_chunks)
96 goto nomem;
97
98 /* Initialize the HMACS parameter.
99 * SCTP-AUTH: Section 3.3
100 * Every endpoint supporting SCTP chunk authentication MUST
101 * support the HMAC based on the SHA-1 algorithm.
102 */
103 auth_hmacs->param_hdr.type = SCTP_PARAM_HMAC_ALGO;
104 auth_hmacs->param_hdr.length =
105 htons(sizeof(sctp_paramhdr_t) + 2);
106 auth_hmacs->hmac_ids[0] = htons(SCTP_AUTH_HMAC_ID_SHA1);
107
108 /* Initialize the CHUNKS parameter */
109 auth_chunks->param_hdr.type = SCTP_PARAM_CHUNKS;
110
111 /* If the Add-IP functionality is enabled, we must
112 * authenticate, ASCONF and ASCONF-ACK chunks
113 */
114 if (sctp_addip_enable) {
115 auth_chunks->chunks[0] = SCTP_CID_ASCONF;
116 auth_chunks->chunks[1] = SCTP_CID_ASCONF_ACK;
117 auth_chunks->param_hdr.length =
118 htons(sizeof(sctp_paramhdr_t) + 2);
119 }
120 }
121
1da177e4
LT
122 /* Initialize the base structure. */
123 /* What type of endpoint are we? */
124 ep->base.type = SCTP_EP_TYPE_SOCKET;
125
126 /* Initialize the basic object fields. */
127 atomic_set(&ep->base.refcnt, 1);
128 ep->base.dead = 0;
129 ep->base.malloced = 1;
130
131 /* Create an input queue. */
132 sctp_inq_init(&ep->base.inqueue);
133
134 /* Set its top-half handler */
c4028958 135 sctp_inq_set_th_handler(&ep->base.inqueue, sctp_endpoint_bh_rcv);
1da177e4
LT
136
137 /* Initialize the bind addr area */
138 sctp_bind_addr_init(&ep->base.bind_addr, 0);
1da177e4
LT
139
140 /* Remember who we are attached to. */
141 ep->base.sk = sk;
142 sock_hold(ep->base.sk);
143
144 /* Create the lists of associations. */
145 INIT_LIST_HEAD(&ep->asocs);
146
1da177e4 147 /* Use SCTP specific send buffer space queues. */
4eb701df 148 ep->sndbuf_policy = sctp_sndbuf_policy;
4d93df0a 149
1da177e4
LT
150 sk->sk_write_space = sctp_write_space;
151 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
152
049b3ff5
NH
153 /* Get the receive buffer policy for this endpoint */
154 ep->rcvbuf_policy = sctp_rcvbuf_policy;
155
1da177e4
LT
156 /* Initialize the secret key used with cookie. */
157 get_random_bytes(&ep->secret_key[0], SCTP_SECRET_SIZE);
158 ep->last_key = ep->current_key = 0;
159 ep->key_changed_at = jiffies;
160
a29a5bd4
VY
161 /* SCTP-AUTH extensions*/
162 INIT_LIST_HEAD(&ep->endpoint_shared_keys);
163 null_key = sctp_auth_shkey_create(0, GFP_KERNEL);
164 if (!null_key)
165 goto nomem;
166
167 list_add(&null_key->key_list, &ep->endpoint_shared_keys);
168
169 /* Allocate and initialize transorms arrays for suported HMACs. */
170 err = sctp_auth_init_hmacs(ep, gfp);
171 if (err)
172 goto nomem_hmacs;
173
174 /* Add the null key to the endpoint shared keys list and
175 * set the hmcas and chunks pointers.
176 */
177 ep->auth_hmacs_list = auth_hmacs;
178 ep->auth_chunk_list = auth_chunks;
179
1da177e4 180 return ep;
a29a5bd4
VY
181
182nomem_hmacs:
183 sctp_auth_destroy_keys(&ep->endpoint_shared_keys);
184nomem:
185 /* Free all allocations */
186 kfree(auth_hmacs);
187 kfree(auth_chunks);
188 kfree(ep->digest);
189 return NULL;
190
1da177e4
LT
191}
192
193/* Create a sctp_endpoint with all that boring stuff initialized.
194 * Returns NULL if there isn't enough memory.
195 */
dd0fc66f 196struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, gfp_t gfp)
1da177e4
LT
197{
198 struct sctp_endpoint *ep;
199
200 /* Build a local endpoint. */
201 ep = t_new(struct sctp_endpoint, gfp);
202 if (!ep)
203 goto fail;
204 if (!sctp_endpoint_init(ep, sk, gfp))
205 goto fail_init;
206 ep->base.malloced = 1;
207 SCTP_DBG_OBJCNT_INC(ep);
208 return ep;
209
210fail_init:
211 kfree(ep);
212fail:
213 return NULL;
214}
215
216/* Add an association to an endpoint. */
217void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
218 struct sctp_association *asoc)
219{
220 struct sock *sk = ep->base.sk;
221
de76e695
VY
222 /* If this is a temporary association, don't bother
223 * since we'll be removing it shortly and don't
224 * want anyone to find it anyway.
225 */
226 if (asoc->temp)
227 return;
228
1da177e4
LT
229 /* Now just add it to our list of asocs */
230 list_add_tail(&asoc->asocs, &ep->asocs);
231
232 /* Increment the backlog value for a TCP-style listening socket. */
233 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
234 sk->sk_ack_backlog++;
235}
236
237/* Free the endpoint structure. Delay cleanup until
238 * all users have released their reference count on this structure.
239 */
240void sctp_endpoint_free(struct sctp_endpoint *ep)
241{
242 ep->base.dead = 1;
cfdeef32
VY
243
244 ep->base.sk->sk_state = SCTP_SS_CLOSED;
245
246 /* Unlink this endpoint, so we can't find it again! */
247 sctp_unhash_endpoint(ep);
248
1da177e4
LT
249 sctp_endpoint_put(ep);
250}
251
252/* Final destructor for endpoint. */
253static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
254{
255 SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
256
1da177e4 257 /* Free up the HMAC transform. */
1b489e11 258 crypto_free_hash(sctp_sk(ep->base.sk)->hmac);
1da177e4 259
b68dbcab
VY
260 /* Free the digest buffer */
261 kfree(ep->digest);
262
a29a5bd4
VY
263 /* SCTP-AUTH: Free up AUTH releated data such as shared keys
264 * chunks and hmacs arrays that were allocated
265 */
266 sctp_auth_destroy_keys(&ep->endpoint_shared_keys);
267 kfree(ep->auth_hmacs_list);
268 kfree(ep->auth_chunk_list);
269
270 /* AUTH - Free any allocated HMAC transform containers */
271 sctp_auth_destroy_hmacs(ep->auth_hmacs);
272
1da177e4
LT
273 /* Cleanup. */
274 sctp_inq_free(&ep->base.inqueue);
275 sctp_bind_addr_free(&ep->base.bind_addr);
276
277 /* Remove and free the port */
278 if (sctp_sk(ep->base.sk)->bind_hash)
279 sctp_put_port(ep->base.sk);
280
281 /* Give up our hold on the sock. */
282 if (ep->base.sk)
283 sock_put(ep->base.sk);
284
285 /* Finally, free up our memory. */
286 if (ep->base.malloced) {
287 kfree(ep);
288 SCTP_DBG_OBJCNT_DEC(ep);
289 }
290}
291
292/* Hold a reference to an endpoint. */
293void sctp_endpoint_hold(struct sctp_endpoint *ep)
294{
295 atomic_inc(&ep->base.refcnt);
296}
297
298/* Release a reference to an endpoint and clean up if there are
299 * no more references.
300 */
301void sctp_endpoint_put(struct sctp_endpoint *ep)
302{
303 if (atomic_dec_and_test(&ep->base.refcnt))
304 sctp_endpoint_destroy(ep);
305}
306
307/* Is this the endpoint we are looking for? */
308struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
309 const union sctp_addr *laddr)
310{
559cf710 311 struct sctp_endpoint *retval = NULL;
1da177e4 312
1c7d1fc1 313 if (htons(ep->base.bind_addr.port) == laddr->v4.sin_port) {
1da177e4 314 if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
559cf710 315 sctp_sk(ep->base.sk)))
1da177e4 316 retval = ep;
1da177e4
LT
317 }
318
1da177e4
LT
319 return retval;
320}
321
322/* Find the association that goes with this chunk.
323 * We do a linear search of the associations for this endpoint.
324 * We return the matching transport address too.
325 */
326static struct sctp_association *__sctp_endpoint_lookup_assoc(
327 const struct sctp_endpoint *ep,
328 const union sctp_addr *paddr,
329 struct sctp_transport **transport)
330{
123ed979
VY
331 struct sctp_association *asoc = NULL;
332 struct sctp_transport *t = NULL;
333 struct sctp_hashbucket *head;
334 struct sctp_ep_common *epb;
d970dbf8 335 struct hlist_node *node;
123ed979 336 int hash;
1da177e4 337 int rport;
1da177e4 338
123ed979 339 *transport = NULL;
cd4ff034 340 rport = ntohs(paddr->v4.sin_port);
1da177e4 341
123ed979
VY
342 hash = sctp_assoc_hashfn(ep->base.bind_addr.port, rport);
343 head = &sctp_assoc_hashtable[hash];
344 read_lock(&head->lock);
d970dbf8 345 sctp_for_each_hentry(epb, node, &head->chain) {
123ed979
VY
346 asoc = sctp_assoc(epb);
347 if (asoc->ep != ep || rport != asoc->peer.port)
348 goto next;
349
350 t = sctp_assoc_lookup_paddr(asoc, paddr);
351 if (t) {
352 *transport = t;
353 break;
1da177e4 354 }
123ed979
VY
355next:
356 asoc = NULL;
1da177e4 357 }
123ed979
VY
358 read_unlock(&head->lock);
359 return asoc;
1da177e4
LT
360}
361
362/* Lookup association on an endpoint based on a peer address. BH-safe. */
363struct sctp_association *sctp_endpoint_lookup_assoc(
364 const struct sctp_endpoint *ep,
365 const union sctp_addr *paddr,
366 struct sctp_transport **transport)
367{
368 struct sctp_association *asoc;
369
370 sctp_local_bh_disable();
371 asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
372 sctp_local_bh_enable();
373
374 return asoc;
375}
376
377/* Look for any peeled off association from the endpoint that matches the
378 * given peer address.
379 */
380int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
381 const union sctp_addr *paddr)
382{
1da177e4
LT
383 struct sctp_sockaddr_entry *addr;
384 struct sctp_bind_addr *bp;
385
1da177e4 386 bp = &ep->base.bind_addr;
559cf710
VY
387 /* This function is called with the socket lock held,
388 * so the address_list can not change.
389 */
390 list_for_each_entry(addr, &bp->address_list, list) {
391 if (sctp_has_association(&addr->a, paddr))
1da177e4 392 return 1;
1da177e4 393 }
1da177e4
LT
394
395 return 0;
396}
397
398/* Do delayed input processing. This is scheduled by sctp_rcv().
399 * This may be called on BH or task time.
400 */
c4028958 401static void sctp_endpoint_bh_rcv(struct work_struct *work)
1da177e4 402{
c4028958
DH
403 struct sctp_endpoint *ep =
404 container_of(work, struct sctp_endpoint,
405 base.inqueue.immediate);
1da177e4
LT
406 struct sctp_association *asoc;
407 struct sock *sk;
408 struct sctp_transport *transport;
409 struct sctp_chunk *chunk;
410 struct sctp_inq *inqueue;
411 sctp_subtype_t subtype;
412 sctp_state_t state;
413 int error = 0;
bbd0d598 414 int first_time = 1; /* is this the first time through the looop */
1da177e4
LT
415
416 if (ep->base.dead)
417 return;
418
419 asoc = NULL;
420 inqueue = &ep->base.inqueue;
421 sk = ep->base.sk;
422
423 while (NULL != (chunk = sctp_inq_pop(inqueue))) {
424 subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
425
bbd0d598
VY
426 /* If the first chunk in the packet is AUTH, do special
427 * processing specified in Section 6.3 of SCTP-AUTH spec
428 */
429 if (first_time && (subtype.chunk == SCTP_CID_AUTH)) {
430 struct sctp_chunkhdr *next_hdr;
431
432 next_hdr = sctp_inq_peek(inqueue);
433 if (!next_hdr)
434 goto normal;
435
436 /* If the next chunk is COOKIE-ECHO, skip the AUTH
437 * chunk while saving a pointer to it so we can do
438 * Authentication later (during cookie-echo
439 * processing).
440 */
441 if (next_hdr->type == SCTP_CID_COOKIE_ECHO) {
442 chunk->auth_chunk = skb_clone(chunk->skb,
443 GFP_ATOMIC);
444 chunk->auth = 1;
445 continue;
446 }
447 }
448normal:
1da177e4
LT
449 /* We might have grown an association since last we
450 * looked, so try again.
451 *
452 * This happens when we've just processed our
453 * COOKIE-ECHO chunk.
454 */
455 if (NULL == chunk->asoc) {
456 asoc = sctp_endpoint_lookup_assoc(ep,
457 sctp_source(chunk),
458 &transport);
459 chunk->asoc = asoc;
460 chunk->transport = transport;
461 }
462
463 state = asoc ? asoc->state : SCTP_STATE_CLOSED;
bbd0d598
VY
464 if (sctp_auth_recv_cid(subtype.chunk, asoc) && !chunk->auth)
465 continue;
1da177e4
LT
466
467 /* Remember where the last DATA chunk came from so we
468 * know where to send the SACK.
469 */
470 if (asoc && sctp_chunk_is_data(chunk))
471 asoc->peer.last_data_from = chunk->transport;
472 else
473 SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS);
474
475 if (chunk->transport)
476 chunk->transport->last_time_heard = jiffies;
477
478 error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
d808ad9a 479 ep, asoc, chunk, GFP_ATOMIC);
1da177e4
LT
480
481 if (error && chunk)
482 chunk->pdiscard = 1;
483
484 /* Check to see if the endpoint is freed in response to
485 * the incoming chunk. If so, get out of the while loop.
486 */
487 if (!sctp_sk(sk)->ep)
488 break;
bbd0d598
VY
489
490 if (first_time)
491 first_time = 0;
1da177e4
LT
492 }
493}