Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1 | /*! \file gprs_ns2_sns.c |
| 2 | * NS Sub-Network Service Protocol implementation |
| 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 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 6 | /* (C) 2018-2021 by Harald Welte <laforge@gnumonks.org> |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 7 | * (C) 2020 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 8 | * Author: Alexander Couzens <lynxis@fe80.eu> |
| 9 | * |
| 10 | * All Rights Reserved |
| 11 | * |
| 12 | * SPDX-License-Identifier: GPL-2.0+ |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | /* The BSS NSE only has one SGSN IP address configured, and it will use the SNS procedures |
| 30 | * to communicated its local IPs/ports as well as all the SGSN side IPs/ports and |
| 31 | * associated weights. The BSS then uses this to establish a full mesh |
| 32 | * of NSVCs between all BSS-side IPs/ports and SGSN-side IPs/ports. |
| 33 | * |
| 34 | * Known limitation/expectation/bugs: |
| 35 | * - No concurrent dual stack. It supports either IPv4 or IPv6, but not both at the same time. |
| 36 | * - SNS Add/Change/Delete: Doesn't answer on the same NSVC as received SNS ADD/CHANGE/DELETE PDUs. |
| 37 | * - SNS Add/Change/Delete: Doesn't communicated the failed IPv4/IPv6 entries on the SNS_ACK. |
| 38 | */ |
| 39 | |
| 40 | #include <errno.h> |
| 41 | |
| 42 | #include <netinet/in.h> |
| 43 | #include <arpa/inet.h> |
| 44 | #include <stdint.h> |
| 45 | |
| 46 | #include <osmocom/core/fsm.h> |
| 47 | #include <osmocom/core/msgb.h> |
| 48 | #include <osmocom/core/socket.h> |
Alexander Couzens | 412bc34 | 2020-11-19 05:24:37 +0100 | [diff] [blame] | 49 | #include <osmocom/core/sockaddr_str.h> |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 50 | #include <osmocom/gsm/tlv.h> |
| 51 | #include <osmocom/gprs/gprs_msgb.h> |
| 52 | #include <osmocom/gprs/gprs_ns2.h> |
| 53 | #include <osmocom/gprs/protocol/gsm_08_16.h> |
| 54 | |
| 55 | #include "gprs_ns2_internal.h" |
| 56 | |
| 57 | #define S(x) (1 << (x)) |
| 58 | |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 59 | enum ns2_sns_role { |
| 60 | GPRS_SNS_ROLE_BSS, |
| 61 | GPRS_SNS_ROLE_SGSN, |
| 62 | }; |
| 63 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 64 | /* BSS-side-only states _ST_BSS_; SGSN-side only states _ST_SGSN_; others shared */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 65 | enum gprs_sns_bss_state { |
| 66 | GPRS_SNS_ST_UNCONFIGURED, |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 67 | GPRS_SNS_ST_BSS_SIZE, /*!< SNS-SIZE procedure ongoing */ |
| 68 | GPRS_SNS_ST_BSS_CONFIG_BSS, /*!< SNS-CONFIG procedure (BSS->SGSN) ongoing */ |
| 69 | GPRS_SNS_ST_BSS_CONFIG_SGSN, /*!< SNS-CONFIG procedure (SGSN->BSS) ongoing */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 70 | GPRS_SNS_ST_CONFIGURED, |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 71 | GPRS_SNS_ST_SGSN_WAIT_CONFIG, /* !< SGSN role: Wait for CONFIG from BSS */ |
| 72 | GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, /* !< SGSN role: Wait for CONFIG-ACK from BSS */ |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 73 | GPRS_SNS_ST_LOCAL_PROCEDURE, /*!< in process of a ADD/DEL/CHANGE procedure towards SGSN (BSS->SGSN) */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 74 | }; |
| 75 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 76 | static const struct value_string gprs_sns_event_names[] = { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 77 | { NS2_SNS_EV_REQ_SELECT_ENDPOINT, "REQ_SELECT_ENDPOINT" }, |
| 78 | { NS2_SNS_EV_RX_SIZE, "RX_SIZE" }, |
| 79 | { NS2_SNS_EV_RX_SIZE_ACK, "RX_SIZE_ACK" }, |
| 80 | { NS2_SNS_EV_RX_CONFIG, "RX_CONFIG" }, |
| 81 | { NS2_SNS_EV_RX_CONFIG_END, "RX_CONFIG_END" }, |
| 82 | { NS2_SNS_EV_RX_CONFIG_ACK, "RX_CONFIG_ACK" }, |
| 83 | { NS2_SNS_EV_RX_ADD, "RX_ADD" }, |
| 84 | { NS2_SNS_EV_RX_DELETE, "RX_DELETE" }, |
| 85 | { NS2_SNS_EV_RX_ACK, "RX_ACK" }, |
| 86 | { NS2_SNS_EV_RX_CHANGE_WEIGHT, "RX_CHANGE_WEIGHT" }, |
| 87 | { NS2_SNS_EV_REQ_NO_NSVC, "REQ_NO_NSVC" }, |
Alexander Couzens | 83f06ce | 2021-08-06 19:50:09 +0200 | [diff] [blame] | 88 | { NS2_SNS_EV_REQ_FREE_NSVCS, "REQ_FREE_NSVCS" }, |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 89 | { NS2_SNS_EV_REQ_NSVC_ALIVE, "REQ_NSVC_ALIVE"}, |
| 90 | { NS2_SNS_EV_REQ_ADD_BIND, "REQ_ADD_BIND"}, |
| 91 | { NS2_SNS_EV_REQ_DELETE_BIND, "REQ_DELETE_BIND"}, |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 92 | { NS2_SNS_EV_REQ_CHANGE_WEIGHT, "REQ_CHANGE_WEIGHT"}, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 93 | { 0, NULL } |
| 94 | }; |
| 95 | |
Alexander Couzens | 9cd39ac | 2021-06-06 18:57:56 +0200 | [diff] [blame^] | 96 | #define GPRS_SNS_FLAG_KEEP_SELECT_ENDPOINT_ORDER (void *) 1 |
| 97 | |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 98 | enum sns_procedure { |
| 99 | SNS_PROC_NONE, /*!< used as invalid/idle value */ |
| 100 | SNS_PROC_ADD, |
| 101 | SNS_PROC_DEL, |
| 102 | SNS_PROC_CHANGE_WEIGHT, |
| 103 | }; |
| 104 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 105 | struct sns_endpoint { |
| 106 | struct llist_head list; |
| 107 | struct osmo_sockaddr saddr; |
| 108 | }; |
| 109 | |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 110 | struct ns2_sns_bind { |
| 111 | struct llist_head list; |
| 112 | struct gprs_ns2_vc_bind *bind; |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 113 | uint8_t change_weight_state; |
| 114 | }; |
| 115 | |
| 116 | struct ns2_sns_procedure { |
| 117 | struct llist_head list; |
| 118 | struct ns2_sns_bind *sbind; |
| 119 | uint16_t sig_weight; |
| 120 | uint16_t data_weight; |
| 121 | /* copy entry to protect against changes of gss->local */ |
| 122 | struct gprs_ns_ie_ip4_elem ip4; |
| 123 | struct gprs_ns_ie_ip6_elem ip6; |
| 124 | enum sns_procedure procedure; |
| 125 | uint8_t trans_id; |
| 126 | /* is the procedure in process */ |
| 127 | bool running; |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 128 | }; |
| 129 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 130 | struct ns2_sns_elems { |
| 131 | struct gprs_ns_ie_ip4_elem *ip4; |
| 132 | unsigned int num_ip4; |
| 133 | struct gprs_ns_ie_ip6_elem *ip6; |
| 134 | unsigned int num_ip6; |
| 135 | }; |
| 136 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 137 | struct ns2_sns_state { |
| 138 | struct gprs_ns2_nse *nse; |
| 139 | |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 140 | /* containing the address family AF_* */ |
| 141 | int family; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 142 | enum ns2_sns_role role; /* local role: BSS or SGSN */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 143 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 144 | /* holds the list of initial SNS endpoints */ |
| 145 | struct llist_head sns_endpoints; |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 146 | /* list of used struct ns2_sns_bind */ |
| 147 | struct llist_head binds; |
| 148 | /* pointer to the bind which was used to initiate the SNS connection */ |
| 149 | struct ns2_sns_bind *initial_bind; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 150 | /* prevent recursive reselection */ |
| 151 | bool reselection_running; |
| 152 | |
| 153 | /* The current initial SNS endpoints. |
| 154 | * The initial connection will be moved into the NSE |
| 155 | * if configured via SNS. Otherwise it will be removed |
| 156 | * in configured state. */ |
| 157 | struct sns_endpoint *initial; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 158 | /* all SNS PDU will be sent over this nsvc */ |
| 159 | struct gprs_ns2_vc *sns_nsvc; |
Alexander Couzens | 90ee963 | 2020-12-07 06:18:32 +0100 | [diff] [blame] | 160 | /* timer N */ |
| 161 | int N; |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 162 | /* true if at least one nsvc is alive */ |
| 163 | bool alive; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 164 | |
| 165 | /* local configuration to send to the remote end */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 166 | struct ns2_sns_elems local; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 167 | |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 168 | /* local configuration after all local procedures applied */ |
| 169 | struct ns2_sns_elems local_procedure; |
| 170 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 171 | /* remote configuration as received */ |
| 172 | struct ns2_sns_elems remote; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 173 | |
| 174 | /* local configuration about our capabilities in terms of connections to |
| 175 | * remote (SGSN) side */ |
| 176 | size_t num_max_nsvcs; |
| 177 | size_t num_max_ip4_remote; |
| 178 | size_t num_max_ip6_remote; |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 179 | |
| 180 | struct llist_head procedures; |
| 181 | struct ns2_sns_procedure *current_procedure; |
| 182 | uint8_t trans_id; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | static inline struct gprs_ns2_nse *nse_inst_from_fi(struct osmo_fsm_inst *fi) |
| 186 | { |
| 187 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 188 | return gss->nse; |
| 189 | } |
| 190 | |
Alexander Couzens | 652ab4d | 2021-06-12 23:09:46 +0200 | [diff] [blame] | 191 | /* The SNS has failed. Etither restart the SNS (BSS) or remove the SNS (SGSN) */ |
| 192 | #define sns_failed(fi, reason) \ |
| 193 | _sns_failed(fi, reason, __FILE__, __LINE__) |
| 194 | static void _sns_failed(struct osmo_fsm_inst *fi, const char *reason, const char *file, int line) |
| 195 | { |
| 196 | struct ns2_sns_state *gss = fi->priv; |
| 197 | |
| 198 | if (reason) |
| 199 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: SNS failed: %s\n", gss->nse->nsei, reason); |
| 200 | |
| 201 | if (gss->role == GPRS_SNS_ROLE_SGSN) { |
| 202 | if (!gss->nse->persistent) |
| 203 | gprs_ns2_free_nse(gss->nse); |
| 204 | else |
| 205 | _osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0, file, line); |
| 206 | } else { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 207 | _osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_REQ_SELECT_ENDPOINT, NULL, file, line); |
Alexander Couzens | 652ab4d | 2021-06-12 23:09:46 +0200 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 211 | /* helper function to compute the sum of all (data or signaling) weights */ |
Alexander Couzens | 6231055 | 2021-06-06 02:43:14 +0200 | [diff] [blame] | 212 | static int ip4_weight_sum(const struct ns2_sns_elems *elems, bool data_weight) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 213 | { |
| 214 | unsigned int i; |
| 215 | int weight_sum = 0; |
| 216 | |
Alexander Couzens | 6231055 | 2021-06-06 02:43:14 +0200 | [diff] [blame] | 217 | for (i = 0; i < elems->num_ip4; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 218 | if (data_weight) |
Alexander Couzens | 6231055 | 2021-06-06 02:43:14 +0200 | [diff] [blame] | 219 | weight_sum += elems->ip4[i].data_weight; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 220 | else |
Alexander Couzens | 6231055 | 2021-06-06 02:43:14 +0200 | [diff] [blame] | 221 | weight_sum += elems->ip4[i].sig_weight; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 222 | } |
| 223 | return weight_sum; |
| 224 | } |
Alexander Couzens | 6231055 | 2021-06-06 02:43:14 +0200 | [diff] [blame] | 225 | #define ip4_weight_sum_data(elems) ip4_weight_sum(elems, true) |
| 226 | #define ip4_weight_sum_sig(elems) ip4_weight_sum(elems, false) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 227 | |
| 228 | /* helper function to compute the sum of all (data or signaling) weights */ |
Alexander Couzens | 6231055 | 2021-06-06 02:43:14 +0200 | [diff] [blame] | 229 | static int ip6_weight_sum(const struct ns2_sns_elems *elems, bool data_weight) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 230 | { |
| 231 | unsigned int i; |
| 232 | int weight_sum = 0; |
| 233 | |
Alexander Couzens | 6231055 | 2021-06-06 02:43:14 +0200 | [diff] [blame] | 234 | for (i = 0; i < elems->num_ip6; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 235 | if (data_weight) |
Alexander Couzens | 6231055 | 2021-06-06 02:43:14 +0200 | [diff] [blame] | 236 | weight_sum += elems->ip6[i].data_weight; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 237 | else |
Alexander Couzens | 6231055 | 2021-06-06 02:43:14 +0200 | [diff] [blame] | 238 | weight_sum += elems->ip6[i].sig_weight; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 239 | } |
| 240 | return weight_sum; |
| 241 | } |
Alexander Couzens | 6231055 | 2021-06-06 02:43:14 +0200 | [diff] [blame] | 242 | #define ip6_weight_sum_data(elems) ip6_weight_sum(elems, true) |
| 243 | #define ip6_weight_sum_sig(elems) ip6_weight_sum(elems, false) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 244 | |
Alexander Couzens | 019da4b | 2021-06-06 02:48:18 +0200 | [diff] [blame] | 245 | static int ip46_weight_sum(const struct ns2_sns_elems *elems, bool data_weight) |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 246 | { |
Alexander Couzens | 019da4b | 2021-06-06 02:48:18 +0200 | [diff] [blame] | 247 | return ip4_weight_sum(elems, data_weight) + |
| 248 | ip6_weight_sum(elems, data_weight); |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 249 | } |
Alexander Couzens | 019da4b | 2021-06-06 02:48:18 +0200 | [diff] [blame] | 250 | #define ip46_weight_sum_data(elems) ip46_weight_sum(elems, true) |
| 251 | #define ip46_weight_sum_sig(elems) ip46_weight_sum(elems, false) |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 252 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 253 | static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse, |
| 254 | const struct gprs_ns_ie_ip4_elem *ip4) |
| 255 | { |
| 256 | struct osmo_sockaddr sa; |
| 257 | /* copy over. Both data structures use network byte order */ |
| 258 | sa.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 259 | sa.u.sin.sin_port = ip4->udp_port; |
| 260 | sa.u.sin.sin_family = AF_INET; |
| 261 | |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 262 | return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse, |
| 266 | const struct gprs_ns_ie_ip6_elem *ip6) |
| 267 | { |
| 268 | struct osmo_sockaddr sa; |
| 269 | /* copy over. Both data structures use network byte order */ |
| 270 | sa.u.sin6.sin6_addr = ip6->ip_addr; |
| 271 | sa.u.sin6.sin6_port = ip6->udp_port; |
| 272 | sa.u.sin6.sin6_family = AF_INET; |
| 273 | |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 274 | return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 275 | } |
| 276 | |
Alexander Couzens | 125298f | 2020-10-11 21:22:42 +0200 | [diff] [blame] | 277 | /*! Return the initial SNS remote socket address |
| 278 | * \param nse NS Entity |
| 279 | * \return address of the initial SNS connection; NULL in case of error |
| 280 | */ |
| 281 | const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse) |
| 282 | { |
| 283 | struct ns2_sns_state *gss; |
| 284 | |
| 285 | if (!nse->bss_sns_fi) |
| 286 | return NULL; |
| 287 | |
| 288 | gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 289 | return &gss->initial->saddr; |
Alexander Couzens | 125298f | 2020-10-11 21:22:42 +0200 | [diff] [blame] | 290 | } |
| 291 | |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 292 | /*! called when a nsvc is beeing freed or the nsvc became dead */ |
| 293 | void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 294 | { |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 295 | struct gprs_ns2_nse *nse = nsvc->nse; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 296 | struct gprs_ns2_vc *tmp; |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 297 | struct osmo_fsm_inst *fi = nse->bss_sns_fi; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 298 | struct ns2_sns_state *gss; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 299 | |
| 300 | if (!fi) |
| 301 | return; |
| 302 | |
| 303 | gss = (struct ns2_sns_state *) fi->priv; |
| 304 | if (nsvc != gss->sns_nsvc) |
| 305 | return; |
| 306 | |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 307 | gss->sns_nsvc = NULL; |
| 308 | if (gss->alive) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 309 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 310 | if (ns2_vc_is_unblocked(tmp)) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 311 | gss->sns_nsvc = tmp; |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 312 | return; |
| 313 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 314 | } |
| 315 | } else { |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 316 | /* the SNS is waiting for its first NS-VC to come up |
| 317 | * choose any other nsvc */ |
| 318 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
| 319 | if (nsvc != tmp) { |
| 320 | gss->sns_nsvc = tmp; |
| 321 | return; |
| 322 | } |
| 323 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 324 | } |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 325 | |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 326 | osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_REQ_NO_NSVC, NULL); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 327 | } |
| 328 | |
Alexander Couzens | d2c6c49 | 2021-06-06 01:57:52 +0200 | [diff] [blame] | 329 | static void ns2_clear_elems(struct ns2_sns_elems *elems) |
Alexander Couzens | 7a7b20b | 2021-01-18 10:47:33 +0100 | [diff] [blame] | 330 | { |
Alexander Couzens | d2c6c49 | 2021-06-06 01:57:52 +0200 | [diff] [blame] | 331 | TALLOC_FREE(elems->ip4); |
| 332 | TALLOC_FREE(elems->ip6); |
Alexander Couzens | 7a7b20b | 2021-01-18 10:47:33 +0100 | [diff] [blame] | 333 | |
Alexander Couzens | d2c6c49 | 2021-06-06 01:57:52 +0200 | [diff] [blame] | 334 | elems->num_ip4 = 0; |
| 335 | elems->num_ip6 = 0; |
Alexander Couzens | 7a7b20b | 2021-01-18 10:47:33 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 338 | static void ns2_clear_procedures(struct ns2_sns_state *gss) |
| 339 | { |
| 340 | struct ns2_sns_procedure *procedure, *tmp; |
| 341 | gss->current_procedure = NULL; |
| 342 | llist_for_each_entry_safe(procedure, tmp, &gss->procedures, list) { |
| 343 | llist_del(&procedure->list); |
| 344 | talloc_free(procedure); |
| 345 | } |
| 346 | } |
| 347 | |
Daniel Willmann | cf8371a | 2021-01-16 15:13:51 +0100 | [diff] [blame] | 348 | static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote, |
| 349 | uint8_t sig_weight, uint8_t data_weight) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 350 | { |
| 351 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 352 | struct gprs_ns2_vc *nsvc; |
| 353 | struct gprs_ns2_vc_bind *bind; |
Daniel Willmann | cf8371a | 2021-01-16 15:13:51 +0100 | [diff] [blame] | 354 | |
| 355 | /* for every bind, create a connection if bind type == IP */ |
| 356 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 357 | if (bind->ll != GPRS_NS2_LL_UDP) |
| 358 | continue; |
| 359 | /* ignore failed connection */ |
| 360 | nsvc = gprs_ns2_ip_connect_inactive(bind, |
| 361 | remote, |
| 362 | nse, 0); |
| 363 | if (!nsvc) { |
| 364 | LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n"); |
| 365 | continue; |
| 366 | } |
| 367 | |
| 368 | nsvc->sig_weight = sig_weight; |
| 369 | nsvc->data_weight = data_weight; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi, |
| 374 | struct gprs_ns2_nse *nse, |
| 375 | const struct gprs_ns_ie_ip4_elem *ip4) |
| 376 | { |
Alexander Couzens | c068d86 | 2020-10-12 04:11:51 +0200 | [diff] [blame] | 377 | struct osmo_sockaddr remote = { }; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 378 | /* copy over. Both data structures use network byte order */ |
| 379 | remote.u.sin.sin_family = AF_INET; |
| 380 | remote.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 381 | remote.u.sin.sin_port = ip4->udp_port; |
| 382 | |
Daniel Willmann | cf8371a | 2021-01-16 15:13:51 +0100 | [diff] [blame] | 383 | ns2_vc_create_ip(fi, nse, &remote, ip4->sig_weight, ip4->data_weight); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi, |
| 387 | struct gprs_ns2_nse *nse, |
| 388 | const struct gprs_ns_ie_ip6_elem *ip6) |
| 389 | { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 390 | struct osmo_sockaddr remote = {}; |
| 391 | /* copy over. Both data structures use network byte order */ |
| 392 | remote.u.sin6.sin6_family = AF_INET6; |
| 393 | remote.u.sin6.sin6_addr = ip6->ip_addr; |
| 394 | remote.u.sin6.sin6_port = ip6->udp_port; |
| 395 | |
Daniel Willmann | cf8371a | 2021-01-16 15:13:51 +0100 | [diff] [blame] | 396 | ns2_vc_create_ip(fi, nse, &remote, ip6->sig_weight, ip6->data_weight); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 397 | } |
| 398 | |
Harald Welte | e8c6106 | 2021-03-24 13:16:27 +0100 | [diff] [blame] | 399 | static struct gprs_ns2_vc *nsvc_for_bind_and_remote(struct gprs_ns2_nse *nse, |
| 400 | struct gprs_ns2_vc_bind *bind, |
| 401 | const struct osmo_sockaddr *remote) |
| 402 | { |
| 403 | struct gprs_ns2_vc *nsvc; |
| 404 | |
| 405 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
| 406 | if (nsvc->bind != bind) |
| 407 | continue; |
| 408 | |
| 409 | if (!osmo_sockaddr_cmp(remote, gprs_ns2_ip_vc_remote(nsvc))) |
| 410 | return nsvc; |
| 411 | } |
| 412 | return NULL; |
| 413 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 414 | |
| 415 | static int create_missing_nsvcs(struct osmo_fsm_inst *fi) |
| 416 | { |
| 417 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 418 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 419 | struct gprs_ns2_vc *nsvc; |
Harald Welte | 3053bbb | 2021-03-24 13:22:18 +0100 | [diff] [blame] | 420 | struct ns2_sns_bind *sbind; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 421 | struct osmo_sockaddr remote = { }; |
| 422 | unsigned int i; |
| 423 | |
Harald Welte | e8c6106 | 2021-03-24 13:16:27 +0100 | [diff] [blame] | 424 | /* iterate over all remote IPv4 endpoints */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 425 | for (i = 0; i < gss->remote.num_ip4; i++) { |
| 426 | const struct gprs_ns_ie_ip4_elem *ip4 = &gss->remote.ip4[i]; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 427 | |
| 428 | remote.u.sin.sin_family = AF_INET; |
| 429 | remote.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 430 | remote.u.sin.sin_port = ip4->udp_port; |
| 431 | |
Harald Welte | 3053bbb | 2021-03-24 13:22:18 +0100 | [diff] [blame] | 432 | /* iterate over all local binds within this SNS */ |
| 433 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 434 | struct gprs_ns2_vc_bind *bind = sbind->bind; |
| 435 | |
Harald Welte | e8c6106 | 2021-03-24 13:16:27 +0100 | [diff] [blame] | 436 | /* we only care about UDP binds */ |
Daniel Willmann | 967e2c1 | 2021-01-14 16:58:17 +0100 | [diff] [blame] | 437 | if (bind->ll != GPRS_NS2_LL_UDP) |
| 438 | continue; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 439 | |
Harald Welte | e8c6106 | 2021-03-24 13:16:27 +0100 | [diff] [blame] | 440 | nsvc = nsvc_for_bind_and_remote(nse, bind, &remote); |
| 441 | if (!nsvc) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 442 | nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0); |
| 443 | if (!nsvc) { |
| 444 | /* TODO: add to a list to send back a NS-STATUS */ |
| 445 | continue; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /* update data / signalling weight */ |
| 450 | nsvc->data_weight = ip4->data_weight; |
| 451 | nsvc->sig_weight = ip4->sig_weight; |
| 452 | nsvc->sns_only = false; |
| 453 | } |
| 454 | } |
| 455 | |
Harald Welte | e8c6106 | 2021-03-24 13:16:27 +0100 | [diff] [blame] | 456 | /* iterate over all remote IPv4 endpoints */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 457 | for (i = 0; i < gss->remote.num_ip6; i++) { |
| 458 | const struct gprs_ns_ie_ip6_elem *ip6 = &gss->remote.ip6[i]; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 459 | |
| 460 | remote.u.sin6.sin6_family = AF_INET6; |
| 461 | remote.u.sin6.sin6_addr = ip6->ip_addr; |
| 462 | remote.u.sin6.sin6_port = ip6->udp_port; |
| 463 | |
Harald Welte | 3053bbb | 2021-03-24 13:22:18 +0100 | [diff] [blame] | 464 | /* iterate over all local binds within this SNS */ |
| 465 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 466 | struct gprs_ns2_vc_bind *bind = sbind->bind; |
| 467 | |
Daniel Willmann | 967e2c1 | 2021-01-14 16:58:17 +0100 | [diff] [blame] | 468 | if (bind->ll != GPRS_NS2_LL_UDP) |
| 469 | continue; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 470 | |
Harald Welte | e8c6106 | 2021-03-24 13:16:27 +0100 | [diff] [blame] | 471 | /* we only care about UDP binds */ |
| 472 | nsvc = nsvc_for_bind_and_remote(nse, bind, &remote); |
| 473 | if (!nsvc) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 474 | nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0); |
| 475 | if (!nsvc) { |
| 476 | /* TODO: add to a list to send back a NS-STATUS */ |
| 477 | continue; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | /* update data / signalling weight */ |
| 482 | nsvc->data_weight = ip6->data_weight; |
| 483 | nsvc->sig_weight = ip6->sig_weight; |
| 484 | nsvc->sns_only = false; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | /* Add a given remote IPv4 element to gprs_sns_state */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 493 | static int add_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems, |
| 494 | const struct gprs_ns_ie_ip4_elem *ip4) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 495 | { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 496 | /* check for duplicates */ |
Alexander Couzens | cc56ddc | 2021-06-06 03:33:35 +0200 | [diff] [blame] | 497 | for (unsigned int i = 0; i < elems->num_ip4; i++) { |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 498 | if (memcmp(&elems->ip4[i], ip4, sizeof(*ip4))) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 499 | continue; |
Alexander Couzens | d3507e8 | 2021-06-06 03:32:32 +0200 | [diff] [blame] | 500 | return -1; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 501 | } |
| 502 | |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 503 | elems->ip4 = talloc_realloc(gss, elems->ip4, struct gprs_ns_ie_ip4_elem, |
| 504 | elems->num_ip4+1); |
| 505 | elems->ip4[elems->num_ip4] = *ip4; |
| 506 | elems->num_ip4 += 1; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | /* Remove a given remote IPv4 element from gprs_sns_state */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 511 | static int remove_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems, |
| 512 | const struct gprs_ns_ie_ip4_elem *ip4) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 513 | { |
| 514 | unsigned int i; |
| 515 | |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 516 | for (i = 0; i < elems->num_ip4; i++) { |
| 517 | if (memcmp(&elems->ip4[i], ip4, sizeof(*ip4))) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 518 | continue; |
| 519 | /* all array elements < i remain as they are; all > i are shifted left by one */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 520 | memmove(&elems->ip4[i], &elems->ip4[i+1], elems->num_ip4-i-1); |
| 521 | elems->num_ip4 -= 1; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 522 | return 0; |
| 523 | } |
| 524 | return -1; |
| 525 | } |
| 526 | |
| 527 | /* update the weights for specified remote IPv4 */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 528 | static int update_ip4_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems, |
| 529 | const struct gprs_ns_ie_ip4_elem *ip4) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 530 | { |
| 531 | unsigned int i; |
| 532 | |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 533 | for (i = 0; i < elems->num_ip4; i++) { |
| 534 | if (elems->ip4[i].ip_addr != ip4->ip_addr || |
| 535 | elems->ip4[i].udp_port != ip4->udp_port) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 536 | continue; |
| 537 | |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 538 | elems->ip4[i].sig_weight = ip4->sig_weight; |
| 539 | elems->ip4[i].data_weight = ip4->data_weight; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 540 | return 0; |
| 541 | } |
| 542 | return -1; |
| 543 | } |
| 544 | |
| 545 | /* Add a given remote IPv6 element to gprs_sns_state */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 546 | static int add_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems, |
| 547 | const struct gprs_ns_ie_ip6_elem *ip6) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 548 | { |
Alexander Couzens | cc56ddc | 2021-06-06 03:33:35 +0200 | [diff] [blame] | 549 | /* check for duplicates */ |
| 550 | for (unsigned int i = 0; i < elems->num_ip6; i++) { |
| 551 | if (memcmp(&elems->ip6[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) || |
| 552 | elems->ip6[i].udp_port != ip6->udp_port) |
| 553 | continue; |
| 554 | return -1; |
| 555 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 556 | |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 557 | elems->ip6 = talloc_realloc(gss, elems->ip6, struct gprs_ns_ie_ip6_elem, |
| 558 | elems->num_ip6+1); |
| 559 | elems->ip6[elems->num_ip6] = *ip6; |
| 560 | elems->num_ip6 += 1; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | /* Remove a given remote IPv6 element from gprs_sns_state */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 565 | static int remove_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems, |
| 566 | const struct gprs_ns_ie_ip6_elem *ip6) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 567 | { |
| 568 | unsigned int i; |
| 569 | |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 570 | for (i = 0; i < elems->num_ip6; i++) { |
| 571 | if (memcmp(&elems->ip6[i], ip6, sizeof(*ip6))) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 572 | continue; |
| 573 | /* all array elements < i remain as they are; all > i are shifted left by one */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 574 | memmove(&elems->ip6[i], &elems->ip6[i+1], elems->num_ip6-i-1); |
| 575 | elems->num_ip6 -= 1; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 576 | return 0; |
| 577 | } |
| 578 | return -1; |
| 579 | } |
| 580 | |
| 581 | /* update the weights for specified remote IPv6 */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 582 | static int update_ip6_elem(struct ns2_sns_state *gss, struct ns2_sns_elems *elems, |
| 583 | const struct gprs_ns_ie_ip6_elem *ip6) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 584 | { |
| 585 | unsigned int i; |
| 586 | |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 587 | for (i = 0; i < elems->num_ip6; i++) { |
| 588 | if (memcmp(&elems->ip6[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) || |
| 589 | elems->ip6[i].udp_port != ip6->udp_port) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 590 | continue; |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 591 | elems->ip6[i].sig_weight = ip6->sig_weight; |
| 592 | elems->ip6[i].data_weight = ip6->data_weight; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | return -1; |
| 596 | } |
| 597 | |
| 598 | static int do_sns_change_weight(struct osmo_fsm_inst *fi, const struct gprs_ns_ie_ip4_elem *ip4, const struct gprs_ns_ie_ip6_elem *ip6) |
| 599 | { |
| 600 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 601 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 602 | struct gprs_ns2_vc *nsvc; |
| 603 | struct osmo_sockaddr sa = {}; |
Alexander Couzens | 9a4cf27 | 2020-10-11 20:48:04 +0200 | [diff] [blame] | 604 | const struct osmo_sockaddr *remote; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 605 | uint8_t new_signal; |
| 606 | uint8_t new_data; |
| 607 | |
| 608 | /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the |
| 609 | * signalling weights of all the peer IP endpoints configured for this NSE is |
| 610 | * equal to zero or if the resulting sum of the data weights of all the peer IP |
| 611 | * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an |
| 612 | * SNS-ACK PDU with a cause code of "Invalid weights". */ |
| 613 | |
| 614 | if (ip4) { |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 615 | if (update_ip4_elem(gss, &gss->remote, ip4)) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 616 | return -NS_CAUSE_UNKN_IP_EP; |
| 617 | |
| 618 | /* copy over. Both data structures use network byte order */ |
| 619 | sa.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 620 | sa.u.sin.sin_port = ip4->udp_port; |
| 621 | sa.u.sin.sin_family = AF_INET; |
| 622 | new_signal = ip4->sig_weight; |
| 623 | new_data = ip4->data_weight; |
| 624 | } else if (ip6) { |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 625 | if (update_ip6_elem(gss, &gss->remote, ip6)) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 626 | return -NS_CAUSE_UNKN_IP_EP; |
| 627 | |
| 628 | /* copy over. Both data structures use network byte order */ |
| 629 | sa.u.sin6.sin6_addr = ip6->ip_addr; |
| 630 | sa.u.sin6.sin6_port = ip6->udp_port; |
| 631 | sa.u.sin6.sin6_family = AF_INET6; |
| 632 | new_signal = ip6->sig_weight; |
| 633 | new_data = ip6->data_weight; |
| 634 | } else { |
| 635 | OSMO_ASSERT(false); |
| 636 | } |
| 637 | |
| 638 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
Alexander Couzens | c4229a4 | 2020-10-11 20:58:04 +0200 | [diff] [blame] | 639 | remote = gprs_ns2_ip_vc_remote(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 640 | /* all nsvc in NSE should be IP/UDP nsvc */ |
| 641 | OSMO_ASSERT(remote); |
| 642 | |
| 643 | if (osmo_sockaddr_cmp(&sa, remote)) |
| 644 | continue; |
| 645 | |
| 646 | LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n", |
| 647 | gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data, |
| 648 | nsvc->sig_weight, new_signal); |
| 649 | |
| 650 | nsvc->data_weight = new_data; |
| 651 | nsvc->sig_weight = new_signal; |
| 652 | } |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static int do_sns_delete(struct osmo_fsm_inst *fi, |
| 658 | const struct gprs_ns_ie_ip4_elem *ip4, |
| 659 | const struct gprs_ns_ie_ip6_elem *ip6) |
| 660 | { |
| 661 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 662 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 663 | struct gprs_ns2_vc *nsvc, *tmp; |
Alexander Couzens | 9a4cf27 | 2020-10-11 20:48:04 +0200 | [diff] [blame] | 664 | const struct osmo_sockaddr *remote; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 665 | struct osmo_sockaddr sa = {}; |
| 666 | |
| 667 | if (ip4) { |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 668 | if (remove_ip4_elem(gss, &gss->remote, ip4) < 0) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 669 | return -NS_CAUSE_UNKN_IP_EP; |
| 670 | /* copy over. Both data structures use network byte order */ |
| 671 | sa.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 672 | sa.u.sin.sin_port = ip4->udp_port; |
| 673 | sa.u.sin.sin_family = AF_INET; |
| 674 | } else if (ip6) { |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 675 | if (remove_ip6_elem(gss, &gss->remote, ip6)) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 676 | return -NS_CAUSE_UNKN_IP_EP; |
| 677 | |
| 678 | /* copy over. Both data structures use network byte order */ |
| 679 | sa.u.sin6.sin6_addr = ip6->ip_addr; |
| 680 | sa.u.sin6.sin6_port = ip6->udp_port; |
| 681 | sa.u.sin6.sin6_family = AF_INET6; |
| 682 | } else { |
| 683 | OSMO_ASSERT(false); |
| 684 | } |
| 685 | |
| 686 | llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) { |
Alexander Couzens | c4229a4 | 2020-10-11 20:58:04 +0200 | [diff] [blame] | 687 | remote = gprs_ns2_ip_vc_remote(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 688 | /* all nsvc in NSE should be IP/UDP nsvc */ |
| 689 | OSMO_ASSERT(remote); |
| 690 | if (osmo_sockaddr_cmp(&sa, remote)) |
| 691 | continue; |
| 692 | |
| 693 | LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc)); |
| 694 | gprs_ns2_free_nsvc(nsvc); |
| 695 | } |
| 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | static int do_sns_add(struct osmo_fsm_inst *fi, |
| 701 | const struct gprs_ns_ie_ip4_elem *ip4, |
| 702 | const struct gprs_ns_ie_ip6_elem *ip6) |
| 703 | { |
| 704 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 705 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 706 | struct gprs_ns2_vc *nsvc; |
| 707 | int rc = 0; |
| 708 | |
| 709 | /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints |
| 710 | * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send |
| 711 | * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */ |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 712 | switch (gss->family) { |
| 713 | case AF_INET: |
Alexander Couzens | d3507e8 | 2021-06-06 03:32:32 +0200 | [diff] [blame] | 714 | if (gss->remote.num_ip4 >= gss->num_max_ip4_remote) |
| 715 | return -NS_CAUSE_INVAL_NR_NS_VC; |
| 716 | /* TODO: log message duplicate */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 717 | rc = add_ip4_elem(gss, &gss->remote, ip4); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 718 | break; |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 719 | case AF_INET6: |
Alexander Couzens | d3507e8 | 2021-06-06 03:32:32 +0200 | [diff] [blame] | 720 | if (gss->remote.num_ip6 >= gss->num_max_ip6_remote) |
| 721 | return -NS_CAUSE_INVAL_NR_NS_VC; |
| 722 | /* TODO: log message duplicate */ |
Alexander Couzens | c2ba25e | 2021-06-06 02:26:40 +0200 | [diff] [blame] | 723 | rc = add_ip6_elem(gss, &gss->remote, ip6); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 724 | break; |
| 725 | default: |
| 726 | /* the gss->ip is initialized with the bss */ |
| 727 | OSMO_ASSERT(false); |
| 728 | } |
| 729 | |
| 730 | if (rc) |
Alexander Couzens | d3507e8 | 2021-06-06 03:32:32 +0200 | [diff] [blame] | 731 | return -NS_CAUSE_PROTO_ERR_UNSPEC; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 732 | |
| 733 | /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the |
| 734 | * NSE shall send an SNS-ACK PDU with the cause code "Protocol error - |
| 735 | * unspecified" */ |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 736 | switch (gss->family) { |
| 737 | case AF_INET: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 738 | nsvc = nsvc_by_ip4_elem(nse, ip4); |
| 739 | if (nsvc) { |
| 740 | /* the nsvc should be already in sync with the ip4 / ip6 elements */ |
| 741 | return -NS_CAUSE_PROTO_ERR_UNSPEC; |
| 742 | } |
| 743 | |
| 744 | /* TODO: failure case */ |
| 745 | ns2_nsvc_create_ip4(fi, nse, ip4); |
| 746 | break; |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 747 | case AF_INET6: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 748 | nsvc = nsvc_by_ip6_elem(nse, ip6); |
| 749 | if (nsvc) { |
| 750 | /* the nsvc should be already in sync with the ip4 / ip6 elements */ |
| 751 | return -NS_CAUSE_PROTO_ERR_UNSPEC; |
| 752 | } |
| 753 | |
| 754 | /* TODO: failure case */ |
| 755 | ns2_nsvc_create_ip6(fi, nse, ip6); |
| 756 | break; |
| 757 | } |
| 758 | |
| 759 | gprs_ns2_start_alive_all_nsvcs(nse); |
| 760 | |
| 761 | return 0; |
| 762 | } |
| 763 | |
| 764 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 765 | static void ns2_sns_st_bss_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 766 | { |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 767 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 768 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 769 | /* empty state - SNS Select will start by ns2_sns_st_all_action() */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 770 | } |
| 771 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 772 | static void ns2_sns_st_bss_size(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 773 | { |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 774 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 775 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 776 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 777 | struct tlv_parsed *tp = NULL; |
| 778 | |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 779 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
| 780 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 781 | switch (event) { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 782 | case NS2_SNS_EV_RX_SIZE_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 783 | tp = data; |
| 784 | if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) { |
| 785 | LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n", |
| 786 | gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE))); |
| 787 | /* TODO: What to do? */ |
| 788 | } else { |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 789 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 790 | nsi->timeout[NS_TOUT_TSNS_PROV], 2); |
| 791 | } |
| 792 | break; |
| 793 | default: |
| 794 | OSMO_ASSERT(0); |
| 795 | } |
| 796 | } |
| 797 | |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 798 | static int ns2_sns_count_num_local_ep(struct osmo_fsm_inst *fi, int ip_proto) |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 799 | { |
| 800 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 801 | struct ns2_sns_bind *sbind; |
| 802 | int count = 0; |
| 803 | |
| 804 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 805 | const struct osmo_sockaddr *sa = gprs_ns2_ip_bind_sockaddr(sbind->bind); |
| 806 | if (!sa) |
| 807 | continue; |
| 808 | |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 809 | switch (ip_proto) { |
| 810 | case AF_INET: |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 811 | if (sa->u.sas.ss_family == AF_INET) |
| 812 | count++; |
| 813 | break; |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 814 | case AF_INET6: |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 815 | if (sa->u.sas.ss_family == AF_INET6) |
| 816 | count++; |
| 817 | break; |
| 818 | } |
| 819 | } |
| 820 | return count; |
| 821 | } |
| 822 | |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 823 | static int ns2_sns_copy_local_endpoints(struct ns2_sns_state *gss) |
| 824 | { |
| 825 | switch (gss->family) { |
| 826 | case AF_INET: |
| 827 | gss->local_procedure.ip4 = talloc_realloc(gss, gss->local_procedure.ip4, struct gprs_ns_ie_ip4_elem, |
| 828 | gss->local.num_ip4); |
| 829 | if (!gss->local_procedure.ip4) |
| 830 | return -ENOMEM; |
| 831 | |
| 832 | gss->local_procedure.num_ip4 = gss->local.num_ip4; |
| 833 | memcpy(gss->local_procedure.ip4, gss->local.ip4, |
| 834 | sizeof(struct gprs_ns_ie_ip4_elem) * gss->local.num_ip4); |
| 835 | break; |
| 836 | case AF_INET6: |
| 837 | gss->local_procedure.ip6 = talloc_realloc(gss, gss->local_procedure.ip6, struct gprs_ns_ie_ip6_elem, |
| 838 | gss->local.num_ip6); |
| 839 | if (!gss->local_procedure.ip6) |
| 840 | return -ENOMEM; |
| 841 | |
| 842 | gss->local_procedure.num_ip6 = gss->local.num_ip6; |
| 843 | memcpy(gss->local_procedure.ip6, gss->local.ip6, |
| 844 | sizeof(struct gprs_ns_ie_ip6_elem) * gss->local.num_ip6); |
| 845 | break; |
| 846 | default: |
| 847 | OSMO_ASSERT(0); |
| 848 | } |
| 849 | |
| 850 | return 0; |
| 851 | } |
| 852 | |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 853 | static void ns2_sns_compute_local_ep_from_binds(struct osmo_fsm_inst *fi) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 854 | { |
| 855 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 856 | struct gprs_ns_ie_ip4_elem *ip4_elems; |
| 857 | struct gprs_ns_ie_ip6_elem *ip6_elems; |
| 858 | struct gprs_ns2_vc_bind *bind; |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 859 | struct ns2_sns_bind *sbind; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 860 | const struct osmo_sockaddr *remote; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 861 | const struct osmo_sockaddr *sa; |
| 862 | struct osmo_sockaddr local; |
| 863 | int count; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 864 | |
Alexander Couzens | d2c6c49 | 2021-06-06 01:57:52 +0200 | [diff] [blame] | 865 | ns2_clear_elems(&gss->local); |
Alexander Couzens | 7a7b20b | 2021-01-18 10:47:33 +0100 | [diff] [blame] | 866 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 867 | /* no initial available */ |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 868 | if (gss->role == GPRS_SNS_ROLE_BSS) { |
| 869 | if (!gss->initial) |
| 870 | return; |
| 871 | remote = &gss->initial->saddr; |
| 872 | } else |
| 873 | remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 874 | |
| 875 | /* count how many bindings are available (only UDP binds) */ |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 876 | count = llist_count(&gss->binds); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 877 | if (count == 0) { |
Harald Welte | 0599287 | 2021-03-04 15:49:21 +0100 | [diff] [blame] | 878 | LOGPFSML(fi, LOGL_ERROR, "No local binds for this NSE -> cannot determine IP endpoints\n"); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 879 | return; |
| 880 | } |
| 881 | |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 882 | switch (gss->family) { |
| 883 | case AF_INET: |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 884 | ip4_elems = talloc_realloc(fi, gss->local.ip4, struct gprs_ns_ie_ip4_elem, count); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 885 | if (!ip4_elems) |
| 886 | return; |
| 887 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 888 | gss->local.ip4 = ip4_elems; |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 889 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 890 | bind = sbind->bind; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 891 | sa = gprs_ns2_ip_bind_sockaddr(bind); |
| 892 | if (!sa) |
| 893 | continue; |
| 894 | |
| 895 | if (sa->u.sas.ss_family != AF_INET) |
| 896 | continue; |
| 897 | |
| 898 | /* check if this is an specific bind */ |
| 899 | if (sa->u.sin.sin_addr.s_addr == 0) { |
| 900 | if (osmo_sockaddr_local_ip(&local, remote)) |
| 901 | continue; |
| 902 | |
| 903 | ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr; |
| 904 | } else { |
| 905 | ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr; |
| 906 | } |
| 907 | |
| 908 | ip4_elems->udp_port = sa->u.sin.sin_port; |
Alexander Couzens | c470476 | 2021-02-08 23:13:12 +0100 | [diff] [blame] | 909 | ip4_elems->sig_weight = bind->sns_sig_weight; |
| 910 | ip4_elems->data_weight = bind->sns_data_weight; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 911 | ip4_elems++; |
| 912 | } |
| 913 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 914 | gss->local.num_ip4 = count; |
| 915 | gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->local.num_ip4, 8); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 916 | break; |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 917 | case AF_INET6: |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 918 | /* IPv6 */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 919 | ip6_elems = talloc_realloc(fi, gss->local.ip6, struct gprs_ns_ie_ip6_elem, count); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 920 | if (!ip6_elems) |
| 921 | return; |
| 922 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 923 | gss->local.ip6 = ip6_elems; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 924 | |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 925 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 926 | bind = sbind->bind; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 927 | sa = gprs_ns2_ip_bind_sockaddr(bind); |
| 928 | if (!sa) |
| 929 | continue; |
| 930 | |
| 931 | if (sa->u.sas.ss_family != AF_INET6) |
| 932 | continue; |
| 933 | |
| 934 | /* check if this is an specific bind */ |
| 935 | if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) { |
| 936 | if (osmo_sockaddr_local_ip(&local, remote)) |
| 937 | continue; |
| 938 | |
| 939 | ip6_elems->ip_addr = local.u.sin6.sin6_addr; |
| 940 | } else { |
| 941 | ip6_elems->ip_addr = sa->u.sin6.sin6_addr; |
| 942 | } |
| 943 | |
| 944 | ip6_elems->udp_port = sa->u.sin.sin_port; |
Alexander Couzens | c470476 | 2021-02-08 23:13:12 +0100 | [diff] [blame] | 945 | ip6_elems->sig_weight = bind->sns_sig_weight; |
| 946 | ip6_elems->data_weight = bind->sns_data_weight; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 947 | |
| 948 | ip6_elems++; |
| 949 | } |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 950 | gss->local.num_ip6 = count; |
| 951 | gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->local.num_ip6, 8); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 952 | break; |
| 953 | } |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 954 | |
| 955 | ns2_sns_copy_local_endpoints(gss); |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 956 | } |
| 957 | |
Alexander Couzens | 6608ce9 | 2021-04-26 20:39:46 +0200 | [diff] [blame] | 958 | static void ns2_sns_choose_next_bind(struct ns2_sns_state *gss) |
| 959 | { |
| 960 | /* take the first bind or take the next bind */ |
| 961 | if (!gss->initial_bind || gss->initial_bind->list.next == &gss->binds) |
| 962 | gss->initial_bind = llist_first_entry_or_null(&gss->binds, struct ns2_sns_bind, list); |
| 963 | else |
| 964 | gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list); |
| 965 | } |
| 966 | |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 967 | /* setup all dynamic SNS settings, create a new nsvc and send the SIZE */ |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 968 | static void ns2_sns_st_bss_size_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 969 | { |
| 970 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 971 | |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 972 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
| 973 | |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 974 | /* on a generic failure, the timer callback will recover */ |
| 975 | if (old_state != GPRS_SNS_ST_UNCONFIGURED) |
| 976 | ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_FAILURE); |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 977 | if (old_state != GPRS_SNS_ST_BSS_SIZE) |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 978 | gss->N = 0; |
| 979 | |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 980 | ns2_clear_procedures(gss); |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 981 | gss->alive = false; |
| 982 | |
| 983 | ns2_sns_compute_local_ep_from_binds(fi); |
Alexander Couzens | 6608ce9 | 2021-04-26 20:39:46 +0200 | [diff] [blame] | 984 | ns2_sns_choose_next_bind(gss); |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 985 | |
| 986 | /* setup the NSVC */ |
| 987 | if (!gss->sns_nsvc) { |
| 988 | struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind; |
| 989 | struct osmo_sockaddr *remote = &gss->initial->saddr; |
| 990 | gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote); |
| 991 | if (!gss->sns_nsvc) |
| 992 | return; |
Harald Welte | c962a2e | 2021-03-05 08:09:08 +0100 | [diff] [blame] | 993 | /* A pre-configured endpoint shall not be used for NSE data or signalling traffic |
| 994 | * (with the exception of Size and Configuration procedures) unless it is configured |
| 995 | * by the SGSN using the auto-configuration procedures */ |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 996 | gss->sns_nsvc->sns_only = true; |
| 997 | } |
| 998 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 999 | if (gss->num_max_ip4_remote > 0) |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1000 | ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->local.num_ip4, -1); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1001 | else |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1002 | ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->local.num_ip6); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1003 | } |
| 1004 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1005 | static void ns2_sns_st_bss_config_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1006 | { |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 1007 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 3df5886 | 2021-02-05 17:18:08 +0100 | [diff] [blame] | 1008 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 1009 | struct tlv_parsed *tp = NULL; |
| 1010 | |
| 1011 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1012 | |
| 1013 | switch (event) { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 1014 | case NS2_SNS_EV_RX_CONFIG_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1015 | tp = (struct tlv_parsed *) data; |
| 1016 | if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) { |
| 1017 | LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n", |
| 1018 | gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE))); |
| 1019 | /* TODO: What to do? */ |
| 1020 | } else { |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1021 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_SGSN, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 3); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1022 | } |
| 1023 | break; |
| 1024 | default: |
| 1025 | OSMO_ASSERT(0); |
| 1026 | } |
| 1027 | } |
| 1028 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1029 | static void ns2_sns_st_bss_config_bss_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1030 | { |
| 1031 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 790a963 | 2021-02-05 17:18:39 +0100 | [diff] [blame] | 1032 | |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 1033 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
| 1034 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1035 | if (old_state != GPRS_SNS_ST_BSS_CONFIG_BSS) |
Alexander Couzens | 790a963 | 2021-02-05 17:18:39 +0100 | [diff] [blame] | 1036 | gss->N = 0; |
| 1037 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1038 | /* Transmit SNS-CONFIG */ |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 1039 | switch (gss->family) { |
| 1040 | case AF_INET: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1041 | ns2_tx_sns_config(gss->sns_nsvc, true, |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1042 | gss->local.ip4, gss->local.num_ip4, |
Alexander Couzens | e78207f | 2020-12-07 06:19:29 +0100 | [diff] [blame] | 1043 | NULL, 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1044 | break; |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 1045 | case AF_INET6: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1046 | ns2_tx_sns_config(gss->sns_nsvc, true, |
Alexander Couzens | e78207f | 2020-12-07 06:19:29 +0100 | [diff] [blame] | 1047 | NULL, 0, |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1048 | gss->local.ip6, gss->local.num_ip6); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1049 | break; |
| 1050 | } |
| 1051 | } |
| 1052 | |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1053 | /* calculate the timeout of the configured state. the configured |
| 1054 | * state will fail if not at least one NS-VC is alive within X second. |
| 1055 | */ |
| 1056 | static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi) |
| 1057 | { |
| 1058 | int secs; |
| 1059 | struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi; |
| 1060 | secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]; |
| 1061 | secs += nsi->timeout[NS_TOUT_TNS_TEST]; |
| 1062 | |
| 1063 | return secs; |
| 1064 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1065 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1066 | /* append the remote endpoints from the parsed TLV array to the ns2_sns_state */ |
| 1067 | static int ns_sns_append_remote_eps(struct osmo_fsm_inst *fi, const struct tlv_parsed *tp) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1068 | { |
| 1069 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1070 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1071 | if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) { |
| 1072 | const struct gprs_ns_ie_ip4_elem *v4_list; |
| 1073 | unsigned int num_v4; |
| 1074 | v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST); |
| 1075 | num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1076 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1077 | if (num_v4 && gss->remote.ip6) |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1078 | return -NS_CAUSE_INVAL_NR_IPv4_EP; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1079 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1080 | /* realloc to the new size */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1081 | gss->remote.ip4 = talloc_realloc(gss, gss->remote.ip4, |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1082 | struct gprs_ns_ie_ip4_elem, |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1083 | gss->remote.num_ip4 + num_v4); |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1084 | /* append the new entries to the end of the list */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1085 | memcpy(&gss->remote.ip4[gss->remote.num_ip4], v4_list, num_v4*sizeof(*v4_list)); |
| 1086 | gss->remote.num_ip4 += num_v4; |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1087 | |
| 1088 | LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n", |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1089 | gss->remote.num_ip4); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1090 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1091 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1092 | if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) { |
| 1093 | const struct gprs_ns_ie_ip6_elem *v6_list; |
| 1094 | unsigned int num_v6; |
| 1095 | v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST); |
| 1096 | num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list); |
| 1097 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1098 | if (num_v6 && gss->remote.ip4) |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1099 | return -NS_CAUSE_INVAL_NR_IPv6_EP; |
| 1100 | |
| 1101 | /* realloc to the new size */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1102 | gss->remote.ip6 = talloc_realloc(gss, gss->remote.ip6, |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1103 | struct gprs_ns_ie_ip6_elem, |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1104 | gss->remote.num_ip6 + num_v6); |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1105 | /* append the new entries to the end of the list */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1106 | memcpy(&gss->remote.ip6[gss->remote.num_ip6], v6_list, num_v6*sizeof(*v6_list)); |
| 1107 | gss->remote.num_ip6 += num_v6; |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1108 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1109 | LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %d entries\n", |
| 1110 | gss->remote.num_ip6); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1111 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1112 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1113 | return 0; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1114 | } |
| 1115 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1116 | static void ns2_sns_st_bss_config_sgsn_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
Alexander Couzens | 790a963 | 2021-02-05 17:18:39 +0100 | [diff] [blame] | 1117 | { |
| 1118 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 1119 | |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 1120 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
| 1121 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1122 | if (old_state != GPRS_SNS_ST_BSS_CONFIG_SGSN) |
Alexander Couzens | 790a963 | 2021-02-05 17:18:39 +0100 | [diff] [blame] | 1123 | gss->N = 0; |
| 1124 | } |
| 1125 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1126 | static void ns2_sns_st_bss_config_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1127 | { |
| 1128 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1129 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 1130 | uint8_t cause; |
| 1131 | int rc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1132 | |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 1133 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
| 1134 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1135 | switch (event) { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 1136 | case NS2_SNS_EV_RX_CONFIG_END: |
| 1137 | case NS2_SNS_EV_RX_CONFIG: |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1138 | rc = ns_sns_append_remote_eps(fi, data); |
| 1139 | if (rc < 0) { |
| 1140 | cause = -rc; |
| 1141 | ns2_tx_sns_config_ack(gss->sns_nsvc, &cause); |
| 1142 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
| 1143 | return; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1144 | } |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 1145 | if (event == NS2_SNS_EV_RX_CONFIG_END) { |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1146 | /* check if sum of data / sig weights == 0 */ |
Alexander Couzens | 019da4b | 2021-06-06 02:48:18 +0200 | [diff] [blame] | 1147 | if (ip46_weight_sum_data(&gss->remote) == 0 || ip46_weight_sum_sig(&gss->remote) == 0) { |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1148 | cause = NS_CAUSE_INVAL_WEIGH; |
| 1149 | ns2_tx_sns_config_ack(gss->sns_nsvc, &cause); |
| 1150 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
| 1151 | return; |
| 1152 | } |
| 1153 | create_missing_nsvcs(fi); |
| 1154 | ns2_tx_sns_config_ack(gss->sns_nsvc, NULL); |
| 1155 | /* start the test procedure on ALL NSVCs! */ |
| 1156 | gprs_ns2_start_alive_all_nsvcs(nse); |
| 1157 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0); |
| 1158 | } else { |
| 1159 | /* just send CONFIG-ACK */ |
| 1160 | ns2_tx_sns_config_ack(gss->sns_nsvc, NULL); |
| 1161 | osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1162 | } |
| 1163 | break; |
| 1164 | default: |
| 1165 | OSMO_ASSERT(0); |
| 1166 | } |
| 1167 | } |
| 1168 | |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 1169 | /* called when receiving NS2_SNS_EV_RX_ADD in state configure */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1170 | static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi, |
| 1171 | struct ns2_sns_state *gss, |
| 1172 | struct tlv_parsed *tp) |
| 1173 | { |
| 1174 | const struct gprs_ns_ie_ip4_elem *v4_list = NULL; |
| 1175 | const struct gprs_ns_ie_ip6_elem *v6_list = NULL; |
| 1176 | int num_v4 = 0, num_v6 = 0; |
| 1177 | uint8_t trans_id, cause = 0xff; |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1178 | unsigned int i; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1179 | int rc = 0; |
| 1180 | |
| 1181 | /* TODO: refactor EV_ADD/CHANGE/REMOVE by |
| 1182 | * check uniqueness within the lists (no doublicate entries) |
| 1183 | * check not-known-by-us and sent back a list of unknown/known values |
| 1184 | * (abnormal behaviour according to 48.016) |
| 1185 | */ |
| 1186 | |
| 1187 | trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID); |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 1188 | if (gss->family == AF_INET) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1189 | if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) { |
| 1190 | cause = NS_CAUSE_INVAL_NR_IPv4_EP; |
| 1191 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1192 | return; |
| 1193 | } |
| 1194 | |
| 1195 | v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST); |
| 1196 | num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1197 | for (i = 0; i < num_v4; i++) { |
| 1198 | unsigned int j; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1199 | rc = do_sns_add(fi, &v4_list[i], NULL); |
| 1200 | if (rc < 0) { |
| 1201 | /* rollback/undo to restore previous state */ |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1202 | for (j = 0; j < i; j++) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1203 | do_sns_delete(fi, &v4_list[j], NULL); |
| 1204 | cause = -rc; |
| 1205 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1206 | break; |
| 1207 | } |
| 1208 | } |
| 1209 | } else { /* IPv6 */ |
| 1210 | if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) { |
| 1211 | cause = NS_CAUSE_INVAL_NR_IPv6_EP; |
| 1212 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1213 | return; |
| 1214 | } |
| 1215 | |
| 1216 | v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST); |
| 1217 | num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1218 | for (i = 0; i < num_v6; i++) { |
| 1219 | unsigned int j; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1220 | rc = do_sns_add(fi, NULL, &v6_list[i]); |
| 1221 | if (rc < 0) { |
| 1222 | /* rollback/undo to restore previous state */ |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1223 | for (j = 0; j < i; j++) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1224 | do_sns_delete(fi, NULL, &v6_list[j]); |
| 1225 | cause = -rc; |
| 1226 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1227 | break; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */ |
| 1233 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6); |
| 1234 | } |
| 1235 | |
| 1236 | static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi, |
| 1237 | struct ns2_sns_state *gss, |
| 1238 | struct tlv_parsed *tp) |
| 1239 | { |
| 1240 | const struct gprs_ns_ie_ip4_elem *v4_list = NULL; |
| 1241 | const struct gprs_ns_ie_ip6_elem *v6_list = NULL; |
| 1242 | int num_v4 = 0, num_v6 = 0; |
| 1243 | uint8_t trans_id, cause = 0xff; |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1244 | unsigned int i; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1245 | int rc = 0; |
| 1246 | |
| 1247 | /* TODO: split up delete into v4 + v6 |
| 1248 | * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa |
| 1249 | * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time |
| 1250 | */ |
| 1251 | trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID); |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 1252 | if (gss->family == AF_INET) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1253 | if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) { |
| 1254 | v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST); |
| 1255 | num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1256 | for ( i = 0; i < num_v4; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1257 | rc = do_sns_delete(fi, &v4_list[i], NULL); |
| 1258 | if (rc < 0) { |
| 1259 | cause = -rc; |
| 1260 | /* continue to delete others */ |
| 1261 | } |
| 1262 | } |
| 1263 | if (cause != 0xff) { |
| 1264 | /* TODO: create list of not-deleted and return it */ |
| 1265 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1266 | return; |
| 1267 | } |
| 1268 | |
| 1269 | } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) { |
| 1270 | /* delete all NS-VCs for given IPv4 address */ |
| 1271 | const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR); |
| 1272 | struct gprs_ns_ie_ip4_elem *ip4_remote; |
| 1273 | uint32_t ip_addr = *(uint32_t *)(ie+1); |
| 1274 | if (ie[0] != 0x01) { /* Address Type != IPv4 */ |
| 1275 | cause = NS_CAUSE_UNKN_IP_ADDR; |
| 1276 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1277 | return; |
| 1278 | } |
| 1279 | /* make a copy as do_sns_delete() will change the array underneath us */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1280 | ip4_remote = talloc_memdup(fi, gss->remote.ip4, |
| 1281 | gss->remote.num_ip4 * sizeof(*v4_list)); |
| 1282 | for (i = 0; i < gss->remote.num_ip4; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1283 | if (ip4_remote[i].ip_addr == ip_addr) { |
| 1284 | rc = do_sns_delete(fi, &ip4_remote[i], NULL); |
| 1285 | if (rc < 0) { |
| 1286 | cause = -rc; |
| 1287 | /* continue to delete others */ |
| 1288 | } |
| 1289 | } |
| 1290 | } |
| 1291 | talloc_free(ip4_remote); |
| 1292 | if (cause != 0xff) { |
| 1293 | /* TODO: create list of not-deleted and return it */ |
| 1294 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1295 | return; |
| 1296 | } |
| 1297 | } else { |
| 1298 | cause = NS_CAUSE_INVAL_NR_IPv4_EP; |
| 1299 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1300 | return; |
| 1301 | } |
| 1302 | } else { /* IPv6 */ |
| 1303 | if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) { |
| 1304 | v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST); |
| 1305 | num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1306 | for (i = 0; i < num_v6; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1307 | rc = do_sns_delete(fi, NULL, &v6_list[i]); |
| 1308 | if (rc < 0) { |
| 1309 | cause = -rc; |
| 1310 | /* continue to delete others */ |
| 1311 | } |
| 1312 | } |
| 1313 | if (cause != 0xff) { |
| 1314 | /* TODO: create list of not-deleted and return it */ |
| 1315 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1316 | return; |
| 1317 | } |
| 1318 | } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) { |
| 1319 | /* delete all NS-VCs for given IPv4 address */ |
| 1320 | const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR); |
| 1321 | struct gprs_ns_ie_ip6_elem *ip6_remote; |
| 1322 | struct in6_addr ip6_addr; |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1323 | unsigned int i; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1324 | if (ie[0] != 0x02) { /* Address Type != IPv6 */ |
| 1325 | cause = NS_CAUSE_UNKN_IP_ADDR; |
| 1326 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1327 | return; |
| 1328 | } |
| 1329 | memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr)); |
| 1330 | /* make a copy as do_sns_delete() will change the array underneath us */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 1331 | ip6_remote = talloc_memdup(fi, gss->remote.ip6, |
| 1332 | gss->remote.num_ip6 * sizeof(*v4_list)); |
| 1333 | for (i = 0; i < gss->remote.num_ip6; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1334 | if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) { |
| 1335 | rc = do_sns_delete(fi, NULL, &ip6_remote[i]); |
| 1336 | if (rc < 0) { |
| 1337 | cause = -rc; |
| 1338 | /* continue to delete others */ |
| 1339 | } |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | talloc_free(ip6_remote); |
| 1344 | if (cause != 0xff) { |
| 1345 | /* TODO: create list of not-deleted and return it */ |
| 1346 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1347 | return; |
| 1348 | } |
| 1349 | } else { |
| 1350 | cause = NS_CAUSE_INVAL_NR_IPv6_EP; |
| 1351 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1352 | return; |
| 1353 | } |
| 1354 | } |
| 1355 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6); |
| 1356 | } |
| 1357 | |
| 1358 | static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi, |
| 1359 | struct ns2_sns_state *gss, |
| 1360 | struct tlv_parsed *tp) |
| 1361 | { |
| 1362 | const struct gprs_ns_ie_ip4_elem *v4_list = NULL; |
| 1363 | const struct gprs_ns_ie_ip6_elem *v6_list = NULL; |
| 1364 | int num_v4 = 0, num_v6 = 0; |
| 1365 | uint8_t trans_id, cause = 0xff; |
| 1366 | int rc = 0; |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1367 | unsigned int i; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1368 | |
| 1369 | trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID); |
| 1370 | if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) { |
| 1371 | v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST); |
| 1372 | num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1373 | for (i = 0; i < num_v4; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1374 | rc = do_sns_change_weight(fi, &v4_list[i], NULL); |
| 1375 | if (rc < 0) { |
| 1376 | cause = -rc; |
| 1377 | /* continue to others */ |
| 1378 | } |
| 1379 | } |
| 1380 | if (cause != 0xff) { |
| 1381 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1382 | return; |
| 1383 | } |
| 1384 | } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) { |
| 1385 | v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST); |
| 1386 | num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1387 | for (i = 0; i < num_v6; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1388 | rc = do_sns_change_weight(fi, NULL, &v6_list[i]); |
| 1389 | if (rc < 0) { |
| 1390 | cause = -rc; |
| 1391 | /* continue to others */ |
| 1392 | } |
| 1393 | } |
| 1394 | if (cause != 0xff) { |
| 1395 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1396 | return; |
| 1397 | } |
| 1398 | } else { |
| 1399 | cause = NS_CAUSE_INVAL_NR_IPv4_EP; |
| 1400 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1401 | return; |
| 1402 | } |
| 1403 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6); |
| 1404 | } |
| 1405 | |
| 1406 | static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 1407 | { |
| 1408 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 1409 | struct tlv_parsed *tp = data; |
| 1410 | |
| 1411 | switch (event) { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 1412 | case NS2_SNS_EV_RX_ADD: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1413 | ns2_sns_st_configured_add(fi, gss, tp); |
| 1414 | break; |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 1415 | case NS2_SNS_EV_RX_DELETE: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1416 | ns2_sns_st_configured_delete(fi, gss, tp); |
| 1417 | break; |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 1418 | case NS2_SNS_EV_RX_CHANGE_WEIGHT: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1419 | ns2_sns_st_configured_change(fi, gss, tp); |
| 1420 | break; |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 1421 | case NS2_SNS_EV_REQ_NSVC_ALIVE: |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1422 | osmo_timer_del(&fi->timer); |
| 1423 | break; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
| 1428 | { |
Alexander Couzens | cdb2baa | 2021-04-01 15:29:16 +0200 | [diff] [blame] | 1429 | struct gprs_ns2_vc *nsvc; |
| 1430 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1431 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
Alexander Couzens | cdb2baa | 2021-04-01 15:29:16 +0200 | [diff] [blame] | 1432 | /* NS-VC status updates are only parsed in ST_CONFIGURED. |
| 1433 | * Do an initial check if there are any nsvc alive atm */ |
| 1434 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
| 1435 | if (ns2_vc_is_unblocked(nsvc)) { |
| 1436 | gss->alive = true; |
| 1437 | osmo_timer_del(&fi->timer); |
| 1438 | break; |
| 1439 | } |
| 1440 | } |
| 1441 | |
Alexander Couzens | 53e7009 | 2021-04-06 15:45:47 +0200 | [diff] [blame] | 1442 | /* remove the initial NSVC if the NSVC isn't part of the configuration */ |
| 1443 | if (gss->sns_nsvc->sns_only) |
| 1444 | gprs_ns2_free_nsvc(gss->sns_nsvc); |
| 1445 | |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1446 | if (old_state != GPRS_SNS_ST_LOCAL_PROCEDURE) |
| 1447 | ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED); |
| 1448 | |
| 1449 | if (!llist_empty(&gss->procedures)) { |
| 1450 | osmo_fsm_inst_state_chg(gss->nse->bss_sns_fi, GPRS_SNS_ST_LOCAL_PROCEDURE, |
| 1451 | gss->nse->nsi->timeout[NS_TOUT_TSNS_PROV], 5); |
| 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | static void ns2_sns_st_local_procedure_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
| 1456 | { |
| 1457 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 1458 | |
| 1459 | /* check if resend or not */ |
| 1460 | if (!gss->current_procedure) { |
| 1461 | /* take next procedure */ |
| 1462 | gss->current_procedure = llist_first_entry_or_null(&gss->procedures, |
| 1463 | struct ns2_sns_procedure, list); |
| 1464 | if (!gss->current_procedure) { |
| 1465 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0); |
| 1466 | return; |
| 1467 | } |
| 1468 | gss->N = 0; |
| 1469 | gss->current_procedure->running = true; |
| 1470 | gss->current_procedure->trans_id = ++gss->trans_id; |
| 1471 | if (gss->trans_id == 0) |
| 1472 | gss->trans_id = gss->current_procedure->trans_id = 1; |
| 1473 | |
| 1474 | } |
| 1475 | |
| 1476 | /* also takes care of retransmitting */ |
| 1477 | switch (gss->current_procedure->procedure) { |
Alexander Couzens | 9cd39ac | 2021-06-06 18:57:56 +0200 | [diff] [blame^] | 1478 | case SNS_PROC_ADD: |
| 1479 | if (gss->family == AF_INET) |
| 1480 | ns2_tx_sns_add(gss->sns_nsvc, gss->current_procedure->trans_id, &gss->current_procedure->ip4, 1, NULL, 0); |
| 1481 | else |
| 1482 | ns2_tx_sns_add(gss->sns_nsvc, gss->current_procedure->trans_id, NULL, 0, &gss->current_procedure->ip6, 1); |
| 1483 | break; |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1484 | case SNS_PROC_CHANGE_WEIGHT: |
| 1485 | if (gss->family == AF_INET) |
| 1486 | ns2_tx_sns_change_weight(gss->sns_nsvc, gss->current_procedure->trans_id, &gss->current_procedure->ip4, 1, NULL, 0); |
| 1487 | else |
| 1488 | ns2_tx_sns_change_weight(gss->sns_nsvc, gss->current_procedure->trans_id, NULL, 0, &gss->current_procedure->ip6, 1); |
| 1489 | break; |
| 1490 | default: |
| 1491 | break; |
| 1492 | } |
| 1493 | } |
| 1494 | |
Alexander Couzens | 9cd39ac | 2021-06-06 18:57:56 +0200 | [diff] [blame^] | 1495 | static void create_nsvc_for_new_sbind(struct ns2_sns_state *gss, struct ns2_sns_bind *sbind) |
| 1496 | { |
| 1497 | struct gprs_ns2_nse *nse = gss->nse; |
| 1498 | struct gprs_ns2_vc_bind *bind = sbind->bind; |
| 1499 | struct gprs_ns2_vc *nsvc; |
| 1500 | struct osmo_sockaddr remote = { }; |
| 1501 | unsigned int i; |
| 1502 | |
| 1503 | /* iterate over all remote IPv4 endpoints */ |
| 1504 | for (i = 0; i < gss->remote.num_ip4; i++) { |
| 1505 | const struct gprs_ns_ie_ip4_elem *ip4 = &gss->remote.ip4[i]; |
| 1506 | |
| 1507 | remote.u.sin.sin_family = AF_INET; |
| 1508 | remote.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 1509 | remote.u.sin.sin_port = ip4->udp_port; |
| 1510 | /* we only care about UDP binds */ |
| 1511 | if (bind->ll != GPRS_NS2_LL_UDP) |
| 1512 | continue; |
| 1513 | |
| 1514 | nsvc = nsvc_for_bind_and_remote(nse, bind, &remote); |
| 1515 | if (!nsvc) { |
| 1516 | nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0); |
| 1517 | if (!nsvc) { |
| 1518 | /* TODO: add to a list to send back a NS-STATUS */ |
| 1519 | continue; |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | /* update data / signalling weight */ |
| 1524 | nsvc->data_weight = ip4->data_weight; |
| 1525 | nsvc->sig_weight = ip4->sig_weight; |
| 1526 | nsvc->sns_only = false; |
| 1527 | } |
| 1528 | |
| 1529 | /* iterate over all remote IPv4 endpoints */ |
| 1530 | for (i = 0; i < gss->remote.num_ip6; i++) { |
| 1531 | const struct gprs_ns_ie_ip6_elem *ip6 = &gss->remote.ip6[i]; |
| 1532 | |
| 1533 | remote.u.sin6.sin6_family = AF_INET6; |
| 1534 | remote.u.sin6.sin6_addr = ip6->ip_addr; |
| 1535 | remote.u.sin6.sin6_port = ip6->udp_port; |
| 1536 | |
| 1537 | /* we only care about UDP binds */ |
| 1538 | nsvc = nsvc_for_bind_and_remote(nse, bind, &remote); |
| 1539 | if (!nsvc) { |
| 1540 | nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0); |
| 1541 | if (!nsvc) { |
| 1542 | /* TODO: add to a list to send back a NS-STATUS */ |
| 1543 | continue; |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | /* update data / signalling weight */ |
| 1548 | nsvc->data_weight = ip6->data_weight; |
| 1549 | nsvc->sig_weight = ip6->sig_weight; |
| 1550 | nsvc->sns_only = false; |
| 1551 | } |
| 1552 | } |
| 1553 | |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1554 | static void ns2_sns_st_local_procedure(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 1555 | { |
| 1556 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 1557 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 1558 | struct gprs_ns_ie_ip4_elem *ip4, *proc4; |
| 1559 | struct gprs_ns_ie_ip6_elem *ip6, *proc6; |
| 1560 | struct tlv_parsed *tp = data; |
| 1561 | uint8_t trans_id; |
| 1562 | uint8_t cause; |
| 1563 | |
| 1564 | switch (event) { |
| 1565 | case NS2_SNS_EV_RX_ADD: |
| 1566 | ns2_sns_st_configured_add(fi, gss, tp); |
| 1567 | break; |
| 1568 | case NS2_SNS_EV_RX_DELETE: |
| 1569 | ns2_sns_st_configured_delete(fi, gss, tp); |
| 1570 | break; |
| 1571 | case NS2_SNS_EV_RX_CHANGE_WEIGHT: |
| 1572 | ns2_sns_st_configured_change(fi, gss, tp); |
| 1573 | break; |
| 1574 | case NS2_SNS_EV_RX_ACK: |
| 1575 | /* presence of trans_id is already checked here */ |
| 1576 | trans_id = tlvp_val8(tp, NS_IE_TRANS_ID, 0); |
| 1577 | if (trans_id != gss->current_procedure->trans_id) { |
| 1578 | LOGPFSML(fi, LOGL_INFO, "NSEI=%u Rx SNS ACK with invalid transaction id %d. Valid %d\n", |
| 1579 | nse->nsei, trans_id, gss->current_procedure->trans_id); |
| 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | if (TLVP_PRESENT(tp, NS_IE_CAUSE)) { |
| 1584 | /* what happend on error cause? return to size? */ |
| 1585 | cause = tlvp_val8(tp, NS_IE_CAUSE, 0); |
| 1586 | LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx SNS ACK trans %d with cause code %d.\n", |
| 1587 | nse->nsei, trans_id, cause); |
| 1588 | sns_failed(fi, NULL); |
| 1589 | break; |
| 1590 | } |
| 1591 | |
| 1592 | switch (gss->current_procedure->procedure) { |
Alexander Couzens | 9cd39ac | 2021-06-06 18:57:56 +0200 | [diff] [blame^] | 1593 | case SNS_PROC_ADD: |
| 1594 | switch (gss->family) { |
| 1595 | case AF_INET: |
| 1596 | add_ip4_elem(gss, &gss->local, &gss->current_procedure->ip4); |
| 1597 | break; |
| 1598 | case AF_INET6: |
| 1599 | add_ip6_elem(gss, &gss->local, &gss->current_procedure->ip6); |
| 1600 | break; |
| 1601 | } |
| 1602 | create_nsvc_for_new_sbind(gss, gss->current_procedure->sbind); |
| 1603 | gprs_ns2_start_alive_all_nsvcs(nse); |
| 1604 | break; |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1605 | case SNS_PROC_CHANGE_WEIGHT: |
| 1606 | switch (gss->family) { |
| 1607 | case AF_INET: |
| 1608 | proc4 = &gss->current_procedure->ip4; |
| 1609 | for (unsigned int i=0; i<gss->local.num_ip4; i++) { |
| 1610 | ip4 = &gss->local.ip4[i]; |
| 1611 | if (ip4->ip_addr != proc4->ip_addr || |
| 1612 | ip4->udp_port != proc4->udp_port) |
| 1613 | continue; |
| 1614 | ip4->sig_weight = proc4->sig_weight; |
| 1615 | ip4->data_weight = proc4->data_weight; |
| 1616 | break; |
| 1617 | } |
| 1618 | break; |
| 1619 | case AF_INET6: |
| 1620 | proc6 = &gss->current_procedure->ip6; |
| 1621 | for (unsigned int i=0; i<gss->local.num_ip6; i++) { |
| 1622 | ip6 = &gss->local.ip6[i]; |
| 1623 | if (memcmp(&ip6->ip_addr, &proc6->ip_addr, sizeof(proc6->ip_addr)) || |
| 1624 | ip6->udp_port != proc6->udp_port) { |
| 1625 | continue; |
| 1626 | } |
| 1627 | ip6->sig_weight = proc6->sig_weight; |
| 1628 | ip6->data_weight = proc6->data_weight; |
| 1629 | break; |
| 1630 | } |
| 1631 | break; |
| 1632 | default: |
| 1633 | OSMO_ASSERT(0); |
| 1634 | } |
| 1635 | break; |
| 1636 | default: |
| 1637 | break; |
| 1638 | } |
| 1639 | |
| 1640 | llist_del(&gss->current_procedure->list); |
| 1641 | talloc_free(gss->current_procedure); |
| 1642 | gss->current_procedure = NULL; |
| 1643 | |
| 1644 | if (llist_empty(&gss->procedures)) |
| 1645 | osmo_fsm_inst_state_chg(gss->nse->bss_sns_fi, GPRS_SNS_ST_CONFIGURED, |
| 1646 | 0, 0); |
| 1647 | else |
| 1648 | osmo_fsm_inst_state_chg(gss->nse->bss_sns_fi, GPRS_SNS_ST_LOCAL_PROCEDURE, |
| 1649 | gss->nse->nsi->timeout[NS_TOUT_TSNS_PROV], 5); |
| 1650 | break; |
| 1651 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | static const struct osmo_fsm_state ns2_sns_bss_states[] = { |
| 1655 | [GPRS_SNS_ST_UNCONFIGURED] = { |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1656 | .in_event_mask = 0, /* handled by all_state_action */ |
Alexander Couzens | 0a7c5ee | 2021-04-10 18:20:21 +0200 | [diff] [blame] | 1657 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
| 1658 | S(GPRS_SNS_ST_BSS_SIZE), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1659 | .name = "UNCONFIGURED", |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1660 | .action = ns2_sns_st_bss_unconfigured, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1661 | }, |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1662 | [GPRS_SNS_ST_BSS_SIZE] = { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 1663 | .in_event_mask = S(NS2_SNS_EV_RX_SIZE_ACK), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1664 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1665 | S(GPRS_SNS_ST_BSS_SIZE) | |
| 1666 | S(GPRS_SNS_ST_BSS_CONFIG_BSS), |
| 1667 | .name = "BSS_SIZE", |
| 1668 | .action = ns2_sns_st_bss_size, |
| 1669 | .onenter = ns2_sns_st_bss_size_onenter, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1670 | }, |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1671 | [GPRS_SNS_ST_BSS_CONFIG_BSS] = { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 1672 | .in_event_mask = S(NS2_SNS_EV_RX_CONFIG_ACK), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1673 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1674 | S(GPRS_SNS_ST_BSS_CONFIG_BSS) | |
| 1675 | S(GPRS_SNS_ST_BSS_CONFIG_SGSN) | |
| 1676 | S(GPRS_SNS_ST_BSS_SIZE), |
| 1677 | .name = "BSS_CONFIG_BSS", |
| 1678 | .action = ns2_sns_st_bss_config_bss, |
| 1679 | .onenter = ns2_sns_st_bss_config_bss_onenter, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1680 | }, |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1681 | [GPRS_SNS_ST_BSS_CONFIG_SGSN] = { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 1682 | .in_event_mask = S(NS2_SNS_EV_RX_CONFIG) | |
| 1683 | S(NS2_SNS_EV_RX_CONFIG_END), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1684 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1685 | S(GPRS_SNS_ST_BSS_CONFIG_SGSN) | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1686 | S(GPRS_SNS_ST_CONFIGURED) | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1687 | S(GPRS_SNS_ST_BSS_SIZE), |
| 1688 | .name = "BSS_CONFIG_SGSN", |
| 1689 | .action = ns2_sns_st_bss_config_sgsn, |
| 1690 | .onenter = ns2_sns_st_bss_config_sgsn_onenter, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1691 | }, |
| 1692 | [GPRS_SNS_ST_CONFIGURED] = { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 1693 | .in_event_mask = S(NS2_SNS_EV_RX_ADD) | |
| 1694 | S(NS2_SNS_EV_RX_DELETE) | |
| 1695 | S(NS2_SNS_EV_RX_CHANGE_WEIGHT) | |
| 1696 | S(NS2_SNS_EV_REQ_NSVC_ALIVE), |
Alexander Couzens | e03d863 | 2020-12-06 03:31:44 +0100 | [diff] [blame] | 1697 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1698 | S(GPRS_SNS_ST_BSS_SIZE) | |
| 1699 | S(GPRS_SNS_ST_LOCAL_PROCEDURE), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1700 | .name = "CONFIGURED", |
| 1701 | .action = ns2_sns_st_configured, |
| 1702 | .onenter = ns2_sns_st_configured_onenter, |
| 1703 | }, |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1704 | [GPRS_SNS_ST_LOCAL_PROCEDURE] = { |
| 1705 | .in_event_mask = S(NS2_SNS_EV_RX_ADD) | |
| 1706 | S(NS2_SNS_EV_RX_DELETE) | |
| 1707 | S(NS2_SNS_EV_RX_CHANGE_WEIGHT) | |
| 1708 | S(NS2_SNS_EV_RX_ACK) | |
| 1709 | S(NS2_SNS_EV_REQ_NSVC_ALIVE), |
| 1710 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
| 1711 | S(GPRS_SNS_ST_BSS_SIZE) | |
| 1712 | S(GPRS_SNS_ST_CONFIGURED) | |
| 1713 | S(GPRS_SNS_ST_LOCAL_PROCEDURE), |
| 1714 | .name = "LOCAL_PROCEDURE", |
| 1715 | .action = ns2_sns_st_local_procedure, |
| 1716 | .onenter = ns2_sns_st_local_procedure_onenter, |
| 1717 | }, |
| 1718 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1719 | }; |
| 1720 | |
| 1721 | static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi) |
| 1722 | { |
Alexander Couzens | 90ee963 | 2020-12-07 06:18:32 +0100 | [diff] [blame] | 1723 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1724 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 1725 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 1726 | |
Alexander Couzens | 90ee963 | 2020-12-07 06:18:32 +0100 | [diff] [blame] | 1727 | gss->N++; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1728 | switch (fi->T) { |
| 1729 | case 1: |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1730 | if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) { |
Alexander Couzens | 652ab4d | 2021-06-12 23:09:46 +0200 | [diff] [blame] | 1731 | sns_failed(fi, "Size retries failed. Selecting next IP-SNS endpoint."); |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1732 | } else { |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1733 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_SIZE, nsi->timeout[NS_TOUT_TSNS_PROV], 1); |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1734 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1735 | break; |
| 1736 | case 2: |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1737 | if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) { |
Alexander Couzens | 652ab4d | 2021-06-12 23:09:46 +0200 | [diff] [blame] | 1738 | sns_failed(fi, "BSS Config retries failed. Selecting next IP-SNS endpoint"); |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1739 | } else { |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1740 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS, nsi->timeout[NS_TOUT_TSNS_PROV], 2); |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1741 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1742 | break; |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1743 | case 3: |
Alexander Couzens | 3df5886 | 2021-02-05 17:18:08 +0100 | [diff] [blame] | 1744 | if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) { |
Alexander Couzens | 652ab4d | 2021-06-12 23:09:46 +0200 | [diff] [blame] | 1745 | sns_failed(fi, "SGSN Config retries failed. Selecting next IP-SNS endpoint."); |
Alexander Couzens | 3df5886 | 2021-02-05 17:18:08 +0100 | [diff] [blame] | 1746 | } else { |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1747 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_SGSN, nsi->timeout[NS_TOUT_TSNS_PROV], 3); |
Alexander Couzens | 3df5886 | 2021-02-05 17:18:08 +0100 | [diff] [blame] | 1748 | } |
| 1749 | break; |
| 1750 | case 4: |
Alexander Couzens | 652ab4d | 2021-06-12 23:09:46 +0200 | [diff] [blame] | 1751 | sns_failed(fi, "Config succeeded but no NS-VC came online. Selecting next IP-SNS endpoint."); |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1752 | break; |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1753 | case 5: |
| 1754 | if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) { |
| 1755 | sns_failed(fi, "SNS Procedure retries failed."); |
| 1756 | } else { |
| 1757 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_LOCAL_PROCEDURE, nsi->timeout[NS_TOUT_TSNS_PROV], 5); |
| 1758 | } |
| 1759 | break; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1760 | } |
| 1761 | return 0; |
| 1762 | } |
| 1763 | |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1764 | static struct gprs_ns_ie_ip4_elem *ns2_get_sbind_ip4_entry(struct ns2_sns_state *gss, |
| 1765 | struct ns2_sns_bind *sbind, |
| 1766 | struct ns2_sns_elems *endpoints) |
| 1767 | { |
| 1768 | const struct osmo_sockaddr *addr; |
| 1769 | struct gprs_ns_ie_ip4_elem *ip4; |
| 1770 | |
| 1771 | if (gss->family != AF_INET) |
| 1772 | return NULL; |
| 1773 | |
| 1774 | addr = gprs_ns2_ip_bind_sockaddr(sbind->bind); |
| 1775 | if (addr->u.sa.sa_family != AF_INET) |
| 1776 | return NULL; |
| 1777 | |
| 1778 | for (unsigned int i=0; i<endpoints->num_ip4; i++) { |
| 1779 | ip4 = &endpoints->ip4[i]; |
| 1780 | if (ip4->ip_addr == addr->u.sin.sin_addr.s_addr && |
| 1781 | ip4->udp_port == addr->u.sin.sin_port) |
| 1782 | return ip4; |
| 1783 | } |
| 1784 | |
| 1785 | return NULL; |
| 1786 | } |
| 1787 | |
| 1788 | static struct gprs_ns_ie_ip6_elem *ns2_get_sbind_ip6_entry(struct ns2_sns_state *gss, |
| 1789 | struct ns2_sns_bind *sbind, |
| 1790 | struct ns2_sns_elems *endpoints) |
| 1791 | { |
| 1792 | const struct osmo_sockaddr *addr; |
| 1793 | struct gprs_ns_ie_ip6_elem *ip6; |
| 1794 | |
| 1795 | if (gss->family != AF_INET6) |
| 1796 | return NULL; |
| 1797 | |
| 1798 | addr = gprs_ns2_ip_bind_sockaddr(sbind->bind); |
| 1799 | if (addr->u.sa.sa_family != AF_INET6) |
| 1800 | return NULL; |
| 1801 | |
| 1802 | for (unsigned int i=0; i<endpoints->num_ip6; i++) { |
| 1803 | ip6 = &endpoints->ip6[i]; |
| 1804 | if (memcmp(&ip6->ip_addr, &addr->u.sin6.sin6_addr, sizeof(ip6->ip_addr)) || |
| 1805 | ip6->udp_port != addr->u.sin6.sin6_port) |
| 1806 | return ip6; |
| 1807 | } |
| 1808 | |
| 1809 | return NULL; |
| 1810 | } |
| 1811 | |
| 1812 | /* return != 0 if the resulting weight is invalid. return 1 if sbind doesn't have an entry */ |
| 1813 | static int ns2_update_weight_entry(struct ns2_sns_state *gss, struct ns2_sns_bind *sbind, |
| 1814 | struct ns2_sns_elems *endpoints) |
| 1815 | { |
| 1816 | struct gprs_ns_ie_ip4_elem *ip4; |
| 1817 | struct gprs_ns_ie_ip6_elem *ip6; |
| 1818 | |
| 1819 | switch (gss->family) { |
| 1820 | case AF_INET: |
| 1821 | ip4 = ns2_get_sbind_ip4_entry(gss, sbind, endpoints); |
| 1822 | if (!ip4) |
| 1823 | return 1; |
| 1824 | ip4->sig_weight = sbind->bind->sns_sig_weight; |
| 1825 | ip4->data_weight = sbind->bind->sns_data_weight; |
| 1826 | return (ip4_weight_sum_sig(endpoints) != 0 && ip4_weight_sum_data(endpoints) != 0); |
| 1827 | break; |
| 1828 | case AF_INET6: |
| 1829 | ip6 = ns2_get_sbind_ip6_entry(gss, sbind, endpoints); |
| 1830 | if (!ip6) |
| 1831 | return 1; |
| 1832 | ip6->sig_weight = sbind->bind->sns_sig_weight; |
| 1833 | ip6->data_weight = sbind->bind->sns_data_weight; |
| 1834 | return (ip6_weight_sum_sig(endpoints) != 0 && ip6_weight_sum_data(endpoints) != 0); |
| 1835 | break; |
| 1836 | default: |
| 1837 | OSMO_ASSERT(0); |
| 1838 | } |
| 1839 | } |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1840 | static void ns2_add_procedure(struct ns2_sns_state *gss, struct ns2_sns_bind *sbind, |
| 1841 | enum sns_procedure procedure_type) |
| 1842 | { |
| 1843 | struct ns2_sns_procedure *procedure = NULL; |
| 1844 | const struct osmo_sockaddr *saddr; |
| 1845 | saddr = gprs_ns2_ip_bind_sockaddr(sbind->bind); |
| 1846 | |
Alexander Couzens | 9cd39ac | 2021-06-06 18:57:56 +0200 | [diff] [blame^] | 1847 | OSMO_ASSERT(saddr->u.sa.sa_family == gss->family); |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1848 | |
| 1849 | switch (procedure_type) { |
Alexander Couzens | 9cd39ac | 2021-06-06 18:57:56 +0200 | [diff] [blame^] | 1850 | case SNS_PROC_ADD: |
| 1851 | break; |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1852 | case SNS_PROC_CHANGE_WEIGHT: |
| 1853 | llist_for_each_entry(procedure, &gss->procedures, list) { |
| 1854 | if (procedure->sbind == sbind && procedure->procedure == procedure_type && |
| 1855 | !procedure->running) { |
| 1856 | switch(gss->family) { |
| 1857 | case AF_INET: |
| 1858 | /* merge it with a previous procedure */ |
| 1859 | procedure->ip4.ip_addr = sbind->bind->sns_sig_weight; |
| 1860 | procedure->ip4.data_weight = sbind->bind->sns_data_weight; |
| 1861 | break; |
| 1862 | case AF_INET6: |
| 1863 | /* merge it with a previous procedure */ |
| 1864 | procedure->ip6.sig_weight = sbind->bind->sns_sig_weight; |
| 1865 | procedure->ip6.data_weight = sbind->bind->sns_data_weight; |
| 1866 | break; |
| 1867 | default: |
| 1868 | OSMO_ASSERT(0); |
| 1869 | } |
| 1870 | return; |
| 1871 | } |
| 1872 | } |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1873 | break; |
| 1874 | default: |
| 1875 | return; |
| 1876 | } |
| 1877 | |
Alexander Couzens | 9cd39ac | 2021-06-06 18:57:56 +0200 | [diff] [blame^] | 1878 | procedure = talloc_zero(gss, struct ns2_sns_procedure); |
| 1879 | if (!procedure) |
| 1880 | return; |
| 1881 | |
| 1882 | llist_add_tail(&procedure->list, &gss->procedures); |
| 1883 | procedure->sbind = sbind; |
| 1884 | procedure->procedure = procedure_type; |
| 1885 | procedure->sig_weight = sbind->bind->sns_sig_weight; |
| 1886 | procedure->data_weight = sbind->bind->sns_data_weight; |
| 1887 | |
| 1888 | switch(gss->family) { |
| 1889 | case AF_INET: |
| 1890 | procedure->ip4.ip_addr = saddr->u.sin.sin_addr.s_addr; |
| 1891 | procedure->ip4.udp_port = saddr->u.sin.sin_port; |
| 1892 | procedure->ip4.sig_weight = sbind->bind->sns_sig_weight; |
| 1893 | procedure->ip4.data_weight = sbind->bind->sns_data_weight; |
| 1894 | break; |
| 1895 | case AF_INET6: |
| 1896 | |
| 1897 | memcpy(&procedure->ip6.ip_addr, &saddr->u.sin6.sin6_addr, sizeof(struct in6_addr)); |
| 1898 | procedure->ip6.udp_port = saddr->u.sin.sin_port; |
| 1899 | procedure->ip6.sig_weight = sbind->bind->sns_sig_weight; |
| 1900 | procedure->ip6.data_weight = sbind->bind->sns_data_weight; |
| 1901 | break; |
| 1902 | default: |
| 1903 | OSMO_ASSERT(0); |
| 1904 | } |
| 1905 | |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1906 | if (gss->nse->bss_sns_fi->state == GPRS_SNS_ST_CONFIGURED) { |
| 1907 | osmo_fsm_inst_state_chg(gss->nse->bss_sns_fi, GPRS_SNS_ST_LOCAL_PROCEDURE, |
| 1908 | gss->nse->nsi->timeout[NS_TOUT_TSNS_PROV], 5); |
| 1909 | } |
| 1910 | } |
| 1911 | |
Alexander Couzens | 9cd39ac | 2021-06-06 18:57:56 +0200 | [diff] [blame^] | 1912 | /* add an entrypoint to sns_endpoints */ |
| 1913 | static int ns2_sns_add_elements(struct ns2_sns_state *gss, struct ns2_sns_bind *sbind, |
| 1914 | struct ns2_sns_elems *elems) |
| 1915 | { |
| 1916 | const struct osmo_sockaddr *saddr; |
| 1917 | struct gprs_ns_ie_ip4_elem ip4; |
| 1918 | struct gprs_ns_ie_ip6_elem ip6; |
| 1919 | int rc = -1; |
| 1920 | |
| 1921 | saddr = gprs_ns2_ip_bind_sockaddr(sbind->bind); |
| 1922 | OSMO_ASSERT(saddr->u.sa.sa_family == gss->family); |
| 1923 | |
| 1924 | switch (gss->family) { |
| 1925 | case AF_INET: |
| 1926 | ip4.ip_addr = saddr->u.sin.sin_addr.s_addr; |
| 1927 | ip4.udp_port= saddr->u.sin.sin_port; |
| 1928 | ip4.sig_weight = sbind->bind->sns_sig_weight; |
| 1929 | ip4.data_weight = sbind->bind->sns_data_weight; |
| 1930 | rc = add_ip4_elem(gss, elems, &ip4); |
| 1931 | break; |
| 1932 | case AF_INET6: |
| 1933 | memcpy(&ip6.ip_addr, &saddr->u.sin6.sin6_addr, sizeof(struct in6_addr)); |
| 1934 | ip6.udp_port= saddr->u.sin.sin_port; |
| 1935 | ip6.sig_weight = sbind->bind->sns_sig_weight; |
| 1936 | ip6.data_weight = sbind->bind->sns_data_weight; |
| 1937 | rc = add_ip6_elem(gss, elems, &ip6); |
| 1938 | break; |
| 1939 | } |
| 1940 | |
| 1941 | return rc; |
| 1942 | } |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1943 | |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 1944 | /* common allstate-action for both roles */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1945 | static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 1946 | { |
| 1947 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 1948 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1949 | struct ns2_sns_bind *sbind; |
| 1950 | struct gprs_ns2_vc *nsvc, *nsvc2; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1951 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1952 | switch (event) { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 1953 | case NS2_SNS_EV_REQ_ADD_BIND: |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1954 | sbind = data; |
| 1955 | switch (fi->state) { |
| 1956 | case GPRS_SNS_ST_UNCONFIGURED: |
Alexander Couzens | 9cd39ac | 2021-06-06 18:57:56 +0200 | [diff] [blame^] | 1957 | if (gss->role == GPRS_SNS_ROLE_BSS) |
| 1958 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1959 | break; |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1960 | case GPRS_SNS_ST_BSS_SIZE: |
Alexander Couzens | 9cd39ac | 2021-06-06 18:57:56 +0200 | [diff] [blame^] | 1961 | switch (gss->family) { |
| 1962 | case AF_INET: |
| 1963 | if (gss->num_max_ip4_remote <= gss->local.num_ip4 || |
| 1964 | gss->num_max_ip4_remote * (gss->local.num_ip4 + 1) > gss->num_max_nsvcs) { |
| 1965 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_SELECT_ENDPOINT, GPRS_SNS_FLAG_KEEP_SELECT_ENDPOINT_ORDER); |
| 1966 | return; |
| 1967 | } |
| 1968 | break; |
| 1969 | case AF_INET6: |
| 1970 | if (gss->num_max_ip6_remote <= gss->local.num_ip6 || |
| 1971 | gss->num_max_ip6_remote * (gss->local.num_ip6 + 1) > gss->num_max_nsvcs) { |
| 1972 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_SELECT_ENDPOINT, GPRS_SNS_FLAG_KEEP_SELECT_ENDPOINT_ORDER); |
| 1973 | return; |
| 1974 | } |
| 1975 | break; |
| 1976 | } |
| 1977 | ns2_sns_add_elements(gss, sbind, &gss->local); |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1978 | break; |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1979 | case GPRS_SNS_ST_BSS_CONFIG_BSS: |
| 1980 | case GPRS_SNS_ST_BSS_CONFIG_SGSN: |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1981 | case GPRS_SNS_ST_CONFIGURED: |
Alexander Couzens | 9cd39ac | 2021-06-06 18:57:56 +0200 | [diff] [blame^] | 1982 | switch (gss->family) { |
| 1983 | case AF_INET: |
| 1984 | if (gss->num_max_ip4_remote <= gss->local.num_ip4) { |
| 1985 | LOGPFSML(fi, LOGL_ERROR, |
| 1986 | "NSE %d: ignoring bind %s because there are too many endpoints for the SNS.\n", |
| 1987 | nse->nsei, sbind->bind->name); |
| 1988 | return; |
| 1989 | } |
| 1990 | if (gss->remote.num_ip4 * (gss->local.num_ip4 + 1) > gss->num_max_nsvcs) { |
| 1991 | LOGPFSML(fi, LOGL_ERROR, |
| 1992 | "NSE %d: ignoring bind %s because there are too many endpoints for the SNS.\n", |
| 1993 | nse->nsei, sbind->bind->name); |
| 1994 | return; |
| 1995 | } |
| 1996 | break; |
| 1997 | case AF_INET6: |
| 1998 | if (gss->num_max_ip6_remote <= gss->local.num_ip6) { |
| 1999 | LOGPFSML(fi, LOGL_ERROR, |
| 2000 | "NSE %d: ignoring bind %s because there are too many endpoints for the SNS.\n", |
| 2001 | nse->nsei, sbind->bind->name); |
| 2002 | return; |
| 2003 | } |
| 2004 | if (gss->remote.num_ip6 * (gss->local.num_ip6 + 1) > gss->num_max_nsvcs) { |
| 2005 | LOGPFSML(fi, LOGL_ERROR, |
| 2006 | "NSE %d: ignoring bind %s because there are too many endpoints for the SNS.\n", |
| 2007 | nse->nsei, sbind->bind->name); |
| 2008 | return; |
| 2009 | } |
| 2010 | break; |
| 2011 | } |
| 2012 | ns2_sns_add_elements(gss, sbind, &gss->local_procedure); |
| 2013 | ns2_add_procedure(gss, sbind, SNS_PROC_ADD); |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 2014 | break; |
| 2015 | } |
| 2016 | break; |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2017 | case NS2_SNS_EV_REQ_DELETE_BIND: |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 2018 | sbind = data; |
| 2019 | switch (fi->state) { |
| 2020 | case GPRS_SNS_ST_UNCONFIGURED: |
| 2021 | break; |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 2022 | case GPRS_SNS_ST_BSS_SIZE: |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 2023 | /* TODO: remove the ip4 element from the list */ |
| 2024 | llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) { |
| 2025 | if (nsvc->bind == sbind->bind) { |
| 2026 | gprs_ns2_free_nsvc(nsvc); |
| 2027 | } |
| 2028 | } |
| 2029 | break; |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 2030 | case GPRS_SNS_ST_BSS_CONFIG_BSS: |
| 2031 | case GPRS_SNS_ST_BSS_CONFIG_SGSN: |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 2032 | case GPRS_SNS_ST_CONFIGURED: |
| 2033 | /* TODO: do an delete SNS-IP procedure */ |
| 2034 | /* TODO: remove the ip4 element to the list */ |
| 2035 | llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) { |
| 2036 | if (nsvc->bind == sbind->bind) { |
| 2037 | gprs_ns2_free_nsvc(nsvc); |
| 2038 | } |
| 2039 | } |
| 2040 | break; |
| 2041 | } |
| 2042 | /* if this is the last bind, the free_nsvc() will trigger a reselection */ |
| 2043 | talloc_free(sbind); |
| 2044 | break; |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 2045 | case NS2_SNS_EV_REQ_CHANGE_WEIGHT: |
| 2046 | sbind = data; |
| 2047 | switch (fi->state) { |
| 2048 | case GPRS_SNS_ST_UNCONFIGURED: |
| 2049 | /* select_endpoint will check if this is a valid configuration */ |
| 2050 | if (gss->role == GPRS_SNS_ROLE_BSS) |
| 2051 | osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
| 2052 | break; |
| 2053 | case GPRS_SNS_ST_BSS_SIZE: |
| 2054 | /* invalid weight? */ |
| 2055 | if (!ns2_update_weight_entry(gss, sbind, &gss->local)) |
| 2056 | sns_failed(fi, "updating weights results in an invalid configuration."); |
| 2057 | break; |
| 2058 | default: |
| 2059 | if (!ns2_update_weight_entry(gss, sbind, &gss->local_procedure)) { |
| 2060 | sns_failed(fi, "updating weights results in an invalid configuration."); |
| 2061 | break; |
| 2062 | } |
| 2063 | ns2_add_procedure(gss, sbind, SNS_PROC_CHANGE_WEIGHT); |
| 2064 | break; |
| 2065 | } |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 2066 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2067 | } |
| 2068 | |
Alexander Couzens | 31d52e1 | 2021-06-05 20:04:04 +0200 | [diff] [blame] | 2069 | /* validate the bss configuration (sns endpoint and binds) |
| 2070 | * - no endpoints -> invalid |
| 2071 | * - no binds -> invalid |
| 2072 | * - only v4 sns endpoints, only v6 binds -> invalid |
| 2073 | * - only v4 sns endpoints, but v4 sig weights == 0 -> invalid ... |
| 2074 | */ |
| 2075 | static int ns2_sns_bss_valid_configuration(struct ns2_sns_state *gss) |
| 2076 | { |
| 2077 | struct ns2_sns_bind *sbind; |
| 2078 | struct sns_endpoint *endpoint; |
| 2079 | const struct osmo_sockaddr *addr; |
| 2080 | int v4_sig = 0, v4_data = 0, v6_sig = 0, v6_data = 0; |
| 2081 | bool v4_endpoints = false; |
| 2082 | bool v6_endpoints = false; |
| 2083 | |
| 2084 | if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds)) |
| 2085 | return 0; |
| 2086 | |
| 2087 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 2088 | addr = gprs_ns2_ip_bind_sockaddr(sbind->bind); |
| 2089 | if (!addr) |
| 2090 | continue; |
| 2091 | switch (addr->u.sa.sa_family) { |
| 2092 | case AF_INET: |
| 2093 | v4_sig += sbind->bind->sns_sig_weight; |
| 2094 | v4_data += sbind->bind->sns_data_weight; |
| 2095 | break; |
| 2096 | case AF_INET6: |
| 2097 | v6_sig += sbind->bind->sns_sig_weight; |
| 2098 | v6_data += sbind->bind->sns_data_weight; |
| 2099 | break; |
| 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | llist_for_each_entry(endpoint, &gss->sns_endpoints, list) { |
| 2104 | switch (endpoint->saddr.u.sa.sa_family) { |
| 2105 | case AF_INET: |
| 2106 | v4_endpoints = true; |
| 2107 | break; |
| 2108 | case AF_INET6: |
| 2109 | v6_endpoints = true; |
| 2110 | break; |
| 2111 | } |
| 2112 | } |
| 2113 | |
| 2114 | return (v4_endpoints && v4_sig && v4_data) || (v6_endpoints && v6_sig && v6_data); |
| 2115 | } |
| 2116 | |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 2117 | /* allstate-action for BSS role */ |
| 2118 | static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 2119 | { |
| 2120 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2121 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 2122 | |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2123 | /* reset when receiving NS2_SNS_EV_REQ_NO_NSVC */ |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 2124 | switch (event) { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2125 | case NS2_SNS_EV_REQ_NO_NSVC: |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 2126 | /* ignore reselection running */ |
| 2127 | if (gss->reselection_running) |
| 2128 | break; |
| 2129 | |
Alexander Couzens | 652ab4d | 2021-06-12 23:09:46 +0200 | [diff] [blame] | 2130 | sns_failed(fi, "no remaining NSVC, resetting SNS FSM"); |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 2131 | break; |
Alexander Couzens | 83f06ce | 2021-08-06 19:50:09 +0200 | [diff] [blame] | 2132 | case NS2_SNS_EV_REQ_FREE_NSVCS: |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2133 | case NS2_SNS_EV_REQ_SELECT_ENDPOINT: |
Alexander Couzens | 9cd39ac | 2021-06-06 18:57:56 +0200 | [diff] [blame^] | 2134 | /* TODO: keep the order of binds when data == GPRS_SNS_FLAG_KEEP_SELECT_ENDPOINT_ORDER */ |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 2135 | /* tear down previous state |
| 2136 | * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */ |
| 2137 | gss->reselection_running = true; |
Alexander Couzens | f074659 | 2021-07-20 19:05:45 +0200 | [diff] [blame] | 2138 | ns2_free_nsvcs(nse); |
Alexander Couzens | d2c6c49 | 2021-06-06 01:57:52 +0200 | [diff] [blame] | 2139 | ns2_clear_elems(&gss->local); |
| 2140 | ns2_clear_elems(&gss->remote); |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 2141 | |
| 2142 | /* Choose the next sns endpoint. */ |
Alexander Couzens | 31d52e1 | 2021-06-05 20:04:04 +0200 | [diff] [blame] | 2143 | if (!ns2_sns_bss_valid_configuration(gss)) { |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 2144 | gss->initial = NULL; |
| 2145 | ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS); |
| 2146 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3); |
| 2147 | return; |
| 2148 | } else if (!gss->initial) { |
| 2149 | gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list); |
| 2150 | } else if (gss->initial->list.next == &gss->sns_endpoints) { |
| 2151 | /* last entry, continue with first */ |
| 2152 | gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list); |
| 2153 | } else { |
| 2154 | /* next element is an entry */ |
| 2155 | gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list); |
| 2156 | } |
| 2157 | |
Alexander Couzens | 68ab9c4 | 2021-06-06 03:03:40 +0200 | [diff] [blame] | 2158 | gss->family = gss->initial->saddr.u.sa.sa_family; |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 2159 | gss->reselection_running = false; |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 2160 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_SIZE, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 1); |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 2161 | break; |
| 2162 | default: |
| 2163 | ns2_sns_st_all_action(fi, event, data); |
| 2164 | break; |
| 2165 | } |
| 2166 | } |
| 2167 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2168 | static struct osmo_fsm gprs_ns2_sns_bss_fsm = { |
| 2169 | .name = "GPRS-NS2-SNS-BSS", |
| 2170 | .states = ns2_sns_bss_states, |
| 2171 | .num_states = ARRAY_SIZE(ns2_sns_bss_states), |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2172 | .allstate_event_mask = S(NS2_SNS_EV_REQ_NO_NSVC) | |
Alexander Couzens | 83f06ce | 2021-08-06 19:50:09 +0200 | [diff] [blame] | 2173 | S(NS2_SNS_EV_REQ_FREE_NSVCS) | |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2174 | S(NS2_SNS_EV_REQ_SELECT_ENDPOINT) | |
| 2175 | S(NS2_SNS_EV_REQ_ADD_BIND) | |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 2176 | S(NS2_SNS_EV_REQ_CHANGE_WEIGHT) | |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2177 | S(NS2_SNS_EV_REQ_DELETE_BIND), |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 2178 | .allstate_action = ns2_sns_st_all_action_bss, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2179 | .cleanup = NULL, |
| 2180 | .timer_cb = ns2_sns_fsm_bss_timer_cb, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2181 | .event_names = gprs_sns_event_names, |
| 2182 | .pre_term = NULL, |
| 2183 | .log_subsys = DLNS, |
| 2184 | }; |
| 2185 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 2186 | /*! Allocate an IP-SNS FSM for the BSS side. |
| 2187 | * \param[in] nse NS Entity in which the FSM runs |
| 2188 | * \param[in] id string identifier |
Alexander Couzens | 23aec35 | 2021-02-15 05:05:15 +0100 | [diff] [blame] | 2189 | * \returns FSM instance on success; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2190 | struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse, |
| 2191 | const char *id) |
| 2192 | { |
| 2193 | struct osmo_fsm_inst *fi; |
| 2194 | struct ns2_sns_state *gss; |
| 2195 | |
| 2196 | fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id); |
| 2197 | if (!fi) |
| 2198 | return fi; |
| 2199 | |
| 2200 | gss = talloc_zero(fi, struct ns2_sns_state); |
| 2201 | if (!gss) |
| 2202 | goto err; |
| 2203 | |
| 2204 | fi->priv = gss; |
| 2205 | gss->nse = nse; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2206 | gss->role = GPRS_SNS_ROLE_BSS; |
Harald Welte | 24f4df5 | 2021-03-04 18:02:54 +0100 | [diff] [blame] | 2207 | /* The SGSN doesn't tell the BSS, so we assume there's always sufficient */ |
| 2208 | gss->num_max_ip4_remote = 8192; |
| 2209 | gss->num_max_ip6_remote = 8192; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 2210 | INIT_LLIST_HEAD(&gss->sns_endpoints); |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 2211 | INIT_LLIST_HEAD(&gss->binds); |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 2212 | INIT_LLIST_HEAD(&gss->procedures); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2213 | |
| 2214 | return fi; |
| 2215 | err: |
| 2216 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 2217 | return NULL; |
| 2218 | } |
| 2219 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 2220 | /*! main entry point for receiving SNS messages from the network. |
| 2221 | * \param[in] nsvc NS-VC on which the message was received |
| 2222 | * \param[in] msg message buffer of the IP-SNS message |
| 2223 | * \param[in] tp parsed TLV structure of message |
Alexander Couzens | 23aec35 | 2021-02-15 05:05:15 +0100 | [diff] [blame] | 2224 | * \returns 0 on success; negative on error */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 2225 | int ns2_sns_rx(struct gprs_ns2_vc *nsvc, struct msgb *msg, struct tlv_parsed *tp) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2226 | { |
| 2227 | struct gprs_ns2_nse *nse = nsvc->nse; |
| 2228 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 2229 | uint16_t nsei = nsvc->nse->nsei; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2230 | struct ns2_sns_state *gss; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2231 | struct osmo_fsm_inst *fi; |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 2232 | int rc = 0; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2233 | |
| 2234 | if (!nse->bss_sns_fi) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 2235 | LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n", |
| 2236 | get_value_string(gprs_ns_pdu_strings, nsh->pdu_type)); |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 2237 | rc = -EINVAL; |
| 2238 | goto out; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2239 | } |
| 2240 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2241 | /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */ |
| 2242 | fi = nse->bss_sns_fi; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2243 | gss = (struct ns2_sns_state *) fi->priv; |
| 2244 | if (!gss->sns_nsvc) |
| 2245 | gss->sns_nsvc = nsvc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2246 | |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 2247 | LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei, |
| 2248 | get_value_string(gprs_ns_pdu_strings, nsh->pdu_type)); |
| 2249 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2250 | switch (nsh->pdu_type) { |
| 2251 | case SNS_PDUT_SIZE: |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2252 | osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_SIZE, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2253 | break; |
| 2254 | case SNS_PDUT_SIZE_ACK: |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2255 | osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_SIZE_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2256 | break; |
| 2257 | case SNS_PDUT_CONFIG: |
| 2258 | if (nsh->data[0] & 0x01) |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2259 | osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_CONFIG_END, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2260 | else |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2261 | osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_CONFIG, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2262 | break; |
| 2263 | case SNS_PDUT_CONFIG_ACK: |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2264 | osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_CONFIG_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2265 | break; |
| 2266 | case SNS_PDUT_ADD: |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2267 | osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_ADD, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2268 | break; |
| 2269 | case SNS_PDUT_DELETE: |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2270 | osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_DELETE, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2271 | break; |
| 2272 | case SNS_PDUT_CHANGE_WEIGHT: |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2273 | osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_CHANGE_WEIGHT, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2274 | break; |
| 2275 | case SNS_PDUT_ACK: |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2276 | osmo_fsm_inst_dispatch(fi, NS2_SNS_EV_RX_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2277 | break; |
| 2278 | default: |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 2279 | LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei, |
| 2280 | get_value_string(gprs_ns_pdu_strings, nsh->pdu_type)); |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 2281 | rc = -EINVAL; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2282 | } |
| 2283 | |
Alexander Couzens | 7619ed4 | 2021-03-24 17:44:03 +0100 | [diff] [blame] | 2284 | out: |
| 2285 | msgb_free(msg); |
| 2286 | |
| 2287 | return rc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2288 | } |
| 2289 | |
| 2290 | #include <osmocom/vty/vty.h> |
| 2291 | #include <osmocom/vty/misc.h> |
| 2292 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 2293 | static void vty_dump_sns_ip4(struct vty *vty, const char *prefix, const struct gprs_ns_ie_ip4_elem *ip4) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2294 | { |
| 2295 | struct in_addr in = { .s_addr = ip4->ip_addr }; |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 2296 | vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2297 | inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE); |
| 2298 | } |
| 2299 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 2300 | static void vty_dump_sns_ip6(struct vty *vty, const char *prefix, const struct gprs_ns_ie_ip6_elem *ip6) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2301 | { |
| 2302 | char ip_addr[INET6_ADDRSTRLEN] = {}; |
| 2303 | if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN))) |
| 2304 | strcpy(ip_addr, "Invalid IPv6"); |
| 2305 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 2306 | vty_out(vty, "%s %s:%u, Signalling Weight: %u, Data Weight: %u%s", prefix, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2307 | ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE); |
| 2308 | } |
| 2309 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 2310 | /*! Dump the IP-SNS state to a vty. |
| 2311 | * \param[in] vty VTY to which the state shall be printed |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 2312 | * \param[in] prefix prefix to print at start of each line (typically indenting) |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 2313 | * \param[in] nse NS Entity whose IP-SNS state shall be printed |
| 2314 | * \param[in] stats Whether or not statistics shall also be printed */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 2315 | void ns2_sns_dump_vty(struct vty *vty, const char *prefix, const struct gprs_ns2_nse *nse, bool stats) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2316 | { |
| 2317 | struct ns2_sns_state *gss; |
| 2318 | unsigned int i; |
| 2319 | |
| 2320 | if (!nse->bss_sns_fi) |
| 2321 | return; |
| 2322 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 2323 | vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2324 | gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv; |
| 2325 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 2326 | vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s", |
| 2327 | prefix, gss->num_max_nsvcs, gss->num_max_ip4_remote, gss->num_max_ip6_remote, VTY_NEWLINE); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2328 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2329 | if (gss->local.num_ip4 && gss->remote.num_ip4) { |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 2330 | vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE); |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2331 | for (i = 0; i < gss->local.num_ip4; i++) |
| 2332 | vty_dump_sns_ip4(vty, prefix, &gss->local.ip4[i]); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2333 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 2334 | vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE); |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2335 | for (i = 0; i < gss->remote.num_ip4; i++) |
| 2336 | vty_dump_sns_ip4(vty, prefix, &gss->remote.ip4[i]); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2337 | } |
| 2338 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2339 | if (gss->local.num_ip6 && gss->remote.num_ip6) { |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 2340 | vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE); |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2341 | for (i = 0; i < gss->local.num_ip6; i++) |
| 2342 | vty_dump_sns_ip6(vty, prefix, &gss->local.ip6[i]); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2343 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 2344 | vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE); |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2345 | for (i = 0; i < gss->remote.num_ip6; i++) |
| 2346 | vty_dump_sns_ip6(vty, prefix, &gss->remote.ip6[i]); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2347 | } |
| 2348 | } |
| 2349 | |
Alexander Couzens | 412bc34 | 2020-11-19 05:24:37 +0100 | [diff] [blame] | 2350 | /*! write IP-SNS to a vty |
| 2351 | * \param[in] vty VTY to which the state shall be printed |
| 2352 | * \param[in] nse NS Entity whose IP-SNS state shall be printed */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 2353 | void ns2_sns_write_vty(struct vty *vty, const struct gprs_ns2_nse *nse) |
Alexander Couzens | 412bc34 | 2020-11-19 05:24:37 +0100 | [diff] [blame] | 2354 | { |
| 2355 | struct ns2_sns_state *gss; |
| 2356 | struct osmo_sockaddr_str addr_str; |
| 2357 | struct sns_endpoint *endpoint; |
| 2358 | |
| 2359 | if (!nse->bss_sns_fi) |
| 2360 | return; |
| 2361 | |
| 2362 | gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv; |
| 2363 | llist_for_each_entry(endpoint, &gss->sns_endpoints, list) { |
Vadim Yanitskiy | d8b7003 | 2021-01-05 14:24:09 +0100 | [diff] [blame] | 2364 | /* It's unlikely that an error happens, but let's better be safe. */ |
| 2365 | if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0) |
| 2366 | addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" }; |
Alexander Couzens | 5fa431c | 2021-02-08 23:21:54 +0100 | [diff] [blame] | 2367 | vty_out(vty, " ip-sns-remote %s %u%s", addr_str.ip, addr_str.port, VTY_NEWLINE); |
Alexander Couzens | 412bc34 | 2020-11-19 05:24:37 +0100 | [diff] [blame] | 2368 | } |
| 2369 | } |
| 2370 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 2371 | static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state, |
| 2372 | const struct osmo_sockaddr *saddr) |
| 2373 | { |
| 2374 | struct sns_endpoint *endpoint; |
| 2375 | |
| 2376 | llist_for_each_entry(endpoint, &state->sns_endpoints, list) { |
| 2377 | if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr)) |
| 2378 | return endpoint; |
| 2379 | } |
| 2380 | |
| 2381 | return NULL; |
| 2382 | } |
| 2383 | |
| 2384 | /*! gprs_ns2_sns_add_endpoint |
| 2385 | * \param[in] nse |
| 2386 | * \param[in] sockaddr |
| 2387 | * \return |
| 2388 | */ |
| 2389 | int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse, |
| 2390 | const struct osmo_sockaddr *saddr) |
| 2391 | { |
| 2392 | struct ns2_sns_state *gss; |
| 2393 | struct sns_endpoint *endpoint; |
| 2394 | bool do_selection = false; |
| 2395 | |
| 2396 | if (nse->ll != GPRS_NS2_LL_UDP) { |
| 2397 | return -EINVAL; |
| 2398 | } |
| 2399 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 2400 | if (nse->dialect != GPRS_NS2_DIALECT_SNS) { |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 2401 | return -EINVAL; |
| 2402 | } |
| 2403 | |
| 2404 | gss = nse->bss_sns_fi->priv; |
| 2405 | |
| 2406 | if (ns2_get_sns_endpoint(gss, saddr)) |
| 2407 | return -EADDRINUSE; |
| 2408 | |
| 2409 | endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint); |
| 2410 | if (!endpoint) |
| 2411 | return -ENOMEM; |
| 2412 | |
| 2413 | endpoint->saddr = *saddr; |
| 2414 | if (llist_empty(&gss->sns_endpoints)) |
| 2415 | do_selection = true; |
| 2416 | |
| 2417 | llist_add_tail(&endpoint->list, &gss->sns_endpoints); |
| 2418 | if (do_selection) |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2419 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 2420 | |
| 2421 | return 0; |
| 2422 | } |
| 2423 | |
| 2424 | /*! gprs_ns2_sns_del_endpoint |
| 2425 | * \param[in] nse |
| 2426 | * \param[in] sockaddr |
| 2427 | * \return 0 on success, otherwise < 0 |
| 2428 | */ |
| 2429 | int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse, |
| 2430 | const struct osmo_sockaddr *saddr) |
| 2431 | { |
| 2432 | struct ns2_sns_state *gss; |
| 2433 | struct sns_endpoint *endpoint; |
| 2434 | |
| 2435 | if (nse->ll != GPRS_NS2_LL_UDP) { |
| 2436 | return -EINVAL; |
| 2437 | } |
| 2438 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 2439 | if (nse->dialect != GPRS_NS2_DIALECT_SNS) { |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 2440 | return -EINVAL; |
| 2441 | } |
| 2442 | |
| 2443 | gss = nse->bss_sns_fi->priv; |
| 2444 | endpoint = ns2_get_sns_endpoint(gss, saddr); |
| 2445 | if (!endpoint) |
| 2446 | return -ENOENT; |
| 2447 | |
| 2448 | /* if this is an unused SNS endpoint it's done */ |
| 2449 | if (gss->initial != endpoint) { |
| 2450 | llist_del(&endpoint->list); |
| 2451 | talloc_free(endpoint); |
| 2452 | return 0; |
| 2453 | } |
| 2454 | |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2455 | /* gprs_ns2_free_nsvcs() will trigger NS2_SNS_EV_REQ_NO_NSVC on the last NS-VC |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 2456 | * and restart SNS SIZE procedure which selects a new initial */ |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 2457 | LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed." |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 2458 | "Closing all NS-VC and restart SNS-SIZE procedure" |
| 2459 | "with a remaining SNS endpoint.\n"); |
| 2460 | |
| 2461 | /* Continue with the next endpoint in the list. |
| 2462 | * Special case if the endpoint is at the start or end of the list */ |
| 2463 | if (endpoint->list.prev == &gss->sns_endpoints || |
| 2464 | endpoint->list.next == &gss->sns_endpoints) |
| 2465 | gss->initial = NULL; |
| 2466 | else |
| 2467 | gss->initial = llist_entry(endpoint->list.next->prev, |
| 2468 | struct sns_endpoint, |
| 2469 | list); |
| 2470 | |
| 2471 | llist_del(&endpoint->list); |
| 2472 | gprs_ns2_free_nsvcs(nse); |
| 2473 | talloc_free(endpoint); |
| 2474 | |
| 2475 | return 0; |
| 2476 | } |
| 2477 | |
| 2478 | /*! gprs_ns2_sns_count |
| 2479 | * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed |
| 2480 | * \return the count of endpoints or < 0 if NSE doesn't contain sns. |
| 2481 | */ |
| 2482 | int gprs_ns2_sns_count(struct gprs_ns2_nse *nse) |
| 2483 | { |
| 2484 | struct ns2_sns_state *gss; |
| 2485 | struct sns_endpoint *endpoint; |
| 2486 | int count = 0; |
| 2487 | |
| 2488 | if (nse->ll != GPRS_NS2_LL_UDP) { |
| 2489 | return -EINVAL; |
| 2490 | } |
| 2491 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 2492 | if (nse->dialect != GPRS_NS2_DIALECT_SNS) { |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 2493 | return -EINVAL; |
| 2494 | } |
| 2495 | |
| 2496 | gss = nse->bss_sns_fi->priv; |
| 2497 | llist_for_each_entry(endpoint, &gss->sns_endpoints, list) |
| 2498 | count++; |
| 2499 | |
| 2500 | return count; |
| 2501 | } |
| 2502 | |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 2503 | void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive) |
| 2504 | { |
| 2505 | struct ns2_sns_state *gss; |
| 2506 | struct gprs_ns2_vc *tmp; |
| 2507 | |
| 2508 | if (!nse->bss_sns_fi) |
| 2509 | return; |
| 2510 | |
| 2511 | gss = nse->bss_sns_fi->priv; |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 2512 | if (nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED && nse->bss_sns_fi->state != GPRS_SNS_ST_LOCAL_PROCEDURE) |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 2513 | return; |
| 2514 | |
| 2515 | if (alive == gss->alive) |
| 2516 | return; |
| 2517 | |
| 2518 | /* check if this is the current SNS NS-VC */ |
| 2519 | if (nsvc == gss->sns_nsvc) { |
| 2520 | /* only replace the SNS NS-VC if there are other alive NS-VC. |
| 2521 | * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED |
| 2522 | * and couldn't confirm yet if the NS-VC comes up */ |
| 2523 | if (gss->alive && !alive) |
| 2524 | ns2_sns_replace_nsvc(nsvc); |
| 2525 | } |
| 2526 | |
| 2527 | if (alive) { |
| 2528 | gss->alive = true; |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2529 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_NSVC_ALIVE, NULL); |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 2530 | } else { |
| 2531 | /* is there at least another alive nsvc? */ |
| 2532 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
| 2533 | if (ns2_vc_is_unblocked(tmp)) |
| 2534 | return; |
| 2535 | } |
| 2536 | |
| 2537 | /* all NS-VC have failed */ |
| 2538 | gss->alive = false; |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2539 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_NO_NSVC, NULL); |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 2540 | } |
| 2541 | } |
| 2542 | |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 2543 | int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse, |
| 2544 | struct gprs_ns2_vc_bind *bind) |
| 2545 | { |
| 2546 | struct ns2_sns_state *gss; |
| 2547 | struct ns2_sns_bind *tmp; |
| 2548 | |
| 2549 | OSMO_ASSERT(nse->bss_sns_fi); |
| 2550 | gss = nse->bss_sns_fi->priv; |
| 2551 | |
| 2552 | if (!gprs_ns2_is_ip_bind(bind)) { |
| 2553 | return -EINVAL; |
| 2554 | } |
| 2555 | |
| 2556 | if (!llist_empty(&gss->binds)) { |
| 2557 | llist_for_each_entry(tmp, &gss->binds, list) { |
| 2558 | if (tmp->bind == bind) |
| 2559 | return -EALREADY; |
| 2560 | } |
| 2561 | } |
| 2562 | |
| 2563 | tmp = talloc_zero(gss, struct ns2_sns_bind); |
| 2564 | if (!tmp) |
| 2565 | return -ENOMEM; |
| 2566 | tmp->bind = bind; |
| 2567 | llist_add_tail(&tmp->list, &gss->binds); |
| 2568 | |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2569 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_ADD_BIND, tmp); |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 2570 | return 0; |
| 2571 | } |
| 2572 | |
| 2573 | /* Remove a bind from the SNS. All assosiated NSVC must be removed. */ |
| 2574 | int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse, |
| 2575 | struct gprs_ns2_vc_bind *bind) |
| 2576 | { |
| 2577 | struct ns2_sns_state *gss; |
| 2578 | struct ns2_sns_bind *tmp, *tmp2; |
| 2579 | bool found = false; |
| 2580 | |
| 2581 | if (!nse->bss_sns_fi) |
| 2582 | return -EINVAL; |
| 2583 | |
| 2584 | gss = nse->bss_sns_fi->priv; |
| 2585 | if (gss->initial_bind && gss->initial_bind->bind == bind) { |
| 2586 | if (gss->initial_bind->list.prev == &gss->binds) |
| 2587 | gss->initial_bind = NULL; |
| 2588 | else |
| 2589 | gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list); |
| 2590 | } |
| 2591 | |
| 2592 | llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) { |
| 2593 | if (tmp->bind == bind) { |
| 2594 | llist_del(&tmp->list); |
| 2595 | found = true; |
Alexander Couzens | a35c296 | 2021-04-19 03:30:15 +0200 | [diff] [blame] | 2596 | break; |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 2597 | } |
| 2598 | } |
| 2599 | |
| 2600 | if (!found) |
| 2601 | return -ENOENT; |
| 2602 | |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2603 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, NS2_SNS_EV_REQ_DELETE_BIND, tmp); |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 2604 | return 0; |
| 2605 | } |
| 2606 | |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2607 | /* Update SNS weights for a bind (local endpoint). |
| 2608 | * \param[in] bind the bind which has been updated |
Alexander Couzens | c470476 | 2021-02-08 23:13:12 +0100 | [diff] [blame] | 2609 | */ |
| 2610 | void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind) |
| 2611 | { |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 2612 | struct ns2_sns_bind *sbind; |
| 2613 | struct gprs_ns2_nse *nse; |
| 2614 | struct ns2_sns_state *gss; |
| 2615 | const struct osmo_sockaddr *addr = gprs_ns2_ip_bind_sockaddr(bind); |
| 2616 | |
| 2617 | llist_for_each_entry(nse, &bind->nsi->nse, list) { |
| 2618 | if (!nse->bss_sns_fi) |
| 2619 | continue; |
| 2620 | |
| 2621 | gss = nse->bss_sns_fi->priv; |
| 2622 | if (addr->u.sa.sa_family != gss->family) |
| 2623 | return; |
| 2624 | |
| 2625 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 2626 | if (sbind->bind == bind) { |
| 2627 | osmo_fsm_inst_dispatch(gss->nse->bss_sns_fi, NS2_SNS_EV_REQ_CHANGE_WEIGHT, sbind); |
| 2628 | break; |
| 2629 | } |
| 2630 | } |
| 2631 | } |
Alexander Couzens | c470476 | 2021-02-08 23:13:12 +0100 | [diff] [blame] | 2632 | } |
| 2633 | |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2634 | |
| 2635 | |
| 2636 | |
| 2637 | /*********************************************************************** |
| 2638 | * SGSN role |
| 2639 | ***********************************************************************/ |
| 2640 | |
Alexander Couzens | a270782 | 2021-07-20 18:59:40 +0200 | [diff] [blame] | 2641 | /* cleanup all state. If nsvc is given, don't remove this nsvc. (nsvc is given when a SIZE PDU received) */ |
| 2642 | static void ns2_clear_sgsn(struct ns2_sns_state *gss, struct gprs_ns2_vc *size_nsvc) |
| 2643 | { |
| 2644 | struct gprs_ns2_vc *nsvc, *nsvc2; |
| 2645 | |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 2646 | ns2_clear_procedures(gss); |
Alexander Couzens | a270782 | 2021-07-20 18:59:40 +0200 | [diff] [blame] | 2647 | ns2_clear_elems(&gss->local); |
| 2648 | ns2_clear_elems(&gss->remote); |
| 2649 | llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) { |
| 2650 | /* Ignore the NSVC over which the SIZE PDU got received */ |
| 2651 | if (size_nsvc && size_nsvc == nsvc) |
| 2652 | continue; |
| 2653 | |
| 2654 | gprs_ns2_free_nsvc(nsvc); |
| 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | static void ns2_sns_st_sgsn_unconfigured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
| 2659 | { |
| 2660 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2661 | |
| 2662 | ns2_clear_sgsn(gss, NULL); |
| 2663 | } |
| 2664 | |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2665 | static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 2666 | { |
| 2667 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2668 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN); |
| 2669 | /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */ |
| 2670 | } |
| 2671 | |
| 2672 | /* We're waiting for inbound SNS-CONFIG from the BSS */ |
| 2673 | static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 2674 | { |
| 2675 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2676 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 2677 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 2678 | uint8_t cause; |
| 2679 | int rc; |
| 2680 | |
| 2681 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN); |
| 2682 | |
| 2683 | switch (event) { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2684 | case NS2_SNS_EV_RX_CONFIG: |
| 2685 | case NS2_SNS_EV_RX_CONFIG_END: |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2686 | rc = ns_sns_append_remote_eps(fi, data); |
| 2687 | if (rc < 0) { |
| 2688 | cause = -rc; |
| 2689 | ns2_tx_sns_config_ack(gss->sns_nsvc, &cause); |
| 2690 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
| 2691 | return; |
| 2692 | } |
| 2693 | /* only change state if last CONFIG was received */ |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2694 | if (event == NS2_SNS_EV_RX_CONFIG_END) { |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2695 | /* ensure sum of data weight / sig weights is > 0 */ |
Alexander Couzens | 019da4b | 2021-06-06 02:48:18 +0200 | [diff] [blame] | 2696 | if (ip46_weight_sum_data(&gss->remote) == 0 || ip46_weight_sum_sig(&gss->remote) == 0) { |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2697 | cause = NS_CAUSE_INVAL_WEIGH; |
| 2698 | ns2_tx_sns_config_ack(gss->sns_nsvc, &cause); |
| 2699 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
| 2700 | break; |
| 2701 | } |
| 2702 | ns2_tx_sns_config_ack(gss->sns_nsvc, NULL); |
| 2703 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3); |
| 2704 | } else { |
| 2705 | /* just send CONFIG-ACK */ |
| 2706 | ns2_tx_sns_config_ack(gss->sns_nsvc, NULL); |
| 2707 | osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0); |
| 2708 | } |
| 2709 | break; |
| 2710 | } |
| 2711 | } |
| 2712 | |
| 2713 | static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
| 2714 | { |
| 2715 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2716 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN); |
| 2717 | |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2718 | /* transmit SGSN-oriented SNS-CONFIG */ |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2719 | ns2_tx_sns_config(gss->sns_nsvc, true, gss->local.ip4, gss->local.num_ip4, |
| 2720 | gss->local.ip6, gss->local.num_ip6); |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | /* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */ |
| 2724 | static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 2725 | { |
| 2726 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2727 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 2728 | struct tlv_parsed *tp = NULL; |
| 2729 | |
| 2730 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN); |
| 2731 | |
| 2732 | switch (event) { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2733 | case NS2_SNS_EV_RX_CONFIG_ACK: |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2734 | tp = data; |
| 2735 | if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) { |
| 2736 | LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n", |
| 2737 | gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE))); |
| 2738 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
| 2739 | break; |
| 2740 | } |
| 2741 | /* we currently only send one SNS-CONFIG with END FLAG */ |
| 2742 | if (true) { |
| 2743 | create_missing_nsvcs(fi); |
| 2744 | /* start the test procedure on ALL NSVCs! */ |
| 2745 | gprs_ns2_start_alive_all_nsvcs(nse); |
| 2746 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4); |
| 2747 | } |
| 2748 | break; |
| 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | /* SGSN-side SNS state machine */ |
| 2753 | static const struct osmo_fsm_state ns2_sns_sgsn_states[] = { |
| 2754 | [GPRS_SNS_ST_UNCONFIGURED] = { |
| 2755 | .in_event_mask = 0, /* handled by all_state_action */ |
| 2756 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
| 2757 | S(GPRS_SNS_ST_SGSN_WAIT_CONFIG), |
| 2758 | .name = "UNCONFIGURED", |
| 2759 | .action = ns2_sns_st_sgsn_unconfigured, |
Alexander Couzens | a270782 | 2021-07-20 18:59:40 +0200 | [diff] [blame] | 2760 | .onenter = ns2_sns_st_sgsn_unconfigured_onenter, |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2761 | }, |
| 2762 | [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2763 | .in_event_mask = S(NS2_SNS_EV_RX_CONFIG) | |
| 2764 | S(NS2_SNS_EV_RX_CONFIG_END), |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2765 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
| 2766 | S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) | |
| 2767 | S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK), |
| 2768 | .name = "SGSN_WAIT_CONFIG", |
| 2769 | .action = ns2_sns_st_sgsn_wait_config, |
| 2770 | }, |
| 2771 | [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2772 | .in_event_mask = S(NS2_SNS_EV_RX_CONFIG_ACK), |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2773 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
Alexander Couzens | a270782 | 2021-07-20 18:59:40 +0200 | [diff] [blame] | 2774 | S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) | |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2775 | S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) | |
| 2776 | S(GPRS_SNS_ST_CONFIGURED), |
| 2777 | .name = "SGSN_WAIT_CONFIG_ACK", |
| 2778 | .action = ns2_sns_st_sgsn_wait_config_ack, |
| 2779 | .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter, |
| 2780 | }, |
| 2781 | [GPRS_SNS_ST_CONFIGURED] = { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2782 | .in_event_mask = S(NS2_SNS_EV_RX_ADD) | |
| 2783 | S(NS2_SNS_EV_RX_DELETE) | |
| 2784 | S(NS2_SNS_EV_RX_CHANGE_WEIGHT) | |
| 2785 | S(NS2_SNS_EV_REQ_NSVC_ALIVE), |
Alexander Couzens | a270782 | 2021-07-20 18:59:40 +0200 | [diff] [blame] | 2786 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 2787 | S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) | |
| 2788 | S(GPRS_SNS_ST_LOCAL_PROCEDURE), |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2789 | .name = "CONFIGURED", |
| 2790 | /* shared with BSS side; once configured there's no difference */ |
| 2791 | .action = ns2_sns_st_configured, |
| 2792 | .onenter = ns2_sns_st_configured_onenter, |
| 2793 | }, |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 2794 | [GPRS_SNS_ST_LOCAL_PROCEDURE] = { |
| 2795 | .in_event_mask = S(NS2_SNS_EV_RX_ADD) | |
| 2796 | S(NS2_SNS_EV_RX_DELETE) | |
| 2797 | S(NS2_SNS_EV_RX_CHANGE_WEIGHT) | |
| 2798 | S(NS2_SNS_EV_RX_ACK) | |
| 2799 | S(NS2_SNS_EV_REQ_CHANGE_WEIGHT) | |
| 2800 | S(NS2_SNS_EV_REQ_NSVC_ALIVE), |
| 2801 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
| 2802 | S(GPRS_SNS_ST_CONFIGURED) | |
| 2803 | S(GPRS_SNS_ST_LOCAL_PROCEDURE), |
| 2804 | .name = "LOCAL_PROCEDURE", |
| 2805 | /* shared with BSS side; once configured there's no difference */ |
| 2806 | .action = ns2_sns_st_local_procedure, |
| 2807 | .onenter = ns2_sns_st_local_procedure_onenter, |
| 2808 | }, |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2809 | }; |
| 2810 | |
| 2811 | static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi) |
| 2812 | { |
| 2813 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2814 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 2815 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 2816 | |
| 2817 | gss->N++; |
| 2818 | switch (fi->T) { |
| 2819 | case 3: |
| 2820 | if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) { |
| 2821 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei); |
| 2822 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3); |
| 2823 | } else { |
| 2824 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3); |
| 2825 | } |
| 2826 | break; |
| 2827 | case 4: |
| 2828 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei); |
| 2829 | break; |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 2830 | case 5: |
| 2831 | if (gss->N >= nsi->timeout[NS_TOUT_TSNS_PROCEDURES_RETRIES]) { |
| 2832 | sns_failed(fi, "SNS Procedure retries failed."); |
| 2833 | } else { |
| 2834 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_LOCAL_PROCEDURE, nsi->timeout[NS_TOUT_TSNS_PROV], |
| 2835 | fi->T); |
| 2836 | } |
| 2837 | break; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2838 | } |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 2839 | |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2840 | return 0; |
| 2841 | } |
| 2842 | |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2843 | /* allstate-action for SGSN role */ |
| 2844 | static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 2845 | { |
| 2846 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2847 | struct tlv_parsed *tp = NULL; |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2848 | size_t num_local_eps, num_remote_eps; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2849 | uint8_t flag; |
Harald Welte | a2c5af5 | 2021-03-04 17:59:35 +0100 | [diff] [blame] | 2850 | uint8_t cause; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2851 | |
| 2852 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN); |
| 2853 | |
| 2854 | switch (event) { |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2855 | case NS2_SNS_EV_RX_SIZE: |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2856 | tp = (struct tlv_parsed *) data; |
Harald Welte | a2c5af5 | 2021-03-04 17:59:35 +0100 | [diff] [blame] | 2857 | /* check for mandatory / conditional IEs */ |
| 2858 | if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1) || |
| 2859 | !TLVP_PRES_LEN(tp, NS_IE_MAX_NR_NSVC, 2)) { |
| 2860 | cause = NS_CAUSE_MISSING_ESSENT_IE; |
| 2861 | ns2_tx_sns_size_ack(gss->sns_nsvc, &cause); |
Alexander Couzens | 1c40525 | 2021-06-13 00:14:48 +0200 | [diff] [blame] | 2862 | if (fi->state == GPRS_SNS_ST_UNCONFIGURED) |
| 2863 | sns_failed(fi, "Rx Size: Missing essential IE"); |
Harald Welte | a2c5af5 | 2021-03-04 17:59:35 +0100 | [diff] [blame] | 2864 | break; |
| 2865 | } |
| 2866 | if (!TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2) && |
| 2867 | !TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) { |
| 2868 | cause = NS_CAUSE_MISSING_ESSENT_IE; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2869 | ns2_tx_sns_size_ack(gss->sns_nsvc, &cause); |
Alexander Couzens | 1c40525 | 2021-06-13 00:14:48 +0200 | [diff] [blame] | 2870 | if (fi->state == GPRS_SNS_ST_UNCONFIGURED) |
| 2871 | sns_failed(fi, "Rx Size: Missing essential IE"); |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2872 | break; |
| 2873 | } |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2874 | if (TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2)) |
| 2875 | gss->num_max_ip4_remote = tlvp_val16be(tp, NS_IE_IPv4_EP_NR); |
| 2876 | if (TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) |
| 2877 | gss->num_max_ip6_remote = tlvp_val16be(tp, NS_IE_IPv6_EP_NR); |
| 2878 | /* decide if we go for IPv4 or IPv6 */ |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 2879 | if (gss->num_max_ip6_remote && ns2_sns_count_num_local_ep(fi, AF_INET6)) { |
| 2880 | gss->family = AF_INET6; |
Harald Welte | 2d807b6 | 2021-03-24 01:57:30 +0100 | [diff] [blame] | 2881 | ns2_sns_compute_local_ep_from_binds(fi); |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2882 | num_local_eps = gss->local.num_ip6; |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2883 | num_remote_eps = gss->num_max_ip6_remote; |
Alexander Couzens | 077ce5a | 2021-06-06 01:32:45 +0200 | [diff] [blame] | 2884 | } else if (gss->num_max_ip4_remote && ns2_sns_count_num_local_ep(fi, AF_INET)) { |
| 2885 | gss->family = AF_INET; |
Harald Welte | 2d807b6 | 2021-03-24 01:57:30 +0100 | [diff] [blame] | 2886 | ns2_sns_compute_local_ep_from_binds(fi); |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2887 | num_local_eps = gss->local.num_ip4; |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2888 | num_remote_eps = gss->num_max_ip4_remote; |
| 2889 | } else { |
Alexander Couzens | 7112867 | 2021-06-05 18:44:01 +0200 | [diff] [blame] | 2890 | if (gss->local.num_ip4 && !gss->num_max_ip4_remote) |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2891 | cause = NS_CAUSE_INVAL_NR_IPv4_EP; |
| 2892 | else |
| 2893 | cause = NS_CAUSE_INVAL_NR_IPv6_EP; |
| 2894 | ns2_tx_sns_size_ack(gss->sns_nsvc, &cause); |
Alexander Couzens | 1c40525 | 2021-06-13 00:14:48 +0200 | [diff] [blame] | 2895 | if (fi->state == GPRS_SNS_ST_UNCONFIGURED) |
| 2896 | sns_failed(fi, "Rx Size: Invalid Nr of IPv4/IPv6 EPs"); |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2897 | break; |
| 2898 | } |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2899 | /* ensure number of NS-VCs is sufficient for full mesh */ |
| 2900 | gss->num_max_nsvcs = tlvp_val16be(tp, NS_IE_MAX_NR_NSVC); |
| 2901 | if (gss->num_max_nsvcs < num_remote_eps * num_local_eps) { |
| 2902 | LOGPFSML(fi, LOGL_ERROR, "%zu local and %zu remote EPs, requires %zu NS-VC, " |
| 2903 | "but BSS supports only %zu maximum NS-VCs\n", num_local_eps, |
| 2904 | num_remote_eps, num_local_eps * num_remote_eps, gss->num_max_nsvcs); |
| 2905 | cause = NS_CAUSE_INVAL_NR_NS_VC; |
| 2906 | ns2_tx_sns_size_ack(gss->sns_nsvc, &cause); |
Alexander Couzens | 1c40525 | 2021-06-13 00:14:48 +0200 | [diff] [blame] | 2907 | if (fi->state == GPRS_SNS_ST_UNCONFIGURED) |
| 2908 | sns_failed(fi, NULL); |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2909 | break; |
| 2910 | } |
| 2911 | /* perform state reset, if requested */ |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2912 | flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG); |
| 2913 | if (flag & 1) { |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2914 | /* clear all state */ |
Alexander Couzens | a270782 | 2021-07-20 18:59:40 +0200 | [diff] [blame] | 2915 | /* TODO: ensure gss->sns_nsvc is always the NSVC on which we received the SIZE PDU */ |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2916 | gss->N = 0; |
Alexander Couzens | a270782 | 2021-07-20 18:59:40 +0200 | [diff] [blame] | 2917 | ns2_clear_sgsn(gss, gss->sns_nsvc); |
| 2918 | /* keep the NSVC we need for SNS, but unconfigure it */ |
| 2919 | gss->sns_nsvc->sig_weight = 0; |
| 2920 | gss->sns_nsvc->data_weight = 0; |
| 2921 | ns2_vc_force_unconfigured(gss->sns_nsvc); |
Harald Welte | 2d807b6 | 2021-03-24 01:57:30 +0100 | [diff] [blame] | 2922 | ns2_sns_compute_local_ep_from_binds(fi); |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2923 | } |
Alexander Couzens | 1c40525 | 2021-06-13 00:14:48 +0200 | [diff] [blame] | 2924 | |
| 2925 | if (fi->state == GPRS_SNS_ST_UNCONFIGURED && !(flag & 1)) { |
| 2926 | sns_failed(fi, "Rx Size without Reset flag, but NSE is unknown"); |
| 2927 | break; |
| 2928 | } |
| 2929 | |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2930 | /* send SIZE_ACK */ |
| 2931 | ns2_tx_sns_size_ack(gss->sns_nsvc, NULL); |
| 2932 | /* only wait for SNS-CONFIG in case of Reset flag */ |
| 2933 | if (flag & 1) |
| 2934 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0); |
| 2935 | break; |
Alexander Couzens | 83f06ce | 2021-08-06 19:50:09 +0200 | [diff] [blame] | 2936 | case NS2_SNS_EV_REQ_FREE_NSVCS: |
| 2937 | sns_failed(fi, "On user request to free all NSVCs"); |
| 2938 | break; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2939 | default: |
| 2940 | ns2_sns_st_all_action(fi, event, data); |
| 2941 | break; |
| 2942 | } |
| 2943 | } |
| 2944 | |
| 2945 | static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = { |
| 2946 | .name = "GPRS-NS2-SNS-SGSN", |
| 2947 | .states = ns2_sns_sgsn_states, |
| 2948 | .num_states = ARRAY_SIZE(ns2_sns_sgsn_states), |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2949 | .allstate_event_mask = S(NS2_SNS_EV_RX_SIZE) | |
| 2950 | S(NS2_SNS_EV_REQ_NO_NSVC) | |
Alexander Couzens | 83f06ce | 2021-08-06 19:50:09 +0200 | [diff] [blame] | 2951 | S(NS2_SNS_EV_REQ_FREE_NSVCS) | |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2952 | S(NS2_SNS_EV_REQ_ADD_BIND) | |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 2953 | S(NS2_SNS_EV_REQ_CHANGE_WEIGHT) | |
Alexander Couzens | 175eb7b | 2021-07-20 18:41:14 +0200 | [diff] [blame] | 2954 | S(NS2_SNS_EV_REQ_DELETE_BIND), |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2955 | .allstate_action = ns2_sns_st_all_action_sgsn, |
| 2956 | .cleanup = NULL, |
| 2957 | .timer_cb = ns2_sns_fsm_sgsn_timer_cb, |
| 2958 | .event_names = gprs_sns_event_names, |
| 2959 | .pre_term = NULL, |
| 2960 | .log_subsys = DLNS, |
| 2961 | }; |
| 2962 | |
| 2963 | /*! Allocate an IP-SNS FSM for the SGSN side. |
| 2964 | * \param[in] nse NS Entity in which the FSM runs |
| 2965 | * \param[in] id string identifier |
| 2966 | * \returns FSM instance on success; NULL on error */ |
| 2967 | struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id) |
| 2968 | { |
| 2969 | struct osmo_fsm_inst *fi; |
| 2970 | struct ns2_sns_state *gss; |
| 2971 | |
| 2972 | fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id); |
| 2973 | if (!fi) |
| 2974 | return fi; |
| 2975 | |
| 2976 | gss = talloc_zero(fi, struct ns2_sns_state); |
| 2977 | if (!gss) |
| 2978 | goto err; |
| 2979 | |
| 2980 | fi->priv = gss; |
| 2981 | gss->nse = nse; |
| 2982 | gss->role = GPRS_SNS_ROLE_SGSN; |
| 2983 | INIT_LLIST_HEAD(&gss->sns_endpoints); |
| 2984 | INIT_LLIST_HEAD(&gss->binds); |
Alexander Couzens | 1f3193d | 2021-06-05 22:08:11 +0200 | [diff] [blame] | 2985 | INIT_LLIST_HEAD(&gss->procedures); |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2986 | |
| 2987 | return fi; |
| 2988 | err: |
| 2989 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 2990 | return NULL; |
| 2991 | } |
| 2992 | |
| 2993 | |
| 2994 | |
| 2995 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2996 | /* initialize osmo_ctx on main tread */ |
| 2997 | static __attribute__((constructor)) void on_dso_load_ctx(void) |
| 2998 | { |
| 2999 | OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0); |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 3000 | OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 3001 | } |