Xin Long | a838631 | 2017-01-06 22:18:33 +0800 | [diff] [blame] | 1 | /* SCTP kernel implementation |
| 2 | * (C) Copyright IBM Corp. 2001, 2004 |
| 3 | * Copyright (c) 1999-2000 Cisco, Inc. |
| 4 | * Copyright (c) 1999-2001 Motorola, Inc. |
| 5 | * Copyright (c) 2001 Intel Corp. |
| 6 | * |
| 7 | * This file is part of the SCTP kernel implementation |
| 8 | * |
| 9 | * These functions manipulate sctp tsn mapping array. |
| 10 | * |
| 11 | * This SCTP implementation is free software; |
| 12 | * you can redistribute it and/or modify it under the terms of |
| 13 | * the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2, or (at your option) |
| 15 | * any later version. |
| 16 | * |
| 17 | * This SCTP implementation is distributed in the hope that it |
| 18 | * will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 19 | * ************************ |
| 20 | * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 21 | * See the GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with GNU CC; see the file COPYING. If not, see |
| 25 | * <http://www.gnu.org/licenses/>. |
| 26 | * |
| 27 | * Please send any bug reports or fixes you make to the |
| 28 | * email address(es): |
| 29 | * lksctp developers <linux-sctp@vger.kernel.org> |
| 30 | * |
| 31 | * Written or modified by: |
| 32 | * Xin Long <lucien.xin@gmail.com> |
| 33 | */ |
| 34 | |
| 35 | #include <net/sctp/sctp.h> |
Xin Long | 7f9d68a | 2017-01-18 00:44:47 +0800 | [diff] [blame] | 36 | #include <net/sctp/sm.h> |
Xin Long | a838631 | 2017-01-06 22:18:33 +0800 | [diff] [blame] | 37 | |
| 38 | struct sctp_stream *sctp_stream_new(__u16 incnt, __u16 outcnt, gfp_t gfp) |
| 39 | { |
| 40 | struct sctp_stream *stream; |
| 41 | int i; |
| 42 | |
| 43 | stream = kzalloc(sizeof(*stream), gfp); |
| 44 | if (!stream) |
| 45 | return NULL; |
| 46 | |
| 47 | stream->outcnt = outcnt; |
| 48 | stream->out = kcalloc(stream->outcnt, sizeof(*stream->out), gfp); |
| 49 | if (!stream->out) { |
| 50 | kfree(stream); |
| 51 | return NULL; |
| 52 | } |
| 53 | for (i = 0; i < stream->outcnt; i++) |
| 54 | stream->out[i].state = SCTP_STREAM_OPEN; |
| 55 | |
| 56 | stream->incnt = incnt; |
| 57 | stream->in = kcalloc(stream->incnt, sizeof(*stream->in), gfp); |
| 58 | if (!stream->in) { |
| 59 | kfree(stream->out); |
| 60 | kfree(stream); |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | return stream; |
| 65 | } |
| 66 | |
| 67 | void sctp_stream_free(struct sctp_stream *stream) |
| 68 | { |
| 69 | if (unlikely(!stream)) |
| 70 | return; |
| 71 | |
| 72 | kfree(stream->out); |
| 73 | kfree(stream->in); |
| 74 | kfree(stream); |
| 75 | } |
| 76 | |
| 77 | void sctp_stream_clear(struct sctp_stream *stream) |
| 78 | { |
| 79 | int i; |
| 80 | |
| 81 | for (i = 0; i < stream->outcnt; i++) |
| 82 | stream->out[i].ssn = 0; |
| 83 | |
| 84 | for (i = 0; i < stream->incnt; i++) |
| 85 | stream->in[i].ssn = 0; |
| 86 | } |
Xin Long | 7f9d68a | 2017-01-18 00:44:47 +0800 | [diff] [blame] | 87 | |
| 88 | static int sctp_send_reconf(struct sctp_association *asoc, |
| 89 | struct sctp_chunk *chunk) |
| 90 | { |
| 91 | struct net *net = sock_net(asoc->base.sk); |
| 92 | int retval = 0; |
| 93 | |
| 94 | retval = sctp_primitive_RECONF(net, asoc, chunk); |
| 95 | if (retval) |
| 96 | sctp_chunk_free(chunk); |
| 97 | |
| 98 | return retval; |
| 99 | } |
| 100 | |
| 101 | int sctp_send_reset_streams(struct sctp_association *asoc, |
| 102 | struct sctp_reset_streams *params) |
| 103 | { |
| 104 | struct sctp_stream *stream = asoc->stream; |
| 105 | __u16 i, str_nums, *str_list; |
| 106 | struct sctp_chunk *chunk; |
| 107 | int retval = -EINVAL; |
| 108 | bool out, in; |
| 109 | |
| 110 | if (!asoc->peer.reconf_capable || |
| 111 | !(asoc->strreset_enable & SCTP_ENABLE_RESET_STREAM_REQ)) { |
| 112 | retval = -ENOPROTOOPT; |
| 113 | goto out; |
| 114 | } |
| 115 | |
| 116 | if (asoc->strreset_outstanding) { |
| 117 | retval = -EINPROGRESS; |
| 118 | goto out; |
| 119 | } |
| 120 | |
| 121 | out = params->srs_flags & SCTP_STREAM_RESET_OUTGOING; |
| 122 | in = params->srs_flags & SCTP_STREAM_RESET_INCOMING; |
| 123 | if (!out && !in) |
| 124 | goto out; |
| 125 | |
| 126 | str_nums = params->srs_number_streams; |
| 127 | str_list = params->srs_stream_list; |
| 128 | if (out && str_nums) |
| 129 | for (i = 0; i < str_nums; i++) |
| 130 | if (str_list[i] >= stream->outcnt) |
| 131 | goto out; |
| 132 | |
| 133 | if (in && str_nums) |
| 134 | for (i = 0; i < str_nums; i++) |
| 135 | if (str_list[i] >= stream->incnt) |
| 136 | goto out; |
| 137 | |
| 138 | chunk = sctp_make_strreset_req(asoc, str_nums, str_list, out, in); |
| 139 | if (!chunk) |
| 140 | goto out; |
| 141 | |
| 142 | if (out) { |
| 143 | if (str_nums) |
| 144 | for (i = 0; i < str_nums; i++) |
| 145 | stream->out[str_list[i]].state = |
| 146 | SCTP_STREAM_CLOSED; |
| 147 | else |
| 148 | for (i = 0; i < stream->outcnt; i++) |
| 149 | stream->out[i].state = SCTP_STREAM_CLOSED; |
| 150 | } |
| 151 | |
| 152 | asoc->strreset_outstanding = out + in; |
| 153 | asoc->strreset_chunk = chunk; |
| 154 | sctp_chunk_hold(asoc->strreset_chunk); |
| 155 | |
| 156 | retval = sctp_send_reconf(asoc, chunk); |
| 157 | if (retval) { |
| 158 | sctp_chunk_put(asoc->strreset_chunk); |
| 159 | asoc->strreset_chunk = NULL; |
| 160 | } |
| 161 | |
| 162 | out: |
| 163 | return retval; |
| 164 | } |