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