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