Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file gprs_ns.c |
| 2 | * GPRS Networks Service (NS) messages on the Gb interface. |
Harald Welte | 17a642d | 2018-07-01 19:09:52 +0200 | [diff] [blame] | 3 | * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) |
| 4 | * as well as its successor 3GPP TS 48.016 */ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 5 | /* |
Harald Welte | 17a642d | 2018-07-01 19:09:52 +0200 | [diff] [blame] | 6 | * (C) 2009-2018 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 7 | * (C) 2016-2017 sysmocom - s.f.m.c. GmbH |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 8 | * |
| 9 | * All Rights Reserved |
| 10 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 11 | * SPDX-License-Identifier: GPL-2.0+ |
| 12 | * |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 21 | * GNU General Public License for more details. |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 22 | * |
Harald Welte | 7fa89c2 | 2014-10-26 20:33:09 +0100 | [diff] [blame] | 23 | * You should have received a copy of the GNU General Public License |
Harald Welte | e4cbb3f | 2011-01-01 15:25:50 +0100 | [diff] [blame] | 24 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 25 | * |
| 26 | */ |
| 27 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 28 | /*! \addtogroup libgb |
| 29 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 30 | * |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 31 | * GPRS Networks Service (NS) messages on the Gb interface |
| 32 | * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) |
| 33 | * |
| 34 | * Some introduction into NS: NS is used typically on top of frame relay, |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 35 | * but in the ip.access world it is encapsulated in UDP packets. It serves |
| 36 | * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't |
| 37 | * do much, apart from providing congestion notification and status indication. |
| 38 | * |
| 39 | * Terms: |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 40 | * |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 41 | * NS Network Service |
| 42 | * NSVC NS Virtual Connection |
| 43 | * NSEI NS Entity Identifier |
| 44 | * NSVL NS Virtual Link |
| 45 | * NSVLI NS Virtual Link Identifier |
| 46 | * BVC BSSGP Virtual Connection |
| 47 | * BVCI BSSGP Virtual Connection Identifier |
| 48 | * NSVCG NS Virtual Connection Goup |
| 49 | * Blocked NS-VC cannot be used for user traffic |
| 50 | * Alive Ability of a NS-VC to provide communication |
| 51 | * |
| 52 | * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 53 | * therefore identify the BSSGP virtual connection by a BVCI passed down to NS. |
| 54 | * NS then has to firgure out which NSVC's are responsible for this BVCI. |
| 55 | * Those mappings are administratively configured. |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 56 | * |
| 57 | * This implementation has the following limitations: |
| 58 | * - Only one NS-VC for each NSE: No load-sharing function |
| 59 | * - NSVCI 65535 and 65534 are reserved for internal use |
| 60 | * - Only UDP is supported as of now, no frame relay support |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 61 | * - There are no BLOCK and UNBLOCK timers (yet?) |
| 62 | * |
| 63 | * \file gprs_ns.c */ |
Harald Welte | dd1c83c | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 64 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 65 | #include <stdlib.h> |
| 66 | #include <unistd.h> |
| 67 | #include <errno.h> |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 68 | #include <stdint.h> |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 69 | |
Holger Hans Peter Freyther | 3c16de2 | 2012-07-12 14:13:41 +0200 | [diff] [blame] | 70 | #include <sys/types.h> |
| 71 | #include <sys/socket.h> |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 72 | #include <arpa/inet.h> |
| 73 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 74 | #include <osmocom/core/fsm.h> |
Pablo Neira Ayuso | ff66323 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 75 | #include <osmocom/core/msgb.h> |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 76 | #include <osmocom/core/byteswap.h> |
Pablo Neira Ayuso | ff66323 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 77 | #include <osmocom/gsm/tlv.h> |
| 78 | #include <osmocom/core/talloc.h> |
| 79 | #include <osmocom/core/select.h> |
| 80 | #include <osmocom/core/rate_ctr.h> |
Jacob Erlbeck | 0a1400f | 2015-10-06 15:23:25 +0200 | [diff] [blame] | 81 | #include <osmocom/core/stat_item.h> |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 82 | #include <osmocom/core/stats.h> |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 83 | #include <osmocom/core/socket.h> |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 84 | #include <osmocom/core/signal.h> |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 85 | #include <osmocom/gprs/gprs_ns.h> |
| 86 | #include <osmocom/gprs/gprs_bssgp.h> |
| 87 | #include <osmocom/gprs/gprs_ns_frgre.h> |
| 88 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 89 | #include "common_vty.h" |
Harald Welte | 17a642d | 2018-07-01 19:09:52 +0200 | [diff] [blame] | 90 | #include "gb_internal.h" |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 91 | |
Neels Hofmeyr | 983dcb9 | 2018-08-20 12:33:22 +0200 | [diff] [blame] | 92 | #define ns_set_state(ns_, st_) ns_set_state_with_log(ns_, st_, false, __FILE__, __LINE__) |
| 93 | #define ns_set_remote_state(ns_, st_) ns_set_state_with_log(ns_, st_, true, __FILE__, __LINE__) |
Max | c513c0f | 2017-10-23 15:11:14 +0200 | [diff] [blame] | 94 | #define ns_mark_blocked(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_BLOCKED) |
| 95 | #define ns_mark_unblocked(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_BLOCKED)); |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 96 | #define ns_mark_alive(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_ALIVE) |
| 97 | #define ns_mark_dead(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_ALIVE)); |
| 98 | |
| 99 | #define ERR_IF_NSVC_USES_SNS(nsvc, reason) \ |
| 100 | do { \ |
| 101 | if ((nsvc)->nsi->bss_sns_fi) { \ |
| 102 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Asked to %s. Rejected on IP-SNS\n", \ |
| 103 | nsvc->nsei, reason); \ |
| 104 | osmo_log_backtrace(DNS, LOGL_ERROR); \ |
| 105 | return -EIO; \ |
| 106 | } \ |
| 107 | } while (0) |
Max | c513c0f | 2017-10-23 15:11:14 +0200 | [diff] [blame] | 108 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 109 | static const struct tlv_definition ns_att_tlvdef = { |
| 110 | .def = { |
| 111 | [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 }, |
| 112 | [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 }, |
| 113 | [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 }, |
| 114 | [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 }, |
| 115 | [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 }, |
Harald Welte | 17a642d | 2018-07-01 19:09:52 +0200 | [diff] [blame] | 116 | [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 }, |
| 117 | [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 }, |
| 118 | [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 }, |
| 119 | [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 }, |
| 120 | [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 }, |
| 121 | [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 }, |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 122 | /* TODO: IP_ADDR can be 5 or 17 bytes long, depending on first byte. This |
| 123 | * cannot be expressed in our TLV parser. However, we don't do IPv6 anyway */ |
| 124 | [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 5 }, |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 125 | }, |
| 126 | }; |
| 127 | |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 128 | enum ns_ctr { |
| 129 | NS_CTR_PKTS_IN, |
| 130 | NS_CTR_PKTS_OUT, |
| 131 | NS_CTR_BYTES_IN, |
| 132 | NS_CTR_BYTES_OUT, |
| 133 | NS_CTR_BLOCKED, |
| 134 | NS_CTR_DEAD, |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 135 | NS_CTR_REPLACED, |
| 136 | NS_CTR_NSEI_CHG, |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 137 | NS_CTR_INV_VCI, |
| 138 | NS_CTR_INV_NSEI, |
Jacob Erlbeck | 0a1400f | 2015-10-06 15:23:25 +0200 | [diff] [blame] | 139 | NS_CTR_LOST_ALIVE, |
| 140 | NS_CTR_LOST_RESET, |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 141 | }; |
| 142 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 143 | static const struct rate_ctr_desc nsvc_ctr_description[] = { |
Harald Welte | a7a5065 | 2017-10-03 17:49:21 +0800 | [diff] [blame] | 144 | { "packets:in", "Packets at NS Level ( In)" }, |
| 145 | { "packets:out","Packets at NS Level (Out)" }, |
| 146 | { "bytes:in", "Bytes at NS Level ( In)" }, |
| 147 | { "bytes:out", "Bytes at NS Level (Out)" }, |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 148 | { "blocked", "NS-VC Block count " }, |
| 149 | { "dead", "NS-VC gone dead count " }, |
| 150 | { "replaced", "NS-VC replaced other count" }, |
Jacob Erlbeck | ab852ee | 2013-10-24 01:33:24 +0200 | [diff] [blame] | 151 | { "nsei-chg", "NS-VC changed NSEI count " }, |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 152 | { "inv-nsvci", "NS-VCI was invalid count " }, |
| 153 | { "inv-nsei", "NSEI was invalid count " }, |
Harald Welte | a7a5065 | 2017-10-03 17:49:21 +0800 | [diff] [blame] | 154 | { "lost:alive", "ALIVE ACK missing count " }, |
| 155 | { "lost:reset", "RESET ACK missing count " }, |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | static const struct rate_ctr_group_desc nsvc_ctrg_desc = { |
Harald Welte | a7a5065 | 2017-10-03 17:49:21 +0800 | [diff] [blame] | 159 | .group_name_prefix = "ns:nsvc", |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 160 | .group_description = "NSVC Peer Statistics", |
| 161 | .num_ctr = ARRAY_SIZE(nsvc_ctr_description), |
| 162 | .ctr_desc = nsvc_ctr_description, |
Jacob Erlbeck | fed2a48 | 2015-11-03 16:18:56 +0100 | [diff] [blame] | 163 | .class_id = OSMO_STATS_CLASS_PEER, |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 164 | }; |
| 165 | |
Jacob Erlbeck | 0a1400f | 2015-10-06 15:23:25 +0200 | [diff] [blame] | 166 | enum ns_stat { |
| 167 | NS_STAT_ALIVE_DELAY, |
| 168 | }; |
| 169 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 170 | static const struct osmo_stat_item_desc nsvc_stat_description[] = { |
Thorsten Alteholz | a81055d | 2017-03-02 22:13:48 +0100 | [diff] [blame] | 171 | { "alive.delay", "ALIVE response time ", "ms", 16, 0 }, |
Jacob Erlbeck | 0a1400f | 2015-10-06 15:23:25 +0200 | [diff] [blame] | 172 | }; |
| 173 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 174 | static const struct osmo_stat_item_group_desc nsvc_statg_desc = { |
Jacob Erlbeck | 0a1400f | 2015-10-06 15:23:25 +0200 | [diff] [blame] | 175 | .group_name_prefix = "ns.nsvc", |
| 176 | .group_description = "NSVC Peer Statistics", |
| 177 | .num_items = ARRAY_SIZE(nsvc_stat_description), |
| 178 | .item_desc = nsvc_stat_description, |
Jacob Erlbeck | bc9d9ac | 2015-11-02 14:49:35 +0100 | [diff] [blame] | 179 | .class_id = OSMO_STATS_CLASS_PEER, |
Jacob Erlbeck | 0a1400f | 2015-10-06 15:23:25 +0200 | [diff] [blame] | 180 | }; |
| 181 | |
Max | 54ca30c | 2017-05-22 14:48:27 +0200 | [diff] [blame] | 182 | const struct value_string gprs_ns_signal_ns_names[] = { |
| 183 | { S_NS_RESET, "NS-RESET" }, |
| 184 | { S_NS_BLOCK, "NS-BLOCK" }, |
| 185 | { S_NS_UNBLOCK, "NS-UNBLOCK" }, |
| 186 | { S_NS_ALIVE_EXP, "NS-ALIVE expired" }, |
| 187 | { S_NS_REPLACED, "NSVC replaced" }, |
| 188 | { S_NS_MISMATCH, "Unexpected IE" }, |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 189 | { S_SNS_CONFIGURED, "SNS Configured" }, |
Max | 54ca30c | 2017-05-22 14:48:27 +0200 | [diff] [blame] | 190 | { 0, NULL } |
| 191 | }; |
| 192 | |
Holger Hans Peter Freyther | 9e1cd5a | 2013-10-25 11:09:26 +0200 | [diff] [blame] | 193 | #define CHECK_TX_RC(rc, nsvc) \ |
| 194 | if (rc < 0) \ |
| 195 | LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n", \ |
| 196 | rc, gprs_ns_ll_str(nsvc)); |
| 197 | |
Harald Welte | 94c9b44 | 2019-03-16 14:38:19 +0100 | [diff] [blame] | 198 | static bool nsvc_is_not_used(const struct gprs_nsvc *nsvc) |
| 199 | { |
| 200 | if (nsvc->data_weight == 0 && nsvc->sig_weight == 0) |
| 201 | return true; |
| 202 | else |
| 203 | return false; |
| 204 | } |
| 205 | |
Jacob Erlbeck | 8d192d7 | 2015-04-07 17:52:45 +0200 | [diff] [blame] | 206 | struct msgb *gprs_ns_msgb_alloc(void) |
| 207 | { |
| 208 | struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM, |
| 209 | "GPRS/NS"); |
| 210 | if (!msg) { |
| 211 | LOGP(DNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n", |
| 212 | NS_ALLOC_SIZE); |
| 213 | } |
| 214 | return msg; |
| 215 | } |
| 216 | |
Max | c513c0f | 2017-10-23 15:11:14 +0200 | [diff] [blame] | 217 | /* FIXME: convert to osmo_fsm */ |
| 218 | static inline void ns_set_state_with_log(struct gprs_nsvc *nsvc, uint32_t state, bool is_remote, |
| 219 | const char *file, unsigned line) |
| 220 | { |
| 221 | uint32_t old_state = is_remote ? nsvc->remote_state : nsvc->state; |
| 222 | |
| 223 | LOGPSRC(DNS, LOGL_DEBUG, file, line, "NSEI %d (NS-VCI=%u) setting %sstate [%s,%s,%s] -> [%s,%s,%s]\n", |
| 224 | nsvc->nsei, nsvc->nsvci, is_remote ? "remote " : "", |
| 225 | NS_DESC_A(old_state), NS_DESC_B(old_state), NS_DESC_R(old_state), |
| 226 | NS_DESC_A(state), NS_DESC_B(state), NS_DESC_R(state)); |
| 227 | |
| 228 | if (is_remote) |
| 229 | nsvc->remote_state = state; |
| 230 | else |
| 231 | nsvc->state = state; |
| 232 | } |
Holger Hans Peter Freyther | 9e1cd5a | 2013-10-25 11:09:26 +0200 | [diff] [blame] | 233 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 234 | /*! Lookup struct gprs_nsvc based on NSVCI |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 235 | * \param[in] nsi NS instance in which to search |
| 236 | * \param[in] nsvci NSVCI to be searched |
| 237 | * \returns gprs_nsvc of respective NSVCI |
| 238 | */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 239 | struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 240 | { |
| 241 | struct gprs_nsvc *nsvc; |
| 242 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 243 | if (nsvc->nsvci == nsvci) |
| 244 | return nsvc; |
| 245 | } |
| 246 | return NULL; |
| 247 | } |
| 248 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 249 | /*! Lookup struct gprs_nsvc based on NSEI |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 250 | * \param[in] nsi NS instance in which to search |
| 251 | * \param[in] nsei NSEI to be searched |
Jacob Erlbeck | 6901715 | 2013-10-14 12:03:54 +0200 | [diff] [blame] | 252 | * \returns first gprs_nsvc of respective NSEI |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 253 | */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 254 | struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei) |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 255 | { |
| 256 | struct gprs_nsvc *nsvc; |
| 257 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 258 | if (nsvc->nsei == nsei) |
| 259 | return nsvc; |
| 260 | } |
| 261 | return NULL; |
| 262 | } |
| 263 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 264 | /*! Determine active NS-VC for given NSEI + BVCI. |
| 265 | * Use this function to determine which of the NS-VCs inside the NS Instance |
| 266 | * shall be used to transmit data for given NSEI + BVCI */ |
Jacob Erlbeck | 6901715 | 2013-10-14 12:03:54 +0200 | [diff] [blame] | 267 | static struct gprs_nsvc *gprs_active_nsvc_by_nsei(struct gprs_ns_inst *nsi, |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 268 | uint16_t nsei, uint16_t bvci) |
Jacob Erlbeck | 6901715 | 2013-10-14 12:03:54 +0200 | [diff] [blame] | 269 | { |
| 270 | struct gprs_nsvc *nsvc; |
| 271 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 272 | /* if signalling BVCI, skip any NSVC with signalling weight == 0 */ |
| 273 | if (bvci == 0 && nsvc->sig_weight == 0) |
| 274 | continue; |
| 275 | /* if point-to-point BVCI, skip any NSVC with data weight == 0 */ |
| 276 | if (bvci != 0 && nsvc->data_weight == 0) |
| 277 | continue; |
Jacob Erlbeck | 6901715 | 2013-10-14 12:03:54 +0200 | [diff] [blame] | 278 | if (nsvc->nsei == nsei) { |
Jacob Erlbeck | bf02196 | 2013-10-17 13:58:35 +0200 | [diff] [blame] | 279 | if (!(nsvc->state & NSE_S_BLOCKED) && |
| 280 | nsvc->state & NSE_S_ALIVE) |
Jacob Erlbeck | 6901715 | 2013-10-14 12:03:54 +0200 | [diff] [blame] | 281 | return nsvc; |
| 282 | } |
| 283 | } |
| 284 | return NULL; |
| 285 | } |
| 286 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 287 | /*! Lookup NS-VC based on specified remote peer socket addr. |
| 288 | * \param[in] nsi NS Instance within which we shall look up the NS-VC |
| 289 | * \param[in] sin Remote peer Socket Address (IP + UDP Port) |
| 290 | * \returns NS-VC matching the given peer; NULL in case of none */ |
| 291 | struct gprs_nsvc *gprs_nsvc_by_rem_addr(struct gprs_ns_inst *nsi, const struct sockaddr_in *sin) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 292 | { |
| 293 | struct gprs_nsvc *nsvc; |
| 294 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
Harald Welte | 38407ef | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 295 | if (nsvc->ip.bts_addr.sin_addr.s_addr == |
| 296 | sin->sin_addr.s_addr && |
| 297 | nsvc->ip.bts_addr.sin_port == sin->sin_port) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 298 | return nsvc; |
| 299 | } |
| 300 | return NULL; |
| 301 | } |
| 302 | |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 303 | static void gprs_ns_timer_cb(void *data); |
| 304 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 305 | /*! Create a new NS-VC (Virtual Circuit) within given instance |
| 306 | * \param[in] nsi NS Instance in which to create the NSVC |
| 307 | * \param[in] nsvci] NS Virtual Connection Identifier for this NSVC |
| 308 | * \param[in] sig_weight Signalling Weight of this NS-VC. Use "0" for no signalling |
| 309 | * \param[in] data_weight Data WEight of this NS-VC. Use "0" for no data |
| 310 | * \returns newly-created gprs_nsvc within nsi. NULL on error. */ |
| 311 | struct gprs_nsvc *gprs_nsvc_create2(struct gprs_ns_inst *nsi, uint16_t nsvci, |
| 312 | uint8_t sig_weight, uint8_t data_weight) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 313 | { |
| 314 | struct gprs_nsvc *nsvc; |
| 315 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 316 | if (gprs_nsvc_by_nsvci(nsi, nsvci)) { |
| 317 | LOGP(DNS, LOGL_ERROR, "Cannot create NS-VC for already-existing NSVCI=%u\n", nsvci); |
| 318 | return NULL; |
| 319 | } |
| 320 | |
Harald Welte | b1020d5 | 2010-05-25 22:17:30 +0200 | [diff] [blame] | 321 | LOGP(DNS, LOGL_INFO, "NSVCI=%u Creating NS-VC\n", nsvci); |
| 322 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 323 | nsvc = talloc_zero(nsi, struct gprs_nsvc); |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 324 | if (!nsvc) |
| 325 | return NULL; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 326 | nsvc->nsvci = nsvci; |
Jacob Erlbeck | 9b591b7 | 2013-11-11 09:43:05 +0100 | [diff] [blame] | 327 | nsvc->nsvci_is_valid = 1; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 328 | /* before RESET procedure: BLOCKED and DEAD */ |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 329 | if (nsi->bss_sns_fi) |
| 330 | ns_set_state(nsvc, 0); |
| 331 | else |
| 332 | ns_set_state(nsvc, NSE_S_BLOCKED); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 333 | nsvc->nsi = nsi; |
Pablo Neira Ayuso | 44f423f | 2017-05-08 18:00:28 +0200 | [diff] [blame] | 334 | osmo_timer_setup(&nsvc->timer, gprs_ns_timer_cb, nsvc); |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 335 | nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, nsvci); |
Harald Welte | 6d3135c | 2019-05-08 14:00:37 +0200 | [diff] [blame] | 336 | if (!nsvc->ctrg) { |
| 337 | talloc_free(nsvc); |
| 338 | return NULL; |
| 339 | } |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 340 | nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, nsvci); |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 341 | nsvc->sig_weight = sig_weight; |
| 342 | nsvc->data_weight = data_weight; |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 343 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 344 | llist_add(&nsvc->list, &nsi->gprs_nsvcs); |
| 345 | |
| 346 | return nsvc; |
| 347 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 348 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 349 | /*! Old API for creating a NS-VC. Uses gprs_nsvc_create2 with fixed weights. */ |
| 350 | struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci) |
| 351 | { |
| 352 | return gprs_nsvc_create2(nsi, nsvci, 1, 1); |
| 353 | } |
| 354 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 355 | /*! Delete given NS-VC |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 356 | * \param[in] nsvc gprs_nsvc to be deleted |
| 357 | */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 358 | void gprs_nsvc_delete(struct gprs_nsvc *nsvc) |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 359 | { |
Pablo Neira Ayuso | 2b15952 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 360 | if (osmo_timer_pending(&nsvc->timer)) |
| 361 | osmo_timer_del(&nsvc->timer); |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 362 | llist_del(&nsvc->list); |
Jacob Erlbeck | a52ba01 | 2013-10-24 01:33:21 +0200 | [diff] [blame] | 363 | rate_ctr_group_free(nsvc->ctrg); |
Jacob Erlbeck | 0975a7f | 2015-12-17 09:51:53 +0100 | [diff] [blame] | 364 | osmo_stat_item_group_free(nsvc->statg); |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 365 | talloc_free(nsvc); |
| 366 | } |
| 367 | |
Pablo Neira Ayuso | 2b88a2a | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 368 | static void ns_osmo_signal_dispatch(struct gprs_nsvc *nsvc, unsigned int signal, |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 369 | uint8_t cause) |
| 370 | { |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 371 | struct ns_signal_data nssd = {0}; |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 372 | |
| 373 | nssd.nsvc = nsvc; |
| 374 | nssd.cause = cause; |
| 375 | |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 376 | osmo_signal_dispatch(SS_L_NS, signal, &nssd); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 377 | } |
| 378 | |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 379 | static void ns_osmo_signal_dispatch_mismatch(struct gprs_nsvc *nsvc, |
| 380 | struct msgb *msg, |
| 381 | uint8_t pdu_type, uint8_t ie_type) |
| 382 | { |
| 383 | struct ns_signal_data nssd = {0}; |
| 384 | |
| 385 | nssd.nsvc = nsvc; |
| 386 | nssd.pdu_type = pdu_type; |
| 387 | nssd.ie_type = ie_type; |
| 388 | nssd.msg = msg; |
| 389 | |
| 390 | osmo_signal_dispatch(SS_L_NS, S_NS_MISMATCH, &nssd); |
| 391 | } |
| 392 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 393 | static void ns_osmo_signal_dispatch_replaced(struct gprs_nsvc *nsvc, struct gprs_nsvc *old_nsvc) |
| 394 | { |
| 395 | struct ns_signal_data nssd = {0}; |
| 396 | |
| 397 | nssd.nsvc = nsvc; |
| 398 | nssd.old_nsvc = old_nsvc; |
| 399 | |
| 400 | osmo_signal_dispatch(SS_L_NS, S_NS_REPLACED, &nssd); |
| 401 | } |
| 402 | |
Max | 584f4b6 | 2017-05-04 11:59:52 +0200 | [diff] [blame] | 403 | const struct value_string gprs_ns_pdu_strings[] = { |
| 404 | /* 3GPP TS 48.016 §9.2 Network Service Control PDUs */ |
| 405 | { NS_PDUT_UNITDATA, "NS-UNITDATA" }, /* §9.2.1 */ |
| 406 | { NS_PDUT_RESET, "NS-RESET" }, /* §9.2.5 */ |
| 407 | { NS_PDUT_RESET_ACK, "NS-RESET-ACK" }, /* §9.2.6 */ |
| 408 | { NS_PDUT_BLOCK, "NS-BLOCK" }, /* §9.2.3 */ |
| 409 | { NS_PDUT_BLOCK_ACK, "NS-BLOCK-ACK" }, /* §9.2.4 */ |
| 410 | { NS_PDUT_UNBLOCK, "NS-UNBLOCK" }, /* §9.2.8 */ |
| 411 | { NS_PDUT_UNBLOCK_ACK, "NS-UNBLOCK-ACK" }, /* §9.2.9 */ |
| 412 | { NS_PDUT_STATUS, "NS-STATUS" }, /* §9.2.7 */ |
| 413 | { NS_PDUT_ALIVE, "NS-ALIVE" }, /* §9.2.1 */ |
| 414 | { NS_PDUT_ALIVE_ACK, "NS-ALIVE-ACK" }, /* §9.2.2 */ |
| 415 | /* 3GPP TS 48.016 §9.3 Sub-Network Service Control PDUs */ |
| 416 | { SNS_PDUT_ACK, "SNS-ACK" }, /* §9.3.1 */ |
| 417 | { SNS_PDUT_ADD, "SNS-ADD" }, /* §9.3.2 */ |
| 418 | { SNS_PDUT_CHANGE_WEIGHT, "SNS-CHANGEWEIGHT" }, /* §9.3.3 */ |
| 419 | { SNS_PDUT_CONFIG, "SNS-CONFIG" }, /* §9.3.4 */ |
| 420 | { SNS_PDUT_CONFIG_ACK, "SNS-CONFIG-ACK" }, /* §9.3.5 */ |
| 421 | { SNS_PDUT_DELETE, "SNS-DELETE" }, /* §9.3.6 */ |
| 422 | { SNS_PDUT_SIZE, "SNS-SIZE" }, /* §9.3.7 */ |
| 423 | { SNS_PDUT_SIZE_ACK, "SNS-SIZE-ACK" }, /* §9.3.8 */ |
| 424 | { 0, NULL } |
| 425 | }; |
| 426 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 427 | /* Section 10.3.2, Table 13 */ |
Harald Welte | 2577d41 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 428 | static const struct value_string ns_cause_str[] = { |
| 429 | { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" }, |
| 430 | { NS_CAUSE_OM_INTERVENTION, "O&M intervention" }, |
| 431 | { NS_CAUSE_EQUIP_FAIL, "Equipment failure" }, |
| 432 | { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" }, |
| 433 | { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" }, |
| 434 | { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" }, |
| 435 | { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" }, |
| 436 | { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" }, |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 437 | { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
Harald Welte | 2577d41 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 438 | { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" }, |
| 439 | { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" }, |
Harald Welte | 47cf21f | 2019-02-23 13:42:06 +0100 | [diff] [blame] | 440 | { NS_CAUSE_INVAL_NR_IPv4_EP, "Invalid Number of IPv4 Endpoints" }, |
| 441 | { NS_CAUSE_INVAL_NR_IPv6_EP, "Invalid Number of IPv6 Endpoints" }, |
| 442 | { NS_CAUSE_INVAL_NR_NS_VC, "Invalid Number of NS-VCs" }, |
| 443 | { NS_CAUSE_INVAL_WEIGH, "Invalid Weights" }, |
| 444 | { NS_CAUSE_UNKN_IP_EP, "Unknown IP Endpoint" }, |
| 445 | { NS_CAUSE_UNKN_IP_ADDR, "Unknown IP Address" }, |
| 446 | { NS_CAUSE_UNKN_IP_TEST_FAILED, "IP Test Failed" }, |
Harald Welte | 2577d41 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 447 | { 0, NULL } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 448 | }; |
| 449 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 450 | /*! Obtain a human-readable string for NS cause value */ |
Harald Welte | 2577d41 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 451 | const char *gprs_ns_cause_str(enum ns_cause cause) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 452 | { |
Harald Welte | 2577d41 | 2010-05-02 09:37:45 +0200 | [diff] [blame] | 453 | return get_value_string(ns_cause_str, cause); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 454 | } |
| 455 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 456 | static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 457 | extern int grps_ns_frgre_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 458 | |
Harald Welte | 94c9b44 | 2019-03-16 14:38:19 +0100 | [diff] [blame] | 459 | static bool ns_is_sns(uint8_t pdu_type) |
| 460 | { |
| 461 | switch (pdu_type) { |
| 462 | case SNS_PDUT_CONFIG: |
| 463 | case SNS_PDUT_ACK: |
| 464 | case SNS_PDUT_ADD: |
| 465 | case SNS_PDUT_CHANGE_WEIGHT: |
| 466 | case SNS_PDUT_DELETE: |
| 467 | case SNS_PDUT_CONFIG_ACK: |
| 468 | case SNS_PDUT_SIZE: |
| 469 | case SNS_PDUT_SIZE_ACK: |
| 470 | return true; |
| 471 | default: |
| 472 | return false; |
| 473 | } |
| 474 | } |
| 475 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 476 | static int gprs_ns_tx(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 477 | { |
Harald Welte | 94c9b44 | 2019-03-16 14:38:19 +0100 | [diff] [blame] | 478 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 479 | int ret; |
| 480 | |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 481 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 482 | |
Harald Welte | 94c9b44 | 2019-03-16 14:38:19 +0100 | [diff] [blame] | 483 | /* A pre-configured endpoint shall not be used for NSE data or signalling |
| 484 | * traffic (with the exception of Size and Configuration procedures) unless it |
| 485 | * is configured by the SGSN using the auto-configuration procedures. */ |
| 486 | if (nsvc_is_not_used(nsvc) && !ns_is_sns(nsh->pdu_type) && nsh->pdu_type != NS_PDUT_STATUS) { |
| 487 | LOGP(DNS, LOGL_NOTICE, "Not transmitting %s on unused/pre-configured endpoint\n", |
| 488 | get_value_string(gprs_ns_pdu_strings, nsh->pdu_type)); |
| 489 | msgb_free(msg); |
| 490 | return -EINVAL; |
| 491 | } |
| 492 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 493 | /* Increment number of Uplink bytes */ |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 494 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_OUT]); |
| 495 | rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_OUT], msgb_l2len(msg)); |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 496 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 497 | switch (nsvc->ll) { |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 498 | case GPRS_NS_LL_UDP: |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 499 | ret = nsip_sendmsg(nsvc, msg); |
Jacob Erlbeck | 8d192d7 | 2015-04-07 17:52:45 +0200 | [diff] [blame] | 500 | if (ret < 0) |
| 501 | LOGP(DNS, LOGL_INFO, |
| 502 | "failed to send NS message via UDP: %s\n", |
| 503 | strerror(-ret)); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 504 | break; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 505 | case GPRS_NS_LL_FR_GRE: |
| 506 | ret = gprs_ns_frgre_sendmsg(nsvc, msg); |
Jacob Erlbeck | 8d192d7 | 2015-04-07 17:52:45 +0200 | [diff] [blame] | 507 | if (ret < 0) |
| 508 | LOGP(DNS, LOGL_INFO, |
| 509 | "failed to send NS message via FR/GRE: %s\n", |
| 510 | strerror(-ret)); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 511 | break; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 512 | default: |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 513 | LOGP(DNS, LOGL_ERROR, "unsupported NS linklayer %u\n", nsvc->ll); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 514 | msgb_free(msg); |
| 515 | ret = -EIO; |
| 516 | break; |
| 517 | } |
| 518 | return ret; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 519 | } |
| 520 | |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 521 | static int gprs_ns_tx_simple(struct gprs_nsvc *nsvc, uint8_t pdu_type) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 522 | { |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 523 | struct msgb *msg = gprs_ns_msgb_alloc(); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 524 | struct gprs_ns_hdr *nsh; |
| 525 | |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 526 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 527 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 528 | if (!msg) |
| 529 | return -ENOMEM; |
| 530 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 531 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 532 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 533 | |
| 534 | nsh->pdu_type = pdu_type; |
| 535 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 536 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 537 | } |
| 538 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 539 | /*! Transmit a NS-RESET on a given NSVC |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 540 | * \param[in] nsvc NS-VC used for transmission |
| 541 | * \paam[in] cause Numeric NS cause value |
| 542 | */ |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 543 | int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause) |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 544 | { |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 545 | struct msgb *msg; |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 546 | struct gprs_ns_hdr *nsh; |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 547 | uint16_t nsvci = osmo_htons(nsvc->nsvci); |
| 548 | uint16_t nsei = osmo_htons(nsvc->nsei); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 549 | |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 550 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 551 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 552 | ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS RESET"); |
| 553 | |
| 554 | msg = gprs_ns_msgb_alloc(); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 555 | if (!msg) |
| 556 | return -ENOMEM; |
| 557 | |
Harald Welte | 6ecaa3d | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 558 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET (NSVCI=%u, cause=%s)\n", |
| 559 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 560 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 561 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 562 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 563 | nsh->pdu_type = NS_PDUT_RESET; |
| 564 | |
| 565 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 566 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 567 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *) &nsei); |
| 568 | |
| 569 | return gprs_ns_tx(nsvc, msg); |
| 570 | |
| 571 | } |
| 572 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 573 | /*! Transmit a NS-STATUS on a given NSVC |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 574 | * \param[in] nsvc NS-VC to be used for transmission |
| 575 | * \param[in] cause Numeric NS cause value |
| 576 | * \param[in] bvci BVCI to be reset within NSVC |
| 577 | * \param[in] orig_msg message causing the STATUS */ |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 578 | int gprs_ns_tx_status(struct gprs_nsvc *nsvc, uint8_t cause, |
| 579 | uint16_t bvci, struct msgb *orig_msg) |
| 580 | { |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 581 | struct msgb *msg = gprs_ns_msgb_alloc(); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 582 | struct gprs_ns_hdr *nsh; |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 583 | uint16_t nsvci = osmo_htons(nsvc->nsvci); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 584 | |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 585 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 586 | |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 587 | bvci = osmo_htons(bvci); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 588 | |
| 589 | if (!msg) |
| 590 | return -ENOMEM; |
| 591 | |
Harald Welte | 90af194 | 2010-05-15 23:02:24 +0200 | [diff] [blame] | 592 | LOGP(DNS, LOGL_NOTICE, "NSEI=%u Tx NS STATUS (NSVCI=%u, cause=%s)\n", |
Harald Welte | 6ecaa3d | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 593 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 594 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 595 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 596 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 597 | nsh->pdu_type = NS_PDUT_STATUS; |
| 598 | |
| 599 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 600 | |
| 601 | /* Section 9.2.7.1: Static conditions for NS-VCI */ |
| 602 | if (cause == NS_CAUSE_NSVC_BLOCKED || |
| 603 | cause == NS_CAUSE_NSVC_UNKNOWN) |
| 604 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 605 | |
| 606 | /* Section 9.2.7.2: Static conditions for NS PDU */ |
| 607 | switch (cause) { |
| 608 | case NS_CAUSE_SEM_INCORR_PDU: |
| 609 | case NS_CAUSE_PDU_INCOMP_PSTATE: |
| 610 | case NS_CAUSE_PROTO_ERR_UNSPEC: |
| 611 | case NS_CAUSE_INVAL_ESSENT_IE: |
| 612 | case NS_CAUSE_MISSING_ESSENT_IE: |
| 613 | msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg), |
| 614 | orig_msg->l2h); |
| 615 | break; |
| 616 | default: |
| 617 | break; |
| 618 | } |
| 619 | |
| 620 | /* Section 9.2.7.3: Static conditions for BVCI */ |
| 621 | if (cause == NS_CAUSE_BVCI_UNKNOWN) |
| 622 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&bvci); |
| 623 | |
| 624 | return gprs_ns_tx(nsvc, msg); |
| 625 | } |
| 626 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 627 | /*! Transmit a NS-BLOCK on a tiven NS-VC |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 628 | * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted |
| 629 | * \param[in] cause Numeric NS Cause value |
| 630 | * \returns 0 in case of success |
| 631 | */ |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 632 | int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause) |
| 633 | { |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 634 | struct msgb *msg; |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 635 | struct gprs_ns_hdr *nsh; |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 636 | uint16_t nsvci = osmo_htons(nsvc->nsvci); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 637 | |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 638 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 639 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 640 | ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS BLOCK"); |
| 641 | |
| 642 | msg = gprs_ns_msgb_alloc(); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 643 | if (!msg) |
| 644 | return -ENOMEM; |
| 645 | |
Harald Welte | 6ecaa3d | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 646 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK (NSVCI=%u, cause=%s)\n", |
| 647 | nsvc->nsei, nsvc->nsvci, gprs_ns_cause_str(cause)); |
| 648 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 649 | /* be conservative and mark it as blocked even now! */ |
Max | c513c0f | 2017-10-23 15:11:14 +0200 | [diff] [blame] | 650 | ns_mark_blocked(nsvc); |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 651 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 652 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 653 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 654 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 655 | nsh->pdu_type = NS_PDUT_BLOCK; |
| 656 | |
| 657 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &cause); |
| 658 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 659 | |
| 660 | return gprs_ns_tx(nsvc, msg); |
| 661 | } |
| 662 | |
Harald Welte | 9ef2e15 | 2019-02-19 23:42:04 +0100 | [diff] [blame] | 663 | /*! Transmit a NS-BLOCK-ACK on a given NS-VC |
| 664 | * \param[in] nsvc NS-VC on which the NS-BLOCK is to be transmitted |
| 665 | * \returns 0 in case of success |
| 666 | */ |
| 667 | static int gprs_ns_tx_block_ack(struct gprs_nsvc *nsvc) |
| 668 | { |
| 669 | struct msgb *msg; |
| 670 | struct gprs_ns_hdr *nsh; |
| 671 | uint16_t nsvci = osmo_htons(nsvc->nsvci); |
| 672 | |
| 673 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
| 674 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 675 | ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS BLOCK ACK"); |
| 676 | |
Harald Welte | 9ef2e15 | 2019-02-19 23:42:04 +0100 | [diff] [blame] | 677 | msg = gprs_ns_msgb_alloc(); |
| 678 | if (!msg) |
| 679 | return -ENOMEM; |
| 680 | |
| 681 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS BLOCK ACK (NSVCI=%u)\n", nsvc->nsei, nsvc->nsvci); |
| 682 | |
| 683 | /* be conservative and mark it as blocked even now! */ |
| 684 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 685 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 686 | nsh->pdu_type = NS_PDUT_BLOCK_ACK; |
| 687 | |
| 688 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *) &nsvci); |
| 689 | |
| 690 | return gprs_ns_tx(nsvc, msg); |
| 691 | } |
| 692 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 693 | /*! Transmit a NS-UNBLOCK on a given NS-VC |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 694 | * \param[in] nsvc NS-VC on which the NS-UNBLOCK is to be transmitted |
| 695 | * \returns 0 in case of success |
| 696 | */ |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 697 | int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc) |
| 698 | { |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 699 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 700 | |
| 701 | ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS UNBLOCK"); |
| 702 | |
Harald Welte | 6ecaa3d | 2010-05-12 12:01:33 +0000 | [diff] [blame] | 703 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS UNBLOCK (NSVCI=%u)\n", |
| 704 | nsvc->nsei, nsvc->nsvci); |
| 705 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 706 | return gprs_ns_tx_simple(nsvc, NS_PDUT_UNBLOCK); |
| 707 | } |
| 708 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 709 | /*! Transmit a NS-ALIVE on a given NS-VC |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 710 | * \param[in] nsvc NS-VC on which the NS-ALIVE is to be transmitted |
| 711 | * \returns 0 in case of success |
| 712 | */ |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 713 | int gprs_ns_tx_alive(struct gprs_nsvc *nsvc) |
| 714 | { |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 715 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 716 | LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE (NSVCI=%u)\n", |
| 717 | nsvc->nsei, nsvc->nsvci); |
| 718 | |
| 719 | return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE); |
| 720 | } |
| 721 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 722 | /*! Transmit a NS-ALIVE-ACK on a given NS-VC |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 723 | * \param[in] nsvc NS-VC on which the NS-ALIVE-ACK is to be transmitted |
| 724 | * \returns 0 in case of success |
| 725 | */ |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 726 | int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc) |
| 727 | { |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 728 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 729 | LOGP(DNS, LOGL_DEBUG, "NSEI=%u Tx NS ALIVE_ACK (NSVCI=%u)\n", |
| 730 | nsvc->nsei, nsvc->nsvci); |
| 731 | |
| 732 | return gprs_ns_tx_simple(nsvc, NS_PDUT_ALIVE_ACK); |
| 733 | } |
| 734 | |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 735 | static const enum ns_timeout timer_mode_tout[_NSVC_TIMER_NR] = { |
| 736 | [NSVC_TIMER_TNS_RESET] = NS_TOUT_TNS_RESET, |
| 737 | [NSVC_TIMER_TNS_ALIVE] = NS_TOUT_TNS_ALIVE, |
| 738 | [NSVC_TIMER_TNS_TEST] = NS_TOUT_TNS_TEST, |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 739 | }; |
| 740 | |
Harald Welte | 7c24b9e | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 741 | static const struct value_string timer_mode_strs[] = { |
| 742 | { NSVC_TIMER_TNS_RESET, "tns-reset" }, |
| 743 | { NSVC_TIMER_TNS_ALIVE, "tns-alive" }, |
| 744 | { NSVC_TIMER_TNS_TEST, "tns-test" }, |
| 745 | { 0, NULL } |
| 746 | }; |
| 747 | |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 748 | static void nsvc_start_timer(struct gprs_nsvc *nsvc, enum nsvc_timer_mode mode) |
| 749 | { |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 750 | enum ns_timeout tout = timer_mode_tout[mode]; |
| 751 | unsigned int seconds = nsvc->nsi->timeout[tout]; |
| 752 | |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 753 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
Harald Welte | 4e187c6 | 2010-05-12 13:55:36 +0000 | [diff] [blame] | 754 | DEBUGP(DNS, "NSEI=%u Starting timer in mode %s (%u seconds)\n", |
| 755 | nsvc->nsei, get_value_string(timer_mode_strs, mode), |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 756 | seconds); |
Harald Welte | 9612fcb | 2019-02-19 11:43:35 +0100 | [diff] [blame] | 757 | |
Pablo Neira Ayuso | 2b15952 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 758 | if (osmo_timer_pending(&nsvc->timer)) |
| 759 | osmo_timer_del(&nsvc->timer); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 760 | |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 761 | osmo_gettimeofday(&nsvc->timer_started, NULL); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 762 | nsvc->timer_mode = mode; |
Pablo Neira Ayuso | 2b15952 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 763 | osmo_timer_schedule(&nsvc->timer, seconds, 0); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 764 | } |
| 765 | |
Jacob Erlbeck | 0a1400f | 2015-10-06 15:23:25 +0200 | [diff] [blame] | 766 | static int nsvc_timer_elapsed_ms(struct gprs_nsvc *nsvc) |
| 767 | { |
| 768 | struct timeval now, elapsed; |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 769 | osmo_gettimeofday(&now, NULL); |
Jacob Erlbeck | 0a1400f | 2015-10-06 15:23:25 +0200 | [diff] [blame] | 770 | timersub(&now, &nsvc->timer_started, &elapsed); |
| 771 | |
| 772 | return 1000 * elapsed.tv_sec + elapsed.tv_usec / 1000; |
| 773 | } |
| 774 | |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 775 | static void gprs_ns_timer_cb(void *data) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 776 | { |
| 777 | struct gprs_nsvc *nsvc = data; |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 778 | enum ns_timeout tout = timer_mode_tout[nsvc->timer_mode]; |
| 779 | unsigned int seconds = nsvc->nsi->timeout[tout]; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 780 | |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 781 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
Harald Welte | 4e187c6 | 2010-05-12 13:55:36 +0000 | [diff] [blame] | 782 | DEBUGP(DNS, "NSEI=%u Timer expired in mode %s (%u seconds)\n", |
| 783 | nsvc->nsei, get_value_string(timer_mode_strs, nsvc->timer_mode), |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 784 | seconds); |
Harald Welte | 9612fcb | 2019-02-19 11:43:35 +0100 | [diff] [blame] | 785 | |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 786 | switch (nsvc->timer_mode) { |
| 787 | case NSVC_TIMER_TNS_ALIVE: |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 788 | /* Tns-alive case: we expired without response ! */ |
Jacob Erlbeck | 0a1400f | 2015-10-06 15:23:25 +0200 | [diff] [blame] | 789 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_ALIVE]); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 790 | nsvc->alive_retries++; |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 791 | if (nsvc->alive_retries > |
| 792 | nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]) { |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 793 | /* mark as dead (and blocked unless IP-SNS) */ |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 794 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_DEAD]); |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 795 | if (!nsvc->nsi->bss_sns_fi) { |
| 796 | ns_set_state(nsvc, NSE_S_BLOCKED); |
| 797 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]); |
| 798 | } else |
| 799 | ns_set_state(nsvc, 0); |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 800 | LOGP(DNS, LOGL_NOTICE, |
| 801 | "NSEI=%u Tns-alive expired more then " |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 802 | "%u times, blocking NS-VC\n", nsvc->nsei, |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 803 | nsvc->nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]); |
Pablo Neira Ayuso | 2b88a2a | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 804 | ns_osmo_signal_dispatch(nsvc, S_NS_ALIVE_EXP, 0); |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 805 | /* FIXME: should we send this signal in case of SNS? */ |
| 806 | if (!nsvc->nsi->bss_sns_fi) |
| 807 | ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, NS_CAUSE_NSVC_BLOCKED); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 808 | return; |
| 809 | } |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 810 | /* Tns-test case: send NS-ALIVE PDU */ |
| 811 | gprs_ns_tx_alive(nsvc); |
| 812 | /* start Tns-alive timer */ |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 813 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 814 | break; |
| 815 | case NSVC_TIMER_TNS_TEST: |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 816 | /* Tns-test case: send NS-ALIVE PDU */ |
Harald Welte | b983c31 | 2010-05-12 12:11:45 +0000 | [diff] [blame] | 817 | gprs_ns_tx_alive(nsvc); |
| 818 | /* start Tns-alive timer (transition into faster |
| 819 | * alive retransmissions) */ |
Harald Welte | 7c24b9e | 2010-05-12 12:21:52 +0000 | [diff] [blame] | 820 | nsvc->alive_retries = 0; |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 821 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_ALIVE); |
| 822 | break; |
| 823 | case NSVC_TIMER_TNS_RESET: |
Jacob Erlbeck | 0a1400f | 2015-10-06 15:23:25 +0200 | [diff] [blame] | 824 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_LOST_RESET]); |
Jacob Erlbeck | cc6ac4c | 2015-12-17 13:22:36 +0100 | [diff] [blame] | 825 | if (!(nsvc->state & NSE_S_RESET)) |
| 826 | LOGP(DNS, LOGL_NOTICE, |
| 827 | "NSEI=%u Reset timed out but RESET flag is not set\n", |
| 828 | nsvc->nsei); |
| 829 | /* Mark NS-VC locally as blocked and dead */ |
Max | c513c0f | 2017-10-23 15:11:14 +0200 | [diff] [blame] | 830 | ns_set_state(nsvc, NSE_S_BLOCKED | NSE_S_RESET); |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 831 | /* Chapter 7.3: Re-send the RESET */ |
Harald Welte | 570fb83 | 2010-05-03 21:05:24 +0200 | [diff] [blame] | 832 | gprs_ns_tx_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 833 | /* Re-start Tns-reset timer */ |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 834 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 835 | break; |
Harald Welte | 2fc725d | 2010-05-11 10:15:26 +0200 | [diff] [blame] | 836 | case _NSVC_TIMER_NR: |
| 837 | break; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 838 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | /* Section 9.2.6 */ |
Harald Welte | c547848 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 842 | static int gprs_ns_tx_reset_ack(struct gprs_nsvc *nsvc) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 843 | { |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 844 | struct msgb *msg; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 845 | struct gprs_ns_hdr *nsh; |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 846 | uint16_t nsvci, nsei; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 847 | |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 848 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 849 | |
| 850 | ERR_IF_NSVC_USES_SNS(nsvc, "transmit NS RESET ACK"); |
| 851 | |
| 852 | msg = gprs_ns_msgb_alloc(); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 853 | if (!msg) |
| 854 | return -ENOMEM; |
| 855 | |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 856 | nsvci = osmo_htons(nsvc->nsvci); |
| 857 | nsei = osmo_htons(nsvc->nsei); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 858 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 859 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 860 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 861 | |
| 862 | nsh->pdu_type = NS_PDUT_RESET_ACK; |
| 863 | |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 864 | LOGP(DNS, LOGL_INFO, "NSEI=%u Tx NS RESET ACK (NSVCI=%u)\n", |
Harald Welte | bbc9fac | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 865 | nsvc->nsei, nsvc->nsvci); |
Harald Welte | c547848 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 866 | |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 867 | msgb_tvlv_put(msg, NS_IE_VCI, 2, (uint8_t *)&nsvci); |
| 868 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 869 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 870 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 871 | } |
| 872 | |
Harald Welte | 17a642d | 2018-07-01 19:09:52 +0200 | [diff] [blame] | 873 | /*! Encode + Transmit a SNS-ACK as per Section 9.3.1. |
| 874 | * \param[in] nsvc NS-VC through which to transmit the ACK |
| 875 | * \param[in] trans_id Transaction ID which to acknowledge |
| 876 | * \param[in] cause Pointer to cause value (NULL if no cause to be sent) |
| 877 | * \param[in] ip4_elems Array of IPv4 Elements |
| 878 | * \param[in] num_ip4_elems number of ip4_elems |
| 879 | * \returns 0 on success; negative in case of error */ |
| 880 | int gprs_ns_tx_sns_ack(struct gprs_nsvc *nsvc, uint8_t trans_id, uint8_t *cause, |
| 881 | const struct gprs_ns_ie_ip4_elem *ip4_elems, |
| 882 | unsigned int num_ip4_elems) |
| 883 | { |
| 884 | struct msgb *msg = gprs_ns_msgb_alloc(); |
| 885 | struct gprs_ns_hdr *nsh; |
| 886 | uint16_t nsei; |
| 887 | |
| 888 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
| 889 | if (!msg) |
| 890 | return -ENOMEM; |
| 891 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 892 | if (!nsvc->nsi->bss_sns_fi) { |
| 893 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n", |
| 894 | nsvc->nsei); |
| 895 | msgb_free(msg); |
| 896 | return -EIO; |
| 897 | } |
| 898 | |
Harald Welte | 17a642d | 2018-07-01 19:09:52 +0200 | [diff] [blame] | 899 | nsei = osmo_htons(nsvc->nsei); |
| 900 | |
| 901 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 902 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 903 | |
| 904 | nsh->pdu_type = SNS_PDUT_ACK; |
| 905 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei); |
| 906 | msgb_v_put(msg, trans_id); |
| 907 | if (cause) |
| 908 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, cause); |
| 909 | if (ip4_elems) { |
| 910 | /* List of IP4 Elements 10.3.2c */ |
| 911 | msgb_tvlv_put(msg, NS_IE_IPv4_LIST, |
| 912 | num_ip4_elems*sizeof(struct gprs_ns_ie_ip4_elem), |
| 913 | (const uint8_t *)ip4_elems); |
| 914 | } |
| 915 | /* FIXME: List of IP6 elements 10.3.2d */ |
| 916 | return gprs_ns_tx(nsvc, msg); |
| 917 | } |
| 918 | |
| 919 | /*! Encode + Transmit a SNS-CONFIG as per Section 9.3.4. |
| 920 | * \param[in] nsvc NS-VC through which to transmit the SNS-CONFIG |
| 921 | * \param[in] end_flag Whether or not this is the last SNS-CONFIG |
| 922 | * \param[in] ip4_elems Array of IPv4 Elements |
| 923 | * \param[in] num_ip4_elems number of ip4_elems |
| 924 | * \returns 0 on success; negative in case of error */ |
| 925 | int gprs_ns_tx_sns_config(struct gprs_nsvc *nsvc, bool end_flag, |
| 926 | const struct gprs_ns_ie_ip4_elem *ip4_elems, |
| 927 | unsigned int num_ip4_elems) |
| 928 | { |
| 929 | struct msgb *msg = gprs_ns_msgb_alloc(); |
| 930 | struct gprs_ns_hdr *nsh; |
| 931 | uint16_t nsei; |
| 932 | |
| 933 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
| 934 | if (!msg) |
| 935 | return -ENOMEM; |
| 936 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 937 | if (!nsvc->nsi->bss_sns_fi) { |
| 938 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n", |
| 939 | nsvc->nsei); |
| 940 | msgb_free(msg); |
| 941 | return -EIO; |
| 942 | } |
| 943 | |
Harald Welte | 17a642d | 2018-07-01 19:09:52 +0200 | [diff] [blame] | 944 | nsei = osmo_htons(nsvc->nsei); |
| 945 | |
| 946 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 947 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 948 | |
| 949 | nsh->pdu_type = SNS_PDUT_CONFIG; |
| 950 | |
| 951 | msgb_v_put(msg, end_flag ? 0x01 : 0x00); |
| 952 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei); |
| 953 | |
| 954 | /* List of IP4 Elements 10.3.2c */ |
| 955 | msgb_tvlv_put(msg, NS_IE_IPv4_LIST, num_ip4_elems*sizeof(struct gprs_ns_ie_ip4_elem), |
| 956 | (const uint8_t *)ip4_elems); |
| 957 | /* FIXME: List of IP6 elements 10.3.2d */ |
| 958 | |
| 959 | return gprs_ns_tx(nsvc, msg); |
| 960 | } |
| 961 | |
| 962 | /*! Encode + Transmit a SNS-CONFIG-ACK as per Section 9.3.5. |
| 963 | * \param[in] nsvc NS-VC through which to transmit the SNS-CONFIG-ACK |
| 964 | * \param[in] cause Pointer to cause value (NULL if no cause to be sent) |
| 965 | * \returns 0 on success; negative in case of error */ |
| 966 | int gprs_ns_tx_sns_config_ack(struct gprs_nsvc *nsvc, uint8_t *cause) |
| 967 | { |
| 968 | struct msgb *msg = gprs_ns_msgb_alloc(); |
| 969 | struct gprs_ns_hdr *nsh; |
| 970 | uint16_t nsei; |
| 971 | |
| 972 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
| 973 | if (!msg) |
| 974 | return -ENOMEM; |
| 975 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 976 | if (!nsvc->nsi->bss_sns_fi) { |
| 977 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n", |
| 978 | nsvc->nsei); |
| 979 | msgb_free(msg); |
| 980 | return -EIO; |
| 981 | } |
| 982 | |
Harald Welte | 17a642d | 2018-07-01 19:09:52 +0200 | [diff] [blame] | 983 | nsei = osmo_htons(nsvc->nsei); |
| 984 | |
| 985 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 986 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 987 | |
| 988 | nsh->pdu_type = SNS_PDUT_CONFIG_ACK; |
| 989 | |
| 990 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei); |
| 991 | if (cause) |
| 992 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, cause); |
| 993 | |
| 994 | return gprs_ns_tx(nsvc, msg); |
| 995 | } |
| 996 | |
| 997 | |
| 998 | /*! Encode + transmit a SNS-SIZE as per Section 9.3.7. |
| 999 | * \param[in] nsvc NS-VC through which to transmit the SNS-SIZE |
| 1000 | * \param[in] reset_flag Whether or not to add a RESET flag |
| 1001 | * \param[in] max_nr_nsvc Maximum number of NS-VCs |
| 1002 | * \param[in] ip4_ep_nr Number of IPv4 endpoints (NULL if none) |
| 1003 | * \param[in] ip6_ep_nr Number of IPv6 endpoints (NULL if none) |
| 1004 | * \returns 0 on success; negative in case of error */ |
| 1005 | int gprs_ns_tx_sns_size(struct gprs_nsvc *nsvc, bool reset_flag, uint16_t max_nr_nsvc, |
| 1006 | uint16_t *ip4_ep_nr, uint16_t *ip6_ep_nr) |
| 1007 | { |
| 1008 | struct msgb *msg = gprs_ns_msgb_alloc(); |
| 1009 | struct gprs_ns_hdr *nsh; |
| 1010 | uint16_t nsei; |
| 1011 | |
| 1012 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
| 1013 | if (!msg) |
| 1014 | return -ENOMEM; |
| 1015 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 1016 | if (!nsvc->nsi->bss_sns_fi) { |
| 1017 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n", |
| 1018 | nsvc->nsei); |
| 1019 | msgb_free(msg); |
| 1020 | return -EIO; |
| 1021 | } |
| 1022 | |
Harald Welte | 17a642d | 2018-07-01 19:09:52 +0200 | [diff] [blame] | 1023 | nsei = osmo_htons(nsvc->nsei); |
| 1024 | |
| 1025 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 1026 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 1027 | |
| 1028 | nsh->pdu_type = SNS_PDUT_SIZE; |
| 1029 | |
| 1030 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei); |
| 1031 | msgb_tv_put(msg, NS_IE_RESET_FLAG, reset_flag ? 0x01 : 0x00); |
| 1032 | msgb_tv16_put(msg, NS_IE_MAX_NR_NSVC, max_nr_nsvc); |
| 1033 | if (ip4_ep_nr) |
| 1034 | msgb_tv16_put(msg, NS_IE_IPv4_EP_NR, *ip4_ep_nr); |
| 1035 | if (ip6_ep_nr) |
| 1036 | msgb_tv16_put(msg, NS_IE_IPv6_EP_NR, *ip6_ep_nr); |
| 1037 | |
| 1038 | return gprs_ns_tx(nsvc, msg); |
| 1039 | } |
| 1040 | |
| 1041 | /*! Encode + Transmit a SNS-SIZE-ACK as per Section 9.3.8. |
| 1042 | * \param[in] nsvc NS-VC through which to transmit the SNS-SIZE-ACK |
| 1043 | * \param[in] cause Pointer to cause value (NULL if no cause to be sent) |
| 1044 | * \returns 0 on success; negative in case of error */ |
| 1045 | int gprs_ns_tx_sns_size_ack(struct gprs_nsvc *nsvc, uint8_t *cause) |
| 1046 | { |
| 1047 | struct msgb *msg = gprs_ns_msgb_alloc(); |
| 1048 | struct gprs_ns_hdr *nsh; |
| 1049 | uint16_t nsei; |
| 1050 | |
| 1051 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
| 1052 | if (!msg) |
| 1053 | return -ENOMEM; |
| 1054 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 1055 | if (!nsvc->nsi->bss_sns_fi) { |
| 1056 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Cannot transmit SNS on NSVC without SNS active\n", |
| 1057 | nsvc->nsei); |
| 1058 | msgb_free(msg); |
| 1059 | return -EIO; |
| 1060 | } |
| 1061 | |
Harald Welte | 17a642d | 2018-07-01 19:09:52 +0200 | [diff] [blame] | 1062 | nsei = osmo_htons(nsvc->nsei); |
| 1063 | |
| 1064 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 1065 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 1066 | |
| 1067 | nsh->pdu_type = SNS_PDUT_SIZE_ACK; |
| 1068 | |
| 1069 | msgb_tvlv_put(msg, NS_IE_NSEI, 2, (uint8_t *)&nsei); |
| 1070 | if (cause) |
| 1071 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, cause); |
| 1072 | |
| 1073 | return gprs_ns_tx(nsvc, msg); |
| 1074 | } |
| 1075 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1076 | /*! High-level function for transmitting a NS-UNITDATA messsage |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1077 | * \param[in] nsi NS-instance on which we shall transmit |
| 1078 | * \param[in] msg struct msgb to be trasnmitted |
| 1079 | * |
| 1080 | * This function obtains the NS-VC by the msgb_nsei(msg) and then checks |
| 1081 | * if the NS-VC is ALIVEV and not BLOCKED. After that, it adds a NS |
| 1082 | * header for the NS-UNITDATA message type and sends it off. |
| 1083 | * |
| 1084 | * Section 9.2.10: transmit side / NS-UNITDATA-REQUEST primitive |
| 1085 | */ |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 1086 | int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1087 | { |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 1088 | struct gprs_nsvc *nsvc; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1089 | struct gprs_ns_hdr *nsh; |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 1090 | uint16_t bvci = msgb_bvci(msg); |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 1091 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 1092 | nsvc = gprs_active_nsvc_by_nsei(nsi, msgb_nsei(msg), msgb_bvci(msg)); |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 1093 | if (!nsvc) { |
Jacob Erlbeck | 6901715 | 2013-10-14 12:03:54 +0200 | [diff] [blame] | 1094 | int rc; |
| 1095 | if (gprs_nsvc_by_nsei(nsi, msgb_nsei(msg))) { |
| 1096 | LOGP(DNS, LOGL_ERROR, |
| 1097 | "All NS-VCs for NSEI %u are either dead or blocked!\n", |
| 1098 | msgb_nsei(msg)); |
| 1099 | rc = -EBUSY; |
| 1100 | } else { |
| 1101 | LOGP(DNS, LOGL_ERROR, "Unable to resolve NSEI %u " |
| 1102 | "to NS-VC!\n", msgb_nsei(msg)); |
| 1103 | rc = -EINVAL; |
| 1104 | } |
| 1105 | |
Harald Welte | f47df64 | 2010-08-09 21:15:40 +0800 | [diff] [blame] | 1106 | msgb_free(msg); |
Jacob Erlbeck | 6901715 | 2013-10-14 12:03:54 +0200 | [diff] [blame] | 1107 | return rc; |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 1108 | } |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 1109 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1110 | |
Harald Welte | ec20ba4 | 2010-05-13 12:18:49 +0200 | [diff] [blame] | 1111 | msg->l2h = msgb_push(msg, sizeof(*nsh) + 3); |
| 1112 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1113 | if (!nsh) { |
Harald Welte | b8a6a83 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 1114 | LOGP(DNS, LOGL_ERROR, "Not enough headroom for NS header\n"); |
Harald Welte | f47df64 | 2010-08-09 21:15:40 +0800 | [diff] [blame] | 1115 | msgb_free(msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1116 | return -EIO; |
| 1117 | } |
| 1118 | |
| 1119 | nsh->pdu_type = NS_PDUT_UNITDATA; |
| 1120 | /* spare octet in data[0] */ |
| 1121 | nsh->data[1] = bvci >> 8; |
| 1122 | nsh->data[2] = bvci & 0xff; |
| 1123 | |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 1124 | return gprs_ns_tx(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | /* Section 9.2.10: receive side */ |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 1128 | static int gprs_ns_rx_unitdata(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1129 | { |
| 1130 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 1131 | uint16_t bvci; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1132 | |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 1133 | if (nsvc->state & NSE_S_BLOCKED) |
| 1134 | return gprs_ns_tx_status(nsvc, NS_CAUSE_NSVC_BLOCKED, |
| 1135 | 0, msg); |
| 1136 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1137 | /* spare octet in data[0] */ |
| 1138 | bvci = nsh->data[1] << 8 | nsh->data[2]; |
Harald Welte | ec19c10 | 2010-05-02 09:50:42 +0200 | [diff] [blame] | 1139 | msgb_bssgph(msg) = &nsh->data[3]; |
Harald Welte | 30bc19a | 2010-05-02 11:19:37 +0200 | [diff] [blame] | 1140 | msgb_bvci(msg) = bvci; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1141 | |
| 1142 | /* call upper layer (BSSGP) */ |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1143 | return nsvc->nsi->cb(GPRS_NS_EVT_UNIT_DATA, nsvc, msg, bvci); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | /* Section 9.2.7 */ |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 1147 | static int gprs_ns_rx_status(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1148 | { |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1149 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1150 | struct tlv_parsed tp; |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 1151 | uint8_t cause; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1152 | int rc; |
| 1153 | |
Harald Welte | 4133783 | 2010-05-16 00:24:26 +0200 | [diff] [blame] | 1154 | LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx NS STATUS ", nsvc->nsei); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1155 | |
Harald Welte | bd33f3d | 2010-05-30 17:19:38 +0200 | [diff] [blame] | 1156 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 1157 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
Harald Welte | 3625038 | 2010-05-28 10:08:14 +0200 | [diff] [blame] | 1158 | if (rc < 0) { |
| 1159 | LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse\n"); |
| 1160 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS STATUS: " |
| 1161 | "Error during TLV Parse\n", nsvc->nsei); |
| 1162 | return rc; |
| 1163 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1164 | |
| 1165 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE)) { |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 1166 | LOGPC(DNS, LOGL_INFO, "missing cause IE\n"); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1167 | return -EINVAL; |
| 1168 | } |
| 1169 | |
| 1170 | cause = *TLVP_VAL(&tp, NS_IE_CAUSE); |
Harald Welte | 4133783 | 2010-05-16 00:24:26 +0200 | [diff] [blame] | 1171 | LOGPC(DNS, LOGL_NOTICE, "cause=%s\n", gprs_ns_cause_str(cause)); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1172 | |
| 1173 | return 0; |
| 1174 | } |
| 1175 | |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1176 | /* Replace a nsvc object with another based on NSVCI. |
| 1177 | * This function replaces looks for a NSVC with the given NSVCI and replaces it |
| 1178 | * if possible and necessary. If replaced, the former value of *nsvc is |
| 1179 | * returned in *old_nsvc. |
| 1180 | * \return != 0 if *nsvc points to a matching NSVC. |
| 1181 | */ |
| 1182 | static int gprs_nsvc_replace_if_found(uint16_t nsvci, |
| 1183 | struct gprs_nsvc **nsvc, |
| 1184 | struct gprs_nsvc **old_nsvc) |
| 1185 | { |
| 1186 | struct gprs_nsvc *matching_nsvc; |
| 1187 | |
| 1188 | if ((*nsvc)->nsvci == nsvci) { |
| 1189 | *old_nsvc = NULL; |
| 1190 | return 1; |
| 1191 | } |
| 1192 | |
| 1193 | matching_nsvc = gprs_nsvc_by_nsvci((*nsvc)->nsi, nsvci); |
| 1194 | |
| 1195 | if (!matching_nsvc) |
| 1196 | return 0; |
| 1197 | |
| 1198 | /* The NS-VCI is already used by this NS-VC */ |
| 1199 | |
| 1200 | char *old_peer; |
| 1201 | |
| 1202 | /* Exchange the NS-VC objects */ |
| 1203 | *old_nsvc = *nsvc; |
| 1204 | *nsvc = matching_nsvc; |
| 1205 | |
| 1206 | /* Do logging */ |
| 1207 | old_peer = talloc_strdup(*old_nsvc, gprs_ns_ll_str(*old_nsvc)); |
| 1208 | LOGP(DNS, LOGL_INFO, "NS-VC changed link (NSVCI=%u) from %s to %s\n", |
| 1209 | nsvci, old_peer, gprs_ns_ll_str(*nsvc)); |
| 1210 | |
| 1211 | talloc_free(old_peer); |
| 1212 | |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1213 | return 1; |
| 1214 | } |
| 1215 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1216 | /* Section 7.3 */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1217 | static int gprs_ns_rx_reset(struct gprs_nsvc **nsvc, struct msgb *msg) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1218 | { |
| 1219 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1220 | struct tlv_parsed tp; |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1221 | uint8_t cause; |
| 1222 | uint16_t nsvci, nsei; |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1223 | struct gprs_nsvc *orig_nsvc = NULL; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1224 | int rc; |
| 1225 | |
Harald Welte | bd33f3d | 2010-05-30 17:19:38 +0200 | [diff] [blame] | 1226 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 1227 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
Harald Welte | 3625038 | 2010-05-28 10:08:14 +0200 | [diff] [blame] | 1228 | if (rc < 0) { |
| 1229 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET " |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1230 | "Error during TLV Parse\n", (*nsvc)->nsei); |
Harald Welte | 3625038 | 2010-05-28 10:08:14 +0200 | [diff] [blame] | 1231 | return rc; |
| 1232 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1233 | |
| 1234 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 1235 | !TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 1236 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
Harald Welte | b8a6a83 | 2010-05-11 05:54:22 +0200 | [diff] [blame] | 1237 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1238 | gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1239 | return -EINVAL; |
| 1240 | } |
| 1241 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1242 | cause = *(uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 1243 | nsvci = tlvp_val16be(&tp, NS_IE_VCI); |
| 1244 | nsei = tlvp_val16be(&tp, NS_IE_NSEI); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1245 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1246 | LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET (NSEI=%u, NSVCI=%u, cause=%s)\n", |
| 1247 | (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)", |
| 1248 | nsei, nsvci, gprs_ns_cause_str(cause)); |
| 1249 | |
Jacob Erlbeck | ab852ee | 2013-10-24 01:33:24 +0200 | [diff] [blame] | 1250 | if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsvci != nsvci) { |
| 1251 | if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) { |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1252 | /* The incoming RESET doesn't match the NSVCI. Send an |
| 1253 | * appropriate RESET_ACK and ignore the RESET. |
| 1254 | * See 3GPP TS 08.16, 7.3.1, 2nd paragraph. |
| 1255 | */ |
| 1256 | ns_osmo_signal_dispatch_mismatch(*nsvc, msg, |
| 1257 | NS_PDUT_RESET, |
| 1258 | NS_IE_VCI); |
| 1259 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]); |
| 1260 | gprs_ns_tx_reset_ack(*nsvc); |
| 1261 | return 0; |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1262 | } |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1263 | |
| 1264 | /* NS-VCI has changed */ |
Jacob Erlbeck | 54b8b2d | 2013-10-24 01:33:25 +0200 | [diff] [blame] | 1265 | if (!gprs_nsvc_replace_if_found(nsvci, nsvc, &orig_nsvc)) { |
| 1266 | LOGP(DNS, LOGL_INFO, "Creating NS-VC %d replacing %d " |
| 1267 | "at %s\n", |
| 1268 | nsvci, (*nsvc)->nsvci, |
| 1269 | gprs_ns_ll_str(*nsvc)); |
| 1270 | orig_nsvc = *nsvc; |
| 1271 | *nsvc = gprs_nsvc_create((*nsvc)->nsi, nsvci); |
Jacob Erlbeck | 54b8b2d | 2013-10-24 01:33:25 +0200 | [diff] [blame] | 1272 | (*nsvc)->nsei = nsei; |
| 1273 | } |
Jacob Erlbeck | ab852ee | 2013-10-24 01:33:24 +0200 | [diff] [blame] | 1274 | } |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1275 | |
Jacob Erlbeck | ab852ee | 2013-10-24 01:33:24 +0200 | [diff] [blame] | 1276 | if ((*nsvc)->nsvci_is_valid && (*nsvc)->nsei != nsei) { |
| 1277 | if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) { |
| 1278 | /* The incoming RESET doesn't match the NSEI. Send an |
| 1279 | * appropriate RESET_ACK and ignore the RESET. |
| 1280 | * See 3GPP TS 08.16, 7.3.1, 3rd paragraph. |
| 1281 | */ |
| 1282 | ns_osmo_signal_dispatch_mismatch(*nsvc, msg, |
| 1283 | NS_PDUT_RESET, |
| 1284 | NS_IE_NSEI); |
| 1285 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]); |
Holger Hans Peter Freyther | 726e272 | 2013-10-25 11:05:10 +0200 | [diff] [blame] | 1286 | rc = gprs_ns_tx_reset_ack(*nsvc); |
Holger Hans Peter Freyther | 9e1cd5a | 2013-10-25 11:09:26 +0200 | [diff] [blame] | 1287 | CHECK_TX_RC(rc, *nsvc); |
Jacob Erlbeck | ab852ee | 2013-10-24 01:33:24 +0200 | [diff] [blame] | 1288 | return 0; |
| 1289 | } |
| 1290 | |
| 1291 | /* NSEI has changed */ |
| 1292 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]); |
| 1293 | (*nsvc)->nsei = nsei; |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1294 | } |
Harald Welte | bbc9fac | 2010-05-03 18:54:12 +0200 | [diff] [blame] | 1295 | |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 1296 | /* Mark NS-VC as blocked and alive */ |
Max | c513c0f | 2017-10-23 15:11:14 +0200 | [diff] [blame] | 1297 | ns_set_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE); |
Harald Welte | e8b9ca2 | 2010-05-11 18:18:31 +0200 | [diff] [blame] | 1298 | |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1299 | if (orig_nsvc) { |
Jacob Erlbeck | 54b8b2d | 2013-10-24 01:33:25 +0200 | [diff] [blame] | 1300 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]); |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1301 | ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc); |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1302 | |
| 1303 | /* Update the ll info fields */ |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1304 | gprs_ns_ll_copy(*nsvc, orig_nsvc); |
| 1305 | gprs_ns_ll_clear(orig_nsvc); |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1306 | } else { |
| 1307 | (*nsvc)->nsei = nsei; |
| 1308 | (*nsvc)->nsvci = nsvci; |
| 1309 | (*nsvc)->nsvci_is_valid = 1; |
Holger Hans Peter Freyther | 495b0db | 2015-11-04 14:39:37 +0100 | [diff] [blame] | 1310 | rate_ctr_group_upd_idx((*nsvc)->ctrg, nsvci); |
| 1311 | osmo_stat_item_group_udp_idx((*nsvc)->statg, nsvci); |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1312 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1313 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 1314 | /* inform interested parties about the fact that this NSVC |
| 1315 | * has received RESET */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1316 | ns_osmo_signal_dispatch(*nsvc, S_NS_RESET, cause); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 1317 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1318 | rc = gprs_ns_tx_reset_ack(*nsvc); |
Jacob Erlbeck | 05395a6 | 2013-10-08 12:04:45 +0200 | [diff] [blame] | 1319 | |
| 1320 | /* start the test procedure */ |
Harald Welte | 35042a2 | 2019-02-23 14:07:00 +0100 | [diff] [blame] | 1321 | gprs_nsvc_start_test(*nsvc); |
Jacob Erlbeck | 05395a6 | 2013-10-08 12:04:45 +0200 | [diff] [blame] | 1322 | |
| 1323 | return rc; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1324 | } |
| 1325 | |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1326 | static int gprs_ns_rx_reset_ack(struct gprs_nsvc **nsvc, struct msgb *msg) |
| 1327 | { |
| 1328 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 1329 | struct tlv_parsed tp; |
| 1330 | uint16_t nsvci, nsei; |
| 1331 | struct gprs_nsvc *orig_nsvc = NULL; |
| 1332 | int rc; |
| 1333 | |
| 1334 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 1335 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
| 1336 | if (rc < 0) { |
| 1337 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS RESET ACK " |
| 1338 | "Error during TLV Parse\n", (*nsvc)->nsei); |
| 1339 | return rc; |
| 1340 | } |
| 1341 | |
| 1342 | if (!TLVP_PRESENT(&tp, NS_IE_VCI) || |
| 1343 | !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
| 1344 | LOGP(DNS, LOGL_ERROR, "NS RESET ACK Missing mandatory IE\n"); |
Holger Hans Peter Freyther | 7c91bfd | 2013-10-25 11:02:05 +0200 | [diff] [blame] | 1345 | rc = gprs_ns_tx_status(*nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Holger Hans Peter Freyther | 9e1cd5a | 2013-10-25 11:09:26 +0200 | [diff] [blame] | 1346 | CHECK_TX_RC(rc, *nsvc); |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1347 | return -EINVAL; |
| 1348 | } |
| 1349 | |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 1350 | nsvci = tlvp_val16be(&tp, NS_IE_VCI); |
| 1351 | nsei = tlvp_val16be(&tp, NS_IE_NSEI); |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1352 | |
| 1353 | LOGP(DNS, LOGL_INFO, "NSVCI=%u%s Rx NS RESET ACK (NSEI=%u, NSVCI=%u)\n", |
| 1354 | (*nsvc)->nsvci, (*nsvc)->nsvci_is_valid ? "" : "(invalid)", |
| 1355 | nsei, nsvci); |
| 1356 | |
| 1357 | if (!((*nsvc)->state & NSE_S_RESET)) { |
| 1358 | /* Not waiting for a RESET_ACK on this NS-VC, ignore it. |
| 1359 | * See 3GPP TS 08.16, 7.3.1, 5th paragraph. |
| 1360 | */ |
| 1361 | LOGP(DNS, LOGL_ERROR, |
| 1362 | "NS RESET ACK Discarding unexpected message for " |
| 1363 | "NS-VCI %d from SGSN NSEI=%d\n", |
| 1364 | nsvci, nsei); |
| 1365 | return 0; |
| 1366 | } |
| 1367 | |
| 1368 | if (!(*nsvc)->nsvci_is_valid) { |
| 1369 | LOGP(DNS, LOGL_NOTICE, |
| 1370 | "NS RESET ACK Uninitialised NS-VC (%u) for " |
| 1371 | "NS-VCI %d, NSEI=%d from %s\n", |
| 1372 | (*nsvc)->nsvci, nsvci, nsei, gprs_ns_ll_str(*nsvc)); |
| 1373 | return -EINVAL; |
| 1374 | } |
| 1375 | |
| 1376 | if ((*nsvc)->nsvci != nsvci) { |
| 1377 | /* NS-VCI has changed */ |
| 1378 | |
| 1379 | /* if !0, use another NSVC object that matches the NSVCI */ |
| 1380 | int use_other_nsvc; |
| 1381 | |
| 1382 | /* Only do this with BSS peers */ |
Jacob Erlbeck | ab852ee | 2013-10-24 01:33:24 +0200 | [diff] [blame] | 1383 | use_other_nsvc = !(*nsvc)->remote_end_is_sgsn && |
| 1384 | !(*nsvc)->persistent; |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1385 | |
| 1386 | if (use_other_nsvc) |
| 1387 | /* Update *nsvc to point to the right NSVC object */ |
| 1388 | use_other_nsvc = gprs_nsvc_replace_if_found(nsvci, nsvc, |
| 1389 | &orig_nsvc); |
| 1390 | |
| 1391 | if (!use_other_nsvc) { |
| 1392 | /* The incoming RESET_ACK doesn't match the NSVCI. |
| 1393 | * See 3GPP TS 08.16, 7.3.1, 4th paragraph. |
| 1394 | */ |
| 1395 | ns_osmo_signal_dispatch_mismatch(*nsvc, msg, |
| 1396 | NS_PDUT_RESET_ACK, |
| 1397 | NS_IE_VCI); |
| 1398 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_VCI]); |
| 1399 | LOGP(DNS, LOGL_ERROR, |
| 1400 | "NS RESET ACK Unknown NS-VCI %d (%s NSEI=%d) " |
| 1401 | "from %s\n", |
| 1402 | nsvci, |
| 1403 | (*nsvc)->remote_end_is_sgsn ? "SGSN" : "BSS", |
| 1404 | nsei, gprs_ns_ll_str(*nsvc)); |
| 1405 | return -EINVAL; |
| 1406 | } |
| 1407 | |
| 1408 | /* Notify others */ |
Jacob Erlbeck | 54b8b2d | 2013-10-24 01:33:25 +0200 | [diff] [blame] | 1409 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_REPLACED]); |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1410 | ns_osmo_signal_dispatch_replaced(*nsvc, orig_nsvc); |
| 1411 | |
| 1412 | /* Update the ll info fields */ |
| 1413 | gprs_ns_ll_copy(*nsvc, orig_nsvc); |
| 1414 | gprs_ns_ll_clear(orig_nsvc); |
| 1415 | } else if ((*nsvc)->nsei != nsei) { |
Jacob Erlbeck | ab852ee | 2013-10-24 01:33:24 +0200 | [diff] [blame] | 1416 | if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) { |
| 1417 | /* The incoming RESET_ACK doesn't match the NSEI. |
| 1418 | * See 3GPP TS 08.16, 7.3.1, 4th paragraph. |
| 1419 | */ |
| 1420 | ns_osmo_signal_dispatch_mismatch(*nsvc, msg, |
| 1421 | NS_PDUT_RESET_ACK, |
| 1422 | NS_IE_NSEI); |
| 1423 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_INV_NSEI]); |
| 1424 | LOGP(DNS, LOGL_ERROR, |
| 1425 | "NS RESET ACK Unknown NSEI %d (NS-VCI=%u) from %s\n", |
| 1426 | nsei, nsvci, gprs_ns_ll_str(*nsvc)); |
| 1427 | return -EINVAL; |
| 1428 | } |
| 1429 | |
| 1430 | /* NSEI has changed */ |
| 1431 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_NSEI_CHG]); |
| 1432 | (*nsvc)->nsei = nsei; |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | /* Mark NS-VC as blocked and alive */ |
Max | c513c0f | 2017-10-23 15:11:14 +0200 | [diff] [blame] | 1436 | ns_set_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE); |
| 1437 | ns_set_remote_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE); |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1438 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_BLOCKED]); |
| 1439 | if ((*nsvc)->persistent || (*nsvc)->remote_end_is_sgsn) { |
| 1440 | /* stop RESET timer */ |
| 1441 | osmo_timer_del(&(*nsvc)->timer); |
| 1442 | } |
| 1443 | /* Initiate TEST proc.: Send ALIVE and start timer */ |
Harald Welte | 35042a2 | 2019-02-23 14:07:00 +0100 | [diff] [blame] | 1444 | gprs_nsvc_start_test(*nsvc); |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1445 | |
Harald Welte | 35042a2 | 2019-02-23 14:07:00 +0100 | [diff] [blame] | 1446 | return 0; |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1447 | } |
| 1448 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 1449 | static int gprs_ns_rx_block(struct gprs_nsvc *nsvc, struct msgb *msg) |
| 1450 | { |
| 1451 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 1452 | struct tlv_parsed tp; |
| 1453 | uint8_t *cause; |
| 1454 | int rc; |
| 1455 | |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 1456 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK\n", nsvc->nsei); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 1457 | |
Max | c513c0f | 2017-10-23 15:11:14 +0200 | [diff] [blame] | 1458 | ns_mark_blocked(nsvc); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 1459 | |
Harald Welte | bd33f3d | 2010-05-30 17:19:38 +0200 | [diff] [blame] | 1460 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 1461 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
Harald Welte | 3625038 | 2010-05-28 10:08:14 +0200 | [diff] [blame] | 1462 | if (rc < 0) { |
| 1463 | LOGP(DNS, LOGL_ERROR, "NSEI=%u Rx NS BLOCK " |
| 1464 | "Error during TLV Parse\n", nsvc->nsei); |
| 1465 | return rc; |
| 1466 | } |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 1467 | |
| 1468 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 1469 | !TLVP_PRESENT(&tp, NS_IE_VCI)) { |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 1470 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 1471 | gprs_ns_tx_status(nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, msg); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 1472 | return -EINVAL; |
| 1473 | } |
| 1474 | |
| 1475 | cause = (uint8_t *) TLVP_VAL(&tp, NS_IE_CAUSE); |
| 1476 | //nsvci = (uint16_t *) TLVP_VAL(&tp, NS_IE_VCI); |
| 1477 | |
Pablo Neira Ayuso | 2b88a2a | 2011-05-06 12:12:31 +0200 | [diff] [blame] | 1478 | ns_osmo_signal_dispatch(nsvc, S_NS_BLOCK, *cause); |
Harald Welte | c51c23c | 2010-05-13 12:55:20 +0200 | [diff] [blame] | 1479 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_BLOCKED]); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 1480 | |
Harald Welte | 9ef2e15 | 2019-02-19 23:42:04 +0100 | [diff] [blame] | 1481 | return gprs_ns_tx_block_ack(nsvc); |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 1482 | } |
| 1483 | |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1484 | int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg, |
| 1485 | struct gprs_nsvc *fallback_nsvc, |
| 1486 | struct gprs_nsvc **new_nsvc); |
| 1487 | |
| 1488 | int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg, |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1489 | struct gprs_nsvc **nsvc); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1490 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1491 | /*! Receive incoming NS message from underlying transport layer |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1492 | * \param nsi NS instance to which the data belongs |
| 1493 | * \param[in] msg message buffer containing newly-received data |
| 1494 | * \param[in] saddr socketaddr from which data was received |
| 1495 | * \param[in] ll link-layer type in which data was received |
| 1496 | * \returns 0 in case of success, < 0 in case of error |
| 1497 | * |
| 1498 | * This is the main entry point int othe NS imlementation where frames |
| 1499 | * from the underlying transport (normally UDP) enter. |
| 1500 | */ |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1501 | int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg, |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 1502 | struct sockaddr_in *saddr, enum gprs_ns_ll ll) |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1503 | { |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1504 | struct gprs_nsvc *nsvc; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1505 | int rc = 0; |
| 1506 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1507 | /* look up the NSVC based on source address */ |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 1508 | nsvc = gprs_nsvc_by_rem_addr(nsi, saddr); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1509 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1510 | if (!nsvc) { |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1511 | struct gprs_nsvc *fallback_nsvc; |
| 1512 | |
| 1513 | fallback_nsvc = nsi->unknown_nsvc; |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 1514 | log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1515 | fallback_nsvc->ip.bts_addr = *saddr; |
| 1516 | fallback_nsvc->ll = ll; |
| 1517 | |
| 1518 | rc = gprs_ns_vc_create(nsi, msg, fallback_nsvc, &nsvc); |
| 1519 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1520 | if (rc < 0) |
Harald Welte | 3625038 | 2010-05-28 10:08:14 +0200 | [diff] [blame] | 1521 | return rc; |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1522 | |
| 1523 | rc = 0; |
| 1524 | } |
| 1525 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1526 | if (nsvc) |
| 1527 | rc = gprs_ns_process_msg(nsi, msg, &nsvc); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1528 | |
| 1529 | return rc; |
| 1530 | } |
| 1531 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 1532 | char *gprs_ns_ll_str_buf(char *buf, size_t buf_len, const struct gprs_nsvc *nsvc) |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1533 | { |
Max | 9530859 | 2017-10-24 15:54:28 +0200 | [diff] [blame] | 1534 | switch(nsvc->ll) { |
| 1535 | case GPRS_NS_LL_UDP: |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 1536 | snprintf(buf, buf_len, "%s:%u", |
Max | 9530859 | 2017-10-24 15:54:28 +0200 | [diff] [blame] | 1537 | inet_ntoa(nsvc->ip.bts_addr.sin_addr), osmo_ntohs(nsvc->ip.bts_addr.sin_port)); |
| 1538 | break; |
| 1539 | case GPRS_NS_LL_FR_GRE: |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 1540 | snprintf(buf, buf_len, "%s:%u", |
Max | 9530859 | 2017-10-24 15:54:28 +0200 | [diff] [blame] | 1541 | inet_ntoa(nsvc->frgre.bts_addr.sin_addr), osmo_ntohs(nsvc->frgre.bts_addr.sin_port)); |
| 1542 | break; |
| 1543 | default: |
| 1544 | buf[0] = '\0'; |
| 1545 | break; |
| 1546 | } |
| 1547 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 1548 | buf[buf_len - 1] = '\0'; |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1549 | |
| 1550 | return buf; |
| 1551 | } |
| 1552 | |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 1553 | const char *gprs_ns_ll_str(const struct gprs_nsvc *nsvc) |
| 1554 | { |
Harald Welte | 171ef82 | 2019-03-28 10:49:05 +0100 | [diff] [blame] | 1555 | static __thread char buf[80]; |
Harald Welte | 4a62eda | 2019-03-18 18:27:00 +0100 | [diff] [blame] | 1556 | return gprs_ns_ll_str_buf(buf, sizeof(buf), nsvc); |
| 1557 | } |
| 1558 | |
Harald Welte | 179f357 | 2019-03-18 18:38:47 +0100 | [diff] [blame] | 1559 | char *gprs_ns_ll_str_c(const void *ctx, const struct gprs_nsvc *nsvc) |
| 1560 | { |
| 1561 | char *buf = talloc_size(ctx, INET6_ADDRSTRLEN+10); |
| 1562 | if (!buf) |
| 1563 | return buf; |
| 1564 | return gprs_ns_ll_str_buf(buf, INET6_ADDRSTRLEN+10, nsvc); |
| 1565 | } |
| 1566 | |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 1567 | void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other) |
| 1568 | { |
| 1569 | nsvc->ll = other->ll; |
| 1570 | |
| 1571 | switch (nsvc->ll) { |
| 1572 | case GPRS_NS_LL_UDP: |
| 1573 | nsvc->ip = other->ip; |
| 1574 | break; |
| 1575 | case GPRS_NS_LL_FR_GRE: |
| 1576 | nsvc->frgre = other->frgre; |
| 1577 | break; |
| 1578 | default: |
| 1579 | break; |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | void gprs_ns_ll_clear(struct gprs_nsvc *nsvc) |
| 1584 | { |
| 1585 | switch (nsvc->ll) { |
| 1586 | case GPRS_NS_LL_UDP: |
| 1587 | nsvc->ip.bts_addr.sin_addr.s_addr = INADDR_ANY; |
| 1588 | nsvc->ip.bts_addr.sin_port = 0; |
| 1589 | break; |
| 1590 | case GPRS_NS_LL_FR_GRE: |
| 1591 | nsvc->frgre.bts_addr.sin_addr.s_addr = INADDR_ANY; |
| 1592 | nsvc->frgre.bts_addr.sin_port = 0; |
| 1593 | break; |
| 1594 | default: |
| 1595 | break; |
| 1596 | } |
| 1597 | } |
| 1598 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1599 | /*! Create/get NS-VC independently from underlying transport layer |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1600 | * \param nsi NS instance to which the data belongs |
| 1601 | * \param[in] msg message buffer containing newly-received data |
| 1602 | * \param[in] fallback_nsvc is used to send error messages back to the peer |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1603 | * and to initialise the ll info of a created NS-VC object |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1604 | * \param[out] new_nsvc contains a pointer to a NS-VC object if one has |
| 1605 | * been created or found |
| 1606 | * \returns < 0 in case of error, GPRS_NS_CS_SKIPPED if a message has been |
| 1607 | * skipped, GPRS_NS_CS_REJECTED if a message has been rejected and |
| 1608 | * answered accordingly, GPRS_NS_CS_CREATED if a new NS-VC object |
| 1609 | * has been created and registered, and GPRS_NS_CS_FOUND if an |
| 1610 | * existing NS-VC object has been found with the same NSEI. |
| 1611 | * |
| 1612 | * This contains the initial NS automaton state (NS-VC not yet attached). |
| 1613 | */ |
| 1614 | int gprs_ns_vc_create(struct gprs_ns_inst *nsi, struct msgb *msg, |
| 1615 | struct gprs_nsvc *fallback_nsvc, |
| 1616 | struct gprs_nsvc **new_nsvc) |
| 1617 | { |
| 1618 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
| 1619 | struct gprs_nsvc *existing_nsvc; |
| 1620 | |
| 1621 | struct tlv_parsed tp; |
Jacob Erlbeck | b6390f9 | 2013-10-08 12:04:46 +0200 | [diff] [blame] | 1622 | uint16_t nsvci; |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1623 | uint16_t nsei; |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1624 | |
| 1625 | int rc; |
| 1626 | |
| 1627 | if (nsh->pdu_type == NS_PDUT_STATUS) { |
Jacob Erlbeck | 3d557b1 | 2013-10-28 13:29:11 +0100 | [diff] [blame] | 1628 | /* Do not respond, see 3GPP TS 08.16, 7.5.1 */ |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1629 | LOGP(DNS, LOGL_INFO, "Ignoring NS STATUS from %s " |
| 1630 | "for non-existing NS-VC\n", |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 1631 | gprs_ns_ll_str(fallback_nsvc)); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1632 | return GPRS_NS_CS_SKIPPED; |
| 1633 | } |
| 1634 | |
Jacob Erlbeck | 3d557b1 | 2013-10-28 13:29:11 +0100 | [diff] [blame] | 1635 | if (nsh->pdu_type == NS_PDUT_ALIVE_ACK) { |
| 1636 | /* Ignore this, see 3GPP TS 08.16, 7.4.1 */ |
| 1637 | LOGP(DNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s " |
| 1638 | "for non-existing NS-VC\n", |
| 1639 | gprs_ns_ll_str(fallback_nsvc)); |
| 1640 | return GPRS_NS_CS_SKIPPED; |
| 1641 | } |
| 1642 | |
| 1643 | if (nsh->pdu_type == NS_PDUT_RESET_ACK) { |
| 1644 | /* Ignore this, see 3GPP TS 08.16, 7.3.1 */ |
| 1645 | LOGP(DNS, LOGL_INFO, "Ignoring NS RESET ACK from %s " |
| 1646 | "for non-existing NS-VC\n", |
| 1647 | gprs_ns_ll_str(fallback_nsvc)); |
| 1648 | return GPRS_NS_CS_SKIPPED; |
| 1649 | } |
| 1650 | |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1651 | /* Only the RESET procedure creates a new NSVC */ |
| 1652 | if (nsh->pdu_type != NS_PDUT_RESET) { |
| 1653 | /* Since we have no NSVC, we have to use a fake */ |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 1654 | log_set_context(LOG_CTX_GB_NSVC, fallback_nsvc); |
Max | 584f4b6 | 2017-05-04 11:59:52 +0200 | [diff] [blame] | 1655 | LOGP(DNS, LOGL_INFO, "Rejecting NS PDU type %s " |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1656 | "from %s for non-existing NS-VC\n", |
Max | 584f4b6 | 2017-05-04 11:59:52 +0200 | [diff] [blame] | 1657 | get_value_string(gprs_ns_pdu_strings, nsh->pdu_type), gprs_ns_ll_str(fallback_nsvc)); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1658 | fallback_nsvc->nsvci = fallback_nsvc->nsei = 0xfffe; |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1659 | fallback_nsvc->nsvci_is_valid = 0; |
Max | c513c0f | 2017-10-23 15:11:14 +0200 | [diff] [blame] | 1660 | ns_set_state(fallback_nsvc, NSE_S_ALIVE); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1661 | |
| 1662 | rc = gprs_ns_tx_status(fallback_nsvc, |
| 1663 | NS_CAUSE_PDU_INCOMP_PSTATE, 0, msg); |
| 1664 | if (rc < 0) { |
| 1665 | LOGP(DNS, LOGL_ERROR, "TX failed (%d) to peer %s\n", |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 1666 | rc, gprs_ns_ll_str(fallback_nsvc)); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1667 | return rc; |
| 1668 | } |
| 1669 | return GPRS_NS_CS_REJECTED; |
| 1670 | } |
| 1671 | |
| 1672 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 1673 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
| 1674 | if (rc < 0) { |
| 1675 | LOGP(DNS, LOGL_ERROR, "Rx NS RESET Error %d during " |
| 1676 | "TLV Parse\n", rc); |
| 1677 | return rc; |
| 1678 | } |
| 1679 | if (!TLVP_PRESENT(&tp, NS_IE_CAUSE) || |
| 1680 | !TLVP_PRESENT(&tp, NS_IE_VCI) || !TLVP_PRESENT(&tp, NS_IE_NSEI)) { |
| 1681 | LOGP(DNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
Holger Hans Peter Freyther | 9e1cd5a | 2013-10-25 11:09:26 +0200 | [diff] [blame] | 1682 | rc = gprs_ns_tx_status(fallback_nsvc, NS_CAUSE_MISSING_ESSENT_IE, 0, |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1683 | msg); |
Holger Hans Peter Freyther | 9e1cd5a | 2013-10-25 11:09:26 +0200 | [diff] [blame] | 1684 | CHECK_TX_RC(rc, fallback_nsvc); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1685 | return -EINVAL; |
| 1686 | } |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 1687 | nsvci = tlvp_val16be(&tp, NS_IE_VCI); |
| 1688 | nsei = tlvp_val16be(&tp, NS_IE_NSEI); |
Jacob Erlbeck | b6390f9 | 2013-10-08 12:04:46 +0200 | [diff] [blame] | 1689 | /* Check if we already know this NSVCI, the remote end might |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1690 | * simply have changed addresses, or it is a SGSN */ |
Jacob Erlbeck | b6390f9 | 2013-10-08 12:04:46 +0200 | [diff] [blame] | 1691 | existing_nsvc = gprs_nsvc_by_nsvci(nsi, nsvci); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1692 | if (!existing_nsvc) { |
| 1693 | *new_nsvc = gprs_nsvc_create(nsi, 0xffff); |
Jacob Erlbeck | 9b591b7 | 2013-11-11 09:43:05 +0100 | [diff] [blame] | 1694 | (*new_nsvc)->nsvci_is_valid = 0; |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 1695 | log_set_context(LOG_CTX_GB_NSVC, *new_nsvc); |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1696 | gprs_ns_ll_copy(*new_nsvc, fallback_nsvc); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1697 | LOGP(DNS, LOGL_INFO, "Creating NS-VC for BSS at %s\n", |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 1698 | gprs_ns_ll_str(fallback_nsvc)); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1699 | |
| 1700 | return GPRS_NS_CS_CREATED; |
| 1701 | } |
| 1702 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1703 | /* Check NSEI */ |
| 1704 | if (existing_nsvc->nsei != nsei) { |
| 1705 | LOGP(DNS, LOGL_NOTICE, |
| 1706 | "NS-VC changed NSEI (NSVCI=%u) from %u to %u\n", |
| 1707 | nsvci, existing_nsvc->nsei, nsei); |
| 1708 | |
| 1709 | /* Override old NSEI */ |
| 1710 | existing_nsvc->nsei = nsei; |
| 1711 | |
| 1712 | /* Do statistics */ |
| 1713 | rate_ctr_inc(&existing_nsvc->ctrg->ctr[NS_CTR_NSEI_CHG]); |
| 1714 | } |
| 1715 | |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1716 | *new_nsvc = existing_nsvc; |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1717 | gprs_ns_ll_copy(*new_nsvc, fallback_nsvc); |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1718 | return GPRS_NS_CS_FOUND; |
| 1719 | } |
| 1720 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1721 | /*! Process NS message independently from underlying transport layer |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1722 | * \param nsi NS instance to which the data belongs |
| 1723 | * \param[in] msg message buffer containing newly-received data |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1724 | * \param[inout] nsvc refers to the virtual connection, may be modified when |
| 1725 | * processing a NS_RESET |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1726 | * \returns 0 in case of success, < 0 in case of error |
| 1727 | * |
| 1728 | * This contains the main NS automaton. |
| 1729 | */ |
| 1730 | int gprs_ns_process_msg(struct gprs_ns_inst *nsi, struct msgb *msg, |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1731 | struct gprs_nsvc **nsvc) |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1732 | { |
| 1733 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 1734 | struct tlv_parsed tp; |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 1735 | int rc = 0; |
| 1736 | |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1737 | msgb_nsei(msg) = (*nsvc)->nsei; |
Harald Welte | c547848 | 2010-03-18 00:01:43 +0800 | [diff] [blame] | 1738 | |
Neels Hofmeyr | 8b86cd7 | 2017-02-23 18:03:28 +0100 | [diff] [blame] | 1739 | log_set_context(LOG_CTX_GB_NSVC, *nsvc); |
Harald Welte | 91f7f4b | 2010-05-15 23:52:02 +0200 | [diff] [blame] | 1740 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 1741 | /* Increment number of Incoming bytes */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1742 | rate_ctr_inc(&(*nsvc)->ctrg->ctr[NS_CTR_PKTS_IN]); |
| 1743 | rate_ctr_add(&(*nsvc)->ctrg->ctr[NS_CTR_BYTES_IN], msgb_l2len(msg)); |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 1744 | |
Harald Welte | 94c9b44 | 2019-03-16 14:38:19 +0100 | [diff] [blame] | 1745 | if (nsvc_is_not_used(*nsvc) && !ns_is_sns(nsh->pdu_type) && nsh->pdu_type != NS_PDUT_STATUS) { |
| 1746 | LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx %s on unused/pre-configured endpoint, discarding\n", |
| 1747 | (*nsvc)->nsei, get_value_string(gprs_ns_pdu_strings, nsh->pdu_type)); |
| 1748 | gprs_ns_tx_status(*nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg); |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1752 | switch (nsh->pdu_type) { |
| 1753 | case NS_PDUT_ALIVE: |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 1754 | /* If we're dead and blocked and suddenly receive a |
| 1755 | * NS-ALIVE out of the blue, we might have been re-started |
| 1756 | * and should send a NS-RESET to make sure everything recovers |
| 1757 | * fine. */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1758 | if ((*nsvc)->state == NSE_S_BLOCKED) |
Jacob Erlbeck | 6ac70a4 | 2014-10-07 14:12:30 +0200 | [diff] [blame] | 1759 | rc = gprs_nsvc_reset((*nsvc), NS_CAUSE_PDU_INCOMP_PSTATE); |
| 1760 | else if (!((*nsvc)->state & NSE_S_RESET)) |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1761 | rc = gprs_ns_tx_alive_ack(*nsvc); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1762 | break; |
| 1763 | case NS_PDUT_ALIVE_ACK: |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 1764 | ns_mark_alive(*nsvc); |
Jacob Erlbeck | 0a1400f | 2015-10-06 15:23:25 +0200 | [diff] [blame] | 1765 | if ((*nsvc)->timer_mode == NSVC_TIMER_TNS_ALIVE) |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 1766 | osmo_stat_item_set((*nsvc)->statg->items[NS_STAT_ALIVE_DELAY], |
Jacob Erlbeck | 0a1400f | 2015-10-06 15:23:25 +0200 | [diff] [blame] | 1767 | nsvc_timer_elapsed_ms(*nsvc)); |
Harald Welte | 90af194 | 2010-05-15 23:02:24 +0200 | [diff] [blame] | 1768 | /* stop Tns-alive and start Tns-test */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1769 | nsvc_start_timer(*nsvc, NSVC_TIMER_TNS_TEST); |
| 1770 | if ((*nsvc)->remote_end_is_sgsn) { |
Harald Welte | 7fb7e61 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 1771 | /* FIXME: this should be one level higher */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1772 | if ((*nsvc)->state & NSE_S_BLOCKED) |
| 1773 | rc = gprs_ns_tx_unblock(*nsvc); |
Harald Welte | 7fb7e61 | 2010-05-03 21:11:22 +0200 | [diff] [blame] | 1774 | } |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1775 | break; |
| 1776 | case NS_PDUT_UNITDATA: |
| 1777 | /* actual user data */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1778 | rc = gprs_ns_rx_unitdata(*nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1779 | break; |
| 1780 | case NS_PDUT_STATUS: |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1781 | rc = gprs_ns_rx_status(*nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1782 | break; |
| 1783 | case NS_PDUT_RESET: |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 1784 | rc = gprs_ns_rx_reset(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1785 | break; |
| 1786 | case NS_PDUT_RESET_ACK: |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 1787 | rc = gprs_ns_rx_reset_ack(nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1788 | break; |
| 1789 | case NS_PDUT_UNBLOCK: |
| 1790 | /* Section 7.2: unblocking procedure */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1791 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK\n", (*nsvc)->nsei); |
Max | c513c0f | 2017-10-23 15:11:14 +0200 | [diff] [blame] | 1792 | ns_mark_unblocked(*nsvc); |
Stefan Sperling | c6bfc63 | 2018-11-20 11:01:36 +0100 | [diff] [blame] | 1793 | /* This UNBLOCK_ACK message will cause our peer to move us into NS_UNBLOCKED state. */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1794 | rc = gprs_ns_tx_simple(*nsvc, NS_PDUT_UNBLOCK_ACK); |
Stefan Sperling | 797558e | 2018-11-19 17:18:41 +0100 | [diff] [blame] | 1795 | if (rc < 0) |
| 1796 | break; |
Stefan Sperling | c6bfc63 | 2018-11-20 11:01:36 +0100 | [diff] [blame] | 1797 | /* |
| 1798 | * UNBLOCK_ACK has been transmitted. |
| 1799 | * Signal handlers may send additional messages following UNBLOCK_ACK under |
| 1800 | * the assumption that NS is now in UNBLOCKED state at our peer's end. |
| 1801 | */ |
Stefan Sperling | 797558e | 2018-11-19 17:18:41 +0100 | [diff] [blame] | 1802 | ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1803 | break; |
| 1804 | case NS_PDUT_UNBLOCK_ACK: |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1805 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS UNBLOCK ACK\n", (*nsvc)->nsei); |
Harald Welte | c1402a6 | 2010-05-12 11:48:44 +0200 | [diff] [blame] | 1806 | /* mark NS-VC as unblocked + active */ |
Max | c513c0f | 2017-10-23 15:11:14 +0200 | [diff] [blame] | 1807 | ns_set_state(*nsvc, NSE_S_ALIVE); |
| 1808 | ns_set_remote_state(*nsvc, NSE_S_ALIVE); |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1809 | ns_osmo_signal_dispatch(*nsvc, S_NS_UNBLOCK, 0); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1810 | break; |
| 1811 | case NS_PDUT_BLOCK: |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1812 | rc = gprs_ns_rx_block(*nsvc, msg); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1813 | break; |
| 1814 | case NS_PDUT_BLOCK_ACK: |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1815 | LOGP(DNS, LOGL_INFO, "NSEI=%u Rx NS BLOCK ACK\n", (*nsvc)->nsei); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1816 | /* mark remote NS-VC as blocked + active */ |
Max | c513c0f | 2017-10-23 15:11:14 +0200 | [diff] [blame] | 1817 | ns_set_remote_state(*nsvc, NSE_S_BLOCKED | NSE_S_ALIVE); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1818 | break; |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 1819 | case SNS_PDUT_CONFIG: |
| 1820 | if (!nsi->bss_sns_fi) |
| 1821 | goto unexpected_sns; |
| 1822 | /* one additional byte ('end flag') before the TLV part starts */ |
| 1823 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data+1, |
| 1824 | msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0); |
| 1825 | if (rc < 0) { |
| 1826 | LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg)); |
| 1827 | return rc; |
| 1828 | } |
| 1829 | /* All sub-network service related message types */ |
| 1830 | rc = gprs_ns_rx_sns(nsi, msg, &tp); |
| 1831 | break; |
| 1832 | case SNS_PDUT_ACK: |
| 1833 | case SNS_PDUT_ADD: |
| 1834 | case SNS_PDUT_CHANGE_WEIGHT: |
| 1835 | case SNS_PDUT_DELETE: |
| 1836 | if (!nsi->bss_sns_fi) |
| 1837 | goto unexpected_sns; |
| 1838 | /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */ |
| 1839 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data+5, |
| 1840 | msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0); |
| 1841 | if (rc < 0) { |
| 1842 | LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg)); |
| 1843 | return rc; |
| 1844 | } |
| 1845 | tp.lv[NS_IE_NSEI].val = nsh->data+2; |
| 1846 | tp.lv[NS_IE_NSEI].len = 2; |
| 1847 | tp.lv[NS_IE_TRANS_ID].val = nsh->data+4; |
| 1848 | tp.lv[NS_IE_TRANS_ID].len = 1; |
| 1849 | rc = gprs_ns_rx_sns(nsi, msg, &tp); |
| 1850 | break; |
| 1851 | case SNS_PDUT_CONFIG_ACK: |
| 1852 | case SNS_PDUT_SIZE: |
| 1853 | case SNS_PDUT_SIZE_ACK: |
| 1854 | if (!nsi->bss_sns_fi) |
| 1855 | goto unexpected_sns; |
| 1856 | rc = tlv_parse(&tp, &ns_att_tlvdef, nsh->data, |
| 1857 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
| 1858 | if (rc < 0) { |
| 1859 | LOGPC(DNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg)); |
| 1860 | return rc; |
| 1861 | } |
| 1862 | /* All sub-network service related message types */ |
| 1863 | rc = gprs_ns_rx_sns(nsi, msg, &tp); |
| 1864 | break; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1865 | default: |
Harald Welte | e4ecc8c | 2010-05-12 00:16:57 +0200 | [diff] [blame] | 1866 | LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx Unknown NS PDU type 0x%02x\n", |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 1867 | (*nsvc)->nsei, nsh->pdu_type); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1868 | rc = -EINVAL; |
| 1869 | break; |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 1870 | unexpected_sns: |
| 1871 | LOGP(DNS, LOGL_NOTICE, "NSEI=%u Rx %s for NS Instance that has no SNS!\n", |
| 1872 | (*nsvc)->nsei, get_value_string(gprs_ns_pdu_strings, nsh->pdu_type)); |
| 1873 | rc = -EINVAL; |
| 1874 | break; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1875 | } |
| 1876 | return rc; |
| 1877 | } |
| 1878 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 1879 | static bool gprs_sns_fsm_registered = false; |
| 1880 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1881 | /*! Create a new GPRS NS instance |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 1882 | * \param[in] cb Call-back function for incoming BSSGP data |
| 1883 | * \returns dynamically allocated gprs_ns_inst |
| 1884 | */ |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 1885 | struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1886 | { |
Harald Welte | 797d533 | 2019-12-01 13:38:35 +0100 | [diff] [blame] | 1887 | struct gprs_ns_inst *nsi; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1888 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 1889 | if (!gprs_sns_fsm_registered) { |
Harald Welte | 797d533 | 2019-12-01 13:38:35 +0100 | [diff] [blame] | 1890 | int rc = gprs_sns_init(); |
| 1891 | if (rc < 0) |
| 1892 | return NULL; |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 1893 | gprs_sns_fsm_registered = true; |
| 1894 | } |
| 1895 | |
Harald Welte | 797d533 | 2019-12-01 13:38:35 +0100 | [diff] [blame] | 1896 | nsi = talloc_zero(ctx, struct gprs_ns_inst); |
| 1897 | if (!nsi) |
| 1898 | return NULL; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1899 | nsi->cb = cb; |
| 1900 | INIT_LLIST_HEAD(&nsi->gprs_nsvcs); |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 1901 | nsi->timeout[NS_TOUT_TNS_BLOCK] = 3; |
| 1902 | nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3; |
| 1903 | nsi->timeout[NS_TOUT_TNS_RESET] = 3; |
| 1904 | nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3; |
| 1905 | nsi->timeout[NS_TOUT_TNS_TEST] = 30; |
| 1906 | nsi->timeout[NS_TOUT_TNS_ALIVE] = 3; |
| 1907 | nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10; |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 1908 | nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */ |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1909 | |
Harald Welte | 60cc6ac | 2010-05-13 14:20:56 +0200 | [diff] [blame] | 1910 | /* Create the dummy NSVC that we use for sending |
| 1911 | * messages to non-existant/unknown NS-VC's */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 1912 | nsi->unknown_nsvc = gprs_nsvc_create(nsi, 0xfffe); |
Jacob Erlbeck | 9b591b7 | 2013-11-11 09:43:05 +0100 | [diff] [blame] | 1913 | nsi->unknown_nsvc->nsvci_is_valid = 0; |
Harald Welte | 60cc6ac | 2010-05-13 14:20:56 +0200 | [diff] [blame] | 1914 | llist_del(&nsi->unknown_nsvc->list); |
Holger Hans Peter Freyther | 777b056 | 2014-07-07 20:00:35 +0200 | [diff] [blame] | 1915 | INIT_LLIST_HEAD(&nsi->unknown_nsvc->list); |
Harald Welte | dd1c83c | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 1916 | |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 1917 | return nsi; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1918 | } |
| 1919 | |
Holger Hans Peter Freyther | c62a1bf | 2013-07-02 09:10:11 +0200 | [diff] [blame] | 1920 | void gprs_ns_close(struct gprs_ns_inst *nsi) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1921 | { |
Andreas Eversberg | aaccdac | 2012-09-28 10:11:25 +0200 | [diff] [blame] | 1922 | struct gprs_nsvc *nsvc, *nsvc2; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1923 | |
Holger Hans Peter Freyther | 777b056 | 2014-07-07 20:00:35 +0200 | [diff] [blame] | 1924 | gprs_nsvc_delete(nsi->unknown_nsvc); |
| 1925 | |
Andreas Eversberg | aaccdac | 2012-09-28 10:11:25 +0200 | [diff] [blame] | 1926 | /* delete all NSVCs and clear their timers */ |
| 1927 | llist_for_each_entry_safe(nsvc, nsvc2, &nsi->gprs_nsvcs, list) |
| 1928 | gprs_nsvc_delete(nsvc); |
| 1929 | |
| 1930 | /* close socket and unregister */ |
| 1931 | if (nsi->nsip.fd.data) { |
| 1932 | close(nsi->nsip.fd.fd); |
| 1933 | osmo_fd_unregister(&nsi->nsip.fd); |
Holger Hans Peter Freyther | c62a1bf | 2013-07-02 09:10:11 +0200 | [diff] [blame] | 1934 | nsi->nsip.fd.data = NULL; |
Andreas Eversberg | aaccdac | 2012-09-28 10:11:25 +0200 | [diff] [blame] | 1935 | } |
Holger Hans Peter Freyther | c62a1bf | 2013-07-02 09:10:11 +0200 | [diff] [blame] | 1936 | } |
Andreas Eversberg | aaccdac | 2012-09-28 10:11:25 +0200 | [diff] [blame] | 1937 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 1938 | /*! Destroy an entire NS instance |
Holger Hans Peter Freyther | c62a1bf | 2013-07-02 09:10:11 +0200 | [diff] [blame] | 1939 | * \param nsi gprs_ns_inst that is to be destroyed |
| 1940 | * |
| 1941 | * This function releases all resources associated with the |
| 1942 | * NS-instance. |
| 1943 | */ |
| 1944 | void gprs_ns_destroy(struct gprs_ns_inst *nsi) |
| 1945 | { |
| 1946 | gprs_ns_close(nsi); |
Andreas Eversberg | aaccdac | 2012-09-28 10:11:25 +0200 | [diff] [blame] | 1947 | /* free the NSI */ |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1948 | talloc_free(nsi); |
| 1949 | } |
| 1950 | |
| 1951 | |
| 1952 | /* NS-over-IP code, according to 3GPP TS 48.016 Chapter 6.2 |
| 1953 | * We don't support Size Procedure, Configuration Procedure, ChangeWeight Procedure */ |
| 1954 | |
| 1955 | /* Read a single NS-over-IP message */ |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1956 | static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error, |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1957 | struct sockaddr_in *saddr) |
| 1958 | { |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 1959 | struct msgb *msg = gprs_ns_msgb_alloc(); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1960 | int ret = 0; |
| 1961 | socklen_t saddr_len = sizeof(*saddr); |
| 1962 | |
| 1963 | if (!msg) { |
| 1964 | *error = -ENOMEM; |
| 1965 | return NULL; |
| 1966 | } |
| 1967 | |
Holger Hans Peter Freyther | 26c3251 | 2010-05-28 03:25:36 +0800 | [diff] [blame] | 1968 | ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0, |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1969 | (struct sockaddr *)saddr, &saddr_len); |
| 1970 | if (ret < 0) { |
Max | 38176d2 | 2018-11-06 17:04:21 +0100 | [diff] [blame] | 1971 | LOGP(DNS, LOGL_ERROR, "recv error %s during NSIP recvfrom %s\n", |
| 1972 | strerror(errno), osmo_sock_get_name2(bfd->fd)); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1973 | msgb_free(msg); |
| 1974 | *error = ret; |
| 1975 | return NULL; |
| 1976 | } else if (ret == 0) { |
| 1977 | msgb_free(msg); |
| 1978 | *error = ret; |
| 1979 | return NULL; |
| 1980 | } |
| 1981 | |
| 1982 | msg->l2h = msg->data; |
| 1983 | msgb_put(msg, ret); |
| 1984 | |
| 1985 | return msg; |
| 1986 | } |
| 1987 | |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 1988 | static int handle_nsip_read(struct osmo_fd *bfd) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 1989 | { |
| 1990 | int error; |
| 1991 | struct sockaddr_in saddr; |
| 1992 | struct gprs_ns_inst *nsi = bfd->data; |
| 1993 | struct msgb *msg = read_nsip_msg(bfd, &error, &saddr); |
| 1994 | |
| 1995 | if (!msg) |
| 1996 | return error; |
| 1997 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 1998 | error = gprs_ns_rcvmsg(nsi, msg, &saddr, GPRS_NS_LL_UDP); |
Harald Welte | 91813cf | 2010-05-12 18:38:45 +0000 | [diff] [blame] | 1999 | |
| 2000 | msgb_free(msg); |
| 2001 | |
| 2002 | return error; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 2003 | } |
| 2004 | |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 2005 | static int handle_nsip_write(struct osmo_fd *bfd) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 2006 | { |
| 2007 | /* FIXME: actually send the data here instead of nsip_sendmsg() */ |
| 2008 | return -EIO; |
| 2009 | } |
| 2010 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 2011 | static int nsip_sendmsg(struct gprs_nsvc *nsvc, struct msgb *msg) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 2012 | { |
| 2013 | int rc; |
| 2014 | struct gprs_ns_inst *nsi = nsvc->nsi; |
| 2015 | struct sockaddr_in *daddr = &nsvc->ip.bts_addr; |
| 2016 | |
| 2017 | rc = sendto(nsi->nsip.fd.fd, msg->data, msg->len, 0, |
| 2018 | (struct sockaddr *)daddr, sizeof(*daddr)); |
| 2019 | |
Holger Hans Peter Freyther | 5274646 | 2012-03-01 20:30:32 +0100 | [diff] [blame] | 2020 | msgb_free(msg); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 2021 | |
| 2022 | return rc; |
| 2023 | } |
| 2024 | |
| 2025 | /* UDP Port 23000 carries the LLC-in-BSSGP-in-NS protocol stack */ |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 2026 | static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 2027 | { |
| 2028 | int rc = 0; |
| 2029 | |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 2030 | if (what & OSMO_FD_READ) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 2031 | rc = handle_nsip_read(bfd); |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 2032 | if (what & OSMO_FD_WRITE) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 2033 | rc = handle_nsip_write(bfd); |
| 2034 | |
| 2035 | return rc; |
| 2036 | } |
| 2037 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2038 | /*! Create a listening socket for GPRS NS/UDP/IP |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 2039 | * \param[in] nsi NS protocol instance to listen |
| 2040 | * \returns >=0 (fd) in case of success, negative in case of error |
| 2041 | * |
| 2042 | * A call to this function will create a UDP socket bound to the port |
| 2043 | * number and IP address specified in the NS protocol instance. The |
| 2044 | * file descriptor of the socket will be stored in nsi->nsip.fd. |
| 2045 | */ |
Harald Welte | 7fb0523 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 2046 | int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi) |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 2047 | { |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 2048 | struct in_addr in; |
Philipp Maier | 224a6dd | 2017-10-17 15:23:53 +0200 | [diff] [blame] | 2049 | struct in_addr remote; |
| 2050 | char remote_str[INET_ADDRSTRLEN]; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 2051 | int ret; |
| 2052 | |
Harald Welte | bfe62e5 | 2017-05-15 12:48:30 +0200 | [diff] [blame] | 2053 | in.s_addr = osmo_htonl(nsi->nsip.local_ip); |
Philipp Maier | 224a6dd | 2017-10-17 15:23:53 +0200 | [diff] [blame] | 2054 | remote.s_addr = osmo_htonl(nsi->nsip.remote_ip); |
Harald Welte | 73952e3 | 2012-06-16 14:59:56 +0800 | [diff] [blame] | 2055 | |
| 2056 | nsi->nsip.fd.cb = nsip_fd_cb; |
| 2057 | nsi->nsip.fd.data = nsi; |
Philipp Maier | 224a6dd | 2017-10-17 15:23:53 +0200 | [diff] [blame] | 2058 | |
| 2059 | if (nsi->nsip.remote_ip && nsi->nsip.remote_port) { |
| 2060 | /* connect to ensure only we only accept packets from the |
| 2061 | * configured remote end/peer */ |
| 2062 | snprintf(remote_str, sizeof(remote_str), "%s", inet_ntoa(remote)); |
| 2063 | ret = |
| 2064 | osmo_sock_init2_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM, |
| 2065 | IPPROTO_UDP, inet_ntoa(in), |
| 2066 | nsi->nsip.local_port, remote_str, |
| 2067 | nsi->nsip.remote_port, OSMO_SOCK_F_BIND | OSMO_SOCK_F_CONNECT); |
| 2068 | |
| 2069 | LOGP(DNS, LOGL_NOTICE, |
| 2070 | "Listening for nsip packets from %s:%u on %s:%u\n", |
| 2071 | remote_str, nsi->nsip.remote_port, inet_ntoa(in), nsi->nsip.local_port); |
| 2072 | } else { |
| 2073 | /* Accept UDP packets from any source IP/Port */ |
| 2074 | ret = osmo_sock_init_ofd(&nsi->nsip.fd, AF_INET, SOCK_DGRAM, |
| 2075 | IPPROTO_UDP, inet_ntoa(in), nsi->nsip.local_port, OSMO_SOCK_F_BIND); |
| 2076 | |
| 2077 | LOGP(DNS, LOGL_NOTICE, "Listening for nsip packets on %s:%u\n", inet_ntoa(in), nsi->nsip.local_port); |
| 2078 | } |
| 2079 | |
Stefan Sperling | f653821 | 2018-10-10 16:09:32 +0200 | [diff] [blame] | 2080 | if (ret < 0) { |
| 2081 | nsi->nsip.fd.cb = NULL; |
| 2082 | nsi->nsip.fd.data = NULL; |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 2083 | return ret; |
Stefan Sperling | f653821 | 2018-10-10 16:09:32 +0200 | [diff] [blame] | 2084 | } |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 2085 | |
Holger Hans Peter Freyther | 2c3393d | 2013-03-25 11:59:58 +0100 | [diff] [blame] | 2086 | ret = setsockopt(nsi->nsip.fd.fd, IPPROTO_IP, IP_TOS, |
| 2087 | &nsi->nsip.dscp, sizeof(nsi->nsip.dscp)); |
| 2088 | if (ret < 0) |
| 2089 | LOGP(DNS, LOGL_ERROR, |
| 2090 | "Failed to set the DSCP to %d with ret(%d) errno(%d)\n", |
| 2091 | nsi->nsip.dscp, ret, errno); |
| 2092 | |
Max | a19c126 | 2017-10-20 15:39:45 +0200 | [diff] [blame] | 2093 | LOGP(DNS, LOGL_NOTICE, "NS UDP socket at %s:%d\n", inet_ntoa(in), nsi->nsip.local_port); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 2094 | |
| 2095 | return ret; |
| 2096 | } |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2097 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2098 | /*! Initiate a RESET procedure |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 2099 | * \param[in] nsvc NS-VC in which to start the procedure |
| 2100 | * \param[in] cause Numeric NS cause value |
| 2101 | * |
| 2102 | * This is a high-level function initiating a NS-RESET procedure. It |
| 2103 | * will not only send a NS-RESET, but also set the state to BLOCKED and |
| 2104 | * start the Tns-reset timer. |
| 2105 | */ |
Jacob Erlbeck | 0540d83 | 2014-10-08 11:47:36 +0200 | [diff] [blame] | 2106 | int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause) |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 2107 | { |
Jacob Erlbeck | 0540d83 | 2014-10-08 11:47:36 +0200 | [diff] [blame] | 2108 | int rc; |
| 2109 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 2110 | ERR_IF_NSVC_USES_SNS(nsvc, "RESET procedure based on API request"); |
| 2111 | |
Harald Welte | b1020d5 | 2010-05-25 22:17:30 +0200 | [diff] [blame] | 2112 | LOGP(DNS, LOGL_INFO, "NSEI=%u RESET procedure based on API request\n", |
| 2113 | nsvc->nsei); |
| 2114 | |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 2115 | /* Mark NS-VC locally as blocked and dead */ |
Max | c513c0f | 2017-10-23 15:11:14 +0200 | [diff] [blame] | 2116 | ns_set_state(nsvc, NSE_S_BLOCKED | NSE_S_RESET); |
Jacob Erlbeck | 6ac70a4 | 2014-10-07 14:12:30 +0200 | [diff] [blame] | 2117 | |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 2118 | /* Send NS-RESET PDU */ |
Jacob Erlbeck | 0540d83 | 2014-10-08 11:47:36 +0200 | [diff] [blame] | 2119 | rc = gprs_ns_tx_reset(nsvc, cause); |
| 2120 | if (rc < 0) { |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 2121 | LOGP(DNS, LOGL_ERROR, "NSEI=%u, error resetting NS-VC\n", |
| 2122 | nsvc->nsei); |
| 2123 | } |
| 2124 | /* Start Tns-reset */ |
| 2125 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_RESET); |
Jacob Erlbeck | 0540d83 | 2014-10-08 11:47:36 +0200 | [diff] [blame] | 2126 | |
| 2127 | return rc; |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 2128 | } |
| 2129 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 2130 | /*! Establish a NS connection (from the BSS) to the SGSN |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 2131 | * \param nsi NS-instance |
| 2132 | * \param[in] dest Destination IP/Port |
| 2133 | * \param[in] nsei NSEI of the to-be-established NS-VC |
| 2134 | * \param[in] nsvci NSVCI of the to-be-established NS-VC |
| 2135 | * \returns struct gprs_nsvc representing the new NS-VC |
| 2136 | * |
| 2137 | * This function will establish a single NS/UDP/IP connection in uplink |
| 2138 | * (BSS to SGSN) direction. |
| 2139 | */ |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 2140 | struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi, |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 2141 | struct sockaddr_in *dest, uint16_t nsei, |
| 2142 | uint16_t nsvci) |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2143 | { |
| 2144 | struct gprs_nsvc *nsvc; |
| 2145 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 2146 | nsvc = gprs_nsvc_by_rem_addr(nsi, dest); |
Harald Welte | 38407ef | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 2147 | if (!nsvc) |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 2148 | nsvc = gprs_nsvc_create(nsi, nsvci); |
Harald Welte | 38407ef | 2010-05-12 11:56:39 +0000 | [diff] [blame] | 2149 | nsvc->ip.bts_addr = *dest; |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 2150 | nsvc->nsei = nsei; |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2151 | nsvc->remote_end_is_sgsn = 1; |
| 2152 | |
Holger Hans Peter Freyther | 5617d99 | 2010-05-23 21:18:01 +0800 | [diff] [blame] | 2153 | gprs_nsvc_reset(nsvc, NS_CAUSE_OM_INTERVENTION); |
| 2154 | return nsvc; |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 2155 | } |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 2156 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 2157 | /*! Establish a NS connection (from the BSS) to the SGSN using SNS auto-configuration |
| 2158 | * \param nsi NS-instance |
| 2159 | * \param[in] dest Destination IP/Port |
| 2160 | * \param[in] nsei NSEI of the to-be-established NS-VC |
| 2161 | * \param[in] nsvci NSVCI of the to-be-established NS-VC |
| 2162 | * \returns struct gprs_nsvc representing the new NS-VC |
| 2163 | * |
| 2164 | * This function will establish a single NS/UDP/IP connection in uplink |
| 2165 | * (BSS to SGSN) direction. It will start with the SNS-SIZE procedure, |
| 2166 | * followed by BSS-originated SNS-CONFIG, then SGSN-originated SNS-CONFIG. |
| 2167 | * |
| 2168 | * Once configuration completes, the user will be notified by the S_SNS_CONFIGURED signal, |
| 2169 | * at which point he typically would want to initiate NS-RESET by means of gprs_nsvc_reset(). |
| 2170 | */ |
| 2171 | struct gprs_nsvc *gprs_ns_nsip_connect_sns(struct gprs_ns_inst *nsi, |
| 2172 | struct sockaddr_in *dest, uint16_t nsei, |
| 2173 | uint16_t nsvci) |
| 2174 | { |
| 2175 | struct gprs_nsvc *nsvc; |
| 2176 | |
| 2177 | /* FIXME: We are getting the order wrong here. Normally, one would want |
| 2178 | * to start the SNS FSM *before* creating any NS-VC and then create the NS-VC |
| 2179 | * after the SNS layer has established the IP/port/etc. However, this would |
| 2180 | * require some massive code and API changes compared to existing libosmogb, |
| 2181 | * so let's keep the old logic. */ |
| 2182 | nsvc = gprs_nsvc_by_rem_addr(nsi, dest); |
Harald Welte | 94c9b44 | 2019-03-16 14:38:19 +0100 | [diff] [blame] | 2183 | if (!nsvc) { |
| 2184 | /* create NSVC with 0 data + signalling weight. This is illegal in SNS |
| 2185 | * and can hence only be created locally and serves as indication that |
| 2186 | * this NS-VC shall not be used for anything except SNS _unless_ it is |
| 2187 | * modified via SNS-{CONFIG,CHANGEWEIGHT,ADD} to become part of the |
| 2188 | * active NS-VCs */ |
| 2189 | nsvc = gprs_nsvc_create2(nsi, nsvci, 0, 0); |
| 2190 | } |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 2191 | nsvc->ip.bts_addr = *dest; |
| 2192 | nsvc->nsei = nsei; |
| 2193 | nsvc->remote_end_is_sgsn = 1; |
| 2194 | /* NSVCs are always UNBLOCKED in IP-SNS */ |
| 2195 | ns_set_state(nsvc, 0); |
| 2196 | |
| 2197 | if (nsi->bss_sns_fi) |
| 2198 | osmo_fsm_inst_term(nsi->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL); |
| 2199 | nsi->bss_sns_fi = gprs_sns_bss_fsm_alloc(nsi, nsvc, "NSIP"); |
| 2200 | gprs_sns_bss_fsm_start(nsi); |
| 2201 | return nsvc; |
| 2202 | } |
| 2203 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 2204 | void gprs_ns_set_log_ss(int ss) |
| 2205 | { |
| 2206 | DNS = ss; |
| 2207 | } |
| 2208 | |
Daniel Willmann | f1318fe | 2018-05-29 20:55:18 +0200 | [diff] [blame] | 2209 | /*! Append the nsvc state to a talloc string |
| 2210 | * \param s The string to append to (allocated with talloc) |
| 2211 | * \param[in] nsvc The NS-VC to print the state of |
| 2212 | * \returns The new string with state information appended to it |
| 2213 | * |
| 2214 | * This function will append a comma-separated state of the NS-VC to the |
| 2215 | * string. The string needs to be allocated with talloc (e.g. talloc_strdup) |
| 2216 | */ |
| 2217 | char *gprs_nsvc_state_append(char *s, struct gprs_nsvc *nsvc) |
| 2218 | { |
| 2219 | s = talloc_asprintf_append(s, |
| 2220 | "%u,%u,%s,%s,%s,%s,%s\n", |
| 2221 | nsvc->nsei, nsvc->nsvci, |
| 2222 | NS_DESC_A(nsvc->state), |
| 2223 | NS_DESC_B(nsvc->state), |
| 2224 | nsvc->remote_end_is_sgsn ? "SGSN" : "BSS", |
| 2225 | NS_DESC_A(nsvc->remote_state), |
| 2226 | NS_DESC_B(nsvc->remote_state)); |
| 2227 | |
| 2228 | return s; |
| 2229 | } |
| 2230 | |
Harald Welte | 35042a2 | 2019-02-23 14:07:00 +0100 | [diff] [blame] | 2231 | /*! Start the ALIVE timer procedure in all NS-VCs part of this NS Instance */ |
| 2232 | void gprs_nsvc_start_test(struct gprs_nsvc *nsvc) |
| 2233 | { |
Harald Welte | 94c9b44 | 2019-03-16 14:38:19 +0100 | [diff] [blame] | 2234 | /* skip the initial NS-VC unless it has explicitly been configured |
| 2235 | * via SNS-CONFIG from the SGSN */ |
| 2236 | if (nsvc_is_not_used(nsvc)) |
| 2237 | return; |
Harald Welte | 35042a2 | 2019-02-23 14:07:00 +0100 | [diff] [blame] | 2238 | gprs_ns_tx_alive(nsvc); |
| 2239 | nsvc_start_timer(nsvc, NSVC_TIMER_TNS_TEST); |
| 2240 | } |
| 2241 | |
Harald Welte | 047f387 | 2018-07-01 21:04:45 +0200 | [diff] [blame] | 2242 | void gprs_start_alive_all_nsvcs(struct gprs_ns_inst *nsi) |
| 2243 | { |
| 2244 | struct gprs_nsvc *nsvc; |
| 2245 | llist_for_each_entry(nsvc, &nsi->gprs_nsvcs, list) { |
| 2246 | /* start the test procedure */ |
| 2247 | gprs_nsvc_start_test(nsvc); |
| 2248 | } |
| 2249 | } |
| 2250 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 2251 | /*! @} */ |