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