1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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>
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>
62
63
64static void sctp_endpoint_bh_rcv(struct sctp_endpoint *ep);
65
66
67
68
69static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
70 struct sock *sk, int gfp)
71{
72 struct sctp_opt *sp = sctp_sk(sk);
73 memset(ep, 0, sizeof(struct sctp_endpoint));
74
75
76
77 ep->base.type = SCTP_EP_TYPE_SOCKET;
78
79
80 atomic_set(&ep->base.refcnt, 1);
81 ep->base.dead = 0;
82 ep->base.malloced = 1;
83
84
85 sctp_inq_init(&ep->base.inqueue);
86
87
88 sctp_inq_set_th_handler(&ep->base.inqueue,
89 (void (*)(void *))sctp_endpoint_bh_rcv, ep);
90
91
92 sctp_bind_addr_init(&ep->base.bind_addr, 0);
93 ep->base.addr_lock = RW_LOCK_UNLOCKED;
94
95
96 ep->base.sk = sk;
97 sock_hold(ep->base.sk);
98
99
100 INIT_LIST_HEAD(&ep->asocs);
101
102
103 ep->timeouts[SCTP_EVENT_TIMEOUT_NONE] = 0;
104 ep->timeouts[SCTP_EVENT_TIMEOUT_T1_COOKIE] =
105 SCTP_DEFAULT_TIMEOUT_T1_COOKIE;
106 ep->timeouts[SCTP_EVENT_TIMEOUT_T1_INIT] =
107 SCTP_DEFAULT_TIMEOUT_T1_INIT;
108 ep->timeouts[SCTP_EVENT_TIMEOUT_T2_SHUTDOWN] =
109 SCTP_MSECS_TO_JIFFIES(sp->rtoinfo.srto_initial);
110 ep->timeouts[SCTP_EVENT_TIMEOUT_T3_RTX] = 0;
111 ep->timeouts[SCTP_EVENT_TIMEOUT_T4_RTO] = 0;
112
113
114
115
116
117 ep->timeouts[SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD]
118 = 5 * SCTP_MSECS_TO_JIFFIES(sp->rtoinfo.srto_max);
119
120 ep->timeouts[SCTP_EVENT_TIMEOUT_HEARTBEAT] =
121 SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
122 ep->timeouts[SCTP_EVENT_TIMEOUT_SACK] =
123 SCTP_DEFAULT_TIMEOUT_SACK;
124 ep->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] =
125 sp->autoclose * HZ;
126
127
128 sk->write_space = sctp_write_space;
129 sk->use_write_queue = 1;
130
131
132 get_random_bytes(&ep->secret_key[0], SCTP_SECRET_SIZE);
133 ep->last_key = ep->current_key = 0;
134 ep->key_changed_at = jiffies;
135
136 ep->debug_name = "unnamedEndpoint";
137 return ep;
138}
139
140
141
142
143struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, int gfp)
144{
145 struct sctp_endpoint *ep;
146
147
148 ep = t_new(struct sctp_endpoint, gfp);
149 if (!ep)
150 goto fail;
151 if (!sctp_endpoint_init(ep, sk, gfp))
152 goto fail_init;
153 ep->base.malloced = 1;
154 SCTP_DBG_OBJCNT_INC(ep);
155 return ep;
156
157fail_init:
158 kfree(ep);
159fail:
160 return NULL;
161}
162
163
164void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
165 struct sctp_association *asoc)
166{
167 struct sock *sk = ep->base.sk;
168
169
170 list_add_tail(&asoc->asocs, &ep->asocs);
171
172
173 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
174 sk->ack_backlog++;
175}
176
177
178
179
180void sctp_endpoint_free(struct sctp_endpoint *ep)
181{
182 ep->base.dead = 1;
183 sctp_endpoint_put(ep);
184}
185
186
187static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
188{
189 SCTP_ASSERT(ep->base.dead, "Endpoint is not dead", return);
190
191 ep->base.sk->state = SCTP_SS_CLOSED;
192
193
194 sctp_unhash_endpoint(ep);
195
196
197 if (sctp_sk(ep->base.sk)->hmac)
198 sctp_crypto_free_tfm(sctp_sk(ep->base.sk)->hmac);
199
200
201 sctp_inq_free(&ep->base.inqueue);
202 sctp_bind_addr_free(&ep->base.bind_addr);
203
204
205 if (ep->base.sk->prev != NULL)
206 sctp_put_port(ep->base.sk);
207
208
209 if (ep->base.sk)
210 sock_put(ep->base.sk);
211
212
213 if (ep->base.malloced) {
214 kfree(ep);
215 SCTP_DBG_OBJCNT_DEC(ep);
216 }
217}
218
219
220void sctp_endpoint_hold(struct sctp_endpoint *ep)
221{
222 atomic_inc(&ep->base.refcnt);
223}
224
225
226
227
228void sctp_endpoint_put(struct sctp_endpoint *ep)
229{
230 if (atomic_dec_and_test(&ep->base.refcnt))
231 sctp_endpoint_destroy(ep);
232}
233
234
235struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
236 const union sctp_addr *laddr)
237{
238 struct sctp_endpoint *retval;
239
240 sctp_read_lock(&ep->base.addr_lock);
241 if (ep->base.bind_addr.port == laddr->v4.sin_port) {
242 if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
243 sctp_sk(ep->base.sk))) {
244 retval = ep;
245 goto out;
246 }
247 }
248
249 retval = NULL;
250
251out:
252 sctp_read_unlock(&ep->base.addr_lock);
253 return retval;
254}
255
256
257
258
259
260static struct sctp_association *__sctp_endpoint_lookup_assoc(
261 const struct sctp_endpoint *ep,
262 const union sctp_addr *paddr,
263 struct sctp_transport **transport)
264{
265 int rport;
266 struct sctp_association *asoc;
267 struct list_head *pos;
268
269 rport = paddr->v4.sin_port;
270
271 list_for_each(pos, &ep->asocs) {
272 asoc = list_entry(pos, struct sctp_association, asocs);
273 if (rport == asoc->peer.port) {
274 sctp_read_lock(&asoc->base.addr_lock);
275 *transport = sctp_assoc_lookup_paddr(asoc, paddr);
276 sctp_read_unlock(&asoc->base.addr_lock);
277
278 if (*transport)
279 return asoc;
280 }
281 }
282
283 *transport = NULL;
284 return NULL;
285}
286
287
288struct sctp_association *sctp_endpoint_lookup_assoc(
289 const struct sctp_endpoint *ep,
290 const union sctp_addr *paddr,
291 struct sctp_transport **transport)
292{
293 struct sctp_association *asoc;
294
295 sctp_local_bh_disable();
296 asoc = __sctp_endpoint_lookup_assoc(ep, paddr, transport);
297 sctp_local_bh_enable();
298
299 return asoc;
300}
301
302
303
304
305int sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
306 const union sctp_addr *paddr)
307{
308 struct list_head *pos;
309 struct sctp_sockaddr_entry *addr;
310 struct sctp_bind_addr *bp;
311
312 sctp_read_lock(&ep->base.addr_lock);
313 bp = &ep->base.bind_addr;
314 list_for_each(pos, &bp->address_list) {
315 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
316 if (sctp_has_association(&addr->a, paddr)) {
317 sctp_read_unlock(&ep->base.addr_lock);
318 return 1;
319 }
320 }
321 sctp_read_unlock(&ep->base.addr_lock);
322
323 return 0;
324}
325
326
327
328
329static void sctp_endpoint_bh_rcv(struct sctp_endpoint *ep)
330{
331 struct sctp_association *asoc;
332 struct sock *sk;
333 struct sctp_transport *transport;
334 struct sctp_chunk *chunk;
335 struct sctp_inq *inqueue;
336 sctp_subtype_t subtype;
337 sctp_state_t state;
338 int error = 0;
339
340 if (ep->base.dead)
341 return;
342
343 asoc = NULL;
344 inqueue = &ep->base.inqueue;
345 sk = ep->base.sk;
346
347 while (NULL != (chunk = sctp_inq_pop(inqueue))) {
348 subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
349
350
351
352
353
354
355
356 if (NULL == chunk->asoc) {
357 asoc = sctp_endpoint_lookup_assoc(ep,
358 sctp_source(chunk),
359 &transport);
360 chunk->asoc = asoc;
361 chunk->transport = transport;
362 }
363
364 state = asoc ? asoc->state : SCTP_STATE_CLOSED;
365
366
367
368
369 if (asoc && sctp_chunk_is_data(chunk))
370 asoc->peer.last_data_from = chunk->transport;
371 else
372 SCTP_INC_STATS(SctpInCtrlChunks);
373
374 if (chunk->transport)
375 chunk->transport->last_time_heard = jiffies;
376
377 error = sctp_do_sm(SCTP_EVENT_T_CHUNK, subtype, state,
378 ep, asoc, chunk, GFP_ATOMIC);
379
380 if (error && chunk)
381 chunk->pdiscard = 1;
382
383
384
385
386 if (!sctp_sk(sk)->ep)
387 break;
388 }
389}
390