0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026 #include <linux/types.h>
0027 #include <linux/slab.h>
0028 #include <linux/in.h>
0029 #include <linux/random.h> /* get_random_bytes() */
0030 #include <net/sock.h>
0031 #include <net/ipv6.h>
0032 #include <net/sctp/sctp.h>
0033 #include <net/sctp/sm.h>
0034
0035
0036 static void sctp_endpoint_bh_rcv(struct work_struct *work);
0037
0038
0039
0040
0041 static struct sctp_endpoint *sctp_endpoint_init(struct sctp_endpoint *ep,
0042 struct sock *sk,
0043 gfp_t gfp)
0044 {
0045 struct net *net = sock_net(sk);
0046 struct sctp_shared_key *null_key;
0047
0048 ep->digest = kzalloc(SCTP_SIGNATURE_SIZE, gfp);
0049 if (!ep->digest)
0050 return NULL;
0051
0052 ep->asconf_enable = net->sctp.addip_enable;
0053 ep->auth_enable = net->sctp.auth_enable;
0054 if (ep->auth_enable) {
0055 if (sctp_auth_init(ep, gfp))
0056 goto nomem;
0057 if (ep->asconf_enable) {
0058 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF);
0059 sctp_auth_ep_add_chunkid(ep, SCTP_CID_ASCONF_ACK);
0060 }
0061 }
0062
0063
0064
0065 ep->base.type = SCTP_EP_TYPE_SOCKET;
0066
0067
0068 refcount_set(&ep->base.refcnt, 1);
0069 ep->base.dead = false;
0070
0071
0072 sctp_inq_init(&ep->base.inqueue);
0073
0074
0075 sctp_inq_set_th_handler(&ep->base.inqueue, sctp_endpoint_bh_rcv);
0076
0077
0078 sctp_bind_addr_init(&ep->base.bind_addr, 0);
0079
0080
0081 INIT_LIST_HEAD(&ep->asocs);
0082
0083
0084 ep->sndbuf_policy = net->sctp.sndbuf_policy;
0085
0086 sk->sk_data_ready = sctp_data_ready;
0087 sk->sk_write_space = sctp_write_space;
0088 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
0089
0090
0091 ep->rcvbuf_policy = net->sctp.rcvbuf_policy;
0092
0093
0094 get_random_bytes(ep->secret_key, sizeof(ep->secret_key));
0095
0096
0097 INIT_LIST_HEAD(&ep->endpoint_shared_keys);
0098 null_key = sctp_auth_shkey_create(0, gfp);
0099 if (!null_key)
0100 goto nomem_shkey;
0101
0102 list_add(&null_key->key_list, &ep->endpoint_shared_keys);
0103
0104
0105
0106
0107 ep->prsctp_enable = net->sctp.prsctp_enable;
0108 ep->reconf_enable = net->sctp.reconf_enable;
0109 ep->ecn_enable = net->sctp.ecn_enable;
0110
0111
0112 ep->base.sk = sk;
0113 ep->base.net = sock_net(sk);
0114 sock_hold(ep->base.sk);
0115
0116 return ep;
0117
0118 nomem_shkey:
0119 sctp_auth_free(ep);
0120 nomem:
0121 kfree(ep->digest);
0122 return NULL;
0123
0124 }
0125
0126
0127
0128
0129 struct sctp_endpoint *sctp_endpoint_new(struct sock *sk, gfp_t gfp)
0130 {
0131 struct sctp_endpoint *ep;
0132
0133
0134 ep = kzalloc(sizeof(*ep), gfp);
0135 if (!ep)
0136 goto fail;
0137
0138 if (!sctp_endpoint_init(ep, sk, gfp))
0139 goto fail_init;
0140
0141 SCTP_DBG_OBJCNT_INC(ep);
0142 return ep;
0143
0144 fail_init:
0145 kfree(ep);
0146 fail:
0147 return NULL;
0148 }
0149
0150
0151 void sctp_endpoint_add_asoc(struct sctp_endpoint *ep,
0152 struct sctp_association *asoc)
0153 {
0154 struct sock *sk = ep->base.sk;
0155
0156
0157
0158
0159
0160 if (asoc->temp)
0161 return;
0162
0163
0164 list_add_tail(&asoc->asocs, &ep->asocs);
0165
0166
0167 if (sctp_style(sk, TCP) && sctp_sstate(sk, LISTENING))
0168 sk_acceptq_added(sk);
0169 }
0170
0171
0172
0173
0174 void sctp_endpoint_free(struct sctp_endpoint *ep)
0175 {
0176 ep->base.dead = true;
0177
0178 inet_sk_set_state(ep->base.sk, SCTP_SS_CLOSED);
0179
0180
0181 sctp_unhash_endpoint(ep);
0182
0183 sctp_endpoint_put(ep);
0184 }
0185
0186
0187 static void sctp_endpoint_destroy_rcu(struct rcu_head *head)
0188 {
0189 struct sctp_endpoint *ep = container_of(head, struct sctp_endpoint, rcu);
0190 struct sock *sk = ep->base.sk;
0191
0192 sctp_sk(sk)->ep = NULL;
0193 sock_put(sk);
0194
0195 kfree(ep);
0196 SCTP_DBG_OBJCNT_DEC(ep);
0197 }
0198
0199 static void sctp_endpoint_destroy(struct sctp_endpoint *ep)
0200 {
0201 struct sock *sk;
0202
0203 if (unlikely(!ep->base.dead)) {
0204 WARN(1, "Attempt to destroy undead endpoint %p!\n", ep);
0205 return;
0206 }
0207
0208
0209 kfree(ep->digest);
0210
0211
0212
0213
0214 sctp_auth_destroy_keys(&ep->endpoint_shared_keys);
0215 sctp_auth_free(ep);
0216
0217
0218 sctp_inq_free(&ep->base.inqueue);
0219 sctp_bind_addr_free(&ep->base.bind_addr);
0220
0221 memset(ep->secret_key, 0, sizeof(ep->secret_key));
0222
0223 sk = ep->base.sk;
0224
0225 if (sctp_sk(sk)->bind_hash)
0226 sctp_put_port(sk);
0227
0228 call_rcu(&ep->rcu, sctp_endpoint_destroy_rcu);
0229 }
0230
0231
0232 int sctp_endpoint_hold(struct sctp_endpoint *ep)
0233 {
0234 return refcount_inc_not_zero(&ep->base.refcnt);
0235 }
0236
0237
0238
0239
0240 void sctp_endpoint_put(struct sctp_endpoint *ep)
0241 {
0242 if (refcount_dec_and_test(&ep->base.refcnt))
0243 sctp_endpoint_destroy(ep);
0244 }
0245
0246
0247 struct sctp_endpoint *sctp_endpoint_is_match(struct sctp_endpoint *ep,
0248 struct net *net,
0249 const union sctp_addr *laddr)
0250 {
0251 struct sctp_endpoint *retval = NULL;
0252
0253 if ((htons(ep->base.bind_addr.port) == laddr->v4.sin_port) &&
0254 net_eq(ep->base.net, net)) {
0255 if (sctp_bind_addr_match(&ep->base.bind_addr, laddr,
0256 sctp_sk(ep->base.sk)))
0257 retval = ep;
0258 }
0259
0260 return retval;
0261 }
0262
0263
0264
0265
0266
0267 struct sctp_association *sctp_endpoint_lookup_assoc(
0268 const struct sctp_endpoint *ep,
0269 const union sctp_addr *paddr,
0270 struct sctp_transport **transport)
0271 {
0272 struct sctp_association *asoc = NULL;
0273 struct sctp_transport *t;
0274
0275 *transport = NULL;
0276
0277
0278
0279
0280 if (!ep->base.bind_addr.port)
0281 return NULL;
0282
0283 rcu_read_lock();
0284 t = sctp_epaddr_lookup_transport(ep, paddr);
0285 if (!t)
0286 goto out;
0287
0288 *transport = t;
0289 asoc = t->asoc;
0290 out:
0291 rcu_read_unlock();
0292 return asoc;
0293 }
0294
0295
0296
0297
0298 bool sctp_endpoint_is_peeled_off(struct sctp_endpoint *ep,
0299 const union sctp_addr *paddr)
0300 {
0301 struct sctp_sockaddr_entry *addr;
0302 struct net *net = ep->base.net;
0303 struct sctp_bind_addr *bp;
0304
0305 bp = &ep->base.bind_addr;
0306
0307
0308
0309 list_for_each_entry(addr, &bp->address_list, list) {
0310 if (sctp_has_association(net, &addr->a, paddr))
0311 return true;
0312 }
0313
0314 return false;
0315 }
0316
0317
0318
0319
0320 static void sctp_endpoint_bh_rcv(struct work_struct *work)
0321 {
0322 struct sctp_endpoint *ep =
0323 container_of(work, struct sctp_endpoint,
0324 base.inqueue.immediate);
0325 struct sctp_association *asoc;
0326 struct sock *sk;
0327 struct net *net;
0328 struct sctp_transport *transport;
0329 struct sctp_chunk *chunk;
0330 struct sctp_inq *inqueue;
0331 union sctp_subtype subtype;
0332 enum sctp_state state;
0333 int error = 0;
0334 int first_time = 1;
0335
0336 if (ep->base.dead)
0337 return;
0338
0339 asoc = NULL;
0340 inqueue = &ep->base.inqueue;
0341 sk = ep->base.sk;
0342 net = sock_net(sk);
0343
0344 while (NULL != (chunk = sctp_inq_pop(inqueue))) {
0345 subtype = SCTP_ST_CHUNK(chunk->chunk_hdr->type);
0346
0347
0348
0349
0350 if (first_time && (subtype.chunk == SCTP_CID_AUTH)) {
0351 struct sctp_chunkhdr *next_hdr;
0352
0353 next_hdr = sctp_inq_peek(inqueue);
0354 if (!next_hdr)
0355 goto normal;
0356
0357
0358
0359
0360
0361
0362 if (next_hdr->type == SCTP_CID_COOKIE_ECHO) {
0363 chunk->auth_chunk = skb_clone(chunk->skb,
0364 GFP_ATOMIC);
0365 chunk->auth = 1;
0366 continue;
0367 }
0368 }
0369 normal:
0370
0371
0372
0373
0374
0375
0376 if (NULL == chunk->asoc) {
0377 asoc = sctp_endpoint_lookup_assoc(ep,
0378 sctp_source(chunk),
0379 &transport);
0380 chunk->asoc = asoc;
0381 chunk->transport = transport;
0382 }
0383
0384 state = asoc ? asoc->state : SCTP_STATE_CLOSED;
0385 if (sctp_auth_recv_cid(subtype.chunk, asoc) && !chunk->auth)
0386 continue;
0387
0388
0389
0390
0391 if (asoc && sctp_chunk_is_data(chunk))
0392 asoc->peer.last_data_from = chunk->transport;
0393 else {
0394 SCTP_INC_STATS(ep->base.net, SCTP_MIB_INCTRLCHUNKS);
0395 if (asoc)
0396 asoc->stats.ictrlchunks++;
0397 }
0398
0399 if (chunk->transport)
0400 chunk->transport->last_time_heard = ktime_get();
0401
0402 error = sctp_do_sm(net, SCTP_EVENT_T_CHUNK, subtype, state,
0403 ep, asoc, chunk, GFP_ATOMIC);
0404
0405 if (error && chunk)
0406 chunk->pdiscard = 1;
0407
0408
0409
0410
0411 if (!sctp_sk(sk)->ep)
0412 break;
0413
0414 if (first_time)
0415 first_time = 0;
0416 }
0417 }