Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1 | /*! \file gprs_ns2.c |
| 2 | * GPRS Networks Service (NS) messages on the Gb interface. |
| 3 | * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) |
| 4 | * as well as its successor 3GPP TS 48.016 */ |
| 5 | |
| 6 | /* (C) 2009-2018 by Harald Welte <laforge@gnumonks.org> |
| 7 | * (C) 2016-2017,2020 sysmocom - s.f.m.c. GmbH |
| 8 | * Author: Alexander Couzens <lynxis@fe80.eu> |
| 9 | * |
| 10 | * |
| 11 | * All Rights Reserved |
| 12 | * |
| 13 | * SPDX-License-Identifier: GPL-2.0+ |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | /*! \addtogroup libgb |
| 31 | * @{ |
| 32 | * |
| 33 | * GPRS Networks Service (NS) messages on the Gb interface |
| 34 | * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) |
| 35 | * |
| 36 | * Some introduction into NS: NS is used typically on top of frame relay, |
| 37 | * but in the ip.access world it is encapsulated in UDP packets. It serves |
| 38 | * as an intermediate shim betwen BSSGP and the underlying medium. It doesn't |
| 39 | * do much, apart from providing congestion notification and status indication. |
| 40 | * |
| 41 | * Terms: |
| 42 | * |
| 43 | * NS Network Service |
| 44 | * NSVC NS Virtual Connection |
| 45 | * NSEI NS Entity Identifier |
| 46 | * NSVL NS Virtual Link |
| 47 | * NSVLI NS Virtual Link Identifier |
| 48 | * BVC BSSGP Virtual Connection |
| 49 | * BVCI BSSGP Virtual Connection Identifier |
| 50 | * NSVCG NS Virtual Connection Goup |
| 51 | * Blocked NS-VC cannot be used for user traffic |
| 52 | * Alive Ability of a NS-VC to provide communication |
| 53 | * |
| 54 | * There can be multiple BSSGP virtual connections over one (group of) NSVC's. BSSGP will |
| 55 | * therefore identify the BSSGP virtual connection by a BVCI passed down to NS. |
| 56 | * NS then has to figure out which NSVC's are responsible for this BVCI. |
| 57 | * Those mappings are administratively configured. |
| 58 | * |
| 59 | * This implementation has the following limitations: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 60 | * - NSVCI 65535 and 65534 are reserved for internal use |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 61 | * - There are no BLOCK and UNBLOCK timers (yet?) |
| 62 | * |
| 63 | * \file gprs_ns2.c */ |
| 64 | |
| 65 | #include <stdlib.h> |
| 66 | #include <unistd.h> |
| 67 | #include <errno.h> |
| 68 | #include <stdint.h> |
| 69 | |
| 70 | #include <sys/types.h> |
| 71 | #include <sys/socket.h> |
| 72 | #include <arpa/inet.h> |
| 73 | |
| 74 | #include <osmocom/core/fsm.h> |
Daniel Willmann | 751977b | 2020-12-02 18:59:44 +0100 | [diff] [blame] | 75 | #include <osmocom/core/logging.h> |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 76 | #include <osmocom/core/msgb.h> |
| 77 | #include <osmocom/core/rate_ctr.h> |
| 78 | #include <osmocom/core/socket.h> |
| 79 | #include <osmocom/core/sockaddr_str.h> |
| 80 | #include <osmocom/core/stats.h> |
| 81 | #include <osmocom/core/stat_item.h> |
| 82 | #include <osmocom/core/talloc.h> |
| 83 | #include <osmocom/gprs/gprs_msgb.h> |
| 84 | #include <osmocom/gsm/prim.h> |
| 85 | #include <osmocom/gsm/tlv.h> |
| 86 | |
| 87 | #include "gprs_ns2_internal.h" |
| 88 | |
| 89 | #define ns_set_state(ns_, st_) ns_set_state_with_log(ns_, st_, false, __FILE__, __LINE__) |
| 90 | #define ns_set_remote_state(ns_, st_) ns_set_state_with_log(ns_, st_, true, __FILE__, __LINE__) |
| 91 | #define ns_mark_blocked(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_BLOCKED) |
| 92 | #define ns_mark_unblocked(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_BLOCKED)); |
| 93 | #define ns_mark_alive(ns_) ns_set_state(ns_, (ns_)->state | NSE_S_ALIVE) |
| 94 | #define ns_mark_dead(ns_) ns_set_state(ns_, (ns_)->state & (~NSE_S_ALIVE)); |
| 95 | |
| 96 | /* HACK: The NS_IE_IP_ADDR does not follow any known TLV rules. |
| 97 | * Since it's a hard ABI break to implement 16 bit tag with fixed length entries to workaround it, |
| 98 | * the parser will be called with ns_att_tlvdef1 and if it's failed with ns_att_tlvdef2. |
| 99 | * The TLV parser depends on 8bit tag in many places. |
| 100 | * The NS_IE_IP_ADDR is only valid for SNS_ACK SNS_ADD and SNS_DELETE. |
| 101 | */ |
| 102 | static const struct tlv_definition ns_att_tlvdef1 = { |
| 103 | .def = { |
| 104 | [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 }, |
| 105 | [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 }, |
| 106 | [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 }, |
| 107 | [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 }, |
| 108 | [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 }, |
| 109 | [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 }, |
| 110 | [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 }, |
| 111 | [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 }, |
| 112 | [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 }, |
| 113 | [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 }, |
| 114 | [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 }, |
| 115 | /* NS_IE_IP_ADDR in the IPv4 version */ |
| 116 | [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 5 }, |
| 117 | }, |
| 118 | }; |
| 119 | |
| 120 | static const struct tlv_definition ns_att_tlvdef2 = { |
| 121 | .def = { |
| 122 | [NS_IE_CAUSE] = { TLV_TYPE_TvLV, 0 }, |
| 123 | [NS_IE_VCI] = { TLV_TYPE_TvLV, 0 }, |
| 124 | [NS_IE_PDU] = { TLV_TYPE_TvLV, 0 }, |
| 125 | [NS_IE_BVCI] = { TLV_TYPE_TvLV, 0 }, |
| 126 | [NS_IE_NSEI] = { TLV_TYPE_TvLV, 0 }, |
| 127 | [NS_IE_IPv4_LIST] = { TLV_TYPE_TvLV, 0 }, |
| 128 | [NS_IE_IPv6_LIST] = { TLV_TYPE_TvLV, 0 }, |
| 129 | [NS_IE_MAX_NR_NSVC] = { TLV_TYPE_FIXED, 2 }, |
| 130 | [NS_IE_IPv4_EP_NR] = { TLV_TYPE_FIXED, 2 }, |
| 131 | [NS_IE_IPv6_EP_NR] = { TLV_TYPE_FIXED, 2 }, |
| 132 | [NS_IE_RESET_FLAG] = { TLV_TYPE_TV, 0 }, |
| 133 | /* NS_IE_IP_ADDR in the IPv6 version */ |
| 134 | [NS_IE_IP_ADDR] = { TLV_TYPE_FIXED, 17 }, |
| 135 | }, |
| 136 | }; |
| 137 | |
| 138 | |
| 139 | /* Section 10.3.2, Table 13 */ |
Alexander Couzens | b3b837c | 2020-10-27 15:12:25 +0100 | [diff] [blame] | 140 | const struct value_string gprs_ns2_cause_strs[] = { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 141 | { NS_CAUSE_TRANSIT_FAIL, "Transit network failure" }, |
| 142 | { NS_CAUSE_OM_INTERVENTION, "O&M intervention" }, |
| 143 | { NS_CAUSE_EQUIP_FAIL, "Equipment failure" }, |
| 144 | { NS_CAUSE_NSVC_BLOCKED, "NS-VC blocked" }, |
| 145 | { NS_CAUSE_NSVC_UNKNOWN, "NS-VC unknown" }, |
| 146 | { NS_CAUSE_BVCI_UNKNOWN, "BVCI unknown" }, |
| 147 | { NS_CAUSE_SEM_INCORR_PDU, "Semantically incorrect PDU" }, |
| 148 | { NS_CAUSE_PDU_INCOMP_PSTATE, "PDU not compatible with protocol state" }, |
| 149 | { NS_CAUSE_PROTO_ERR_UNSPEC, "Protocol error, unspecified" }, |
| 150 | { NS_CAUSE_INVAL_ESSENT_IE, "Invalid essential IE" }, |
| 151 | { NS_CAUSE_MISSING_ESSENT_IE, "Missing essential IE" }, |
| 152 | { NS_CAUSE_INVAL_NR_IPv4_EP, "Invalid Number of IPv4 Endpoints" }, |
| 153 | { NS_CAUSE_INVAL_NR_IPv6_EP, "Invalid Number of IPv6 Endpoints" }, |
| 154 | { NS_CAUSE_INVAL_NR_NS_VC, "Invalid Number of NS-VCs" }, |
| 155 | { NS_CAUSE_INVAL_WEIGH, "Invalid Weights" }, |
| 156 | { NS_CAUSE_UNKN_IP_EP, "Unknown IP Endpoint" }, |
| 157 | { NS_CAUSE_UNKN_IP_ADDR, "Unknown IP Address" }, |
| 158 | { NS_CAUSE_UNKN_IP_TEST_FAILED, "IP Test Failed" }, |
| 159 | { 0, NULL } |
| 160 | }; |
| 161 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 162 | static const struct rate_ctr_desc nsvc_ctr_description[] = { |
Harald Welte | 3d5eaee | 2021-01-30 21:33:02 +0100 | [diff] [blame] | 163 | [NS_CTR_PKTS_IN] = { "packets:in", "Packets at NS Level ( In)" }, |
| 164 | [NS_CTR_PKTS_OUT] = { "packets:out", "Packets at NS Level (Out)" }, |
Harald Welte | f22ae5a | 2021-01-30 22:01:28 +0100 | [diff] [blame] | 165 | [NS_CTR_PKTS_OUT_DROP] = { "packets:out:drop", "Dropped Packets (Out)" }, |
Harald Welte | 3d5eaee | 2021-01-30 21:33:02 +0100 | [diff] [blame] | 166 | [NS_CTR_BYTES_IN] = { "bytes:in", "Bytes at NS Level ( In)" }, |
| 167 | [NS_CTR_BYTES_OUT] = { "bytes:out", "Bytes at NS Level (Out)" }, |
Harald Welte | f22ae5a | 2021-01-30 22:01:28 +0100 | [diff] [blame] | 168 | [NS_CTR_BYTES_OUT_DROP] = { "bytes:out:drop", "Dropped Bytes (Out)" }, |
Harald Welte | 3d5eaee | 2021-01-30 21:33:02 +0100 | [diff] [blame] | 169 | [NS_CTR_BLOCKED] = { "blocked", "NS-VC Block count " }, |
Harald Welte | b40bf8b | 2021-01-30 22:43:01 +0100 | [diff] [blame^] | 170 | [NS_CTR_UNBLOCKED] = { "unblocked", "NS-VC Unblock count " }, |
Harald Welte | 3d5eaee | 2021-01-30 21:33:02 +0100 | [diff] [blame] | 171 | [NS_CTR_DEAD] = { "dead", "NS-VC gone dead count " }, |
| 172 | [NS_CTR_REPLACED] = { "replaced", "NS-VC replaced other count" }, |
| 173 | [NS_CTR_NSEI_CHG] = { "nsei-chg", "NS-VC changed NSEI count " }, |
| 174 | [NS_CTR_INV_VCI] = { "inv-nsvci", "NS-VCI was invalid count " }, |
| 175 | [NS_CTR_INV_NSEI] = { "inv-nsei", "NSEI was invalid count " }, |
| 176 | [NS_CTR_LOST_ALIVE] = { "lost:alive", "ALIVE ACK missing count " }, |
| 177 | [NS_CTR_LOST_RESET] = { "lost:reset", "RESET ACK missing count " }, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | static const struct rate_ctr_group_desc nsvc_ctrg_desc = { |
| 181 | .group_name_prefix = "ns:nsvc", |
| 182 | .group_description = "NSVC Peer Statistics", |
| 183 | .num_ctr = ARRAY_SIZE(nsvc_ctr_description), |
| 184 | .ctr_desc = nsvc_ctr_description, |
| 185 | .class_id = OSMO_STATS_CLASS_PEER, |
| 186 | }; |
| 187 | |
| 188 | |
| 189 | static const struct osmo_stat_item_desc nsvc_stat_description[] = { |
Harald Welte | 3d5eaee | 2021-01-30 21:33:02 +0100 | [diff] [blame] | 190 | [NS_STAT_ALIVE_DELAY] = { "alive.delay", "ALIVE response time ", "ms", 16, 0 }, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | static const struct osmo_stat_item_group_desc nsvc_statg_desc = { |
| 194 | .group_name_prefix = "ns.nsvc", |
| 195 | .group_description = "NSVC Peer Statistics", |
| 196 | .num_items = ARRAY_SIZE(nsvc_stat_description), |
| 197 | .item_desc = nsvc_stat_description, |
| 198 | .class_id = OSMO_STATS_CLASS_PEER, |
| 199 | }; |
| 200 | |
Alexander Couzens | 2498f1d | 2020-10-27 01:09:01 +0100 | [diff] [blame] | 201 | const struct value_string gprs_ns2_aff_cause_prim_strs[] = { |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 202 | { GPRS_NS2_AFF_CAUSE_VC_FAILURE, "NSVC failure" }, |
| 203 | { GPRS_NS2_AFF_CAUSE_VC_RECOVERY, "NSVC recovery" }, |
| 204 | { GPRS_NS2_AFF_CAUSE_FAILURE, "NSE failure" }, |
| 205 | { GPRS_NS2_AFF_CAUSE_RECOVERY, "NSE recovery" }, |
| 206 | { GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED, "NSE SNS configured" }, |
| 207 | { GPRS_NS2_AFF_CAUSE_SNS_FAILURE, "NSE SNS failure" }, |
Alexander Couzens | 2498f1d | 2020-10-27 01:09:01 +0100 | [diff] [blame] | 208 | { 0, NULL } |
| 209 | }; |
| 210 | |
Alexander Couzens | 0ab028c | 2020-11-04 02:41:44 +0100 | [diff] [blame] | 211 | const struct value_string gprs_ns2_prim_strs[] = { |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 212 | { GPRS_NS2_PRIM_UNIT_DATA, "UNIT DATA" }, |
| 213 | { GPRS_NS2_PRIM_CONGESTION, "CONGESTION" }, |
| 214 | { GPRS_NS2_PRIM_STATUS, "STATUS" }, |
Alexander Couzens | 2498f1d | 2020-10-27 01:09:01 +0100 | [diff] [blame] | 215 | { 0, NULL } |
| 216 | }; |
| 217 | |
Harald Welte | a24e7ee | 2020-11-29 17:38:48 +0100 | [diff] [blame] | 218 | const struct value_string gprs_ns2_lltype_strs[] = { |
| 219 | { GPRS_NS2_LL_UDP, "UDP" }, |
| 220 | { GPRS_NS2_LL_FR_GRE, "FR_GRE" }, |
| 221 | { GPRS_NS2_LL_FR, "FR" }, |
| 222 | { 0, NULL } |
| 223 | }; |
| 224 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 225 | /*! string-format a given NS-VC into a user-supplied buffer. |
| 226 | * \param[in] buf user-allocated output buffer |
| 227 | * \param[in] buf_len size of user-allocated output buffer in bytes |
| 228 | * \param[in] nsvc NS-VC to be string-formatted |
| 229 | * \return pointer to buf on success; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 230 | char *gprs_ns2_ll_str_buf(char *buf, size_t buf_len, struct gprs_ns2_vc *nsvc) |
| 231 | { |
Alexander Couzens | 9a4cf27 | 2020-10-11 20:48:04 +0200 | [diff] [blame] | 232 | const struct osmo_sockaddr *local; |
| 233 | const struct osmo_sockaddr *remote; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 234 | struct osmo_sockaddr_str local_str; |
| 235 | struct osmo_sockaddr_str remote_str; |
| 236 | |
| 237 | if (!buf_len) |
Harald Welte | 92ad029 | 2020-09-18 22:34:24 +0200 | [diff] [blame] | 238 | return NULL; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 239 | |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 240 | switch (nsvc->nse->ll) { |
Alexander Couzens | 24a14ac | 2020-11-19 02:34:49 +0100 | [diff] [blame] | 241 | case GPRS_NS2_LL_UDP: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 242 | if (!gprs_ns2_is_ip_bind(nsvc->bind)) { |
| 243 | buf[0] = '\0'; |
| 244 | return buf; |
| 245 | } |
| 246 | |
| 247 | local = gprs_ns2_ip_bind_sockaddr(nsvc->bind); |
Alexander Couzens | c4229a4 | 2020-10-11 20:58:04 +0200 | [diff] [blame] | 248 | remote = gprs_ns2_ip_vc_remote(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 249 | if (osmo_sockaddr_str_from_sockaddr(&local_str, &local->u.sas)) |
| 250 | strcpy(local_str.ip, "invalid"); |
| 251 | if (osmo_sockaddr_str_from_sockaddr(&remote_str, &remote->u.sas)) |
| 252 | strcpy(remote_str.ip, "invalid"); |
| 253 | |
| 254 | if (nsvc->nsvci_is_valid) |
| 255 | snprintf(buf, buf_len, "udp)[%s]:%u<%u>[%s]:%u", |
| 256 | local_str.ip, local_str.port, |
| 257 | nsvc->nsvci, |
| 258 | remote_str.ip, remote_str.port); |
| 259 | else |
| 260 | snprintf(buf, buf_len, "udp)[%s]:%u<>[%s]:%u", |
| 261 | local_str.ip, local_str.port, |
| 262 | remote_str.ip, remote_str.port); |
| 263 | break; |
Alexander Couzens | 24a14ac | 2020-11-19 02:34:49 +0100 | [diff] [blame] | 264 | case GPRS_NS2_LL_FR_GRE: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 265 | snprintf(buf, buf_len, "frgre)"); |
| 266 | break; |
Alexander Couzens | 24a14ac | 2020-11-19 02:34:49 +0100 | [diff] [blame] | 267 | case GPRS_NS2_LL_FR: |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 268 | snprintf(buf, buf_len, "fr)netif: %s dlci: %u", gprs_ns2_fr_bind_netif(nsvc->bind), |
| 269 | gprs_ns2_fr_nsvc_dlci(nsvc)); |
| 270 | break; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 271 | default: |
Harald Welte | 6188e00 | 2020-12-01 17:20:07 +0100 | [diff] [blame] | 272 | snprintf(buf, buf_len, "unknown)"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 273 | break; |
| 274 | } |
| 275 | |
| 276 | buf[buf_len - 1] = '\0'; |
| 277 | |
| 278 | return buf; |
| 279 | } |
| 280 | |
| 281 | /* udp is the longest: udp)[IP6]:65536<65536>[IP6]:65536 */ |
| 282 | #define NS2_LL_MAX_STR 4+2*(INET6_ADDRSTRLEN+9)+8 |
| 283 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 284 | /*! string-format a given NS-VC to a thread-local static buffer. |
| 285 | * \param[in] nsvc NS-VC to be string-formatted |
| 286 | * \return pointer to the string on success; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 287 | const char *gprs_ns2_ll_str(struct gprs_ns2_vc *nsvc) |
| 288 | { |
| 289 | static __thread char buf[NS2_LL_MAX_STR]; |
| 290 | return gprs_ns2_ll_str_buf(buf, sizeof(buf), nsvc); |
| 291 | } |
| 292 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 293 | /*! string-format a given NS-VC to a dynamically allocated string. |
| 294 | * \param[in] ctx talloc context from which to allocate |
| 295 | * \param[in] nsvc NS-VC to be string-formatted |
| 296 | * \return pointer to the string on success; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 297 | char *gprs_ns2_ll_str_c(const void *ctx, struct gprs_ns2_vc *nsvc) |
| 298 | { |
| 299 | char *buf = talloc_size(ctx, NS2_LL_MAX_STR); |
| 300 | if (!buf) |
| 301 | return buf; |
| 302 | return gprs_ns2_ll_str_buf(buf, NS2_LL_MAX_STR, nsvc); |
| 303 | } |
| 304 | |
Daniel Willmann | f128654 | 2020-11-03 23:03:33 +0100 | [diff] [blame] | 305 | /*! Return the current state name of a given NS-VC to a thread-local static buffer. |
| 306 | * \param[in] nsvc NS-VC to return the state of |
| 307 | * \return pointer to the string on success; NULL on error */ |
| 308 | const char *gprs_ns2_nsvc_state_name(struct gprs_ns2_vc *nsvc) |
| 309 | { |
| 310 | return osmo_fsm_inst_state_name(nsvc->fi); |
| 311 | } |
| 312 | |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 313 | /* select a signalling NSVC and respect sig_counter |
| 314 | * param[out] reset_counter - all counter has to be resetted to their signal weight |
| 315 | * return the chosen nsvc or NULL |
| 316 | */ |
| 317 | static struct gprs_ns2_vc *ns2_load_sharing_signal(struct gprs_ns2_nse *nse) |
| 318 | { |
| 319 | struct gprs_ns2_vc *nsvc = NULL, *last = NULL, *tmp; |
| 320 | |
| 321 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
| 322 | if (tmp->sig_weight == 0) |
| 323 | continue; |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 324 | if (!ns2_vc_is_unblocked(tmp)) |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 325 | continue; |
| 326 | if (tmp->sig_counter == 0) { |
| 327 | last = tmp; |
| 328 | continue; |
| 329 | } |
| 330 | |
| 331 | tmp->sig_counter--; |
| 332 | nsvc = tmp; |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | /* all counter were zero, but there are valid nsvc */ |
| 337 | if (!nsvc && last) { |
| 338 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
| 339 | tmp->sig_counter = tmp->sig_weight; |
| 340 | } |
| 341 | |
| 342 | last->sig_counter--; |
| 343 | return last; |
| 344 | } else { |
| 345 | return nsvc; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /* 4.4.1 Load Sharing function for the Frame Relay Sub-Network */ |
| 350 | static struct gprs_ns2_vc *ns2_load_sharing_modulor( |
| 351 | struct gprs_ns2_nse *nse, |
| 352 | uint16_t bvci, |
| 353 | uint32_t load_selector) |
| 354 | { |
| 355 | struct gprs_ns2_vc *tmp; |
Alexander Couzens | 8a2a1a4 | 2020-12-26 18:00:17 +0100 | [diff] [blame] | 356 | uint32_t mod = (bvci + load_selector) % nse->nsvc_count; |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 357 | uint32_t i = 0; |
| 358 | |
| 359 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 360 | if (!ns2_vc_is_unblocked(tmp)) |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 361 | continue; |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 362 | if (i == mod) |
| 363 | return tmp; |
| 364 | i++; |
| 365 | } |
| 366 | |
| 367 | return NULL; |
| 368 | } |
| 369 | |
| 370 | /* pick the first available data NSVC - no load sharing */ |
| 371 | struct gprs_ns2_vc *ns2_load_sharing_first(struct gprs_ns2_nse *nse) |
| 372 | { |
| 373 | struct gprs_ns2_vc *nsvc = NULL, *tmp; |
| 374 | |
| 375 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 376 | if (!ns2_vc_is_unblocked(tmp)) |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 377 | continue; |
| 378 | if (tmp->data_weight == 0) |
| 379 | continue; |
| 380 | |
| 381 | nsvc = tmp; |
| 382 | break; |
| 383 | } |
| 384 | |
| 385 | return nsvc; |
| 386 | } |
| 387 | |
| 388 | |
| 389 | static struct gprs_ns2_vc *ns2_load_sharing( |
| 390 | struct gprs_ns2_nse *nse, |
| 391 | uint16_t bvci, |
| 392 | uint32_t link_selector) |
| 393 | { |
| 394 | struct gprs_ns2_vc *nsvc = NULL; |
| 395 | |
Alexander Couzens | 8a2a1a4 | 2020-12-26 18:00:17 +0100 | [diff] [blame] | 396 | switch (nse->ll) { |
| 397 | case GPRS_NS2_LL_FR: |
| 398 | nsvc = ns2_load_sharing_modulor(nse, bvci, link_selector); |
| 399 | break; |
| 400 | case GPRS_NS2_LL_UDP: |
| 401 | default: |
| 402 | if (bvci == 0) { |
| 403 | /* signalling */ |
| 404 | nsvc = ns2_load_sharing_signal(nse); |
| 405 | } else { |
| 406 | /* data with load sharing parameter */ |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 407 | nsvc = ns2_load_sharing_first(nse); |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 408 | } |
Alexander Couzens | 8a2a1a4 | 2020-12-26 18:00:17 +0100 | [diff] [blame] | 409 | break; |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | return nsvc; |
| 413 | } |
| 414 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 415 | /*! Receive a primitive from the NS User (Gb). |
| 416 | * \param[in] nsi NS instance to which the primitive is issued |
| 417 | * \param[in] oph The primitive |
| 418 | * \return 0 on success; negative on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 419 | int gprs_ns2_recv_prim(struct gprs_ns2_inst *nsi, struct osmo_prim_hdr *oph) |
| 420 | { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 421 | /* TODO: implement resource distribution */ |
| 422 | /* TODO: check for empty PDUs which can be sent to Request/Confirm |
| 423 | * the IP endpoint */ |
| 424 | struct osmo_gprs_ns2_prim *nsp; |
| 425 | struct gprs_ns2_nse *nse = NULL; |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 426 | struct gprs_ns2_vc *nsvc = NULL; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 427 | uint16_t bvci, nsei; |
| 428 | uint8_t sducontrol = 0; |
| 429 | |
| 430 | if (oph->sap != SAP_NS) |
| 431 | return -EINVAL; |
| 432 | |
| 433 | nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph); |
| 434 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 435 | if (oph->operation != PRIM_OP_REQUEST || oph->primitive != GPRS_NS2_PRIM_UNIT_DATA) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 436 | return -EINVAL; |
| 437 | |
| 438 | if (!oph->msg) |
| 439 | return -EINVAL; |
| 440 | |
| 441 | bvci = nsp->bvci; |
| 442 | nsei = nsp->nsei; |
| 443 | |
| 444 | nse = gprs_ns2_nse_by_nsei(nsi, nsei); |
| 445 | if (!nse) |
| 446 | return -EINVAL; |
| 447 | |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 448 | nsvc = ns2_load_sharing(nse, bvci, nsp->u.unitdata.link_selector); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 449 | |
| 450 | /* TODO: send a status primitive back */ |
| 451 | if (!nsvc) |
| 452 | return 0; |
| 453 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 454 | if (nsp->u.unitdata.change == GPRS_NS2_ENDPOINT_REQUEST_CHANGE) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 455 | sducontrol = 1; |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 456 | else if (nsp->u.unitdata.change == GPRS_NS2_ENDPOINT_CONFIRM_CHANGE) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 457 | sducontrol = 2; |
| 458 | |
| 459 | return ns2_tx_unit_data(nsvc, bvci, sducontrol, oph->msg); |
| 460 | } |
| 461 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 462 | /*! Send a STATUS.ind primitive to the specified NS instance user. |
| 463 | * \param[in] nsi NS instance on which we operate |
| 464 | * \param[in] nsei NSEI to which the statue relates |
| 465 | * \param[in] bvci BVCI to which the status relates |
| 466 | * \param[in] cause The cause of the status */ |
Alexander Couzens | bf95f0f | 2020-10-01 22:56:03 +0200 | [diff] [blame] | 467 | void ns2_prim_status_ind(struct gprs_ns2_nse *nse, |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 468 | struct gprs_ns2_vc *nsvc, |
Alexander Couzens | bf95f0f | 2020-10-01 22:56:03 +0200 | [diff] [blame] | 469 | uint16_t bvci, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 470 | enum gprs_ns2_affecting_cause cause) |
| 471 | { |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 472 | char nsvc_str[NS2_LL_MAX_STR]; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 473 | struct osmo_gprs_ns2_prim nsp = {}; |
Alexander Couzens | bf95f0f | 2020-10-01 22:56:03 +0200 | [diff] [blame] | 474 | nsp.nsei = nse->nsei; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 475 | nsp.bvci = bvci; |
| 476 | nsp.u.status.cause = cause; |
Alexander Couzens | 1c8785d | 2020-12-17 06:58:53 +0100 | [diff] [blame] | 477 | nsp.u.status.transfer = ns2_count_transfer_cap(nse, bvci); |
Alexander Couzens | da0a285 | 2020-10-01 23:24:07 +0200 | [diff] [blame] | 478 | nsp.u.status.first = nse->first; |
| 479 | nsp.u.status.persistent = nse->persistent; |
Daniel Willmann | 15c09a8 | 2020-11-03 23:05:43 +0100 | [diff] [blame] | 480 | if (nsvc) |
| 481 | nsp.u.status.nsvc = gprs_ns2_ll_str_buf(nsvc_str, sizeof(nsvc_str), nsvc); |
| 482 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 483 | osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_STATUS, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 484 | PRIM_OP_INDICATION, NULL); |
Alexander Couzens | bf95f0f | 2020-10-01 22:56:03 +0200 | [diff] [blame] | 485 | nse->nsi->cb(&nsp.oph, nse->nsi->cb_data); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 486 | } |
| 487 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 488 | /*! Allocate a NS-VC within the given bind + NSE. |
| 489 | * \param[in] bind The 'bind' on which we operate |
| 490 | * \param[in] nse The NS Entity on which we operate |
| 491 | * \param[in] initiater - if this is an incoming remote (!initiater) or a local outgoing connection (initater) |
Harald Welte | 603f404 | 2020-11-29 17:39:19 +0100 | [diff] [blame] | 492 | * \param[in] id - human-readable identifier |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 493 | * \return newly allocated NS-VC on success; NULL on error */ |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 494 | struct gprs_ns2_vc *ns2_vc_alloc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_nse *nse, bool initiater, |
Harald Welte | 603f404 | 2020-11-29 17:39:19 +0100 | [diff] [blame] | 495 | enum gprs_ns2_vc_mode vc_mode, const char *id) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 496 | { |
Daniel Willmann | bb89905 | 2021-01-16 14:02:45 +0100 | [diff] [blame] | 497 | /* Sanity check */ |
| 498 | OSMO_ASSERT(bind->ll == nse->ll); |
| 499 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 500 | struct gprs_ns2_vc *nsvc = talloc_zero(bind, struct gprs_ns2_vc); |
| 501 | |
| 502 | if (!nsvc) |
| 503 | return NULL; |
| 504 | |
| 505 | nsvc->bind = bind; |
| 506 | nsvc->nse = nse; |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 507 | nsvc->mode = vc_mode; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 508 | nsvc->sig_weight = 1; |
| 509 | nsvc->data_weight = 1; |
| 510 | |
| 511 | nsvc->ctrg = rate_ctr_group_alloc(nsvc, &nsvc_ctrg_desc, bind->nsi->rate_ctr_idx); |
| 512 | if (!nsvc->ctrg) { |
| 513 | goto err; |
| 514 | } |
| 515 | nsvc->statg = osmo_stat_item_group_alloc(nsvc, &nsvc_statg_desc, bind->nsi->rate_ctr_idx); |
| 516 | if (!nsvc->statg) |
| 517 | goto err_group; |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 518 | if (!ns2_vc_fsm_alloc(nsvc, id, initiater)) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 519 | goto err_statg; |
| 520 | |
| 521 | bind->nsi->rate_ctr_idx++; |
| 522 | |
| 523 | llist_add(&nsvc->list, &nse->nsvc); |
| 524 | llist_add(&nsvc->blist, &bind->nsvc); |
| 525 | |
| 526 | return nsvc; |
| 527 | |
| 528 | err_statg: |
| 529 | osmo_stat_item_group_free(nsvc->statg); |
| 530 | err_group: |
| 531 | rate_ctr_group_free(nsvc->ctrg); |
| 532 | err: |
| 533 | talloc_free(nsvc); |
| 534 | |
| 535 | return NULL; |
| 536 | } |
| 537 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 538 | /*! Destroy/release given NS-VC. |
| 539 | * \param[in] nsvc NS-VC to destroy */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 540 | void gprs_ns2_free_nsvc(struct gprs_ns2_vc *nsvc) |
| 541 | { |
| 542 | if (!nsvc) |
| 543 | return; |
| 544 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 545 | ns2_prim_status_ind(nsvc->nse, nsvc, 0, GPRS_NS2_AFF_CAUSE_VC_FAILURE); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 546 | |
| 547 | llist_del(&nsvc->list); |
| 548 | llist_del(&nsvc->blist); |
| 549 | |
| 550 | /* notify nse this nsvc is unavailable */ |
| 551 | ns2_nse_notify_unblocked(nsvc, false); |
| 552 | |
| 553 | /* check if sns is using this VC */ |
| 554 | ns2_sns_free_nsvc(nsvc); |
| 555 | osmo_fsm_inst_term(nsvc->fi, OSMO_FSM_TERM_REQUEST, NULL); |
| 556 | |
| 557 | /* let the driver/bind clean up it's internal state */ |
| 558 | if (nsvc->priv && nsvc->bind->free_vc) |
| 559 | nsvc->bind->free_vc(nsvc); |
| 560 | |
| 561 | osmo_stat_item_group_free(nsvc->statg); |
| 562 | rate_ctr_group_free(nsvc->ctrg); |
| 563 | |
| 564 | talloc_free(nsvc); |
| 565 | } |
| 566 | |
Alexander Couzens | 4755879 | 2020-12-06 03:16:11 +0100 | [diff] [blame] | 567 | /*! Destroy/release all NS-VC of given NSE |
| 568 | * \param[in] nse NSE |
| 569 | */ |
| 570 | void gprs_ns2_free_nsvcs(struct gprs_ns2_nse *nse) |
| 571 | { |
| 572 | struct gprs_ns2_vc *nsvc, *tmp; |
| 573 | |
| 574 | if (!nse) |
| 575 | return; |
| 576 | |
| 577 | llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) { |
| 578 | gprs_ns2_free_nsvc(nsvc); |
| 579 | } |
| 580 | } |
| 581 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 582 | /*! Allocate a message buffer for use with the NS2 stack. */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 583 | struct msgb *ns2_msgb_alloc(void) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 584 | { |
| 585 | struct msgb *msg = msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM, |
| 586 | "GPRS/NS"); |
| 587 | if (!msg) { |
| 588 | LOGP(DLNS, LOGL_ERROR, "Failed to allocate NS message of size %d\n", |
| 589 | NS_ALLOC_SIZE); |
| 590 | } |
| 591 | return msg; |
| 592 | } |
| 593 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 594 | /*! Create a status message to be sent over a new connection. |
| 595 | * \param[in] orig_msg the original message |
| 596 | * \param[in] tp TLVP parsed of the original message |
| 597 | * \param[out] reject callee-allocated message buffer of the generated NS-STATUS |
| 598 | * \param[in] cause Cause for the rejection |
| 599 | * \return 0 on success */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 600 | static int reject_status_msg(struct msgb *orig_msg, struct tlv_parsed *tp, struct msgb **reject, enum ns_cause cause) |
| 601 | { |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 602 | struct msgb *msg = ns2_msgb_alloc(); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 603 | struct gprs_ns_hdr *nsh; |
| 604 | bool have_vci = false; |
| 605 | uint8_t _cause = cause; |
| 606 | uint16_t nsei = 0; |
| 607 | |
| 608 | if (!msg) |
| 609 | return -ENOMEM; |
| 610 | |
Harald Welte | 798efea | 2020-12-03 16:01:02 +0100 | [diff] [blame] | 611 | if (TLVP_PRES_LEN(tp, NS_IE_NSEI, 2)) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 612 | nsei = tlvp_val16be(tp, NS_IE_NSEI); |
| 613 | |
| 614 | LOGP(DLNS, LOGL_NOTICE, "NSEI=%u Rejecting message without NSVCI. Tx NS STATUS (cause=%s)\n", |
| 615 | nsei, gprs_ns2_cause_str(cause)); |
| 616 | } |
| 617 | |
| 618 | msg->l2h = msgb_put(msg, sizeof(*nsh)); |
| 619 | nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 620 | nsh->pdu_type = NS_PDUT_STATUS; |
| 621 | |
| 622 | msgb_tvlv_put(msg, NS_IE_CAUSE, 1, &_cause); |
Harald Welte | 798efea | 2020-12-03 16:01:02 +0100 | [diff] [blame] | 623 | have_vci = TLVP_PRES_LEN(tp, NS_IE_VCI, 2); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 624 | |
| 625 | /* Section 9.2.7.1: Static conditions for NS-VCI */ |
| 626 | if (cause == NS_CAUSE_NSVC_BLOCKED || |
| 627 | cause == NS_CAUSE_NSVC_UNKNOWN) { |
| 628 | if (!have_vci) { |
| 629 | msgb_free(msg); |
| 630 | return -EINVAL; |
| 631 | } |
| 632 | |
| 633 | msgb_tvlv_put(msg, NS_IE_VCI, 2, TLVP_VAL(tp, NS_IE_VCI)); |
| 634 | } |
| 635 | |
| 636 | /* Section 9.2.7.2: Static conditions for NS PDU */ |
| 637 | switch (cause) { |
| 638 | case NS_CAUSE_SEM_INCORR_PDU: |
| 639 | case NS_CAUSE_PDU_INCOMP_PSTATE: |
| 640 | case NS_CAUSE_PROTO_ERR_UNSPEC: |
| 641 | case NS_CAUSE_INVAL_ESSENT_IE: |
| 642 | case NS_CAUSE_MISSING_ESSENT_IE: |
| 643 | msgb_tvlv_put(msg, NS_IE_PDU, msgb_l2len(orig_msg), |
| 644 | orig_msg->l2h); |
| 645 | break; |
| 646 | default: |
| 647 | break; |
| 648 | } |
| 649 | |
| 650 | *reject = msg; |
| 651 | return 0; |
| 652 | } |
| 653 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 654 | /*! Resolve a NS Entity based on its NSEI. |
| 655 | * \param[in] nsi NS Instance in which we do the look-up |
| 656 | * \param[in] nsei NSEI to look up |
| 657 | * \return NS Entity in successful case; NULL if none found */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 658 | struct gprs_ns2_nse *gprs_ns2_nse_by_nsei(struct gprs_ns2_inst *nsi, uint16_t nsei) |
| 659 | { |
| 660 | struct gprs_ns2_nse *nse; |
| 661 | |
| 662 | llist_for_each_entry(nse, &nsi->nse, list) { |
| 663 | if (nse->nsei == nsei) |
| 664 | return nse; |
| 665 | } |
| 666 | |
| 667 | return NULL; |
| 668 | } |
| 669 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 670 | /*! Resolve a NS-VC Entity based on its NS-VCI. |
| 671 | * \param[in] nsi NS Instance in which we do the look-up |
| 672 | * \param[in] nsvci NS-VCI to look up |
| 673 | * \return NS-VC Entity in successful case; NULL if none found */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 674 | struct gprs_ns2_vc *gprs_ns2_nsvc_by_nsvci(struct gprs_ns2_inst *nsi, uint16_t nsvci) |
| 675 | { |
| 676 | struct gprs_ns2_nse *nse; |
| 677 | struct gprs_ns2_vc *nsvc; |
| 678 | |
| 679 | llist_for_each_entry(nse, &nsi->nse, list) { |
| 680 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
| 681 | if (nsvc->nsvci_is_valid && nsvc->nsvci == nsvci) |
| 682 | return nsvc; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | return NULL; |
| 687 | } |
| 688 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 689 | /*! Create a NS Entity within given NS instance. |
| 690 | * \param[in] nsi NS instance in which to create NS Entity |
| 691 | * \param[in] nsei NS Entity Identifier of to-be-created NSE |
| 692 | * \returns newly-allocated NS-E in successful case; NULL on error */ |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 693 | struct gprs_ns2_nse *gprs_ns2_create_nse(struct gprs_ns2_inst *nsi, uint16_t nsei, |
| 694 | enum gprs_ns2_ll linklayer, enum gprs_ns2_dialect dialect) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 695 | { |
| 696 | struct gprs_ns2_nse *nse; |
Alexander Couzens | 93ad499 | 2020-12-06 03:03:03 +0100 | [diff] [blame] | 697 | char sns[16]; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 698 | |
| 699 | nse = gprs_ns2_nse_by_nsei(nsi, nsei); |
| 700 | if (nse) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 701 | LOGNSE(nse, LOGL_ERROR, "Can not create a NSE with already taken NSEI\n"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 702 | return nse; |
| 703 | } |
| 704 | |
| 705 | nse = talloc_zero(nsi, struct gprs_ns2_nse); |
| 706 | if (!nse) |
| 707 | return NULL; |
| 708 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 709 | if (dialect == GPRS_NS2_DIALECT_SNS) { |
Alexander Couzens | 93ad499 | 2020-12-06 03:03:03 +0100 | [diff] [blame] | 710 | snprintf(sns, sizeof(sns), "NSE%05u-SNS", nsei); |
| 711 | nse->bss_sns_fi = ns2_sns_bss_fsm_alloc(nse, sns); |
| 712 | if (!nse->bss_sns_fi) { |
| 713 | talloc_free(nse); |
| 714 | return NULL; |
| 715 | } |
| 716 | } |
| 717 | |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 718 | nse->dialect = dialect; |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 719 | nse->ll = linklayer; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 720 | nse->nsei = nsei; |
| 721 | nse->nsi = nsi; |
Alexander Couzens | da0a285 | 2020-10-01 23:24:07 +0200 | [diff] [blame] | 722 | nse->first = true; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 723 | llist_add(&nse->list, &nsi->nse); |
| 724 | INIT_LLIST_HEAD(&nse->nsvc); |
| 725 | |
| 726 | return nse; |
| 727 | } |
| 728 | |
Alexander Couzens | 05e7f7d | 2020-10-11 19:51:46 +0200 | [diff] [blame] | 729 | /*! Return the NSEI |
| 730 | * \param[in] nse NS Entity |
| 731 | * \return the nsei. |
| 732 | */ |
| 733 | uint16_t gprs_ns2_nse_nsei(struct gprs_ns2_nse *nse) |
| 734 | { |
| 735 | return nse->nsei; |
| 736 | } |
| 737 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 738 | /*! Destroy given NS Entity. |
| 739 | * \param[in] nse NS Entity to destroy */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 740 | void gprs_ns2_free_nse(struct gprs_ns2_nse *nse) |
| 741 | { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 742 | if (!nse) |
| 743 | return; |
| 744 | |
Alexander Couzens | d1cd650 | 2021-01-15 02:16:23 +0100 | [diff] [blame] | 745 | nse->alive = false; |
Alexander Couzens | 4755879 | 2020-12-06 03:16:11 +0100 | [diff] [blame] | 746 | gprs_ns2_free_nsvcs(nse); |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 747 | ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_FAILURE); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 748 | |
| 749 | llist_del(&nse->list); |
| 750 | if (nse->bss_sns_fi) |
| 751 | osmo_fsm_inst_term(nse->bss_sns_fi, OSMO_FSM_TERM_REQUEST, NULL); |
| 752 | talloc_free(nse); |
| 753 | } |
| 754 | |
Alexander Couzens | 4b6c8af | 2020-10-11 20:15:25 +0200 | [diff] [blame] | 755 | void gprs_ns2_free_nses(struct gprs_ns2_inst *nsi) |
| 756 | { |
| 757 | struct gprs_ns2_nse *nse, *ntmp; |
| 758 | |
| 759 | llist_for_each_entry_safe(nse, ntmp, &nsi->nse, list) { |
| 760 | gprs_ns2_free_nse(nse); |
| 761 | } |
| 762 | } |
| 763 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 764 | static inline int ns2_tlv_parse(struct tlv_parsed *dec, |
| 765 | const uint8_t *buf, int buf_len, uint8_t lv_tag, |
| 766 | uint8_t lv_tag2) |
| 767 | { |
| 768 | /* workaround for NS_IE_IP_ADDR not following any known TLV rules. |
| 769 | * See comment of ns_att_tlvdef1. */ |
| 770 | int rc = tlv_parse(dec, &ns_att_tlvdef1, buf, buf_len, lv_tag, lv_tag2); |
| 771 | if (rc < 0) |
| 772 | return tlv_parse(dec, &ns_att_tlvdef2, buf, buf_len, lv_tag, lv_tag2); |
| 773 | return rc; |
| 774 | } |
| 775 | |
| 776 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 777 | /*! Create a new NS-VC based on a [received] message. Depending on the bind it might create a NSE. |
| 778 | * \param[in] bind the bind through which msg was received |
| 779 | * \param[in] msg the actual received message |
| 780 | * \param[in] logname A name to describe the VC. E.g. ip address pair |
| 781 | * \param[out] reject A message filled to be sent back. Only used in failure cases. |
| 782 | * \param[out] success A pointer which will be set to the new VC on success |
| 783 | * \return enum value indicating the status, e.g. GPRS_NS2_CS_CREATED */ |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 784 | enum ns2_cs ns2_create_vc(struct gprs_ns2_vc_bind *bind, |
| 785 | struct msgb *msg, |
| 786 | const char *logname, |
| 787 | struct msgb **reject, |
| 788 | struct gprs_ns2_vc **success) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 789 | { |
| 790 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *)msg->l2h; |
| 791 | struct tlv_parsed tp; |
| 792 | struct gprs_ns2_vc *nsvc; |
| 793 | struct gprs_ns2_nse *nse; |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 794 | enum gprs_ns2_dialect dialect; |
| 795 | enum gprs_ns2_vc_mode vc_mode; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 796 | uint16_t nsvci; |
| 797 | uint16_t nsei; |
Harald Welte | 603f404 | 2020-11-29 17:39:19 +0100 | [diff] [blame] | 798 | char idbuf[32]; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 799 | |
Alexander Couzens | 12e5e6b | 2020-12-10 00:18:17 +0100 | [diff] [blame] | 800 | int rc, tlv; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 801 | |
| 802 | if (msg->len < sizeof(struct gprs_ns_hdr)) |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 803 | return NS2_CS_ERROR; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 804 | |
Alexander Couzens | 12e5e6b | 2020-12-10 00:18:17 +0100 | [diff] [blame] | 805 | /* parse the tlv early to allow reject status msg to |
| 806 | * work with valid tp. |
| 807 | * Ignore the return code until the pdu type is parsed because |
| 808 | * an unknown pdu type should be ignored */ |
| 809 | tlv = ns2_tlv_parse(&tp, nsh->data, |
Alexander Couzens | bac5b01 | 2020-12-09 23:32:22 +0100 | [diff] [blame] | 810 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
Alexander Couzens | bac5b01 | 2020-12-09 23:32:22 +0100 | [diff] [blame] | 811 | |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 812 | switch (nsh->pdu_type) { |
| 813 | case NS_PDUT_STATUS: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 814 | /* Do not respond, see 3GPP TS 08.16, 7.5.1 */ |
| 815 | LOGP(DLNS, LOGL_INFO, "Ignoring NS STATUS from %s " |
| 816 | "for non-existing NS-VC\n", |
| 817 | logname); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 818 | return NS2_CS_SKIPPED; |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 819 | case NS_PDUT_ALIVE_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 820 | /* Ignore this, see 3GPP TS 08.16, 7.4.1 */ |
| 821 | LOGP(DLNS, LOGL_INFO, "Ignoring NS ALIVE ACK from %s " |
| 822 | "for non-existing NS-VC\n", |
| 823 | logname); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 824 | return NS2_CS_SKIPPED; |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 825 | case NS_PDUT_RESET_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 826 | /* Ignore this, see 3GPP TS 08.16, 7.3.1 */ |
| 827 | LOGP(DLNS, LOGL_INFO, "Ignoring NS RESET ACK from %s " |
| 828 | "for non-existing NS-VC\n", |
| 829 | logname); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 830 | return NS2_CS_SKIPPED; |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 831 | case NS_PDUT_RESET: |
| 832 | /* accept PDU RESET when vc_mode matches */ |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 833 | if (bind->accept_ipaccess) { |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 834 | dialect = GPRS_NS2_DIALECT_IPACCESS; |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 835 | break; |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 836 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 837 | |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 838 | rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE); |
Alexander Couzens | 3c22f91 | 2020-12-10 00:20:18 +0100 | [diff] [blame] | 839 | if (rc < 0) |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 840 | LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 841 | return NS2_CS_REJECTED; |
Alexander Couzens | d87a2f1 | 2020-11-30 22:20:20 +0100 | [diff] [blame] | 842 | default: |
Alexander Couzens | 8ebc1ac | 2020-10-12 03:57:26 +0200 | [diff] [blame] | 843 | rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PDU_INCOMP_PSTATE); |
Alexander Couzens | 3c22f91 | 2020-12-10 00:20:18 +0100 | [diff] [blame] | 844 | if (rc < 0) |
Alexander Couzens | 8ebc1ac | 2020-10-12 03:57:26 +0200 | [diff] [blame] | 845 | LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 846 | return NS2_CS_REJECTED; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 847 | } |
| 848 | |
Alexander Couzens | 12e5e6b | 2020-12-10 00:18:17 +0100 | [diff] [blame] | 849 | if (tlv < 0) { |
| 850 | /* TODO: correct behaviour would checking what's wrong. |
| 851 | * If it's an essential TLV for the PDU return NS_CAUSE_INVAL_ESSENT_IE. |
| 852 | * Otherwise ignore the non-essential TLV. */ |
| 853 | LOGP(DLNS, LOGL_ERROR, "Rx NS RESET Error %d during " |
| 854 | "TLV Parse\n", tlv); |
| 855 | rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_PROTO_ERR_UNSPEC); |
Alexander Couzens | 3c22f91 | 2020-12-10 00:20:18 +0100 | [diff] [blame] | 856 | if (rc < 0) |
Alexander Couzens | 12e5e6b | 2020-12-10 00:18:17 +0100 | [diff] [blame] | 857 | LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 858 | return NS2_CS_REJECTED; |
Alexander Couzens | 12e5e6b | 2020-12-10 00:18:17 +0100 | [diff] [blame] | 859 | } |
| 860 | |
Harald Welte | 798efea | 2020-12-03 16:01:02 +0100 | [diff] [blame] | 861 | if (!TLVP_PRES_LEN(&tp, NS_IE_CAUSE, 1) || |
| 862 | !TLVP_PRES_LEN(&tp, NS_IE_VCI, 2) || !TLVP_PRES_LEN(&tp, NS_IE_NSEI, 2)) { |
Alexander Couzens | 8ebc1ac | 2020-10-12 03:57:26 +0200 | [diff] [blame] | 863 | LOGP(DLNS, LOGL_ERROR, "NS RESET Missing mandatory IE\n"); |
| 864 | rc = reject_status_msg(msg, &tp, reject, NS_CAUSE_MISSING_ESSENT_IE); |
Alexander Couzens | 3c22f91 | 2020-12-10 00:20:18 +0100 | [diff] [blame] | 865 | if (rc < 0) |
| 866 | LOGP(DLNS, LOGL_ERROR, "Failed to generate reject message (%d)\n", rc); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 867 | return NS2_CS_REJECTED; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 868 | } |
| 869 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 870 | nsei = tlvp_val16be(&tp, NS_IE_NSEI); |
Alexander Couzens | 05ac4e5 | 2021-01-04 19:07:05 +0100 | [diff] [blame] | 871 | nsvci = tlvp_val16be(&tp, NS_IE_VCI); |
| 872 | |
| 873 | /* find or create NSE */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 874 | nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei); |
| 875 | if (!nse) { |
Alexander Couzens | 05ac4e5 | 2021-01-04 19:07:05 +0100 | [diff] [blame] | 876 | /* only create nse for udp & ipaccess */ |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 877 | if (bind->ll != GPRS_NS2_LL_UDP || dialect != GPRS_NS2_DIALECT_IPACCESS) |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 878 | return NS2_CS_SKIPPED; |
Alexander Couzens | 05ac4e5 | 2021-01-04 19:07:05 +0100 | [diff] [blame] | 879 | |
Alexander Couzens | 2e1a3a9 | 2021-01-27 20:44:41 +0100 | [diff] [blame] | 880 | if (!bind->accept_ipaccess) |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 881 | return NS2_CS_SKIPPED; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 882 | |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 883 | nse = gprs_ns2_create_nse(bind->nsi, nsei, bind->ll, dialect); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 884 | if (!nse) { |
Alexander Couzens | 05ac4e5 | 2021-01-04 19:07:05 +0100 | [diff] [blame] | 885 | LOGP(DLNS, LOGL_ERROR, "Failed to create NSE(%05u)\n", nsei); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 886 | return NS2_CS_ERROR; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 887 | } |
Alexander Couzens | 05ac4e5 | 2021-01-04 19:07:05 +0100 | [diff] [blame] | 888 | } else { |
| 889 | /* nsei already known */ |
| 890 | if (nse->ll != bind->ll) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 891 | LOGNSE(nse, LOGL_ERROR, "Received NS-RESET NS-VCI(%05u) with wrong linklayer(%s)" |
| 892 | " for already known NSE(%s)\n", nsvci, gprs_ns2_lltype_str(bind->ll), |
| 893 | gprs_ns2_lltype_str(nse->ll)); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 894 | return NS2_CS_SKIPPED; |
Alexander Couzens | 05ac4e5 | 2021-01-04 19:07:05 +0100 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
| 898 | nsvc = gprs_ns2_nsvc_by_nsvci(bind->nsi, nsvci); |
| 899 | if (nsvc) { |
| 900 | if (nsvc->persistent) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 901 | LOGNSVC(nsvc, LOGL_ERROR, "Received NS-RESET for a persistent NSE over wrong connection.\n"); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 902 | return NS2_CS_SKIPPED; |
Alexander Couzens | 05ac4e5 | 2021-01-04 19:07:05 +0100 | [diff] [blame] | 903 | } |
| 904 | /* destroy old dynamic nsvc */ |
| 905 | gprs_ns2_free_nsvc(nsvc); |
| 906 | } |
| 907 | |
| 908 | /* do nse persistent check late to be more precise on the error message */ |
| 909 | if (nse->persistent) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 910 | LOGNSE(nse, LOGL_ERROR, "Received NS-RESET for a persistent NSE but the unknown " |
| 911 | "NS-VCI(%05u)\n", nsvci); |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 912 | return NS2_CS_SKIPPED; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 913 | } |
| 914 | |
Harald Welte | 603f404 | 2020-11-29 17:39:19 +0100 | [diff] [blame] | 915 | nsvci = tlvp_val16be(&tp, NS_IE_VCI); |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 916 | vc_mode = ns2_dialect_to_vc_mode(dialect); |
Harald Welte | 603f404 | 2020-11-29 17:39:19 +0100 | [diff] [blame] | 917 | snprintf(idbuf, sizeof(idbuf), "%s-NSE%05u-NSVC%05u", gprs_ns2_lltype_str(nse->ll), |
| 918 | nse->nsei, nsvci); |
| 919 | nsvc = ns2_vc_alloc(bind, nse, false, vc_mode, idbuf); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 920 | if (!nsvc) |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 921 | return NS2_CS_SKIPPED; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 922 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 923 | nsvc->nsvci = nsvci; |
| 924 | nsvc->nsvci_is_valid = true; |
| 925 | |
| 926 | *success = nsvc; |
| 927 | |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 928 | return NS2_CS_CREATED; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 929 | } |
| 930 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 931 | /*! Create, and connect an inactive, new IP-based NS-VC |
| 932 | * \param[in] bind bind in which the new NS-VC is to be created |
| 933 | * \param[in] remote remote address to which to connect |
| 934 | * \param[in] nse NS Entity in which the NS-VC is to be created |
| 935 | * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET |
| 936 | * \return pointer to newly-allocated, connected and inactive NS-VC; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 937 | struct gprs_ns2_vc *gprs_ns2_ip_connect_inactive(struct gprs_ns2_vc_bind *bind, |
Vadim Yanitskiy | a07f25e | 2020-10-09 21:47:01 +0700 | [diff] [blame] | 938 | const struct osmo_sockaddr *remote, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 939 | struct gprs_ns2_nse *nse, |
| 940 | uint16_t nsvci) |
| 941 | { |
| 942 | struct gprs_ns2_vc *nsvc; |
| 943 | |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 944 | nsvc = ns2_ip_bind_connect(bind, nse, remote); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 945 | if (!nsvc) |
| 946 | return NULL; |
| 947 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 948 | if (nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 949 | nsvc->nsvci = nsvci; |
| 950 | nsvc->nsvci_is_valid = true; |
| 951 | } |
| 952 | |
| 953 | return nsvc; |
| 954 | } |
| 955 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 956 | /*! Create, connect and activate a new IP-based NS-VC |
| 957 | * \param[in] bind bind in which the new NS-VC is to be created |
| 958 | * \param[in] remote remote address to which to connect |
| 959 | * \param[in] nse NS Entity in which the NS-VC is to be created |
| 960 | * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET |
| 961 | * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 962 | struct gprs_ns2_vc *gprs_ns2_ip_connect(struct gprs_ns2_vc_bind *bind, |
Vadim Yanitskiy | a07f25e | 2020-10-09 21:47:01 +0700 | [diff] [blame] | 963 | const struct osmo_sockaddr *remote, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 964 | struct gprs_ns2_nse *nse, |
| 965 | uint16_t nsvci) |
| 966 | { |
| 967 | struct gprs_ns2_vc *nsvc; |
| 968 | nsvc = gprs_ns2_ip_connect_inactive(bind, remote, nse, nsvci); |
| 969 | if (!nsvc) |
| 970 | return NULL; |
| 971 | |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 972 | ns2_vc_fsm_start(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 973 | |
| 974 | return nsvc; |
| 975 | } |
| 976 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 977 | /*! Create, connect and activate a new IP-based NS-VC |
| 978 | * \param[in] bind bind in which the new NS-VC is to be created |
| 979 | * \param[in] remote remote address to which to connect |
| 980 | * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created |
| 981 | * \param[in] nsvci is only required when bind->vc_mode == NS2_VC_MODE_BLOCKRESET |
| 982 | * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 983 | struct gprs_ns2_vc *gprs_ns2_ip_connect2(struct gprs_ns2_vc_bind *bind, |
Vadim Yanitskiy | a07f25e | 2020-10-09 21:47:01 +0700 | [diff] [blame] | 984 | const struct osmo_sockaddr *remote, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 985 | uint16_t nsei, |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 986 | uint16_t nsvci, |
| 987 | enum gprs_ns2_dialect dialect) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 988 | { |
| 989 | struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei); |
| 990 | |
| 991 | if (!nse) { |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 992 | nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_UDP, dialect); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 993 | if (!nse) |
| 994 | return NULL; |
| 995 | } |
| 996 | |
| 997 | return gprs_ns2_ip_connect(bind, remote, nse, nsvci); |
| 998 | } |
| 999 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1000 | /*! Find NS-VC for given socket address. |
| 1001 | * \param[in] nse NS Entity in which to search |
| 1002 | * \param[in] sockaddr socket address to search for |
| 1003 | * \return NS-VC matching sockaddr; NULL if none found */ |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 1004 | struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_nse(struct gprs_ns2_nse *nse, |
Vadim Yanitskiy | a07f25e | 2020-10-09 21:47:01 +0700 | [diff] [blame] | 1005 | const struct osmo_sockaddr *sockaddr) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1006 | { |
| 1007 | struct gprs_ns2_vc *nsvc; |
Alexander Couzens | 9a4cf27 | 2020-10-11 20:48:04 +0200 | [diff] [blame] | 1008 | const struct osmo_sockaddr *remote; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1009 | |
| 1010 | OSMO_ASSERT(nse); |
| 1011 | OSMO_ASSERT(sockaddr); |
| 1012 | |
| 1013 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
Alexander Couzens | c4229a4 | 2020-10-11 20:58:04 +0200 | [diff] [blame] | 1014 | remote = gprs_ns2_ip_vc_remote(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1015 | if (!osmo_sockaddr_cmp(sockaddr, remote)) |
| 1016 | return nsvc; |
| 1017 | } |
| 1018 | |
| 1019 | return NULL; |
| 1020 | } |
| 1021 | |
Alexander Couzens | 6cb5d5f | 2020-10-11 23:23:31 +0200 | [diff] [blame] | 1022 | /*! |
| 1023 | * Iterate over all nsvc of a NS Entity and call the callback. |
| 1024 | * If the callback returns < 0 it aborts the loop and returns the callback return code. |
| 1025 | * \param[in] nse NS Entity to iterate over all nsvcs |
| 1026 | * \param[in] cb the callback to call |
| 1027 | * \param[inout] cb_data the private data of the callback |
| 1028 | * \return 0 if the loop completes. If a callback returns < 0 it will returns this value. |
| 1029 | */ |
| 1030 | int gprs_ns2_nse_foreach_nsvc(struct gprs_ns2_nse *nse, gprs_ns2_foreach_nsvc_cb cb, void *cb_data) |
| 1031 | { |
| 1032 | struct gprs_ns2_vc *nsvc, *tmp; |
| 1033 | int rc = 0; |
| 1034 | llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) { |
| 1035 | rc = cb(nsvc, cb_data); |
| 1036 | if (rc < 0) |
| 1037 | return rc; |
| 1038 | } |
| 1039 | |
| 1040 | return 0; |
| 1041 | } |
| 1042 | |
| 1043 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1044 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1045 | /*! Bottom-side entry-point for received NS PDU from the driver/bind |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1046 | * \param[in] nsvc NS-VC for which the message was received |
| 1047 | * \param msg the received message. Ownership is trasnferred, caller must not free it! |
| 1048 | * \return 0 on success; negative on error */ |
Alexander Couzens | ffd49d0 | 2020-09-24 00:47:17 +0200 | [diff] [blame] | 1049 | int ns2_recv_vc(struct gprs_ns2_vc *nsvc, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1050 | struct msgb *msg) |
| 1051 | { |
| 1052 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
Alexander Couzens | ba63453 | 2021-01-25 13:46:38 +0100 | [diff] [blame] | 1053 | struct tlv_parsed tp = { }; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1054 | int rc = 0; |
| 1055 | |
Daniel Willmann | 751977b | 2020-12-02 18:59:44 +0100 | [diff] [blame] | 1056 | log_set_context(LOG_CTX_GB_NSE, nsvc->nse); |
| 1057 | log_set_context(LOG_CTX_GB_NSVC, nsvc); |
| 1058 | |
Harald Welte | e5f55f7 | 2021-01-30 21:51:15 +0100 | [diff] [blame] | 1059 | rate_ctr_inc(&nsvc->ctrg->ctr[NS_CTR_PKTS_IN]); |
| 1060 | rate_ctr_add(&nsvc->ctrg->ctr[NS_CTR_BYTES_IN], msg->len); |
| 1061 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1062 | if (msg->len < sizeof(struct gprs_ns_hdr)) |
| 1063 | return -EINVAL; |
| 1064 | |
| 1065 | switch (nsh->pdu_type) { |
| 1066 | case SNS_PDUT_CONFIG: |
| 1067 | /* one additional byte ('end flag') before the TLV part starts */ |
| 1068 | rc = ns2_tlv_parse(&tp, nsh->data+1, |
| 1069 | msgb_l2len(msg) - sizeof(*nsh)-1, 0, 0); |
| 1070 | if (rc < 0) { |
Alexander Couzens | bb0a53b | 2020-10-12 04:18:03 +0200 | [diff] [blame] | 1071 | LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1072 | return rc; |
| 1073 | } |
| 1074 | /* All sub-network service related message types */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1075 | rc = ns2_sns_rx(nsvc, msg, &tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1076 | break; |
| 1077 | case SNS_PDUT_ACK: |
| 1078 | case SNS_PDUT_ADD: |
| 1079 | case SNS_PDUT_CHANGE_WEIGHT: |
| 1080 | case SNS_PDUT_DELETE: |
| 1081 | /* weird layout: NSEI TLV, then value-only transaction IE, then TLV again */ |
Harald Welte | 36be9d8 | 2020-10-09 15:39:25 +0200 | [diff] [blame] | 1082 | rc = ns2_tlv_parse(&tp, nsh->data+5, |
| 1083 | msgb_l2len(msg) - sizeof(*nsh)-5, 0, 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1084 | if (rc < 0) { |
Alexander Couzens | bb0a53b | 2020-10-12 04:18:03 +0200 | [diff] [blame] | 1085 | LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1086 | return rc; |
| 1087 | } |
| 1088 | tp.lv[NS_IE_NSEI].val = nsh->data+2; |
| 1089 | tp.lv[NS_IE_NSEI].len = 2; |
| 1090 | tp.lv[NS_IE_TRANS_ID].val = nsh->data+4; |
| 1091 | tp.lv[NS_IE_TRANS_ID].len = 1; |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1092 | rc = ns2_sns_rx(nsvc, msg, &tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1093 | break; |
| 1094 | case SNS_PDUT_CONFIG_ACK: |
| 1095 | case SNS_PDUT_SIZE: |
| 1096 | case SNS_PDUT_SIZE_ACK: |
| 1097 | rc = ns2_tlv_parse(&tp, nsh->data, |
| 1098 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
| 1099 | if (rc < 0) { |
Alexander Couzens | bb0a53b | 2020-10-12 04:18:03 +0200 | [diff] [blame] | 1100 | LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse in %s\n", msgb_hexdump(msg)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1101 | return rc; |
| 1102 | } |
| 1103 | /* All sub-network service related message types */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1104 | rc = ns2_sns_rx(nsvc, msg, &tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1105 | break; |
| 1106 | |
| 1107 | case NS_PDUT_UNITDATA: |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1108 | rc = ns2_vc_rx(nsvc, msg, &tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1109 | break; |
| 1110 | default: |
| 1111 | rc = ns2_tlv_parse(&tp, nsh->data, |
| 1112 | msgb_l2len(msg) - sizeof(*nsh), 0, 0); |
| 1113 | if (rc < 0) { |
Alexander Couzens | bb0a53b | 2020-10-12 04:18:03 +0200 | [diff] [blame] | 1114 | LOGP(DLNS, LOGL_NOTICE, "Error during TLV Parse\n"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1115 | if (nsh->pdu_type != NS_PDUT_STATUS) |
| 1116 | ns2_tx_status(nsvc, NS_CAUSE_PROTO_ERR_UNSPEC, 0, msg); |
| 1117 | return rc; |
| 1118 | } |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1119 | rc = ns2_vc_rx(nsvc, msg, &tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1120 | break; |
| 1121 | } |
| 1122 | |
| 1123 | return rc; |
| 1124 | } |
| 1125 | |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 1126 | /* summarize all active data nsvcs */ |
| 1127 | void ns2_nse_data_sum(struct gprs_ns2_nse *nse) |
| 1128 | { |
| 1129 | struct gprs_ns2_vc *nsvc; |
Harald Welte | 3221f87 | 2021-01-18 14:58:51 +0100 | [diff] [blame] | 1130 | |
Alexander Couzens | 8a2a1a4 | 2020-12-26 18:00:17 +0100 | [diff] [blame] | 1131 | nse->nsvc_count = 0; |
Harald Welte | 3221f87 | 2021-01-18 14:58:51 +0100 | [diff] [blame] | 1132 | nse->sum_data_weight = 0; |
| 1133 | nse->sum_sig_weight = 0; |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 1134 | |
| 1135 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1136 | if (!ns2_vc_is_unblocked(nsvc)) |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 1137 | continue; |
Alexander Couzens | 8a2a1a4 | 2020-12-26 18:00:17 +0100 | [diff] [blame] | 1138 | |
| 1139 | nse->nsvc_count++; |
Harald Welte | 3221f87 | 2021-01-18 14:58:51 +0100 | [diff] [blame] | 1140 | nse->sum_data_weight += nsvc->data_weight; |
| 1141 | nse->sum_sig_weight += nsvc->sig_weight; |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1145 | /*! Notify a nse about the change of a NS-VC. |
| 1146 | * \param[in] nsvc NS-VC which has detected the change (and shall not be notified). |
| 1147 | * \param[in] unblocked whether the NSE should be marked as unblocked (true) or blocked (false) */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1148 | void ns2_nse_notify_unblocked(struct gprs_ns2_vc *nsvc, bool unblocked) |
| 1149 | { |
| 1150 | struct gprs_ns2_nse *nse = nsvc->nse; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1151 | |
Alexander Couzens | fc3dd1f | 2020-11-19 00:41:47 +0100 | [diff] [blame] | 1152 | ns2_nse_data_sum(nse); |
| 1153 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1154 | if (unblocked == nse->alive) |
| 1155 | return; |
| 1156 | |
Harald Welte | 3221f87 | 2021-01-18 14:58:51 +0100 | [diff] [blame] | 1157 | /* wait until both data_weight and sig_weight are != 0 before declaring NSE as alive */ |
| 1158 | if (unblocked && nse->sum_data_weight && nse->sum_sig_weight) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1159 | nse->alive = true; |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1160 | ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_RECOVERY); |
Alexander Couzens | da0a285 | 2020-10-01 23:24:07 +0200 | [diff] [blame] | 1161 | nse->first = false; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1162 | return; |
| 1163 | } |
| 1164 | |
Harald Welte | 3221f87 | 2021-01-18 14:58:51 +0100 | [diff] [blame] | 1165 | if (nse->alive && (nse->sum_data_weight == 0 || nse->sum_sig_weight == 0)) { |
| 1166 | /* nse became unavailable */ |
| 1167 | nse->alive = false; |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1168 | ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_FAILURE); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1169 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | /*! Create a new GPRS NS instance |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1173 | * \param[in] ctx a talloc context to allocate NS instance from |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 1174 | * \param[in] cb Call-back function for dispatching primitives to the user. The Call-back must free all msgb* given in the primitive. |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1175 | * \param[in] cb_data transparent user data passed to Call-back |
| 1176 | * \returns dynamically allocated gprs_ns_inst; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1177 | struct gprs_ns2_inst *gprs_ns2_instantiate(void *ctx, osmo_prim_cb cb, void *cb_data) |
| 1178 | { |
| 1179 | struct gprs_ns2_inst *nsi; |
| 1180 | |
| 1181 | nsi = talloc_zero(ctx, struct gprs_ns2_inst); |
| 1182 | if (!nsi) |
| 1183 | return NULL; |
| 1184 | |
| 1185 | nsi->cb = cb; |
| 1186 | nsi->cb_data = cb_data; |
| 1187 | INIT_LLIST_HEAD(&nsi->binding); |
| 1188 | INIT_LLIST_HEAD(&nsi->nse); |
| 1189 | |
| 1190 | nsi->timeout[NS_TOUT_TNS_BLOCK] = 3; |
| 1191 | nsi->timeout[NS_TOUT_TNS_BLOCK_RETRIES] = 3; |
| 1192 | nsi->timeout[NS_TOUT_TNS_RESET] = 3; |
| 1193 | nsi->timeout[NS_TOUT_TNS_RESET_RETRIES] = 3; |
| 1194 | nsi->timeout[NS_TOUT_TNS_TEST] = 30; |
| 1195 | nsi->timeout[NS_TOUT_TNS_ALIVE] = 3; |
| 1196 | nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES] = 10; |
| 1197 | nsi->timeout[NS_TOUT_TSNS_PROV] = 3; /* 1..10 */ |
Alexander Couzens | 90ee963 | 2020-12-07 06:18:32 +0100 | [diff] [blame] | 1198 | nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES] = 3; |
| 1199 | nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES] = 3; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1200 | |
| 1201 | return nsi; |
| 1202 | } |
| 1203 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1204 | /*! Destroy a NS Instance (including all its NSEs, binds, ...). |
| 1205 | * \param[in] nsi NS instance to destroy */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1206 | void gprs_ns2_free(struct gprs_ns2_inst *nsi) |
| 1207 | { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1208 | if (!nsi) |
| 1209 | return; |
| 1210 | |
Alexander Couzens | 4b6c8af | 2020-10-11 20:15:25 +0200 | [diff] [blame] | 1211 | gprs_ns2_free_nses(nsi); |
Alexander Couzens | 896fcd5 | 2020-10-11 19:52:36 +0200 | [diff] [blame] | 1212 | gprs_ns2_free_binds(nsi); |
Alexander Couzens | 3531504 | 2020-10-10 03:28:17 +0200 | [diff] [blame] | 1213 | |
| 1214 | talloc_free(nsi); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1215 | } |
| 1216 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1217 | /*! Start the NS-ALIVE FSM in all NS-VCs of given NSE. |
| 1218 | * \param[in] nse NS Entity in whihc to start NS-ALIVE FSMs */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1219 | void gprs_ns2_start_alive_all_nsvcs(struct gprs_ns2_nse *nse) |
| 1220 | { |
| 1221 | struct gprs_ns2_vc *nsvc; |
| 1222 | OSMO_ASSERT(nse); |
| 1223 | |
| 1224 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
| 1225 | if (nsvc->sns_only) |
| 1226 | continue; |
| 1227 | |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1228 | ns2_vc_fsm_start(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1232 | /*! Destroy a given bind. |
| 1233 | * \param[in] bind the bind we want to destroy */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1234 | void gprs_ns2_free_bind(struct gprs_ns2_vc_bind *bind) |
| 1235 | { |
| 1236 | struct gprs_ns2_vc *nsvc, *tmp; |
| 1237 | if (!bind) |
| 1238 | return; |
| 1239 | |
| 1240 | llist_for_each_entry_safe(nsvc, tmp, &bind->nsvc, blist) { |
| 1241 | gprs_ns2_free_nsvc(nsvc); |
| 1242 | } |
| 1243 | |
| 1244 | if (bind->driver->free_bind) |
| 1245 | bind->driver->free_bind(bind); |
| 1246 | |
| 1247 | llist_del(&bind->list); |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 1248 | talloc_free((char *)bind->name); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1249 | talloc_free(bind); |
| 1250 | } |
Alexander Couzens | 896fcd5 | 2020-10-11 19:52:36 +0200 | [diff] [blame] | 1251 | |
| 1252 | void gprs_ns2_free_binds(struct gprs_ns2_inst *nsi) |
| 1253 | { |
| 1254 | struct gprs_ns2_vc_bind *bind, *tbind; |
| 1255 | |
| 1256 | llist_for_each_entry_safe(bind, tbind, &nsi->binding, list) { |
| 1257 | gprs_ns2_free_bind(bind); |
| 1258 | } |
| 1259 | } |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 1260 | |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 1261 | /*! Search for a bind with a unique name |
| 1262 | * \param[in] nsi NS instance on which we operate |
| 1263 | * \param[in] name The unique bind name to search for |
| 1264 | * \return the bind or NULL if not found |
| 1265 | */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1266 | struct gprs_ns2_vc_bind *gprs_ns2_bind_by_name(struct gprs_ns2_inst *nsi, const char *name) |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 1267 | { |
| 1268 | struct gprs_ns2_vc_bind *bind; |
| 1269 | |
| 1270 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 1271 | if (!strcmp(bind->name, name)) |
| 1272 | return bind; |
| 1273 | } |
| 1274 | |
| 1275 | return NULL; |
| 1276 | } |
| 1277 | |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1278 | enum gprs_ns2_vc_mode ns2_dialect_to_vc_mode(enum gprs_ns2_dialect dialect) |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 1279 | { |
| 1280 | switch (dialect) { |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1281 | case GPRS_NS2_DIALECT_SNS: |
| 1282 | case GPRS_NS2_DIALECT_STATIC_ALIVE: |
| 1283 | return GPRS_NS2_VC_MODE_ALIVE; |
| 1284 | case GPRS_NS2_DIALECT_STATIC_RESETBLOCK: |
| 1285 | case GPRS_NS2_DIALECT_IPACCESS: |
| 1286 | return GPRS_NS2_VC_MODE_BLOCKRESET; |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 1287 | default: |
| 1288 | return -1; |
| 1289 | } |
| 1290 | } |
| 1291 | |
Alexander Couzens | 1c8785d | 2020-12-17 06:58:53 +0100 | [diff] [blame] | 1292 | static void add_bind_array(struct gprs_ns2_vc_bind **array, |
| 1293 | struct gprs_ns2_vc_bind *bind, int size) |
| 1294 | { |
| 1295 | int i; |
| 1296 | for (i=0; i < size; i++) { |
| 1297 | if (array[i] == bind) |
| 1298 | return; |
| 1299 | if (!array[i]) |
| 1300 | break; |
| 1301 | } |
| 1302 | |
| 1303 | if (i == size) |
| 1304 | return; |
| 1305 | |
| 1306 | array[i] = bind; |
| 1307 | } |
| 1308 | |
| 1309 | /*! calculate the transfer capabilities for a nse |
| 1310 | * \param nse the nse to count the transfer capability |
| 1311 | * \param bvci a bvci - unused |
| 1312 | * \return the transfer capability in mbit. On error < 0. |
| 1313 | */ |
| 1314 | int ns2_count_transfer_cap(struct gprs_ns2_nse *nse, |
| 1315 | uint16_t bvci) |
| 1316 | { |
| 1317 | struct gprs_ns2_vc *nsvc; |
| 1318 | struct gprs_ns2_vc_bind **active_binds; |
| 1319 | int i, active_nsvcs = 0, transfer_cap = 0; |
| 1320 | |
| 1321 | /* calculate the transfer capabilities based on the binds. |
| 1322 | * A bind has a transfer capability which is shared across all NSVCs. |
| 1323 | * Take care the bind cap is not counted twice within a NSE. |
| 1324 | * This should be accurate for FR and UDP but not for FR/GRE. */ |
| 1325 | |
| 1326 | if (!nse->alive) |
| 1327 | return 0; |
| 1328 | |
| 1329 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1330 | if (ns2_vc_is_unblocked(nsvc)) |
Alexander Couzens | 1c8785d | 2020-12-17 06:58:53 +0100 | [diff] [blame] | 1331 | active_nsvcs++; |
| 1332 | } |
| 1333 | /* an alive nse should always have active_nsvcs */ |
| 1334 | OSMO_ASSERT(active_nsvcs); |
| 1335 | |
| 1336 | active_binds = talloc_zero_array(nse, struct gprs_ns2_vc_bind*, active_nsvcs); |
| 1337 | if (!active_binds) |
| 1338 | return -ENOMEM; |
| 1339 | |
| 1340 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1341 | if (!ns2_vc_is_unblocked(nsvc)) |
Alexander Couzens | 1c8785d | 2020-12-17 06:58:53 +0100 | [diff] [blame] | 1342 | continue; |
| 1343 | add_bind_array(active_binds, nsvc->bind, active_nsvcs); |
| 1344 | } |
| 1345 | |
| 1346 | /* TODO: change calcuation for FR/GRE */ |
| 1347 | for (i = 0; i < active_nsvcs; i++) { |
| 1348 | if (active_binds[i]) |
| 1349 | transfer_cap += active_binds[i]->transfer_capability; |
| 1350 | } |
| 1351 | |
| 1352 | talloc_free(active_binds); |
| 1353 | return transfer_cap; |
| 1354 | } |
| 1355 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1356 | /*! @} */ |