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 | |
| 59 | enum ns2_sns_type { |
| 60 | IPv4, |
| 61 | IPv6, |
| 62 | }; |
| 63 | |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 64 | enum ns2_sns_role { |
| 65 | GPRS_SNS_ROLE_BSS, |
| 66 | GPRS_SNS_ROLE_SGSN, |
| 67 | }; |
| 68 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 69 | /* 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] | 70 | enum gprs_sns_bss_state { |
| 71 | GPRS_SNS_ST_UNCONFIGURED, |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 72 | GPRS_SNS_ST_BSS_SIZE, /*!< SNS-SIZE procedure ongoing */ |
| 73 | GPRS_SNS_ST_BSS_CONFIG_BSS, /*!< SNS-CONFIG procedure (BSS->SGSN) ongoing */ |
| 74 | GPRS_SNS_ST_BSS_CONFIG_SGSN, /*!< SNS-CONFIG procedure (SGSN->BSS) ongoing */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 75 | GPRS_SNS_ST_CONFIGURED, |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 76 | GPRS_SNS_ST_SGSN_WAIT_CONFIG, /* !< SGSN role: Wait for CONFIG from BSS */ |
| 77 | GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, /* !< SGSN role: Wait for CONFIG-ACK from BSS */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | enum gprs_sns_event { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 81 | GPRS_SNS_EV_REQ_SELECT_ENDPOINT, /*!< Select a SNS endpoint from the list */ |
| 82 | GPRS_SNS_EV_RX_SIZE, |
| 83 | GPRS_SNS_EV_RX_SIZE_ACK, |
| 84 | GPRS_SNS_EV_RX_CONFIG, |
| 85 | GPRS_SNS_EV_RX_CONFIG_END, /*!< SNS-CONFIG with end flag received */ |
| 86 | GPRS_SNS_EV_RX_CONFIG_ACK, |
| 87 | GPRS_SNS_EV_RX_ADD, |
| 88 | GPRS_SNS_EV_RX_DELETE, |
| 89 | GPRS_SNS_EV_RX_CHANGE_WEIGHT, |
Harald Welte | b9f2387 | 2021-03-02 20:48:54 +0100 | [diff] [blame] | 90 | GPRS_SNS_EV_RX_ACK, /*!< Rx of SNS-ACK (response to ADD/DELETE/CHG_WEIGHT */ |
Harald Welte | 04647e1 | 2021-03-02 18:50:40 +0100 | [diff] [blame] | 91 | GPRS_SNS_EV_REQ_NO_NSVC, /*!< no more NS-VC remaining (all dead) */ |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 92 | GPRS_SNS_EV_REQ_NSVC_ALIVE, /*!< a NS-VC became alive */ |
Harald Welte | 04647e1 | 2021-03-02 18:50:40 +0100 | [diff] [blame] | 93 | GPRS_SNS_EV_REQ_ADD_BIND, /*!< add a new local bind to this NSE */ |
| 94 | GPRS_SNS_EV_REQ_DELETE_BIND, /*!< remove a local bind from this NSE */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | static const struct value_string gprs_sns_event_names[] = { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 98 | { GPRS_SNS_EV_REQ_SELECT_ENDPOINT, "REQ_SELECT_ENDPOINT" }, |
| 99 | { GPRS_SNS_EV_RX_SIZE, "RX_SIZE" }, |
| 100 | { GPRS_SNS_EV_RX_SIZE_ACK, "RX_SIZE_ACK" }, |
| 101 | { GPRS_SNS_EV_RX_CONFIG, "RX_CONFIG" }, |
| 102 | { GPRS_SNS_EV_RX_CONFIG_END, "RX_CONFIG_END" }, |
| 103 | { GPRS_SNS_EV_RX_CONFIG_ACK, "RX_CONFIG_ACK" }, |
| 104 | { GPRS_SNS_EV_RX_ADD, "RX_ADD" }, |
| 105 | { GPRS_SNS_EV_RX_DELETE, "RX_DELETE" }, |
Harald Welte | b9f2387 | 2021-03-02 20:48:54 +0100 | [diff] [blame] | 106 | { GPRS_SNS_EV_RX_ACK, "RX_ACK" }, |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 107 | { GPRS_SNS_EV_RX_CHANGE_WEIGHT, "RX_CHANGE_WEIGHT" }, |
| 108 | { GPRS_SNS_EV_REQ_NO_NSVC, "REQ_NO_NSVC" }, |
| 109 | { GPRS_SNS_EV_REQ_NSVC_ALIVE, "REQ_NSVC_ALIVE"}, |
| 110 | { GPRS_SNS_EV_REQ_ADD_BIND, "REQ_ADD_BIND"}, |
| 111 | { GPRS_SNS_EV_REQ_DELETE_BIND, "REQ_DELETE_BIND"}, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 112 | { 0, NULL } |
| 113 | }; |
| 114 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 115 | struct sns_endpoint { |
| 116 | struct llist_head list; |
| 117 | struct osmo_sockaddr saddr; |
| 118 | }; |
| 119 | |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 120 | struct ns2_sns_bind { |
| 121 | struct llist_head list; |
| 122 | struct gprs_ns2_vc_bind *bind; |
| 123 | }; |
| 124 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 125 | struct ns2_sns_state { |
| 126 | struct gprs_ns2_nse *nse; |
| 127 | |
| 128 | enum ns2_sns_type ip; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 129 | enum ns2_sns_role role; /* local role: BSS or SGSN */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 130 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 131 | /* holds the list of initial SNS endpoints */ |
| 132 | struct llist_head sns_endpoints; |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 133 | /* list of used struct ns2_sns_bind */ |
| 134 | struct llist_head binds; |
| 135 | /* pointer to the bind which was used to initiate the SNS connection */ |
| 136 | struct ns2_sns_bind *initial_bind; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 137 | /* prevent recursive reselection */ |
| 138 | bool reselection_running; |
| 139 | |
| 140 | /* The current initial SNS endpoints. |
| 141 | * The initial connection will be moved into the NSE |
| 142 | * if configured via SNS. Otherwise it will be removed |
| 143 | * in configured state. */ |
| 144 | struct sns_endpoint *initial; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 145 | /* all SNS PDU will be sent over this nsvc */ |
| 146 | struct gprs_ns2_vc *sns_nsvc; |
Alexander Couzens | 90ee963 | 2020-12-07 06:18:32 +0100 | [diff] [blame] | 147 | /* timer N */ |
| 148 | int N; |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 149 | /* true if at least one nsvc is alive */ |
| 150 | bool alive; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 151 | |
| 152 | /* local configuration to send to the remote end */ |
| 153 | struct gprs_ns_ie_ip4_elem *ip4_local; |
| 154 | size_t num_ip4_local; |
| 155 | |
| 156 | /* local configuration to send to the remote end */ |
| 157 | struct gprs_ns_ie_ip6_elem *ip6_local; |
| 158 | size_t num_ip6_local; |
| 159 | |
| 160 | /* local configuration about our capabilities in terms of connections to |
| 161 | * remote (SGSN) side */ |
| 162 | size_t num_max_nsvcs; |
| 163 | size_t num_max_ip4_remote; |
| 164 | size_t num_max_ip6_remote; |
| 165 | |
| 166 | /* remote configuration as received */ |
| 167 | struct gprs_ns_ie_ip4_elem *ip4_remote; |
| 168 | unsigned int num_ip4_remote; |
| 169 | |
| 170 | /* remote configuration as received */ |
| 171 | struct gprs_ns_ie_ip6_elem *ip6_remote; |
| 172 | unsigned int num_ip6_remote; |
| 173 | }; |
| 174 | |
| 175 | static inline struct gprs_ns2_nse *nse_inst_from_fi(struct osmo_fsm_inst *fi) |
| 176 | { |
| 177 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 178 | return gss->nse; |
| 179 | } |
| 180 | |
| 181 | /* helper function to compute the sum of all (data or signaling) weights */ |
| 182 | static int ip4_weight_sum(const struct gprs_ns_ie_ip4_elem *ip4, unsigned int num, |
| 183 | bool data_weight) |
| 184 | { |
| 185 | unsigned int i; |
| 186 | int weight_sum = 0; |
| 187 | |
| 188 | for (i = 0; i < num; i++) { |
| 189 | if (data_weight) |
| 190 | weight_sum += ip4[i].data_weight; |
| 191 | else |
| 192 | weight_sum += ip4[i].sig_weight; |
| 193 | } |
| 194 | return weight_sum; |
| 195 | } |
| 196 | #define ip4_weight_sum_data(x,y) ip4_weight_sum(x, y, true) |
| 197 | #define ip4_weight_sum_sig(x,y) ip4_weight_sum(x, y, false) |
| 198 | |
| 199 | /* helper function to compute the sum of all (data or signaling) weights */ |
| 200 | static int ip6_weight_sum(const struct gprs_ns_ie_ip6_elem *ip6, unsigned int num, |
| 201 | bool data_weight) |
| 202 | { |
| 203 | unsigned int i; |
| 204 | int weight_sum = 0; |
| 205 | |
| 206 | for (i = 0; i < num; i++) { |
| 207 | if (data_weight) |
| 208 | weight_sum += ip6[i].data_weight; |
| 209 | else |
| 210 | weight_sum += ip6[i].sig_weight; |
| 211 | } |
| 212 | return weight_sum; |
| 213 | } |
| 214 | #define ip6_weight_sum_data(x,y) ip6_weight_sum(x, y, true) |
| 215 | #define ip6_weight_sum_sig(x,y) ip6_weight_sum(x, y, false) |
| 216 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 217 | static int nss_weight_sum(const struct ns2_sns_state *nss, bool data_weight) |
| 218 | { |
| 219 | return ip4_weight_sum(nss->ip4_remote, nss->num_ip4_remote, data_weight) + |
| 220 | ip6_weight_sum(nss->ip6_remote, nss->num_ip6_remote, data_weight); |
| 221 | } |
| 222 | #define nss_weight_sum_data(nss) nss_weight_sum(nss, true) |
| 223 | #define nss_weight_sum_sig(nss) nss_weight_sum(nss, false) |
| 224 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 225 | static struct gprs_ns2_vc *nsvc_by_ip4_elem(struct gprs_ns2_nse *nse, |
| 226 | const struct gprs_ns_ie_ip4_elem *ip4) |
| 227 | { |
| 228 | struct osmo_sockaddr sa; |
| 229 | /* copy over. Both data structures use network byte order */ |
| 230 | sa.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 231 | sa.u.sin.sin_port = ip4->udp_port; |
| 232 | sa.u.sin.sin_family = AF_INET; |
| 233 | |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 234 | return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static struct gprs_ns2_vc *nsvc_by_ip6_elem(struct gprs_ns2_nse *nse, |
| 238 | const struct gprs_ns_ie_ip6_elem *ip6) |
| 239 | { |
| 240 | struct osmo_sockaddr sa; |
| 241 | /* copy over. Both data structures use network byte order */ |
| 242 | sa.u.sin6.sin6_addr = ip6->ip_addr; |
| 243 | sa.u.sin6.sin6_port = ip6->udp_port; |
| 244 | sa.u.sin6.sin6_family = AF_INET; |
| 245 | |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 246 | return gprs_ns2_nsvc_by_sockaddr_nse(nse, &sa); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Alexander Couzens | 125298f | 2020-10-11 21:22:42 +0200 | [diff] [blame] | 249 | /*! Return the initial SNS remote socket address |
| 250 | * \param nse NS Entity |
| 251 | * \return address of the initial SNS connection; NULL in case of error |
| 252 | */ |
| 253 | const struct osmo_sockaddr *gprs_ns2_nse_sns_remote(struct gprs_ns2_nse *nse) |
| 254 | { |
| 255 | struct ns2_sns_state *gss; |
| 256 | |
| 257 | if (!nse->bss_sns_fi) |
| 258 | return NULL; |
| 259 | |
| 260 | gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 261 | return &gss->initial->saddr; |
Alexander Couzens | 125298f | 2020-10-11 21:22:42 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 264 | /*! called when a nsvc is beeing freed or the nsvc became dead */ |
| 265 | void ns2_sns_replace_nsvc(struct gprs_ns2_vc *nsvc) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 266 | { |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 267 | struct gprs_ns2_nse *nse = nsvc->nse; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 268 | struct gprs_ns2_vc *tmp; |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 269 | struct osmo_fsm_inst *fi = nse->bss_sns_fi; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 270 | struct ns2_sns_state *gss; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 271 | |
| 272 | if (!fi) |
| 273 | return; |
| 274 | |
| 275 | gss = (struct ns2_sns_state *) fi->priv; |
| 276 | if (nsvc != gss->sns_nsvc) |
| 277 | return; |
| 278 | |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 279 | gss->sns_nsvc = NULL; |
| 280 | if (gss->alive) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 281 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 282 | if (ns2_vc_is_unblocked(tmp)) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 283 | gss->sns_nsvc = tmp; |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 284 | return; |
| 285 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 286 | } |
| 287 | } else { |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 288 | /* the SNS is waiting for its first NS-VC to come up |
| 289 | * choose any other nsvc */ |
| 290 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
| 291 | if (nsvc != tmp) { |
| 292 | gss->sns_nsvc = tmp; |
| 293 | return; |
| 294 | } |
| 295 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 296 | } |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 297 | |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 298 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Harald Welte | 46eb764 | 2021-03-04 17:49:59 +0100 | [diff] [blame] | 301 | static void ns2_clear_ipv46_entries_local(struct ns2_sns_state *gss) |
Alexander Couzens | 7a7b20b | 2021-01-18 10:47:33 +0100 | [diff] [blame] | 302 | { |
| 303 | TALLOC_FREE(gss->ip4_local); |
Alexander Couzens | 7a7b20b | 2021-01-18 10:47:33 +0100 | [diff] [blame] | 304 | TALLOC_FREE(gss->ip6_local); |
Alexander Couzens | 7a7b20b | 2021-01-18 10:47:33 +0100 | [diff] [blame] | 305 | |
| 306 | gss->num_ip4_local = 0; |
Alexander Couzens | 7a7b20b | 2021-01-18 10:47:33 +0100 | [diff] [blame] | 307 | gss->num_ip6_local = 0; |
Harald Welte | 46eb764 | 2021-03-04 17:49:59 +0100 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static void ns2_clear_ipv46_entries_remote(struct ns2_sns_state *gss) |
| 311 | { |
| 312 | TALLOC_FREE(gss->ip4_remote); |
| 313 | TALLOC_FREE(gss->ip6_remote); |
| 314 | |
| 315 | gss->num_ip4_remote = 0; |
Alexander Couzens | 7a7b20b | 2021-01-18 10:47:33 +0100 | [diff] [blame] | 316 | gss->num_ip6_remote = 0; |
| 317 | } |
| 318 | |
Daniel Willmann | cf8371a | 2021-01-16 15:13:51 +0100 | [diff] [blame] | 319 | static void ns2_vc_create_ip(struct osmo_fsm_inst *fi, struct gprs_ns2_nse *nse, const struct osmo_sockaddr *remote, |
| 320 | uint8_t sig_weight, uint8_t data_weight) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 321 | { |
| 322 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 323 | struct gprs_ns2_vc *nsvc; |
| 324 | struct gprs_ns2_vc_bind *bind; |
Daniel Willmann | cf8371a | 2021-01-16 15:13:51 +0100 | [diff] [blame] | 325 | |
| 326 | /* for every bind, create a connection if bind type == IP */ |
| 327 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 328 | if (bind->ll != GPRS_NS2_LL_UDP) |
| 329 | continue; |
| 330 | /* ignore failed connection */ |
| 331 | nsvc = gprs_ns2_ip_connect_inactive(bind, |
| 332 | remote, |
| 333 | nse, 0); |
| 334 | if (!nsvc) { |
| 335 | LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG: Failed to create NSVC\n"); |
| 336 | continue; |
| 337 | } |
| 338 | |
| 339 | nsvc->sig_weight = sig_weight; |
| 340 | nsvc->data_weight = data_weight; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | static void ns2_nsvc_create_ip4(struct osmo_fsm_inst *fi, |
| 345 | struct gprs_ns2_nse *nse, |
| 346 | const struct gprs_ns_ie_ip4_elem *ip4) |
| 347 | { |
Alexander Couzens | c068d86 | 2020-10-12 04:11:51 +0200 | [diff] [blame] | 348 | struct osmo_sockaddr remote = { }; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 349 | /* copy over. Both data structures use network byte order */ |
| 350 | remote.u.sin.sin_family = AF_INET; |
| 351 | remote.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 352 | remote.u.sin.sin_port = ip4->udp_port; |
| 353 | |
Daniel Willmann | cf8371a | 2021-01-16 15:13:51 +0100 | [diff] [blame] | 354 | 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] | 355 | } |
| 356 | |
| 357 | static void ns2_nsvc_create_ip6(struct osmo_fsm_inst *fi, |
| 358 | struct gprs_ns2_nse *nse, |
| 359 | const struct gprs_ns_ie_ip6_elem *ip6) |
| 360 | { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 361 | struct osmo_sockaddr remote = {}; |
| 362 | /* copy over. Both data structures use network byte order */ |
| 363 | remote.u.sin6.sin6_family = AF_INET6; |
| 364 | remote.u.sin6.sin6_addr = ip6->ip_addr; |
| 365 | remote.u.sin6.sin6_port = ip6->udp_port; |
| 366 | |
Daniel Willmann | cf8371a | 2021-01-16 15:13:51 +0100 | [diff] [blame] | 367 | 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] | 368 | } |
| 369 | |
| 370 | |
| 371 | static int create_missing_nsvcs(struct osmo_fsm_inst *fi) |
| 372 | { |
| 373 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 374 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 375 | struct gprs_ns2_vc *nsvc; |
| 376 | struct gprs_ns2_vc_bind *bind; |
| 377 | struct osmo_sockaddr remote = { }; |
| 378 | unsigned int i; |
| 379 | |
| 380 | for (i = 0; i < gss->num_ip4_remote; i++) { |
| 381 | const struct gprs_ns_ie_ip4_elem *ip4 = &gss->ip4_remote[i]; |
| 382 | |
| 383 | remote.u.sin.sin_family = AF_INET; |
| 384 | remote.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 385 | remote.u.sin.sin_port = ip4->udp_port; |
| 386 | |
| 387 | llist_for_each_entry(bind, &nse->nsi->binding, list) { |
| 388 | bool found = false; |
Daniel Willmann | 967e2c1 | 2021-01-14 16:58:17 +0100 | [diff] [blame] | 389 | if (bind->ll != GPRS_NS2_LL_UDP) |
| 390 | continue; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 391 | |
| 392 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
| 393 | if (nsvc->bind != bind) |
| 394 | continue; |
| 395 | |
Alexander Couzens | c4229a4 | 2020-10-11 20:58:04 +0200 | [diff] [blame] | 396 | if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 397 | found = true; |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | if (!found) { |
| 403 | nsvc = gprs_ns2_ip_connect_inactive(bind, &remote, nse, 0); |
| 404 | if (!nsvc) { |
| 405 | /* TODO: add to a list to send back a NS-STATUS */ |
| 406 | continue; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /* update data / signalling weight */ |
| 411 | nsvc->data_weight = ip4->data_weight; |
| 412 | nsvc->sig_weight = ip4->sig_weight; |
| 413 | nsvc->sns_only = false; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | for (i = 0; i < gss->num_ip6_remote; i++) { |
| 418 | const struct gprs_ns_ie_ip6_elem *ip6 = &gss->ip6_remote[i]; |
| 419 | |
| 420 | remote.u.sin6.sin6_family = AF_INET6; |
| 421 | remote.u.sin6.sin6_addr = ip6->ip_addr; |
| 422 | remote.u.sin6.sin6_port = ip6->udp_port; |
| 423 | |
| 424 | llist_for_each_entry(bind, &nse->nsi->binding, list) { |
| 425 | bool found = false; |
Daniel Willmann | 967e2c1 | 2021-01-14 16:58:17 +0100 | [diff] [blame] | 426 | if (bind->ll != GPRS_NS2_LL_UDP) |
| 427 | continue; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 428 | |
| 429 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
| 430 | if (nsvc->bind != bind) |
| 431 | continue; |
| 432 | |
Alexander Couzens | c4229a4 | 2020-10-11 20:58:04 +0200 | [diff] [blame] | 433 | if (!osmo_sockaddr_cmp(&remote, gprs_ns2_ip_vc_remote(nsvc))) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 434 | found = true; |
| 435 | break; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | if (!found) { |
| 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 = ip6->data_weight; |
| 449 | nsvc->sig_weight = ip6->sig_weight; |
| 450 | nsvc->sns_only = false; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | |
| 455 | return 0; |
| 456 | } |
| 457 | |
| 458 | /* Add a given remote IPv4 element to gprs_sns_state */ |
| 459 | static int add_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4) |
| 460 | { |
| 461 | unsigned int i; |
| 462 | |
| 463 | if (gss->num_ip4_remote >= gss->num_max_ip4_remote) |
| 464 | return -NS_CAUSE_INVAL_NR_NS_VC; |
| 465 | |
| 466 | /* check for duplicates */ |
| 467 | for (i = 0; i < gss->num_ip4_remote; i++) { |
| 468 | if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4))) |
| 469 | continue; |
| 470 | /* TODO: log message duplicate */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 471 | return -NS_CAUSE_PROTO_ERR_UNSPEC; |
| 472 | } |
| 473 | |
| 474 | gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote, struct gprs_ns_ie_ip4_elem, |
| 475 | gss->num_ip4_remote+1); |
| 476 | gss->ip4_remote[gss->num_ip4_remote] = *ip4; |
| 477 | gss->num_ip4_remote += 1; |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | /* Remove a given remote IPv4 element from gprs_sns_state */ |
| 482 | static int remove_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4) |
| 483 | { |
| 484 | unsigned int i; |
| 485 | |
| 486 | for (i = 0; i < gss->num_ip4_remote; i++) { |
| 487 | if (memcmp(&gss->ip4_remote[i], ip4, sizeof(*ip4))) |
| 488 | continue; |
| 489 | /* all array elements < i remain as they are; all > i are shifted left by one */ |
| 490 | memmove(&gss->ip4_remote[i], &gss->ip4_remote[i+1], gss->num_ip4_remote-i-1); |
| 491 | gss->num_ip4_remote -= 1; |
| 492 | return 0; |
| 493 | } |
| 494 | return -1; |
| 495 | } |
| 496 | |
| 497 | /* update the weights for specified remote IPv4 */ |
| 498 | static int update_remote_ip4_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip4_elem *ip4) |
| 499 | { |
| 500 | unsigned int i; |
| 501 | |
| 502 | for (i = 0; i < gss->num_ip4_remote; i++) { |
| 503 | if (gss->ip4_remote[i].ip_addr != ip4->ip_addr || |
| 504 | gss->ip4_remote[i].udp_port != ip4->udp_port) |
| 505 | continue; |
| 506 | |
| 507 | gss->ip4_remote[i].sig_weight = ip4->sig_weight; |
| 508 | gss->ip4_remote[i].data_weight = ip4->data_weight; |
| 509 | return 0; |
| 510 | } |
| 511 | return -1; |
| 512 | } |
| 513 | |
| 514 | /* Add a given remote IPv6 element to gprs_sns_state */ |
| 515 | static int add_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6) |
| 516 | { |
| 517 | if (gss->num_ip6_remote >= gss->num_max_ip6_remote) |
| 518 | return -NS_CAUSE_INVAL_NR_NS_VC; |
| 519 | |
| 520 | gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote, struct gprs_ns_ie_ip6_elem, |
| 521 | gss->num_ip6_remote+1); |
| 522 | gss->ip6_remote[gss->num_ip6_remote] = *ip6; |
| 523 | gss->num_ip6_remote += 1; |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | /* Remove a given remote IPv6 element from gprs_sns_state */ |
| 528 | static int remove_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6) |
| 529 | { |
| 530 | unsigned int i; |
| 531 | |
| 532 | for (i = 0; i < gss->num_ip6_remote; i++) { |
| 533 | if (memcmp(&gss->ip6_remote[i], ip6, sizeof(*ip6))) |
| 534 | continue; |
| 535 | /* all array elements < i remain as they are; all > i are shifted left by one */ |
| 536 | memmove(&gss->ip6_remote[i], &gss->ip6_remote[i+1], gss->num_ip6_remote-i-1); |
| 537 | gss->num_ip6_remote -= 1; |
| 538 | return 0; |
| 539 | } |
| 540 | return -1; |
| 541 | } |
| 542 | |
| 543 | /* update the weights for specified remote IPv6 */ |
| 544 | static int update_remote_ip6_elem(struct ns2_sns_state *gss, const struct gprs_ns_ie_ip6_elem *ip6) |
| 545 | { |
| 546 | unsigned int i; |
| 547 | |
| 548 | for (i = 0; i < gss->num_ip6_remote; i++) { |
| 549 | if (memcmp(&gss->ip6_remote[i].ip_addr, &ip6->ip_addr, sizeof(ip6->ip_addr)) || |
| 550 | gss->ip6_remote[i].udp_port != ip6->udp_port) |
| 551 | continue; |
| 552 | gss->ip6_remote[i].sig_weight = ip6->sig_weight; |
| 553 | gss->ip6_remote[i].data_weight = ip6->data_weight; |
| 554 | return 0; |
| 555 | } |
| 556 | return -1; |
| 557 | } |
| 558 | |
| 559 | 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) |
| 560 | { |
| 561 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 562 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 563 | struct gprs_ns2_vc *nsvc; |
| 564 | struct osmo_sockaddr sa = {}; |
Alexander Couzens | 9a4cf27 | 2020-10-11 20:48:04 +0200 | [diff] [blame] | 565 | const struct osmo_sockaddr *remote; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 566 | uint8_t new_signal; |
| 567 | uint8_t new_data; |
| 568 | |
| 569 | /* TODO: Upon receiving an SNS-CHANGEWEIGHT PDU, if the resulting sum of the |
| 570 | * signalling weights of all the peer IP endpoints configured for this NSE is |
| 571 | * equal to zero or if the resulting sum of the data weights of all the peer IP |
| 572 | * endpoints configured for this NSE is equal to zero, the BSS/SGSN shall send an |
| 573 | * SNS-ACK PDU with a cause code of "Invalid weights". */ |
| 574 | |
| 575 | if (ip4) { |
| 576 | if (update_remote_ip4_elem(gss, ip4)) |
| 577 | return -NS_CAUSE_UNKN_IP_EP; |
| 578 | |
| 579 | /* copy over. Both data structures use network byte order */ |
| 580 | sa.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 581 | sa.u.sin.sin_port = ip4->udp_port; |
| 582 | sa.u.sin.sin_family = AF_INET; |
| 583 | new_signal = ip4->sig_weight; |
| 584 | new_data = ip4->data_weight; |
| 585 | } else if (ip6) { |
| 586 | if (update_remote_ip6_elem(gss, ip6)) |
| 587 | return -NS_CAUSE_UNKN_IP_EP; |
| 588 | |
| 589 | /* copy over. Both data structures use network byte order */ |
| 590 | sa.u.sin6.sin6_addr = ip6->ip_addr; |
| 591 | sa.u.sin6.sin6_port = ip6->udp_port; |
| 592 | sa.u.sin6.sin6_family = AF_INET6; |
| 593 | new_signal = ip6->sig_weight; |
| 594 | new_data = ip6->data_weight; |
| 595 | } else { |
| 596 | OSMO_ASSERT(false); |
| 597 | } |
| 598 | |
| 599 | llist_for_each_entry(nsvc, &nse->nsvc, list) { |
Alexander Couzens | c4229a4 | 2020-10-11 20:58:04 +0200 | [diff] [blame] | 600 | remote = gprs_ns2_ip_vc_remote(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 601 | /* all nsvc in NSE should be IP/UDP nsvc */ |
| 602 | OSMO_ASSERT(remote); |
| 603 | |
| 604 | if (osmo_sockaddr_cmp(&sa, remote)) |
| 605 | continue; |
| 606 | |
| 607 | LOGPFSML(fi, LOGL_INFO, "CHANGE-WEIGHT NS-VC %s data_weight %u->%u, sig_weight %u->%u\n", |
| 608 | gprs_ns2_ll_str(nsvc), nsvc->data_weight, new_data, |
| 609 | nsvc->sig_weight, new_signal); |
| 610 | |
| 611 | nsvc->data_weight = new_data; |
| 612 | nsvc->sig_weight = new_signal; |
| 613 | } |
| 614 | |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | static int do_sns_delete(struct osmo_fsm_inst *fi, |
| 619 | const struct gprs_ns_ie_ip4_elem *ip4, |
| 620 | const struct gprs_ns_ie_ip6_elem *ip6) |
| 621 | { |
| 622 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 623 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 624 | struct gprs_ns2_vc *nsvc, *tmp; |
Alexander Couzens | 9a4cf27 | 2020-10-11 20:48:04 +0200 | [diff] [blame] | 625 | const struct osmo_sockaddr *remote; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 626 | struct osmo_sockaddr sa = {}; |
| 627 | |
| 628 | if (ip4) { |
| 629 | if (remove_remote_ip4_elem(gss, ip4) < 0) |
| 630 | return -NS_CAUSE_UNKN_IP_EP; |
| 631 | /* copy over. Both data structures use network byte order */ |
| 632 | sa.u.sin.sin_addr.s_addr = ip4->ip_addr; |
| 633 | sa.u.sin.sin_port = ip4->udp_port; |
| 634 | sa.u.sin.sin_family = AF_INET; |
| 635 | } else if (ip6) { |
| 636 | if (remove_remote_ip6_elem(gss, ip6)) |
| 637 | return -NS_CAUSE_UNKN_IP_EP; |
| 638 | |
| 639 | /* copy over. Both data structures use network byte order */ |
| 640 | sa.u.sin6.sin6_addr = ip6->ip_addr; |
| 641 | sa.u.sin6.sin6_port = ip6->udp_port; |
| 642 | sa.u.sin6.sin6_family = AF_INET6; |
| 643 | } else { |
| 644 | OSMO_ASSERT(false); |
| 645 | } |
| 646 | |
| 647 | llist_for_each_entry_safe(nsvc, tmp, &nse->nsvc, list) { |
Alexander Couzens | c4229a4 | 2020-10-11 20:58:04 +0200 | [diff] [blame] | 648 | remote = gprs_ns2_ip_vc_remote(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 649 | /* all nsvc in NSE should be IP/UDP nsvc */ |
| 650 | OSMO_ASSERT(remote); |
| 651 | if (osmo_sockaddr_cmp(&sa, remote)) |
| 652 | continue; |
| 653 | |
| 654 | LOGPFSML(fi, LOGL_INFO, "DELETE NS-VC %s\n", gprs_ns2_ll_str(nsvc)); |
| 655 | gprs_ns2_free_nsvc(nsvc); |
| 656 | } |
| 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | static int do_sns_add(struct osmo_fsm_inst *fi, |
| 662 | const struct gprs_ns_ie_ip4_elem *ip4, |
| 663 | const struct gprs_ns_ie_ip6_elem *ip6) |
| 664 | { |
| 665 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 666 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 667 | struct gprs_ns2_vc *nsvc; |
| 668 | int rc = 0; |
| 669 | |
| 670 | /* Upon receiving an SNS-ADD PDU, if the consequent number of IPv4 endpoints |
| 671 | * exceeds the number of IPv4 endpoints supported by the NSE, the NSE shall send |
| 672 | * an SNS-ACK PDU with a cause code set to "Invalid number of IP4 Endpoints". */ |
| 673 | switch (gss->ip) { |
| 674 | case IPv4: |
| 675 | rc = add_remote_ip4_elem(gss, ip4); |
| 676 | break; |
| 677 | case IPv6: |
| 678 | rc = add_remote_ip6_elem(gss, ip6); |
| 679 | break; |
| 680 | default: |
| 681 | /* the gss->ip is initialized with the bss */ |
| 682 | OSMO_ASSERT(false); |
| 683 | } |
| 684 | |
| 685 | if (rc) |
| 686 | return rc; |
| 687 | |
| 688 | /* Upon receiving an SNS-ADD PDU containing an already configured IP endpoint the |
| 689 | * NSE shall send an SNS-ACK PDU with the cause code "Protocol error - |
| 690 | * unspecified" */ |
| 691 | switch (gss->ip) { |
| 692 | case IPv4: |
| 693 | nsvc = nsvc_by_ip4_elem(nse, ip4); |
| 694 | if (nsvc) { |
| 695 | /* the nsvc should be already in sync with the ip4 / ip6 elements */ |
| 696 | return -NS_CAUSE_PROTO_ERR_UNSPEC; |
| 697 | } |
| 698 | |
| 699 | /* TODO: failure case */ |
| 700 | ns2_nsvc_create_ip4(fi, nse, ip4); |
| 701 | break; |
| 702 | case IPv6: |
| 703 | nsvc = nsvc_by_ip6_elem(nse, ip6); |
| 704 | if (nsvc) { |
| 705 | /* the nsvc should be already in sync with the ip4 / ip6 elements */ |
| 706 | return -NS_CAUSE_PROTO_ERR_UNSPEC; |
| 707 | } |
| 708 | |
| 709 | /* TODO: failure case */ |
| 710 | ns2_nsvc_create_ip6(fi, nse, ip6); |
| 711 | break; |
| 712 | } |
| 713 | |
| 714 | gprs_ns2_start_alive_all_nsvcs(nse); |
| 715 | |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 720 | 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] | 721 | { |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 722 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 723 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 724 | /* empty state - SNS Select will start by ns2_sns_st_all_action() */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 725 | } |
| 726 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 727 | 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] | 728 | { |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 729 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 730 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 731 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 732 | struct tlv_parsed *tp = NULL; |
| 733 | |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 734 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
| 735 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 736 | switch (event) { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 737 | case GPRS_SNS_EV_RX_SIZE_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 738 | tp = data; |
| 739 | if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) { |
| 740 | LOGPFSML(fi, LOGL_ERROR, "SNS-SIZE-ACK with cause %s\n", |
| 741 | gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE))); |
| 742 | /* TODO: What to do? */ |
| 743 | } else { |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 744 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_BSS_CONFIG_BSS, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 745 | nsi->timeout[NS_TOUT_TSNS_PROV], 2); |
| 746 | } |
| 747 | break; |
| 748 | default: |
| 749 | OSMO_ASSERT(0); |
| 750 | } |
| 751 | } |
| 752 | |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 753 | static int ns2_sns_count_num_local_ep(struct osmo_fsm_inst *fi, enum ns2_sns_type stype) |
| 754 | { |
| 755 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 756 | struct ns2_sns_bind *sbind; |
| 757 | int count = 0; |
| 758 | |
| 759 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 760 | const struct osmo_sockaddr *sa = gprs_ns2_ip_bind_sockaddr(sbind->bind); |
| 761 | if (!sa) |
| 762 | continue; |
| 763 | |
| 764 | switch (stype) { |
| 765 | case IPv4: |
| 766 | if (sa->u.sas.ss_family == AF_INET) |
| 767 | count++; |
| 768 | break; |
| 769 | case IPv6: |
| 770 | if (sa->u.sas.ss_family == AF_INET6) |
| 771 | count++; |
| 772 | break; |
| 773 | } |
| 774 | } |
| 775 | return count; |
| 776 | } |
| 777 | |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 778 | 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] | 779 | { |
| 780 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 781 | struct gprs_ns_ie_ip4_elem *ip4_elems; |
| 782 | struct gprs_ns_ie_ip6_elem *ip6_elems; |
| 783 | struct gprs_ns2_vc_bind *bind; |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 784 | struct ns2_sns_bind *sbind; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 785 | const struct osmo_sockaddr *remote; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 786 | const struct osmo_sockaddr *sa; |
| 787 | struct osmo_sockaddr local; |
| 788 | int count; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 789 | |
Harald Welte | 46eb764 | 2021-03-04 17:49:59 +0100 | [diff] [blame] | 790 | ns2_clear_ipv46_entries_local(gss); |
Alexander Couzens | 7a7b20b | 2021-01-18 10:47:33 +0100 | [diff] [blame] | 791 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 792 | /* no initial available */ |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 793 | if (gss->role == GPRS_SNS_ROLE_BSS) { |
| 794 | if (!gss->initial) |
| 795 | return; |
| 796 | remote = &gss->initial->saddr; |
| 797 | } else |
| 798 | remote = gprs_ns2_ip_vc_remote(gss->sns_nsvc); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 799 | |
| 800 | /* count how many bindings are available (only UDP binds) */ |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 801 | count = llist_count(&gss->binds); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 802 | if (count == 0) { |
Harald Welte | 0599287 | 2021-03-04 15:49:21 +0100 | [diff] [blame] | 803 | 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] | 804 | return; |
| 805 | } |
| 806 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 807 | switch (gss->ip) { |
| 808 | case IPv4: |
| 809 | ip4_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip4_elem) * count); |
| 810 | if (!ip4_elems) |
| 811 | return; |
| 812 | |
| 813 | gss->ip4_local = ip4_elems; |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 814 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 815 | bind = sbind->bind; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 816 | sa = gprs_ns2_ip_bind_sockaddr(bind); |
| 817 | if (!sa) |
| 818 | continue; |
| 819 | |
| 820 | if (sa->u.sas.ss_family != AF_INET) |
| 821 | continue; |
| 822 | |
| 823 | /* check if this is an specific bind */ |
| 824 | if (sa->u.sin.sin_addr.s_addr == 0) { |
| 825 | if (osmo_sockaddr_local_ip(&local, remote)) |
| 826 | continue; |
| 827 | |
| 828 | ip4_elems->ip_addr = local.u.sin.sin_addr.s_addr; |
| 829 | } else { |
| 830 | ip4_elems->ip_addr = sa->u.sin.sin_addr.s_addr; |
| 831 | } |
| 832 | |
| 833 | ip4_elems->udp_port = sa->u.sin.sin_port; |
Alexander Couzens | c470476 | 2021-02-08 23:13:12 +0100 | [diff] [blame] | 834 | ip4_elems->sig_weight = bind->sns_sig_weight; |
| 835 | ip4_elems->data_weight = bind->sns_data_weight; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 836 | ip4_elems++; |
| 837 | } |
| 838 | |
| 839 | gss->num_ip4_local = count; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 840 | gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip4_remote * gss->num_ip4_local, 8); |
| 841 | break; |
| 842 | case IPv6: |
| 843 | /* IPv6 */ |
| 844 | ip6_elems = talloc_zero_size(fi, sizeof(struct gprs_ns_ie_ip6_elem) * count); |
| 845 | if (!ip6_elems) |
| 846 | return; |
| 847 | |
| 848 | gss->ip6_local = ip6_elems; |
| 849 | |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 850 | llist_for_each_entry(sbind, &gss->binds, list) { |
| 851 | bind = sbind->bind; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 852 | sa = gprs_ns2_ip_bind_sockaddr(bind); |
| 853 | if (!sa) |
| 854 | continue; |
| 855 | |
| 856 | if (sa->u.sas.ss_family != AF_INET6) |
| 857 | continue; |
| 858 | |
| 859 | /* check if this is an specific bind */ |
| 860 | if (IN6_IS_ADDR_UNSPECIFIED(&sa->u.sin6.sin6_addr)) { |
| 861 | if (osmo_sockaddr_local_ip(&local, remote)) |
| 862 | continue; |
| 863 | |
| 864 | ip6_elems->ip_addr = local.u.sin6.sin6_addr; |
| 865 | } else { |
| 866 | ip6_elems->ip_addr = sa->u.sin6.sin6_addr; |
| 867 | } |
| 868 | |
| 869 | ip6_elems->udp_port = sa->u.sin.sin_port; |
Alexander Couzens | c470476 | 2021-02-08 23:13:12 +0100 | [diff] [blame] | 870 | ip6_elems->sig_weight = bind->sns_sig_weight; |
| 871 | ip6_elems->data_weight = bind->sns_data_weight; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 872 | |
| 873 | ip6_elems++; |
| 874 | } |
| 875 | gss->num_ip6_local = count; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 876 | gss->num_max_nsvcs = OSMO_MAX(gss->num_max_ip6_remote * gss->num_ip6_local, 8); |
| 877 | break; |
| 878 | } |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | /* 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] | 882 | 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] | 883 | { |
| 884 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 885 | |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 886 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
| 887 | |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 888 | /* on a generic failure, the timer callback will recover */ |
| 889 | if (old_state != GPRS_SNS_ST_UNCONFIGURED) |
| 890 | 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] | 891 | if (old_state != GPRS_SNS_ST_BSS_SIZE) |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 892 | gss->N = 0; |
| 893 | |
| 894 | gss->alive = false; |
| 895 | |
| 896 | ns2_sns_compute_local_ep_from_binds(fi); |
| 897 | |
| 898 | /* take the first bind or take the next bind */ |
| 899 | if (!gss->initial_bind) { |
| 900 | gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list); |
| 901 | } else { |
| 902 | if (gss->initial_bind->list.next != &gss->binds) { |
| 903 | gss->initial_bind = llist_entry(gss->initial_bind->list.next, struct ns2_sns_bind, list); |
| 904 | } else { |
| 905 | gss->initial_bind = llist_first_entry(&gss->binds, struct ns2_sns_bind, list); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | |
| 910 | /* setup the NSVC */ |
| 911 | if (!gss->sns_nsvc) { |
| 912 | struct gprs_ns2_vc_bind *bind = gss->initial_bind->bind; |
| 913 | struct osmo_sockaddr *remote = &gss->initial->saddr; |
| 914 | gss->sns_nsvc = ns2_ip_bind_connect(bind, gss->nse, remote); |
| 915 | if (!gss->sns_nsvc) |
| 916 | return; |
Harald Welte | c962a2e | 2021-03-05 08:09:08 +0100 | [diff] [blame] | 917 | /* A pre-configured endpoint shall not be used for NSE data or signalling traffic |
| 918 | * (with the exception of Size and Configuration procedures) unless it is configured |
| 919 | * by the SGSN using the auto-configuration procedures */ |
Harald Welte | 24920e2 | 2021-03-04 13:03:27 +0100 | [diff] [blame] | 920 | gss->sns_nsvc->sns_only = true; |
| 921 | } |
| 922 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 923 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 924 | if (gss->num_max_ip4_remote > 0) |
Harald Welte | 4e41acc | 2021-03-23 11:55:25 +0100 | [diff] [blame] | 925 | ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, gss->num_ip4_local, -1); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 926 | else |
Harald Welte | 4e41acc | 2021-03-23 11:55:25 +0100 | [diff] [blame] | 927 | ns2_tx_sns_size(gss->sns_nsvc, true, gss->num_max_nsvcs, -1, gss->num_ip6_local); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 928 | } |
| 929 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 930 | 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] | 931 | { |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 932 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 3df5886 | 2021-02-05 17:18:08 +0100 | [diff] [blame] | 933 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 934 | struct tlv_parsed *tp = NULL; |
| 935 | |
| 936 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 937 | |
| 938 | switch (event) { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 939 | case GPRS_SNS_EV_RX_CONFIG_ACK: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 940 | tp = (struct tlv_parsed *) data; |
| 941 | if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) { |
| 942 | LOGPFSML(fi, LOGL_ERROR, "SNS-CONFIG-ACK with cause %s\n", |
| 943 | gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE))); |
| 944 | /* TODO: What to do? */ |
| 945 | } else { |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 946 | 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] | 947 | } |
| 948 | break; |
| 949 | default: |
| 950 | OSMO_ASSERT(0); |
| 951 | } |
| 952 | } |
| 953 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 954 | 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] | 955 | { |
| 956 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 790a963 | 2021-02-05 17:18:39 +0100 | [diff] [blame] | 957 | |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 958 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
| 959 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 960 | if (old_state != GPRS_SNS_ST_BSS_CONFIG_BSS) |
Alexander Couzens | 790a963 | 2021-02-05 17:18:39 +0100 | [diff] [blame] | 961 | gss->N = 0; |
| 962 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 963 | /* Transmit SNS-CONFIG */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 964 | switch (gss->ip) { |
| 965 | case IPv4: |
| 966 | ns2_tx_sns_config(gss->sns_nsvc, true, |
Alexander Couzens | e78207f | 2020-12-07 06:19:29 +0100 | [diff] [blame] | 967 | gss->ip4_local, gss->num_ip4_local, |
| 968 | NULL, 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 969 | break; |
| 970 | case IPv6: |
| 971 | ns2_tx_sns_config(gss->sns_nsvc, true, |
Alexander Couzens | e78207f | 2020-12-07 06:19:29 +0100 | [diff] [blame] | 972 | NULL, 0, |
| 973 | gss->ip6_local, gss->num_ip6_local); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 974 | break; |
| 975 | } |
| 976 | } |
| 977 | |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 978 | /* calculate the timeout of the configured state. the configured |
| 979 | * state will fail if not at least one NS-VC is alive within X second. |
| 980 | */ |
| 981 | static inline int ns_sns_configured_timeout(struct osmo_fsm_inst *fi) |
| 982 | { |
| 983 | int secs; |
| 984 | struct gprs_ns2_inst *nsi = nse_inst_from_fi(fi)->nsi; |
| 985 | secs = nsi->timeout[NS_TOUT_TNS_ALIVE] * nsi->timeout[NS_TOUT_TNS_ALIVE_RETRIES]; |
| 986 | secs += nsi->timeout[NS_TOUT_TNS_TEST]; |
| 987 | |
| 988 | return secs; |
| 989 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 990 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 991 | /* append the remote endpoints from the parsed TLV array to the ns2_sns_state */ |
| 992 | 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] | 993 | { |
| 994 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 995 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 996 | if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) { |
| 997 | const struct gprs_ns_ie_ip4_elem *v4_list; |
| 998 | unsigned int num_v4; |
| 999 | v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST); |
| 1000 | num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1001 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1002 | if (num_v4 && gss->ip6_remote) |
| 1003 | return -NS_CAUSE_INVAL_NR_IPv4_EP; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1004 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1005 | /* realloc to the new size */ |
| 1006 | gss->ip4_remote = talloc_realloc(gss, gss->ip4_remote, |
| 1007 | struct gprs_ns_ie_ip4_elem, |
| 1008 | gss->num_ip4_remote + num_v4); |
| 1009 | /* append the new entries to the end of the list */ |
| 1010 | memcpy(&gss->ip4_remote[gss->num_ip4_remote], v4_list, num_v4*sizeof(*v4_list)); |
| 1011 | gss->num_ip4_remote += num_v4; |
| 1012 | |
| 1013 | LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv4 list now %u entries\n", |
| 1014 | gss->num_ip4_remote); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1015 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1016 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1017 | if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) { |
| 1018 | const struct gprs_ns_ie_ip6_elem *v6_list; |
| 1019 | unsigned int num_v6; |
| 1020 | v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST); |
| 1021 | num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list); |
| 1022 | |
| 1023 | if (num_v6 && gss->ip4_remote) |
| 1024 | return -NS_CAUSE_INVAL_NR_IPv6_EP; |
| 1025 | |
| 1026 | /* realloc to the new size */ |
| 1027 | gss->ip6_remote = talloc_realloc(gss, gss->ip6_remote, |
| 1028 | struct gprs_ns_ie_ip6_elem, |
| 1029 | gss->num_ip6_remote + num_v6); |
| 1030 | /* append the new entries to the end of the list */ |
| 1031 | memcpy(&gss->ip6_remote[gss->num_ip6_remote], v6_list, num_v6*sizeof(*v6_list)); |
| 1032 | gss->num_ip6_remote += num_v6; |
| 1033 | |
| 1034 | LOGPFSML(fi, LOGL_INFO, "Rx SNS-CONFIG: Remote IPv6 list now %u entries\n", |
| 1035 | gss->num_ip6_remote); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1036 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1037 | |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1038 | return 0; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1039 | } |
| 1040 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1041 | 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] | 1042 | { |
| 1043 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 1044 | |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 1045 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
| 1046 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1047 | if (old_state != GPRS_SNS_ST_BSS_CONFIG_SGSN) |
Alexander Couzens | 790a963 | 2021-02-05 17:18:39 +0100 | [diff] [blame] | 1048 | gss->N = 0; |
| 1049 | } |
| 1050 | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1051 | 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] | 1052 | { |
| 1053 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1054 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 1055 | uint8_t cause; |
| 1056 | int rc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1057 | |
Harald Welte | f61a915 | 2021-03-02 22:20:17 +0100 | [diff] [blame] | 1058 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_BSS); |
| 1059 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1060 | switch (event) { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1061 | case GPRS_SNS_EV_RX_CONFIG_END: |
| 1062 | case GPRS_SNS_EV_RX_CONFIG: |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1063 | rc = ns_sns_append_remote_eps(fi, data); |
| 1064 | if (rc < 0) { |
| 1065 | cause = -rc; |
| 1066 | ns2_tx_sns_config_ack(gss->sns_nsvc, &cause); |
| 1067 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
| 1068 | return; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1069 | } |
Harald Welte | c1c7e4a | 2021-03-02 20:47:29 +0100 | [diff] [blame] | 1070 | if (event == GPRS_SNS_EV_RX_CONFIG_END) { |
| 1071 | /* check if sum of data / sig weights == 0 */ |
| 1072 | if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) { |
| 1073 | cause = NS_CAUSE_INVAL_WEIGH; |
| 1074 | ns2_tx_sns_config_ack(gss->sns_nsvc, &cause); |
| 1075 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
| 1076 | return; |
| 1077 | } |
| 1078 | create_missing_nsvcs(fi); |
| 1079 | ns2_tx_sns_config_ack(gss->sns_nsvc, NULL); |
| 1080 | /* start the test procedure on ALL NSVCs! */ |
| 1081 | gprs_ns2_start_alive_all_nsvcs(nse); |
| 1082 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, 0, 0); |
| 1083 | } else { |
| 1084 | /* just send CONFIG-ACK */ |
| 1085 | ns2_tx_sns_config_ack(gss->sns_nsvc, NULL); |
| 1086 | 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] | 1087 | } |
| 1088 | break; |
| 1089 | default: |
| 1090 | OSMO_ASSERT(0); |
| 1091 | } |
| 1092 | } |
| 1093 | |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1094 | /* called when receiving GPRS_SNS_EV_RX_ADD in state configure */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1095 | static void ns2_sns_st_configured_add(struct osmo_fsm_inst *fi, |
| 1096 | struct ns2_sns_state *gss, |
| 1097 | struct tlv_parsed *tp) |
| 1098 | { |
| 1099 | const struct gprs_ns_ie_ip4_elem *v4_list = NULL; |
| 1100 | const struct gprs_ns_ie_ip6_elem *v6_list = NULL; |
| 1101 | int num_v4 = 0, num_v6 = 0; |
| 1102 | uint8_t trans_id, cause = 0xff; |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1103 | unsigned int i; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1104 | int rc = 0; |
| 1105 | |
| 1106 | /* TODO: refactor EV_ADD/CHANGE/REMOVE by |
| 1107 | * check uniqueness within the lists (no doublicate entries) |
| 1108 | * check not-known-by-us and sent back a list of unknown/known values |
| 1109 | * (abnormal behaviour according to 48.016) |
| 1110 | */ |
| 1111 | |
| 1112 | trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID); |
| 1113 | if (gss->ip == IPv4) { |
| 1114 | if (!TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) { |
| 1115 | cause = NS_CAUSE_INVAL_NR_IPv4_EP; |
| 1116 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1117 | return; |
| 1118 | } |
| 1119 | |
| 1120 | v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST); |
| 1121 | num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1122 | for (i = 0; i < num_v4; i++) { |
| 1123 | unsigned int j; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1124 | rc = do_sns_add(fi, &v4_list[i], NULL); |
| 1125 | if (rc < 0) { |
| 1126 | /* rollback/undo to restore previous state */ |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1127 | for (j = 0; j < i; j++) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1128 | do_sns_delete(fi, &v4_list[j], NULL); |
| 1129 | cause = -rc; |
| 1130 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1131 | break; |
| 1132 | } |
| 1133 | } |
| 1134 | } else { /* IPv6 */ |
| 1135 | if (!TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) { |
| 1136 | cause = NS_CAUSE_INVAL_NR_IPv6_EP; |
| 1137 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1138 | return; |
| 1139 | } |
| 1140 | |
| 1141 | v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST); |
| 1142 | num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1143 | for (i = 0; i < num_v6; i++) { |
| 1144 | unsigned int j; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1145 | rc = do_sns_add(fi, NULL, &v6_list[i]); |
| 1146 | if (rc < 0) { |
| 1147 | /* rollback/undo to restore previous state */ |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1148 | for (j = 0; j < i; j++) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1149 | do_sns_delete(fi, NULL, &v6_list[j]); |
| 1150 | cause = -rc; |
| 1151 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1152 | break; |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | /* TODO: correct behaviour is to answer to the *same* NSVC from which the SNS_ADD was received */ |
| 1158 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6); |
| 1159 | } |
| 1160 | |
| 1161 | static void ns2_sns_st_configured_delete(struct osmo_fsm_inst *fi, |
| 1162 | struct ns2_sns_state *gss, |
| 1163 | struct tlv_parsed *tp) |
| 1164 | { |
| 1165 | const struct gprs_ns_ie_ip4_elem *v4_list = NULL; |
| 1166 | const struct gprs_ns_ie_ip6_elem *v6_list = NULL; |
| 1167 | int num_v4 = 0, num_v6 = 0; |
| 1168 | uint8_t trans_id, cause = 0xff; |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1169 | unsigned int i; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1170 | int rc = 0; |
| 1171 | |
| 1172 | /* TODO: split up delete into v4 + v6 |
| 1173 | * TODO: check if IPv4_LIST or IP_ADDR(v4) is present on IPv6 and vice versa |
| 1174 | * TODO: check if IPv4_LIST/IPv6_LIST and IP_ADDR is present at the same time |
| 1175 | */ |
| 1176 | trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID); |
| 1177 | if (gss->ip == IPv4) { |
| 1178 | if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) { |
| 1179 | v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST); |
| 1180 | num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1181 | for ( i = 0; i < num_v4; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1182 | rc = do_sns_delete(fi, &v4_list[i], NULL); |
| 1183 | if (rc < 0) { |
| 1184 | cause = -rc; |
| 1185 | /* continue to delete others */ |
| 1186 | } |
| 1187 | } |
| 1188 | if (cause != 0xff) { |
| 1189 | /* TODO: create list of not-deleted and return it */ |
| 1190 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1191 | return; |
| 1192 | } |
| 1193 | |
| 1194 | } else if (TLVP_PRESENT(tp, NS_IE_IP_ADDR) && TLVP_LEN(tp, NS_IE_IP_ADDR) == 5) { |
| 1195 | /* delete all NS-VCs for given IPv4 address */ |
| 1196 | const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR); |
| 1197 | struct gprs_ns_ie_ip4_elem *ip4_remote; |
| 1198 | uint32_t ip_addr = *(uint32_t *)(ie+1); |
| 1199 | if (ie[0] != 0x01) { /* Address Type != IPv4 */ |
| 1200 | cause = NS_CAUSE_UNKN_IP_ADDR; |
| 1201 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1202 | return; |
| 1203 | } |
| 1204 | /* make a copy as do_sns_delete() will change the array underneath us */ |
| 1205 | ip4_remote = talloc_memdup(fi, gss->ip4_remote, |
| 1206 | gss->num_ip4_remote * sizeof(*v4_list)); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1207 | for (i = 0; i < gss->num_ip4_remote; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1208 | if (ip4_remote[i].ip_addr == ip_addr) { |
| 1209 | rc = do_sns_delete(fi, &ip4_remote[i], NULL); |
| 1210 | if (rc < 0) { |
| 1211 | cause = -rc; |
| 1212 | /* continue to delete others */ |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| 1216 | talloc_free(ip4_remote); |
| 1217 | if (cause != 0xff) { |
| 1218 | /* TODO: create list of not-deleted and return it */ |
| 1219 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1220 | return; |
| 1221 | } |
| 1222 | } else { |
| 1223 | cause = NS_CAUSE_INVAL_NR_IPv4_EP; |
| 1224 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1225 | return; |
| 1226 | } |
| 1227 | } else { /* IPv6 */ |
| 1228 | if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) { |
| 1229 | v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST); |
| 1230 | num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1231 | for (i = 0; i < num_v6; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1232 | rc = do_sns_delete(fi, NULL, &v6_list[i]); |
| 1233 | if (rc < 0) { |
| 1234 | cause = -rc; |
| 1235 | /* continue to delete others */ |
| 1236 | } |
| 1237 | } |
| 1238 | if (cause != 0xff) { |
| 1239 | /* TODO: create list of not-deleted and return it */ |
| 1240 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1241 | return; |
| 1242 | } |
| 1243 | } else if (TLVP_PRES_LEN(tp, NS_IE_IP_ADDR, 17)) { |
| 1244 | /* delete all NS-VCs for given IPv4 address */ |
| 1245 | const uint8_t *ie = TLVP_VAL(tp, NS_IE_IP_ADDR); |
| 1246 | struct gprs_ns_ie_ip6_elem *ip6_remote; |
| 1247 | struct in6_addr ip6_addr; |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1248 | unsigned int i; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1249 | if (ie[0] != 0x02) { /* Address Type != IPv6 */ |
| 1250 | cause = NS_CAUSE_UNKN_IP_ADDR; |
| 1251 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1252 | return; |
| 1253 | } |
| 1254 | memcpy(&ip6_addr, (ie+1), sizeof(struct in6_addr)); |
| 1255 | /* make a copy as do_sns_delete() will change the array underneath us */ |
| 1256 | ip6_remote = talloc_memdup(fi, gss->ip6_remote, |
| 1257 | gss->num_ip6_remote * sizeof(*v4_list)); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1258 | for (i = 0; i < gss->num_ip6_remote; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1259 | if (!memcmp(&ip6_remote[i].ip_addr, &ip6_addr, sizeof(struct in6_addr))) { |
| 1260 | rc = do_sns_delete(fi, NULL, &ip6_remote[i]); |
| 1261 | if (rc < 0) { |
| 1262 | cause = -rc; |
| 1263 | /* continue to delete others */ |
| 1264 | } |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | talloc_free(ip6_remote); |
| 1269 | if (cause != 0xff) { |
| 1270 | /* TODO: create list of not-deleted and return it */ |
| 1271 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1272 | return; |
| 1273 | } |
| 1274 | } else { |
| 1275 | cause = NS_CAUSE_INVAL_NR_IPv6_EP; |
| 1276 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1277 | return; |
| 1278 | } |
| 1279 | } |
| 1280 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6); |
| 1281 | } |
| 1282 | |
| 1283 | static void ns2_sns_st_configured_change(struct osmo_fsm_inst *fi, |
| 1284 | struct ns2_sns_state *gss, |
| 1285 | struct tlv_parsed *tp) |
| 1286 | { |
| 1287 | const struct gprs_ns_ie_ip4_elem *v4_list = NULL; |
| 1288 | const struct gprs_ns_ie_ip6_elem *v6_list = NULL; |
| 1289 | int num_v4 = 0, num_v6 = 0; |
| 1290 | uint8_t trans_id, cause = 0xff; |
| 1291 | int rc = 0; |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1292 | unsigned int i; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1293 | |
| 1294 | trans_id = *TLVP_VAL(tp, NS_IE_TRANS_ID); |
| 1295 | if (TLVP_PRESENT(tp, NS_IE_IPv4_LIST)) { |
| 1296 | v4_list = (const struct gprs_ns_ie_ip4_elem *) TLVP_VAL(tp, NS_IE_IPv4_LIST); |
| 1297 | num_v4 = TLVP_LEN(tp, NS_IE_IPv4_LIST) / sizeof(*v4_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1298 | for (i = 0; i < num_v4; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1299 | rc = do_sns_change_weight(fi, &v4_list[i], NULL); |
| 1300 | if (rc < 0) { |
| 1301 | cause = -rc; |
| 1302 | /* continue to others */ |
| 1303 | } |
| 1304 | } |
| 1305 | if (cause != 0xff) { |
| 1306 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1307 | return; |
| 1308 | } |
| 1309 | } else if (TLVP_PRESENT(tp, NS_IE_IPv6_LIST)) { |
| 1310 | v6_list = (const struct gprs_ns_ie_ip6_elem *) TLVP_VAL(tp, NS_IE_IPv6_LIST); |
| 1311 | num_v6 = TLVP_LEN(tp, NS_IE_IPv6_LIST) / sizeof(*v6_list); |
Harald Welte | 7da6ace | 2020-09-18 09:48:05 +0200 | [diff] [blame] | 1312 | for (i = 0; i < num_v6; i++) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1313 | rc = do_sns_change_weight(fi, NULL, &v6_list[i]); |
| 1314 | if (rc < 0) { |
| 1315 | cause = -rc; |
| 1316 | /* continue to others */ |
| 1317 | } |
| 1318 | } |
| 1319 | if (cause != 0xff) { |
| 1320 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1321 | return; |
| 1322 | } |
| 1323 | } else { |
| 1324 | cause = NS_CAUSE_INVAL_NR_IPv4_EP; |
| 1325 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, &cause, NULL, 0, NULL, 0); |
| 1326 | return; |
| 1327 | } |
| 1328 | ns2_tx_sns_ack(gss->sns_nsvc, trans_id, NULL, v4_list, num_v4, v6_list, num_v6); |
| 1329 | } |
| 1330 | |
| 1331 | static void ns2_sns_st_configured(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 1332 | { |
| 1333 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 1334 | struct tlv_parsed *tp = data; |
| 1335 | |
| 1336 | switch (event) { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1337 | case GPRS_SNS_EV_RX_ADD: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1338 | ns2_sns_st_configured_add(fi, gss, tp); |
| 1339 | break; |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1340 | case GPRS_SNS_EV_RX_DELETE: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1341 | ns2_sns_st_configured_delete(fi, gss, tp); |
| 1342 | break; |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1343 | case GPRS_SNS_EV_RX_CHANGE_WEIGHT: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1344 | ns2_sns_st_configured_change(fi, gss, tp); |
| 1345 | break; |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1346 | case GPRS_SNS_EV_REQ_NSVC_ALIVE: |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1347 | osmo_timer_del(&fi->timer); |
| 1348 | break; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | static void ns2_sns_st_configured_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
| 1353 | { |
| 1354 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1355 | ns2_prim_status_ind(nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | static const struct osmo_fsm_state ns2_sns_bss_states[] = { |
| 1359 | [GPRS_SNS_ST_UNCONFIGURED] = { |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1360 | .in_event_mask = 0, /* handled by all_state_action */ |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1361 | .out_state_mask = S(GPRS_SNS_ST_BSS_SIZE), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1362 | .name = "UNCONFIGURED", |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1363 | .action = ns2_sns_st_bss_unconfigured, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1364 | }, |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1365 | [GPRS_SNS_ST_BSS_SIZE] = { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1366 | .in_event_mask = S(GPRS_SNS_EV_RX_SIZE_ACK), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1367 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1368 | S(GPRS_SNS_ST_BSS_SIZE) | |
| 1369 | S(GPRS_SNS_ST_BSS_CONFIG_BSS), |
| 1370 | .name = "BSS_SIZE", |
| 1371 | .action = ns2_sns_st_bss_size, |
| 1372 | .onenter = ns2_sns_st_bss_size_onenter, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1373 | }, |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1374 | [GPRS_SNS_ST_BSS_CONFIG_BSS] = { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1375 | .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1376 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1377 | S(GPRS_SNS_ST_BSS_CONFIG_BSS) | |
| 1378 | S(GPRS_SNS_ST_BSS_CONFIG_SGSN) | |
| 1379 | S(GPRS_SNS_ST_BSS_SIZE), |
| 1380 | .name = "BSS_CONFIG_BSS", |
| 1381 | .action = ns2_sns_st_bss_config_bss, |
| 1382 | .onenter = ns2_sns_st_bss_config_bss_onenter, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1383 | }, |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1384 | [GPRS_SNS_ST_BSS_CONFIG_SGSN] = { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1385 | .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) | |
| 1386 | S(GPRS_SNS_EV_RX_CONFIG_END), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1387 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1388 | S(GPRS_SNS_ST_BSS_CONFIG_SGSN) | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1389 | S(GPRS_SNS_ST_CONFIGURED) | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1390 | S(GPRS_SNS_ST_BSS_SIZE), |
| 1391 | .name = "BSS_CONFIG_SGSN", |
| 1392 | .action = ns2_sns_st_bss_config_sgsn, |
| 1393 | .onenter = ns2_sns_st_bss_config_sgsn_onenter, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1394 | }, |
| 1395 | [GPRS_SNS_ST_CONFIGURED] = { |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1396 | .in_event_mask = S(GPRS_SNS_EV_RX_ADD) | |
| 1397 | S(GPRS_SNS_EV_RX_DELETE) | |
| 1398 | S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) | |
| 1399 | S(GPRS_SNS_EV_REQ_NSVC_ALIVE), |
Alexander Couzens | e03d863 | 2020-12-06 03:31:44 +0100 | [diff] [blame] | 1400 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1401 | S(GPRS_SNS_ST_BSS_SIZE), |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1402 | .name = "CONFIGURED", |
| 1403 | .action = ns2_sns_st_configured, |
| 1404 | .onenter = ns2_sns_st_configured_onenter, |
| 1405 | }, |
| 1406 | }; |
| 1407 | |
| 1408 | static int ns2_sns_fsm_bss_timer_cb(struct osmo_fsm_inst *fi) |
| 1409 | { |
Alexander Couzens | 90ee963 | 2020-12-07 06:18:32 +0100 | [diff] [blame] | 1410 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1411 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 1412 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 1413 | |
Alexander Couzens | 90ee963 | 2020-12-07 06:18:32 +0100 | [diff] [blame] | 1414 | gss->N++; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1415 | switch (fi->T) { |
| 1416 | case 1: |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1417 | if (gss->N >= nsi->timeout[NS_TOUT_TSNS_SIZE_RETRIES]) { |
| 1418 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: Size retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei); |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1419 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1420 | } else { |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1421 | 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] | 1422 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1423 | break; |
| 1424 | case 2: |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1425 | if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) { |
Alexander Couzens | 3df5886 | 2021-02-05 17:18:08 +0100 | [diff] [blame] | 1426 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: BSS Config retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei); |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1427 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
Alexander Couzens | a367d08 | 2020-12-21 14:06:24 +0100 | [diff] [blame] | 1428 | } else { |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1429 | 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] | 1430 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1431 | break; |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1432 | case 3: |
Alexander Couzens | 3df5886 | 2021-02-05 17:18:08 +0100 | [diff] [blame] | 1433 | if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) { |
| 1434 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Selecting next IP-SNS endpoint.\n", nse->nsei); |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1435 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
Alexander Couzens | 3df5886 | 2021-02-05 17:18:08 +0100 | [diff] [blame] | 1436 | } else { |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1437 | 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] | 1438 | } |
| 1439 | break; |
| 1440 | case 4: |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1441 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online. Selecting next IP-SNS endpoint.\n", nse->nsei); |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1442 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1443 | break; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1444 | } |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 1448 | /* common allstate-action for both roles */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1449 | static void ns2_sns_st_all_action(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 1450 | { |
| 1451 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1452 | struct ns2_sns_bind *sbind; |
| 1453 | struct gprs_ns2_vc *nsvc, *nsvc2; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1454 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1455 | switch (event) { |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1456 | case GPRS_SNS_EV_REQ_ADD_BIND: |
| 1457 | sbind = data; |
| 1458 | switch (fi->state) { |
| 1459 | case GPRS_SNS_ST_UNCONFIGURED: |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1460 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1461 | break; |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1462 | case GPRS_SNS_ST_BSS_SIZE: |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1463 | /* TODO: add the ip4 element to the list */ |
| 1464 | break; |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1465 | case GPRS_SNS_ST_BSS_CONFIG_BSS: |
| 1466 | case GPRS_SNS_ST_BSS_CONFIG_SGSN: |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1467 | case GPRS_SNS_ST_CONFIGURED: |
| 1468 | /* TODO: add to SNS-IP procedure queue & add nsvc() */ |
| 1469 | break; |
| 1470 | } |
| 1471 | break; |
| 1472 | case GPRS_SNS_EV_REQ_DELETE_BIND: |
| 1473 | sbind = data; |
| 1474 | switch (fi->state) { |
| 1475 | case GPRS_SNS_ST_UNCONFIGURED: |
| 1476 | break; |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1477 | case GPRS_SNS_ST_BSS_SIZE: |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1478 | /* TODO: remove the ip4 element from the list */ |
| 1479 | llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) { |
| 1480 | if (nsvc->bind == sbind->bind) { |
| 1481 | gprs_ns2_free_nsvc(nsvc); |
| 1482 | } |
| 1483 | } |
| 1484 | break; |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1485 | case GPRS_SNS_ST_BSS_CONFIG_BSS: |
| 1486 | case GPRS_SNS_ST_BSS_CONFIG_SGSN: |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1487 | case GPRS_SNS_ST_CONFIGURED: |
| 1488 | /* TODO: do an delete SNS-IP procedure */ |
| 1489 | /* TODO: remove the ip4 element to the list */ |
| 1490 | llist_for_each_entry_safe(nsvc, nsvc2, &nse->nsvc, list) { |
| 1491 | if (nsvc->bind == sbind->bind) { |
| 1492 | gprs_ns2_free_nsvc(nsvc); |
| 1493 | } |
| 1494 | } |
| 1495 | break; |
| 1496 | } |
| 1497 | /* if this is the last bind, the free_nsvc() will trigger a reselection */ |
| 1498 | talloc_free(sbind); |
| 1499 | break; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1500 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1501 | } |
| 1502 | |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 1503 | /* allstate-action for BSS role */ |
| 1504 | static void ns2_sns_st_all_action_bss(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 1505 | { |
| 1506 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 1507 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 1508 | |
| 1509 | /* reset when receiving GPRS_SNS_EV_REQ_NO_NSVC */ |
| 1510 | switch (event) { |
| 1511 | case GPRS_SNS_EV_REQ_NO_NSVC: |
| 1512 | /* ignore reselection running */ |
| 1513 | if (gss->reselection_running) |
| 1514 | break; |
| 1515 | |
| 1516 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: no remaining NSVC, resetting SNS FSM\n", nse->nsei); |
| 1517 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
| 1518 | break; |
| 1519 | case GPRS_SNS_EV_REQ_SELECT_ENDPOINT: |
| 1520 | /* tear down previous state |
| 1521 | * gprs_ns2_free_nsvcs() will trigger NO_NSVC, prevent this from triggering a reselection */ |
| 1522 | gss->reselection_running = true; |
| 1523 | gprs_ns2_free_nsvcs(nse); |
Harald Welte | 46eb764 | 2021-03-04 17:49:59 +0100 | [diff] [blame] | 1524 | ns2_clear_ipv46_entries_local(gss); |
| 1525 | ns2_clear_ipv46_entries_remote(gss); |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 1526 | |
| 1527 | /* Choose the next sns endpoint. */ |
| 1528 | if (llist_empty(&gss->sns_endpoints) || llist_empty(&gss->binds)) { |
| 1529 | gss->initial = NULL; |
| 1530 | ns2_prim_status_ind(gss->nse, NULL, 0, GPRS_NS2_AFF_CAUSE_SNS_NO_ENDPOINTS); |
| 1531 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 3); |
| 1532 | return; |
| 1533 | } else if (!gss->initial) { |
| 1534 | gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list); |
| 1535 | } else if (gss->initial->list.next == &gss->sns_endpoints) { |
| 1536 | /* last entry, continue with first */ |
| 1537 | gss->initial = llist_first_entry(&gss->sns_endpoints, struct sns_endpoint, list); |
| 1538 | } else { |
| 1539 | /* next element is an entry */ |
| 1540 | gss->initial = llist_entry(gss->initial->list.next, struct sns_endpoint, list); |
| 1541 | } |
| 1542 | |
| 1543 | gss->reselection_running = false; |
Harald Welte | 694dad5 | 2021-03-23 15:22:16 +0100 | [diff] [blame] | 1544 | 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] | 1545 | break; |
| 1546 | default: |
| 1547 | ns2_sns_st_all_action(fi, event, data); |
| 1548 | break; |
| 1549 | } |
| 1550 | } |
| 1551 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1552 | static struct osmo_fsm gprs_ns2_sns_bss_fsm = { |
| 1553 | .name = "GPRS-NS2-SNS-BSS", |
| 1554 | .states = ns2_sns_bss_states, |
| 1555 | .num_states = ARRAY_SIZE(ns2_sns_bss_states), |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1556 | .allstate_event_mask = S(GPRS_SNS_EV_REQ_NO_NSVC) | |
| 1557 | S(GPRS_SNS_EV_REQ_SELECT_ENDPOINT) | |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1558 | S(GPRS_SNS_EV_REQ_ADD_BIND) | |
| 1559 | S(GPRS_SNS_EV_REQ_DELETE_BIND), |
Harald Welte | 9e37bf4 | 2021-03-02 20:48:31 +0100 | [diff] [blame] | 1560 | .allstate_action = ns2_sns_st_all_action_bss, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1561 | .cleanup = NULL, |
| 1562 | .timer_cb = ns2_sns_fsm_bss_timer_cb, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1563 | .event_names = gprs_sns_event_names, |
| 1564 | .pre_term = NULL, |
| 1565 | .log_subsys = DLNS, |
| 1566 | }; |
| 1567 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1568 | /*! Allocate an IP-SNS FSM for the BSS side. |
| 1569 | * \param[in] nse NS Entity in which the FSM runs |
| 1570 | * \param[in] id string identifier |
Alexander Couzens | 23aec35 | 2021-02-15 05:05:15 +0100 | [diff] [blame] | 1571 | * \returns FSM instance on success; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1572 | struct osmo_fsm_inst *ns2_sns_bss_fsm_alloc(struct gprs_ns2_nse *nse, |
| 1573 | const char *id) |
| 1574 | { |
| 1575 | struct osmo_fsm_inst *fi; |
| 1576 | struct ns2_sns_state *gss; |
| 1577 | |
| 1578 | fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_bss_fsm, nse, NULL, LOGL_DEBUG, id); |
| 1579 | if (!fi) |
| 1580 | return fi; |
| 1581 | |
| 1582 | gss = talloc_zero(fi, struct ns2_sns_state); |
| 1583 | if (!gss) |
| 1584 | goto err; |
| 1585 | |
| 1586 | fi->priv = gss; |
| 1587 | gss->nse = nse; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 1588 | gss->role = GPRS_SNS_ROLE_BSS; |
Harald Welte | 24f4df5 | 2021-03-04 18:02:54 +0100 | [diff] [blame] | 1589 | /* The SGSN doesn't tell the BSS, so we assume there's always sufficient */ |
| 1590 | gss->num_max_ip4_remote = 8192; |
| 1591 | gss->num_max_ip6_remote = 8192; |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1592 | INIT_LLIST_HEAD(&gss->sns_endpoints); |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1593 | INIT_LLIST_HEAD(&gss->binds); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1594 | |
| 1595 | return fi; |
| 1596 | err: |
| 1597 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 1598 | return NULL; |
| 1599 | } |
| 1600 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1601 | /*! main entry point for receiving SNS messages from the network. |
| 1602 | * \param[in] nsvc NS-VC on which the message was received |
| 1603 | * \param[in] msg message buffer of the IP-SNS message |
| 1604 | * \param[in] tp parsed TLV structure of message |
Alexander Couzens | 23aec35 | 2021-02-15 05:05:15 +0100 | [diff] [blame] | 1605 | * \returns 0 on success; negative on error */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1606 | 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] | 1607 | { |
| 1608 | struct gprs_ns2_nse *nse = nsvc->nse; |
| 1609 | struct gprs_ns_hdr *nsh = (struct gprs_ns_hdr *) msg->l2h; |
| 1610 | uint16_t nsei = nsvc->nse->nsei; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 1611 | struct ns2_sns_state *gss; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1612 | struct osmo_fsm_inst *fi; |
| 1613 | |
| 1614 | if (!nse->bss_sns_fi) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 1615 | LOGNSVC(nsvc, LOGL_NOTICE, "Rx %s for NS Instance that has no SNS!\n", |
| 1616 | get_value_string(gprs_ns_pdu_strings, nsh->pdu_type)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1617 | return -EINVAL; |
| 1618 | } |
| 1619 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1620 | /* FIXME: how to resolve SNS FSM Instance by NSEI (SGSN)? */ |
| 1621 | fi = nse->bss_sns_fi; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 1622 | gss = (struct ns2_sns_state *) fi->priv; |
| 1623 | if (!gss->sns_nsvc) |
| 1624 | gss->sns_nsvc = nsvc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1625 | |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 1626 | LOGPFSML(fi, LOGL_DEBUG, "NSEI=%u Rx SNS PDU type %s\n", nsei, |
| 1627 | get_value_string(gprs_ns_pdu_strings, nsh->pdu_type)); |
| 1628 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1629 | switch (nsh->pdu_type) { |
| 1630 | case SNS_PDUT_SIZE: |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1631 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1632 | break; |
| 1633 | case SNS_PDUT_SIZE_ACK: |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1634 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_SIZE_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1635 | break; |
| 1636 | case SNS_PDUT_CONFIG: |
| 1637 | if (nsh->data[0] & 0x01) |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1638 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_END, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1639 | else |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1640 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1641 | break; |
| 1642 | case SNS_PDUT_CONFIG_ACK: |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1643 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CONFIG_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1644 | break; |
| 1645 | case SNS_PDUT_ADD: |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1646 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ADD, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1647 | break; |
| 1648 | case SNS_PDUT_DELETE: |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1649 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_DELETE, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1650 | break; |
| 1651 | case SNS_PDUT_CHANGE_WEIGHT: |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1652 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_CHANGE_WEIGHT, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1653 | break; |
| 1654 | case SNS_PDUT_ACK: |
Harald Welte | b9f2387 | 2021-03-02 20:48:54 +0100 | [diff] [blame] | 1655 | osmo_fsm_inst_dispatch(fi, GPRS_SNS_EV_RX_ACK, tp); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1656 | break; |
| 1657 | default: |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 1658 | LOGPFSML(fi, LOGL_ERROR, "NSEI=%u Rx unknown SNS PDU type %s\n", nsei, |
| 1659 | get_value_string(gprs_ns_pdu_strings, nsh->pdu_type)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1660 | return -EINVAL; |
| 1661 | } |
| 1662 | |
| 1663 | return 0; |
| 1664 | } |
| 1665 | |
| 1666 | #include <osmocom/vty/vty.h> |
| 1667 | #include <osmocom/vty/misc.h> |
| 1668 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1669 | 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] | 1670 | { |
| 1671 | struct in_addr in = { .s_addr = ip4->ip_addr }; |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1672 | 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] | 1673 | inet_ntoa(in), ntohs(ip4->udp_port), ip4->sig_weight, ip4->data_weight, VTY_NEWLINE); |
| 1674 | } |
| 1675 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1676 | 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] | 1677 | { |
| 1678 | char ip_addr[INET6_ADDRSTRLEN] = {}; |
| 1679 | if (!inet_ntop(AF_INET6, &ip6->ip_addr, ip_addr, (INET6_ADDRSTRLEN))) |
| 1680 | strcpy(ip_addr, "Invalid IPv6"); |
| 1681 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1682 | 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] | 1683 | ip_addr, ntohs(ip6->udp_port), ip6->sig_weight, ip6->data_weight, VTY_NEWLINE); |
| 1684 | } |
| 1685 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 1686 | /*! Dump the IP-SNS state to a vty. |
| 1687 | * \param[in] vty VTY to which the state shall be printed |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1688 | * \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] | 1689 | * \param[in] nse NS Entity whose IP-SNS state shall be printed |
| 1690 | * \param[in] stats Whether or not statistics shall also be printed */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1691 | 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] | 1692 | { |
| 1693 | struct ns2_sns_state *gss; |
| 1694 | unsigned int i; |
| 1695 | |
| 1696 | if (!nse->bss_sns_fi) |
| 1697 | return; |
| 1698 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1699 | vty_out_fsm_inst2(vty, prefix, nse->bss_sns_fi); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1700 | gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv; |
| 1701 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1702 | vty_out(vty, "%sMaximum number of remote NS-VCs: %zu, IPv4 Endpoints: %zu, IPv6 Endpoints: %zu%s", |
| 1703 | 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] | 1704 | |
| 1705 | if (gss->num_ip4_local && gss->num_ip4_remote) { |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1706 | vty_out(vty, "%sLocal IPv4 Endpoints:%s", prefix, VTY_NEWLINE); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1707 | for (i = 0; i < gss->num_ip4_local; i++) |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1708 | vty_dump_sns_ip4(vty, prefix, &gss->ip4_local[i]); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1709 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1710 | vty_out(vty, "%sRemote IPv4 Endpoints:%s", prefix, VTY_NEWLINE); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1711 | for (i = 0; i < gss->num_ip4_remote; i++) |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1712 | vty_dump_sns_ip4(vty, prefix, &gss->ip4_remote[i]); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | if (gss->num_ip6_local && gss->num_ip6_remote) { |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1716 | vty_out(vty, "%sLocal IPv6 Endpoints:%s", prefix, VTY_NEWLINE); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1717 | for (i = 0; i < gss->num_ip6_local; i++) |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1718 | vty_dump_sns_ip6(vty, prefix, &gss->ip6_local[i]); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1719 | |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1720 | vty_out(vty, "%sRemote IPv6 Endpoints:%s", prefix, VTY_NEWLINE); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1721 | for (i = 0; i < gss->num_ip6_remote; i++) |
Harald Welte | 1262c4f | 2021-01-19 20:58:33 +0100 | [diff] [blame] | 1722 | vty_dump_sns_ip6(vty, prefix, &gss->ip6_remote[i]); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1723 | } |
| 1724 | } |
| 1725 | |
Alexander Couzens | 412bc34 | 2020-11-19 05:24:37 +0100 | [diff] [blame] | 1726 | /*! write IP-SNS to a vty |
| 1727 | * \param[in] vty VTY to which the state shall be printed |
| 1728 | * \param[in] nse NS Entity whose IP-SNS state shall be printed */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 1729 | 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] | 1730 | { |
| 1731 | struct ns2_sns_state *gss; |
| 1732 | struct osmo_sockaddr_str addr_str; |
| 1733 | struct sns_endpoint *endpoint; |
| 1734 | |
| 1735 | if (!nse->bss_sns_fi) |
| 1736 | return; |
| 1737 | |
| 1738 | gss = (struct ns2_sns_state *) nse->bss_sns_fi->priv; |
| 1739 | llist_for_each_entry(endpoint, &gss->sns_endpoints, list) { |
Vadim Yanitskiy | d8b7003 | 2021-01-05 14:24:09 +0100 | [diff] [blame] | 1740 | /* It's unlikely that an error happens, but let's better be safe. */ |
| 1741 | if (osmo_sockaddr_str_from_sockaddr(&addr_str, &endpoint->saddr.u.sas) != 0) |
| 1742 | addr_str = (struct osmo_sockaddr_str) { .ip = "<INVALID>" }; |
Alexander Couzens | 5fa431c | 2021-02-08 23:21:54 +0100 | [diff] [blame] | 1743 | 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] | 1744 | } |
| 1745 | } |
| 1746 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1747 | static struct sns_endpoint *ns2_get_sns_endpoint(struct ns2_sns_state *state, |
| 1748 | const struct osmo_sockaddr *saddr) |
| 1749 | { |
| 1750 | struct sns_endpoint *endpoint; |
| 1751 | |
| 1752 | llist_for_each_entry(endpoint, &state->sns_endpoints, list) { |
| 1753 | if (!osmo_sockaddr_cmp(saddr, &endpoint->saddr)) |
| 1754 | return endpoint; |
| 1755 | } |
| 1756 | |
| 1757 | return NULL; |
| 1758 | } |
| 1759 | |
| 1760 | /*! gprs_ns2_sns_add_endpoint |
| 1761 | * \param[in] nse |
| 1762 | * \param[in] sockaddr |
| 1763 | * \return |
| 1764 | */ |
| 1765 | int gprs_ns2_sns_add_endpoint(struct gprs_ns2_nse *nse, |
| 1766 | const struct osmo_sockaddr *saddr) |
| 1767 | { |
| 1768 | struct ns2_sns_state *gss; |
| 1769 | struct sns_endpoint *endpoint; |
| 1770 | bool do_selection = false; |
| 1771 | |
| 1772 | if (nse->ll != GPRS_NS2_LL_UDP) { |
| 1773 | return -EINVAL; |
| 1774 | } |
| 1775 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1776 | if (nse->dialect != GPRS_NS2_DIALECT_SNS) { |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1777 | return -EINVAL; |
| 1778 | } |
| 1779 | |
| 1780 | gss = nse->bss_sns_fi->priv; |
| 1781 | |
| 1782 | if (ns2_get_sns_endpoint(gss, saddr)) |
| 1783 | return -EADDRINUSE; |
| 1784 | |
| 1785 | endpoint = talloc_zero(nse->bss_sns_fi->priv, struct sns_endpoint); |
| 1786 | if (!endpoint) |
| 1787 | return -ENOMEM; |
| 1788 | |
| 1789 | endpoint->saddr = *saddr; |
| 1790 | if (llist_empty(&gss->sns_endpoints)) |
| 1791 | do_selection = true; |
| 1792 | |
| 1793 | llist_add_tail(&endpoint->list, &gss->sns_endpoints); |
| 1794 | if (do_selection) |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1795 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_SELECT_ENDPOINT, NULL); |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1796 | |
| 1797 | return 0; |
| 1798 | } |
| 1799 | |
| 1800 | /*! gprs_ns2_sns_del_endpoint |
| 1801 | * \param[in] nse |
| 1802 | * \param[in] sockaddr |
| 1803 | * \return 0 on success, otherwise < 0 |
| 1804 | */ |
| 1805 | int gprs_ns2_sns_del_endpoint(struct gprs_ns2_nse *nse, |
| 1806 | const struct osmo_sockaddr *saddr) |
| 1807 | { |
| 1808 | struct ns2_sns_state *gss; |
| 1809 | struct sns_endpoint *endpoint; |
| 1810 | |
| 1811 | if (nse->ll != GPRS_NS2_LL_UDP) { |
| 1812 | return -EINVAL; |
| 1813 | } |
| 1814 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1815 | if (nse->dialect != GPRS_NS2_DIALECT_SNS) { |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1816 | return -EINVAL; |
| 1817 | } |
| 1818 | |
| 1819 | gss = nse->bss_sns_fi->priv; |
| 1820 | endpoint = ns2_get_sns_endpoint(gss, saddr); |
| 1821 | if (!endpoint) |
| 1822 | return -ENOENT; |
| 1823 | |
| 1824 | /* if this is an unused SNS endpoint it's done */ |
| 1825 | if (gss->initial != endpoint) { |
| 1826 | llist_del(&endpoint->list); |
| 1827 | talloc_free(endpoint); |
| 1828 | return 0; |
| 1829 | } |
| 1830 | |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1831 | /* gprs_ns2_free_nsvcs() will trigger GPRS_SNS_EV_REQ_NO_NSVC on the last NS-VC |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1832 | * and restart SNS SIZE procedure which selects a new initial */ |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 1833 | LOGNSE(nse, LOGL_INFO, "Current in-use SNS endpoint is being removed." |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1834 | "Closing all NS-VC and restart SNS-SIZE procedure" |
| 1835 | "with a remaining SNS endpoint.\n"); |
| 1836 | |
| 1837 | /* Continue with the next endpoint in the list. |
| 1838 | * Special case if the endpoint is at the start or end of the list */ |
| 1839 | if (endpoint->list.prev == &gss->sns_endpoints || |
| 1840 | endpoint->list.next == &gss->sns_endpoints) |
| 1841 | gss->initial = NULL; |
| 1842 | else |
| 1843 | gss->initial = llist_entry(endpoint->list.next->prev, |
| 1844 | struct sns_endpoint, |
| 1845 | list); |
| 1846 | |
| 1847 | llist_del(&endpoint->list); |
| 1848 | gprs_ns2_free_nsvcs(nse); |
| 1849 | talloc_free(endpoint); |
| 1850 | |
| 1851 | return 0; |
| 1852 | } |
| 1853 | |
| 1854 | /*! gprs_ns2_sns_count |
| 1855 | * \param[in] nse NS Entity whose IP-SNS endpoints shall be printed |
| 1856 | * \return the count of endpoints or < 0 if NSE doesn't contain sns. |
| 1857 | */ |
| 1858 | int gprs_ns2_sns_count(struct gprs_ns2_nse *nse) |
| 1859 | { |
| 1860 | struct ns2_sns_state *gss; |
| 1861 | struct sns_endpoint *endpoint; |
| 1862 | int count = 0; |
| 1863 | |
| 1864 | if (nse->ll != GPRS_NS2_LL_UDP) { |
| 1865 | return -EINVAL; |
| 1866 | } |
| 1867 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 1868 | if (nse->dialect != GPRS_NS2_DIALECT_SNS) { |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 1869 | return -EINVAL; |
| 1870 | } |
| 1871 | |
| 1872 | gss = nse->bss_sns_fi->priv; |
| 1873 | llist_for_each_entry(endpoint, &gss->sns_endpoints, list) |
| 1874 | count++; |
| 1875 | |
| 1876 | return count; |
| 1877 | } |
| 1878 | |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1879 | void ns2_sns_notify_alive(struct gprs_ns2_nse *nse, struct gprs_ns2_vc *nsvc, bool alive) |
| 1880 | { |
| 1881 | struct ns2_sns_state *gss; |
| 1882 | struct gprs_ns2_vc *tmp; |
| 1883 | |
| 1884 | if (!nse->bss_sns_fi) |
| 1885 | return; |
| 1886 | |
| 1887 | gss = nse->bss_sns_fi->priv; |
| 1888 | if(nse->bss_sns_fi->state != GPRS_SNS_ST_CONFIGURED) |
| 1889 | return; |
| 1890 | |
| 1891 | if (alive == gss->alive) |
| 1892 | return; |
| 1893 | |
| 1894 | /* check if this is the current SNS NS-VC */ |
| 1895 | if (nsvc == gss->sns_nsvc) { |
| 1896 | /* only replace the SNS NS-VC if there are other alive NS-VC. |
| 1897 | * There aren't any other alive NS-VC when the SNS fsm just reached CONFIGURED |
| 1898 | * and couldn't confirm yet if the NS-VC comes up */ |
| 1899 | if (gss->alive && !alive) |
| 1900 | ns2_sns_replace_nsvc(nsvc); |
| 1901 | } |
| 1902 | |
| 1903 | if (alive) { |
| 1904 | gss->alive = true; |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1905 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NSVC_ALIVE, NULL); |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1906 | } else { |
| 1907 | /* is there at least another alive nsvc? */ |
| 1908 | llist_for_each_entry(tmp, &nse->nsvc, list) { |
| 1909 | if (ns2_vc_is_unblocked(tmp)) |
| 1910 | return; |
| 1911 | } |
| 1912 | |
| 1913 | /* all NS-VC have failed */ |
| 1914 | gss->alive = false; |
Alexander Couzens | 67725e2 | 2021-02-15 02:37:03 +0100 | [diff] [blame] | 1915 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_NO_NSVC, NULL); |
Alexander Couzens | be7cecc | 2021-02-03 18:25:27 +0100 | [diff] [blame] | 1916 | } |
| 1917 | } |
| 1918 | |
Alexander Couzens | 6b9d232 | 2021-02-12 03:17:59 +0100 | [diff] [blame] | 1919 | int gprs_ns2_sns_add_bind(struct gprs_ns2_nse *nse, |
| 1920 | struct gprs_ns2_vc_bind *bind) |
| 1921 | { |
| 1922 | struct ns2_sns_state *gss; |
| 1923 | struct ns2_sns_bind *tmp; |
| 1924 | |
| 1925 | OSMO_ASSERT(nse->bss_sns_fi); |
| 1926 | gss = nse->bss_sns_fi->priv; |
| 1927 | |
| 1928 | if (!gprs_ns2_is_ip_bind(bind)) { |
| 1929 | return -EINVAL; |
| 1930 | } |
| 1931 | |
| 1932 | if (!llist_empty(&gss->binds)) { |
| 1933 | llist_for_each_entry(tmp, &gss->binds, list) { |
| 1934 | if (tmp->bind == bind) |
| 1935 | return -EALREADY; |
| 1936 | } |
| 1937 | } |
| 1938 | |
| 1939 | tmp = talloc_zero(gss, struct ns2_sns_bind); |
| 1940 | if (!tmp) |
| 1941 | return -ENOMEM; |
| 1942 | tmp->bind = bind; |
| 1943 | llist_add_tail(&tmp->list, &gss->binds); |
| 1944 | |
| 1945 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_ADD_BIND, tmp); |
| 1946 | return 0; |
| 1947 | } |
| 1948 | |
| 1949 | /* Remove a bind from the SNS. All assosiated NSVC must be removed. */ |
| 1950 | int gprs_ns2_sns_del_bind(struct gprs_ns2_nse *nse, |
| 1951 | struct gprs_ns2_vc_bind *bind) |
| 1952 | { |
| 1953 | struct ns2_sns_state *gss; |
| 1954 | struct ns2_sns_bind *tmp, *tmp2; |
| 1955 | bool found = false; |
| 1956 | |
| 1957 | if (!nse->bss_sns_fi) |
| 1958 | return -EINVAL; |
| 1959 | |
| 1960 | gss = nse->bss_sns_fi->priv; |
| 1961 | if (gss->initial_bind && gss->initial_bind->bind == bind) { |
| 1962 | if (gss->initial_bind->list.prev == &gss->binds) |
| 1963 | gss->initial_bind = NULL; |
| 1964 | else |
| 1965 | gss->initial_bind = llist_entry(gss->initial_bind->list.prev, struct ns2_sns_bind, list); |
| 1966 | } |
| 1967 | |
| 1968 | llist_for_each_entry_safe(tmp, tmp2, &gss->binds, list) { |
| 1969 | if (tmp->bind == bind) { |
| 1970 | llist_del(&tmp->list); |
| 1971 | found = true; |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | if (!found) |
| 1976 | return -ENOENT; |
| 1977 | |
| 1978 | osmo_fsm_inst_dispatch(nse->bss_sns_fi, GPRS_SNS_EV_REQ_DELETE_BIND, tmp); |
| 1979 | return 0; |
| 1980 | } |
| 1981 | |
Alexander Couzens | c470476 | 2021-02-08 23:13:12 +0100 | [diff] [blame] | 1982 | /* Update SNS weights |
| 1983 | * \param[in] nsvc the NSVC which should be updated |
| 1984 | */ |
| 1985 | void ns2_sns_update_weights(struct gprs_ns2_vc_bind *bind) |
| 1986 | { |
| 1987 | /* TODO: implement weights after binds per sns implemented */ |
| 1988 | } |
| 1989 | |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 1990 | |
| 1991 | |
| 1992 | |
| 1993 | /*********************************************************************** |
| 1994 | * SGSN role |
| 1995 | ***********************************************************************/ |
| 1996 | |
| 1997 | static void ns2_sns_st_sgsn_unconfigured(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 1998 | { |
| 1999 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2000 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN); |
| 2001 | /* do nothing; Rx SNS-SIZE handled in ns2_sns_st_all_action_sgsn() */ |
| 2002 | } |
| 2003 | |
| 2004 | /* We're waiting for inbound SNS-CONFIG from the BSS */ |
| 2005 | static void ns2_sns_st_sgsn_wait_config(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 2006 | { |
| 2007 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2008 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 2009 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 2010 | uint8_t cause; |
| 2011 | int rc; |
| 2012 | |
| 2013 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN); |
| 2014 | |
| 2015 | switch (event) { |
| 2016 | case GPRS_SNS_EV_RX_CONFIG: |
| 2017 | case GPRS_SNS_EV_RX_CONFIG_END: |
| 2018 | rc = ns_sns_append_remote_eps(fi, data); |
| 2019 | if (rc < 0) { |
| 2020 | cause = -rc; |
| 2021 | ns2_tx_sns_config_ack(gss->sns_nsvc, &cause); |
| 2022 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
| 2023 | return; |
| 2024 | } |
| 2025 | /* only change state if last CONFIG was received */ |
| 2026 | if (event == GPRS_SNS_EV_RX_CONFIG_END) { |
| 2027 | /* ensure sum of data weight / sig weights is > 0 */ |
| 2028 | if (nss_weight_sum_data(gss) == 0 || nss_weight_sum_sig(gss) == 0) { |
| 2029 | cause = NS_CAUSE_INVAL_WEIGH; |
| 2030 | ns2_tx_sns_config_ack(gss->sns_nsvc, &cause); |
| 2031 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
| 2032 | break; |
| 2033 | } |
| 2034 | ns2_tx_sns_config_ack(gss->sns_nsvc, NULL); |
| 2035 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3); |
| 2036 | } else { |
| 2037 | /* just send CONFIG-ACK */ |
| 2038 | ns2_tx_sns_config_ack(gss->sns_nsvc, NULL); |
| 2039 | osmo_timer_schedule(&fi->timer, nse->nsi->timeout[NS_TOUT_TSNS_PROV], 0); |
| 2040 | } |
| 2041 | break; |
| 2042 | } |
| 2043 | } |
| 2044 | |
| 2045 | static void ns2_sns_st_sgsn_wait_config_ack_onenter(struct osmo_fsm_inst *fi, uint32_t old_state) |
| 2046 | { |
| 2047 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2048 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN); |
| 2049 | |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2050 | /* transmit SGSN-oriented SNS-CONFIG */ |
| 2051 | ns2_tx_sns_config(gss->sns_nsvc, true, gss->ip4_local, gss->num_ip4_local, |
| 2052 | gss->ip6_local, gss->num_ip6_local); |
| 2053 | } |
| 2054 | |
| 2055 | /* We're waiting for SNS-CONFIG-ACK from the BSS (in response to our outbound SNS-CONFIG) */ |
| 2056 | static void ns2_sns_st_sgsn_wait_config_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 2057 | { |
| 2058 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2059 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 2060 | struct tlv_parsed *tp = NULL; |
| 2061 | |
| 2062 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN); |
| 2063 | |
| 2064 | switch (event) { |
| 2065 | case GPRS_SNS_EV_RX_CONFIG_ACK: |
| 2066 | tp = data; |
| 2067 | if (TLVP_VAL_MINLEN(tp, NS_IE_CAUSE, 1)) { |
| 2068 | LOGPFSML(fi, LOGL_ERROR, "Rx SNS-CONFIG-ACK with cause %s\n", |
| 2069 | gprs_ns2_cause_str(*TLVP_VAL(tp, NS_IE_CAUSE))); |
| 2070 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
| 2071 | break; |
| 2072 | } |
| 2073 | /* we currently only send one SNS-CONFIG with END FLAG */ |
| 2074 | if (true) { |
| 2075 | create_missing_nsvcs(fi); |
| 2076 | /* start the test procedure on ALL NSVCs! */ |
| 2077 | gprs_ns2_start_alive_all_nsvcs(nse); |
| 2078 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_CONFIGURED, ns_sns_configured_timeout(fi), 4); |
| 2079 | } |
| 2080 | break; |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | /* SGSN-side SNS state machine */ |
| 2085 | static const struct osmo_fsm_state ns2_sns_sgsn_states[] = { |
| 2086 | [GPRS_SNS_ST_UNCONFIGURED] = { |
| 2087 | .in_event_mask = 0, /* handled by all_state_action */ |
| 2088 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
| 2089 | S(GPRS_SNS_ST_SGSN_WAIT_CONFIG), |
| 2090 | .name = "UNCONFIGURED", |
| 2091 | .action = ns2_sns_st_sgsn_unconfigured, |
| 2092 | }, |
| 2093 | [GPRS_SNS_ST_SGSN_WAIT_CONFIG] = { |
| 2094 | .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG) | |
| 2095 | S(GPRS_SNS_EV_RX_CONFIG_END), |
| 2096 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
| 2097 | S(GPRS_SNS_ST_SGSN_WAIT_CONFIG) | |
| 2098 | S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK), |
| 2099 | .name = "SGSN_WAIT_CONFIG", |
| 2100 | .action = ns2_sns_st_sgsn_wait_config, |
| 2101 | }, |
| 2102 | [GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK] = { |
| 2103 | .in_event_mask = S(GPRS_SNS_EV_RX_CONFIG_ACK), |
| 2104 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED) | |
| 2105 | S(GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK) | |
| 2106 | S(GPRS_SNS_ST_CONFIGURED), |
| 2107 | .name = "SGSN_WAIT_CONFIG_ACK", |
| 2108 | .action = ns2_sns_st_sgsn_wait_config_ack, |
| 2109 | .onenter = ns2_sns_st_sgsn_wait_config_ack_onenter, |
| 2110 | }, |
| 2111 | [GPRS_SNS_ST_CONFIGURED] = { |
| 2112 | .in_event_mask = S(GPRS_SNS_EV_RX_ADD) | |
| 2113 | S(GPRS_SNS_EV_RX_DELETE) | |
| 2114 | S(GPRS_SNS_EV_RX_CHANGE_WEIGHT) | |
| 2115 | S(GPRS_SNS_EV_REQ_NSVC_ALIVE), |
| 2116 | .out_state_mask = S(GPRS_SNS_ST_UNCONFIGURED), |
| 2117 | .name = "CONFIGURED", |
| 2118 | /* shared with BSS side; once configured there's no difference */ |
| 2119 | .action = ns2_sns_st_configured, |
| 2120 | .onenter = ns2_sns_st_configured_onenter, |
| 2121 | }, |
| 2122 | }; |
| 2123 | |
| 2124 | static int ns2_sns_fsm_sgsn_timer_cb(struct osmo_fsm_inst *fi) |
| 2125 | { |
| 2126 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2127 | struct gprs_ns2_nse *nse = nse_inst_from_fi(fi); |
| 2128 | struct gprs_ns2_inst *nsi = nse->nsi; |
| 2129 | |
| 2130 | gss->N++; |
| 2131 | switch (fi->T) { |
| 2132 | case 3: |
| 2133 | if (gss->N >= nsi->timeout[NS_TOUT_TSNS_CONFIG_RETRIES]) { |
| 2134 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: SGSN Config retries failed. Giving up.\n", nse->nsei); |
| 2135 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, nsi->timeout[NS_TOUT_TSNS_PROV], 3); |
| 2136 | } else { |
| 2137 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG_ACK, nsi->timeout[NS_TOUT_TSNS_PROV], 3); |
| 2138 | } |
| 2139 | break; |
| 2140 | case 4: |
| 2141 | LOGPFSML(fi, LOGL_ERROR, "NSE %d: Config succeeded but no NS-VC came online.\n", nse->nsei); |
| 2142 | break; |
| 2143 | } |
| 2144 | return 0; |
| 2145 | } |
| 2146 | |
| 2147 | |
| 2148 | /* allstate-action for SGSN role */ |
| 2149 | static void ns2_sns_st_all_action_sgsn(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 2150 | { |
| 2151 | struct ns2_sns_state *gss = (struct ns2_sns_state *) fi->priv; |
| 2152 | struct tlv_parsed *tp = NULL; |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2153 | size_t num_local_eps, num_remote_eps; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2154 | uint8_t flag; |
Harald Welte | a2c5af5 | 2021-03-04 17:59:35 +0100 | [diff] [blame] | 2155 | uint8_t cause; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2156 | |
| 2157 | OSMO_ASSERT(gss->role == GPRS_SNS_ROLE_SGSN); |
| 2158 | |
| 2159 | switch (event) { |
| 2160 | case GPRS_SNS_EV_RX_SIZE: |
| 2161 | tp = (struct tlv_parsed *) data; |
Harald Welte | a2c5af5 | 2021-03-04 17:59:35 +0100 | [diff] [blame] | 2162 | /* check for mandatory / conditional IEs */ |
| 2163 | if (!TLVP_PRES_LEN(tp, NS_IE_RESET_FLAG, 1) || |
| 2164 | !TLVP_PRES_LEN(tp, NS_IE_MAX_NR_NSVC, 2)) { |
| 2165 | cause = NS_CAUSE_MISSING_ESSENT_IE; |
| 2166 | ns2_tx_sns_size_ack(gss->sns_nsvc, &cause); |
| 2167 | break; |
| 2168 | } |
| 2169 | if (!TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2) && |
| 2170 | !TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) { |
| 2171 | cause = NS_CAUSE_MISSING_ESSENT_IE; |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2172 | ns2_tx_sns_size_ack(gss->sns_nsvc, &cause); |
| 2173 | break; |
| 2174 | } |
Harald Welte | 01fa6a3 | 2021-03-04 19:49:38 +0100 | [diff] [blame] | 2175 | if (TLVP_PRES_LEN(tp, NS_IE_IPv4_EP_NR, 2)) |
| 2176 | gss->num_max_ip4_remote = tlvp_val16be(tp, NS_IE_IPv4_EP_NR); |
| 2177 | if (TLVP_PRES_LEN(tp, NS_IE_IPv6_EP_NR, 2)) |
| 2178 | gss->num_max_ip6_remote = tlvp_val16be(tp, NS_IE_IPv6_EP_NR); |
| 2179 | /* decide if we go for IPv4 or IPv6 */ |
| 2180 | if (gss->num_max_ip6_remote && ns2_sns_count_num_local_ep(fi, IPv6)) { |
| 2181 | gss->ip = IPv6; |
| 2182 | num_local_eps = gss->num_ip6_local; |
| 2183 | num_remote_eps = gss->num_max_ip6_remote; |
| 2184 | } else if (gss->num_max_ip4_remote && ns2_sns_count_num_local_ep(fi, IPv4)) { |
| 2185 | gss->ip = IPv4; |
| 2186 | num_local_eps = gss->num_ip4_local; |
| 2187 | num_remote_eps = gss->num_max_ip4_remote; |
| 2188 | } else { |
| 2189 | if (gss->num_ip4_local && !gss->num_max_ip4_remote) |
| 2190 | cause = NS_CAUSE_INVAL_NR_IPv4_EP; |
| 2191 | else |
| 2192 | cause = NS_CAUSE_INVAL_NR_IPv6_EP; |
| 2193 | ns2_tx_sns_size_ack(gss->sns_nsvc, &cause); |
| 2194 | break; |
| 2195 | } |
| 2196 | ns2_sns_compute_local_ep_from_binds(fi); |
| 2197 | /* ensure number of NS-VCs is sufficient for full mesh */ |
| 2198 | gss->num_max_nsvcs = tlvp_val16be(tp, NS_IE_MAX_NR_NSVC); |
| 2199 | if (gss->num_max_nsvcs < num_remote_eps * num_local_eps) { |
| 2200 | LOGPFSML(fi, LOGL_ERROR, "%zu local and %zu remote EPs, requires %zu NS-VC, " |
| 2201 | "but BSS supports only %zu maximum NS-VCs\n", num_local_eps, |
| 2202 | num_remote_eps, num_local_eps * num_remote_eps, gss->num_max_nsvcs); |
| 2203 | cause = NS_CAUSE_INVAL_NR_NS_VC; |
| 2204 | ns2_tx_sns_size_ack(gss->sns_nsvc, &cause); |
| 2205 | break; |
| 2206 | } |
| 2207 | /* perform state reset, if requested */ |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2208 | flag = *TLVP_VAL(tp, NS_IE_RESET_FLAG); |
| 2209 | if (flag & 1) { |
| 2210 | struct gprs_ns2_vc *nsvc, *nsvc2; |
| 2211 | /* clear all state */ |
Harald Welte | 46eb764 | 2021-03-04 17:49:59 +0100 | [diff] [blame] | 2212 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_UNCONFIGURED, 0, 0); |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2213 | gss->N = 0; |
Harald Welte | 46eb764 | 2021-03-04 17:49:59 +0100 | [diff] [blame] | 2214 | ns2_clear_ipv46_entries_local(gss); |
| 2215 | ns2_clear_ipv46_entries_remote(gss); |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2216 | llist_for_each_entry_safe(nsvc, nsvc2, &gss->nse->nsvc, list) { |
| 2217 | if (nsvc == gss->sns_nsvc) { |
| 2218 | /* keep the NSVC we need for SNS, but unconfigure it */ |
| 2219 | nsvc->sig_weight = 0; |
| 2220 | nsvc->data_weight = 0; |
| 2221 | ns2_vc_force_unconfigured(nsvc); |
| 2222 | } else { |
| 2223 | /* free all other NS-VCs */ |
| 2224 | gprs_ns2_free_nsvc(nsvc); |
| 2225 | } |
| 2226 | } |
| 2227 | } |
| 2228 | /* send SIZE_ACK */ |
| 2229 | ns2_tx_sns_size_ack(gss->sns_nsvc, NULL); |
| 2230 | /* only wait for SNS-CONFIG in case of Reset flag */ |
| 2231 | if (flag & 1) |
| 2232 | osmo_fsm_inst_state_chg(fi, GPRS_SNS_ST_SGSN_WAIT_CONFIG, 0, 0); |
| 2233 | break; |
| 2234 | default: |
| 2235 | ns2_sns_st_all_action(fi, event, data); |
| 2236 | break; |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | static struct osmo_fsm gprs_ns2_sns_sgsn_fsm = { |
| 2241 | .name = "GPRS-NS2-SNS-SGSN", |
| 2242 | .states = ns2_sns_sgsn_states, |
| 2243 | .num_states = ARRAY_SIZE(ns2_sns_sgsn_states), |
| 2244 | .allstate_event_mask = S(GPRS_SNS_EV_RX_SIZE) | |
| 2245 | S(GPRS_SNS_EV_REQ_NO_NSVC) | |
| 2246 | S(GPRS_SNS_EV_REQ_ADD_BIND) | |
| 2247 | S(GPRS_SNS_EV_REQ_DELETE_BIND), |
| 2248 | .allstate_action = ns2_sns_st_all_action_sgsn, |
| 2249 | .cleanup = NULL, |
| 2250 | .timer_cb = ns2_sns_fsm_sgsn_timer_cb, |
| 2251 | .event_names = gprs_sns_event_names, |
| 2252 | .pre_term = NULL, |
| 2253 | .log_subsys = DLNS, |
| 2254 | }; |
| 2255 | |
| 2256 | /*! Allocate an IP-SNS FSM for the SGSN side. |
| 2257 | * \param[in] nse NS Entity in which the FSM runs |
| 2258 | * \param[in] id string identifier |
| 2259 | * \returns FSM instance on success; NULL on error */ |
| 2260 | struct osmo_fsm_inst *ns2_sns_sgsn_fsm_alloc(struct gprs_ns2_nse *nse, const char *id) |
| 2261 | { |
| 2262 | struct osmo_fsm_inst *fi; |
| 2263 | struct ns2_sns_state *gss; |
| 2264 | |
| 2265 | fi = osmo_fsm_inst_alloc(&gprs_ns2_sns_sgsn_fsm, nse, NULL, LOGL_DEBUG, id); |
| 2266 | if (!fi) |
| 2267 | return fi; |
| 2268 | |
| 2269 | gss = talloc_zero(fi, struct ns2_sns_state); |
| 2270 | if (!gss) |
| 2271 | goto err; |
| 2272 | |
| 2273 | fi->priv = gss; |
| 2274 | gss->nse = nse; |
| 2275 | gss->role = GPRS_SNS_ROLE_SGSN; |
| 2276 | INIT_LLIST_HEAD(&gss->sns_endpoints); |
| 2277 | INIT_LLIST_HEAD(&gss->binds); |
| 2278 | |
| 2279 | return fi; |
| 2280 | err: |
| 2281 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_ERROR, NULL); |
| 2282 | return NULL; |
| 2283 | } |
| 2284 | |
| 2285 | |
| 2286 | |
| 2287 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2288 | /* initialize osmo_ctx on main tread */ |
| 2289 | static __attribute__((constructor)) void on_dso_load_ctx(void) |
| 2290 | { |
| 2291 | OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_bss_fsm) == 0); |
Harald Welte | 4f12746 | 2021-03-02 20:49:10 +0100 | [diff] [blame] | 2292 | OSMO_ASSERT(osmo_fsm_register(&gprs_ns2_sns_sgsn_fsm) == 0); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 2293 | } |