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