[PATCH] PCMCIA: handle sysfs, PCI errors
[linux-2.6.22.y-op.git] / net / sctp / endpointola.c
blob35c49ff2d0621b445dbced33f74894663a189b4d
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
9 * This file is part of the SCTP kernel reference Implementation
11 * This abstraction represents an SCTP endpoint.
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.
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 * Jon Grimm <jgrimm@austin.ibm.com>
45 * Daisy Chang <daisyc@us.ibm.com>
46 * Dajiang Zhang <dajiang.zhang@nokia.com>
48 * Any bugs reported given to us we will try to fix... any fixes shared will
49 * be incorporated into the next SCTP release.
52 #include <linux/types.h>
53 #include <linux/sched.h>
54 #include <linux/slab.h>
55 #include <linux/in.h>
56 #include <linux/random.h> /* get_random_bytes() */
57 #include <linux/crypto.h>
58 #include <net/sock.h>
59 #include <net/ipv6.h>
60 #include <net/sctp/sctp.h>
61 #include <net/sctp/sm.h>
63 /* Forward declarations for internal helpers. */
64 static void sctp_endpoint_bh_rcv(struct sctp_endpoint *ep);
67 * Initialize the base fields of the endpoint structure.
69 static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
70 struct sock *sk,
71 gfp_t gfp)
73 memset(ep, 0, sizeof(struct sctp_endpoint));
75 /* Initialize the base structure. */
76 /* What type of endpoint are we? */
77 ep->base.type = SCTP_EP_TYPE_SOCKET;
79 /* Initialize the basic object fields. */
80 atomic_set(&ep->base.refcnt, 1);
81 ep->base.dead = 0;
82 ep->base.malloced = 1;
84 /* Create an input queue. */
85 sctp_inq_init(&ep->base.inqueue);
87 /* Set its top-half handler */
88 sctp_inq_set_th_handler(&ep->base.inqueue,
89 (void (*)(void *))sctp_endpoint_bh_rcv, ep);
91 /* Initialize the bind addr area */
92 sctp_bind_addr_init(&ep->base.bind_addr, 0);
93 rwlock_init(&ep->base.addr_lock);
95 /* Remember who we are attached to. */
96 ep->base.sk = sk;
97 sock_hold(ep->base.sk);
99 /* Create the lists of associations. */
100 INIT_LIST_HEAD(&ep->asocs);
102 /* Use SCTP specific send buffer space queues. */
103 ep->sndbuf_policy = sctp_sndbuf_policy;
104 sk->sk_write_space = sctp_write_space;
105 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
107 /* Get the receive buffer policy for this endpoint */
108 ep->rcvbuf_policy = sctp_rcvbuf_policy;
110 /* Initialize the secret key used with cookie. */
111 get_random_bytes(&ep->secret_key[0], SCTP_SECRET_SIZE);
112 ep->last_key = ep->current_key = 0;
113 ep->key_changed_at = jiffies;
115 return ep;
118 /* Create a sctp_endpoint with all that boring stuff initialized.
119 * Returns NULL if there isn't enough memory.
121 struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, gfp_t gfp)
123 struct sctp_endpoint *ep;
125 /* Build a local endpoint. */
126 ep = t_new(struct sctp_endpoint, gfp);
127 if (!ep)
128 goto fail;
129 if (!sctp_endpoint_init(ep, sk, gfp))
130 goto fail_init;
131 ep->base.malloced = 1;
132 SCTP_DBG_OBJCNT_INC(ep);
133 return ep;
135 fail_init:
136 kfree(ep);
137 fail:
138 return NULL;
141 /* Add an association to an endpoint. */
142 void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
143 struct sctp_association *asoc)
145 struct sock *sk = ep->base.sk;
147 /* Now just add it to our list of asocs */
148 list_add_tail(&asoc->asocs, &ep->asocs);
150 /* Increment the backlog value for a TCP-style listening socket. */
151 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
152 sk->sk_ack_backlog++;
155 /* Free the endpoint structure. Delay cleanup until
156 * all users have released their reference count on this structure.
158 void sctp_endpoint_free(struct sctp_endpoint *ep)
160 ep->base.dead = 1;
162 ep->base.sk->sk_state = SCTP_SS_CLOSED;
164 /* Unlink this endpoint, so we can't find it again! */
165 sctp_unhash_endpoint(ep);
167 sctp_endpoint_put(ep);
170 /* Final destructor for endpoint. */
171 static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
173 SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
175 /* Free up the HMAC transform. */
176 crypto_free_hash(sctp_sk(ep->base.sk)->hmac);
178 /* Cleanup. */
179 sctp_inq_free(&ep->base.inqueue);
180 sctp_bind_addr_free(&ep->base.bind_addr);
182 /* Remove and free the port */
183 if (sctp_sk(ep->base.sk)->bind_hash)
184 sctp_put_port(ep->base.sk);
186 /* Give up our hold on the sock. */
187 if (ep->base.sk)
188 sock_put(ep->base.sk);
190 /* Finally, free up our memory. */
191 if (ep->base.malloced) {
192 kfree(ep);
193 SCTP_DBG_OBJCNT_DEC(ep);
197 /* Hold a reference to an endpoint. */
198 void sctp_endpoint_hold(struct sctp_endpoint *ep)
200 atomic_inc(&ep->base.refcnt);
203 /* Release a reference to an endpoint and clean up if there are
204 * no more references.
206 void sctp_endpoint_put(struct sctp_endpoint *ep)
208 if (atomic_dec_and_test(&ep->base.refcnt))
209 sctp_endpoint_destroy(ep);
212 /* Is this the endpoint we are looking for? */
213 struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
214 const union sctp_addr *laddr)
216 struct sctp_endpoint *retval;
218 sctp_read_lock(&ep->base.addr_lock);
219 if (ep->base.bind_addr.port == laddr->v4.sin_port) {
220 if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
221 sctp_sk(ep->base.sk))) {
222 retval = ep;
223 goto out;
227 retval = NULL;
229 out:
230 sctp_read_unlock(&ep->base.addr_lock);
231 return retval;
234 /* Find the association that goes with this chunk.
235 * We do a linear search of the associations for this endpoint.
236 * We return the matching transport address too.
238 static struct sctp_association *__sctp_endpoint_lookup_assoc(
239 const struct sctp_endpoint *ep,
240 const union sctp_addr *paddr,
241 struct sctp_transport **transport)
243 int rport;
244 struct sctp_association *asoc;
245 struct list_head *pos;
247 rport = paddr->v4.sin_port;
249 list_for_each(pos, &ep->asocs) {
250 asoc = list_entry(pos, struct sctp_association, asocs);
251 if (rport == asoc->peer.port) {
252 sctp_read_lock(&asoc->base.addr_lock);
253 *transport = sctp_assoc_lookup_paddr(asoc, paddr);
254 sctp_read_unlock(&asoc->base.addr_lock);
256 if (*transport)
257 return asoc;
261 *transport = NULL;
262 return NULL;
265 /* Lookup association on an endpoint based on a peer address. BH-safe. */
266 struct sctp_association *sctp_endpoint_lookup_assoc(
267 const struct sctp_endpoint *ep,
268 const union sctp_addr *paddr,
269 struct sctp_transport **transport)
271 struct sctp_association *asoc;
273 sctp_local_bh_disable();
274 asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
275 sctp_local_bh_enable();
277 return asoc;
280 /* Look for any peeled off association from the endpoint that matches the
281 * given peer address.
283 int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
284 const union sctp_addr *paddr)
286 struct list_head *pos;
287 struct sctp_sockaddr_entry *addr;
288 struct sctp_bind_addr *bp;
290 sctp_read_lock(&ep->base.addr_lock);
291 bp = &ep->base.bind_addr;
292 list_for_each(pos, &bp->address_list) {
293 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
294 if (sctp_has_association(&addr->a, paddr)) {
295 sctp_read_unlock(&ep->base.addr_lock);
296 return 1;
299 sctp_read_unlock(&ep->base.addr_lock);
301 return 0;
304 /* Do delayed input processing. This is scheduled by sctp_rcv().
305 * This may be called on BH or task time.
307 static void sctp_endpoint_bh_rcv(struct sctp_endpoint *ep)
309 struct sctp_association *asoc;
310 struct sock *sk;
311 struct sctp_transport *transport;
312 struct sctp_chunk *chunk;
313 struct sctp_inq *inqueue;
314 sctp_subtype_t subtype;
315 sctp_state_t state;
316 int error = 0;
318 if (ep->base.dead)
319 return;
321 asoc = NULL;
322 inqueue = &ep->base.inqueue;
323 sk = ep->base.sk;
325 while (NULL != (chunk = sctp_inq_pop(inqueue))) {
326 subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
328 /* We might have grown an association since last we
329 * looked, so try again.
331 * This happens when we've just processed our
332 * COOKIE-ECHO chunk.
334 if (NULL == chunk->asoc) {
335 asoc = sctp_endpoint_lookup_assoc(ep,
336 sctp_source(chunk),
337 &transport);
338 chunk->asoc = asoc;
339 chunk->transport = transport;
342 state = asoc ? asoc->state : SCTP_STATE_CLOSED;
344 /* Remember where the last DATA chunk came from so we
345 * know where to send the SACK.
347 if (asoc && sctp_chunk_is_data(chunk))
348 asoc->peer.last_data_from = chunk->transport;
349 else
350 SCTP_INC_STATS(SCTP_MIB_INCTRLCHUNKS);
352 if (chunk->transport)
353 chunk->transport->last_time_heard = jiffies;
355 error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
356 ep, asoc, chunk, GFP_ATOMIC);
358 if (error && chunk)
359 chunk->pdiscard = 1;
361 /* Check to see if the endpoint is freed in response to
362 * the incoming chunk. If so, get out of the while loop.
364 if (!sctp_sk(sk)->ep)
365 break;