xref: /openbmc/linux/net/sctp/endpointola.c (revision 96de0e252cedffad61b3cb5e05662c591898e69a)
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>
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. */
63 static void sctp_endpoint_bh_rcv(struct work_struct *work);
64 
65 /*
66  * Initialize the base fields of the endpoint structure.
67  */
68 static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
69 						struct sock *sk,
70 						gfp_t gfp)
71 {
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 
77 	memset(ep, 0, sizeof(struct sctp_endpoint));
78 
79 	ep->digest = kzalloc(SCTP_SIGNATURE_SIZE, gfp);
80 	if (!ep->digest)
81 		return NULL;
82 
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 
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 */
135 	sctp_inq_set_th_handler(&ep->base.inqueue, sctp_endpoint_bh_rcv);
136 
137 	/* Initialize the bind addr area */
138 	sctp_bind_addr_init(&ep->base.bind_addr, 0);
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 
147 	/* Use SCTP specific send buffer space queues.  */
148 	ep->sndbuf_policy = sctp_sndbuf_policy;
149 
150 	sk->sk_write_space = sctp_write_space;
151 	sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
152 
153 	/* Get the receive buffer policy for this endpoint */
154 	ep->rcvbuf_policy = sctp_rcvbuf_policy;
155 
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 
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 
180 	return ep;
181 
182 nomem_hmacs:
183 	sctp_auth_destroy_keys(&ep->endpoint_shared_keys);
184 nomem:
185 	/* Free all allocations */
186 	kfree(auth_hmacs);
187 	kfree(auth_chunks);
188 	kfree(ep->digest);
189 	return NULL;
190 
191 }
192 
193 /* Create a sctp_endpoint with all that boring stuff initialized.
194  * Returns NULL if there isn't enough memory.
195  */
196 struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, gfp_t gfp)
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 
210 fail_init:
211 	kfree(ep);
212 fail:
213 	return NULL;
214 }
215 
216 /* Add an association to an endpoint.  */
217 void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
218 			    struct sctp_association *asoc)
219 {
220 	struct sock *sk = ep->base.sk;
221 
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 
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  */
240 void sctp_endpoint_free(struct sctp_endpoint *ep)
241 {
242 	ep->base.dead = 1;
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 
249 	sctp_endpoint_put(ep);
250 }
251 
252 /* Final destructor for endpoint.  */
253 static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
254 {
255 	SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
256 
257 	/* Free up the HMAC transform. */
258 	crypto_free_hash(sctp_sk(ep->base.sk)->hmac);
259 
260 	/* Free the digest buffer */
261 	kfree(ep->digest);
262 
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 
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. */
293 void 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  */
301 void 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?  */
308 struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
309 					       const union sctp_addr *laddr)
310 {
311 	struct sctp_endpoint *retval = NULL;
312 
313 	if (htons(ep->base.bind_addr.port) == laddr->v4.sin_port) {
314 		if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
315 					 sctp_sk(ep->base.sk)))
316 			retval = ep;
317 	}
318 
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  */
326 static 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 {
331 	int rport;
332 	struct sctp_association *asoc;
333 	struct list_head *pos;
334 
335 	rport = ntohs(paddr->v4.sin_port);
336 
337 	list_for_each(pos, &ep->asocs) {
338 		asoc = list_entry(pos, struct sctp_association, asocs);
339 		if (rport == asoc->peer.port) {
340 			*transport = sctp_assoc_lookup_paddr(asoc, paddr);
341 
342 			if (*transport)
343 				return asoc;
344 		}
345 	}
346 
347 	*transport = NULL;
348 	return NULL;
349 }
350 
351 /* Lookup association on an endpoint based on a peer address.  BH-safe.  */
352 struct sctp_association *sctp_endpoint_lookup_assoc(
353 	const struct sctp_endpoint *ep,
354 	const union sctp_addr *paddr,
355 	struct sctp_transport **transport)
356 {
357 	struct sctp_association *asoc;
358 
359 	sctp_local_bh_disable();
360 	asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
361 	sctp_local_bh_enable();
362 
363 	return asoc;
364 }
365 
366 /* Look for any peeled off association from the endpoint that matches the
367  * given peer address.
368  */
369 int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
370 				const union sctp_addr *paddr)
371 {
372 	struct sctp_sockaddr_entry *addr;
373 	struct sctp_bind_addr *bp;
374 
375 	bp = &ep->base.bind_addr;
376 	/* This function is called with the socket lock held,
377 	 * so the address_list can not change.
378 	 */
379 	list_for_each_entry(addr, &bp->address_list, list) {
380 		if (sctp_has_association(&addr->a, paddr))
381 			return 1;
382 	}
383 
384 	return 0;
385 }
386 
387 /* Do delayed input processing.  This is scheduled by sctp_rcv().
388  * This may be called on BH or task time.
389  */
390 static void sctp_endpoint_bh_rcv(struct work_struct *work)
391 {
392 	struct sctp_endpoint *ep =
393 		container_of(work, struct sctp_endpoint,
394 			     base.inqueue.immediate);
395 	struct sctp_association *asoc;
396 	struct sock *sk;
397 	struct sctp_transport *transport;
398 	struct sctp_chunk *chunk;
399 	struct sctp_inq *inqueue;
400 	sctp_subtype_t subtype;
401 	sctp_state_t state;
402 	int error = 0;
403 	int first_time = 1;	/* is this the first time through the looop */
404 
405 	if (ep->base.dead)
406 		return;
407 
408 	asoc = NULL;
409 	inqueue = &ep->base.inqueue;
410 	sk = ep->base.sk;
411 
412 	while (NULL != (chunk = sctp_inq_pop(inqueue))) {
413 		subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
414 
415 		/* If the first chunk in the packet is AUTH, do special
416 		 * processing specified in Section 6.3 of SCTP-AUTH spec
417 		 */
418 		if (first_time && (subtype.chunk == SCTP_CID_AUTH)) {
419 			struct sctp_chunkhdr *next_hdr;
420 
421 			next_hdr = sctp_inq_peek(inqueue);
422 			if (!next_hdr)
423 				goto normal;
424 
425 			/* If the next chunk is COOKIE-ECHO, skip the AUTH
426 			 * chunk while saving a pointer to it so we can do
427 			 * Authentication later (during cookie-echo
428 			 * processing).
429 			 */
430 			if (next_hdr->type == SCTP_CID_COOKIE_ECHO) {
431 				chunk->auth_chunk = skb_clone(chunk->skb,
432 								GFP_ATOMIC);
433 				chunk->auth = 1;
434 				continue;
435 			}
436 		}
437 normal:
438 		/* We might have grown an association since last we
439 		 * looked, so try again.
440 		 *
441 		 * This happens when we've just processed our
442 		 * COOKIE-ECHO chunk.
443 		 */
444 		if (NULL == chunk->asoc) {
445 			asoc = sctp_endpoint_lookup_assoc(ep,
446 							  sctp_source(chunk),
447 							  &transport);
448 			chunk->asoc = asoc;
449 			chunk->transport = transport;
450 		}
451 
452 		state = asoc ? asoc->state : SCTP_STATE_CLOSED;
453 		if (sctp_auth_recv_cid(subtype.chunk, asoc) && !chunk->auth)
454 			continue;
455 
456 		/* Remember where the last DATA chunk came from so we
457 		 * know where to send the SACK.
458 		 */
459 		if (asoc && sctp_chunk_is_data(chunk))
460 			asoc->peer.last_data_from = chunk->transport;
461 		else
462 			SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS);
463 
464 		if (chunk->transport)
465 			chunk->transport->last_time_heard = jiffies;
466 
467 		error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
468 				   ep, asoc, chunk, GFP_ATOMIC);
469 
470 		if (error && chunk)
471 			chunk->pdiscard = 1;
472 
473 		/* Check to see if the endpoint is freed in response to
474 		 * the incoming chunk. If so, get out of the while loop.
475 		 */
476 		if (!sctp_sk(sk)->ep)
477 			break;
478 
479 		if (first_time)
480 			first_time = 0;
481 	}
482 }
483