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