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