Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1 | /*! \file gprs_ns2_udp.c |
| 2 | * NS-over-UDP implementation. |
| 3 | * GPRS Networks Service (NS) messages on the Gb interface. |
| 4 | * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) |
| 5 | * as well as its successor 3GPP TS 48.016 */ |
| 6 | |
| 7 | /* (C) 2020 sysmocom - s.f.m.c. GmbH |
| 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 | #include <errno.h> |
| 30 | |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 31 | #include <osmocom/core/osmo_io.h> |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 32 | #include <osmocom/core/select.h> |
| 33 | #include <osmocom/core/sockaddr_str.h> |
| 34 | #include <osmocom/core/socket.h> |
| 35 | #include <osmocom/gprs/gprs_ns2.h> |
| 36 | |
| 37 | #include "common_vty.h" |
| 38 | #include "gprs_ns2_internal.h" |
| 39 | |
| 40 | |
| 41 | static void free_bind(struct gprs_ns2_vc_bind *bind); |
| 42 | |
| 43 | |
| 44 | struct gprs_ns2_vc_driver vc_driver_ip = { |
| 45 | .name = "GB UDP IPv4/IPv6", |
| 46 | .free_bind = free_bind, |
| 47 | }; |
| 48 | |
| 49 | struct priv_bind { |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 50 | struct osmo_io_fd *iofd; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 51 | struct osmo_sockaddr addr; |
| 52 | int dscp; |
Harald Welte | d99e4ee | 2021-04-28 19:57:12 +0200 | [diff] [blame] | 53 | uint8_t priority; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | struct priv_vc { |
| 57 | struct osmo_sockaddr remote; |
| 58 | }; |
| 59 | |
| 60 | /*! clean up all private driver state. Should be only called by gprs_ns2_free_bind() */ |
| 61 | static void free_bind(struct gprs_ns2_vc_bind *bind) |
| 62 | { |
| 63 | struct priv_bind *priv; |
| 64 | |
| 65 | if (!bind) |
| 66 | return; |
| 67 | |
Alexander Couzens | 55bc869 | 2021-01-18 18:39:57 +0100 | [diff] [blame] | 68 | OSMO_ASSERT(gprs_ns2_is_ip_bind(bind)); |
| 69 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 70 | priv = bind->priv; |
| 71 | |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 72 | osmo_iofd_free(priv->iofd); |
| 73 | priv->iofd = NULL; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 74 | talloc_free(priv); |
| 75 | } |
| 76 | |
| 77 | static void free_vc(struct gprs_ns2_vc *nsvc) |
| 78 | { |
Alexander Couzens | ea37724 | 2021-01-17 16:51:55 +0100 | [diff] [blame] | 79 | if (!nsvc) |
| 80 | return; |
| 81 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 82 | if (!nsvc->priv) |
| 83 | return; |
| 84 | |
Alexander Couzens | 55bc869 | 2021-01-18 18:39:57 +0100 | [diff] [blame] | 85 | OSMO_ASSERT(gprs_ns2_is_ip_bind(nsvc->bind)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 86 | talloc_free(nsvc->priv); |
| 87 | nsvc->priv = NULL; |
| 88 | } |
| 89 | |
Alexander Couzens | 22f3471 | 2020-10-02 02:34:39 +0200 | [diff] [blame] | 90 | static void dump_vty(const struct gprs_ns2_vc_bind *bind, |
Alexander Couzens | 75b6188 | 2021-03-21 16:18:17 +0100 | [diff] [blame] | 91 | struct vty *vty, bool stats) |
Alexander Couzens | 22f3471 | 2020-10-02 02:34:39 +0200 | [diff] [blame] | 92 | { |
| 93 | struct priv_bind *priv; |
| 94 | struct gprs_ns2_vc *nsvc; |
| 95 | struct osmo_sockaddr_str sockstr = {}; |
| 96 | unsigned long nsvcs = 0; |
| 97 | |
| 98 | if (!bind) |
| 99 | return; |
| 100 | |
| 101 | priv = bind->priv; |
| 102 | if (osmo_sockaddr_str_from_sockaddr(&sockstr, &priv->addr.u.sas)) |
| 103 | strcpy(sockstr.ip, "invalid"); |
| 104 | |
| 105 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
| 106 | nsvcs++; |
| 107 | } |
| 108 | |
Harald Welte | d99e4ee | 2021-04-28 19:57:12 +0200 | [diff] [blame] | 109 | vty_out(vty, "UDP bind: %s:%d DSCP: %d Priority: %u%s", sockstr.ip, sockstr.port, |
| 110 | priv->dscp, priv->priority, VTY_NEWLINE); |
Alexander Couzens | c470476 | 2021-02-08 23:13:12 +0100 | [diff] [blame] | 111 | vty_out(vty, " IP-SNS signalling weight: %u data weight: %u%s", |
| 112 | bind->sns_sig_weight, bind->sns_data_weight, VTY_NEWLINE); |
Alexander Couzens | 8bd63b6 | 2021-03-21 17:45:22 +0100 | [diff] [blame] | 113 | vty_out(vty, " %lu NS-VC:%s", nsvcs, VTY_NEWLINE); |
Alexander Couzens | 22f3471 | 2020-10-02 02:34:39 +0200 | [diff] [blame] | 114 | |
| 115 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
Alexander Couzens | 75b6188 | 2021-03-21 16:18:17 +0100 | [diff] [blame] | 116 | ns2_vty_dump_nsvc(vty, nsvc, stats); |
Alexander Couzens | 22f3471 | 2020-10-02 02:34:39 +0200 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 121 | /*! Find a NS-VC by its remote socket address. |
| 122 | * \param[in] bind in which to search |
Pau Espin Pedrol | f6ef9ba | 2023-04-27 17:50:10 +0200 | [diff] [blame] | 123 | * \param[in] rem_addr remote peer socket address to search |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 124 | * \returns NS-VC matching sockaddr; NULL if none found */ |
Vadim Yanitskiy | a07f25e | 2020-10-09 21:47:01 +0700 | [diff] [blame] | 125 | struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_bind(struct gprs_ns2_vc_bind *bind, |
Pau Espin Pedrol | f6ef9ba | 2023-04-27 17:50:10 +0200 | [diff] [blame] | 126 | const struct osmo_sockaddr *rem_addr) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 127 | { |
| 128 | struct gprs_ns2_vc *nsvc; |
| 129 | struct priv_vc *vcpriv; |
| 130 | |
Alexander Couzens | 55bc869 | 2021-01-18 18:39:57 +0100 | [diff] [blame] | 131 | OSMO_ASSERT(gprs_ns2_is_ip_bind(bind)); |
| 132 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 133 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
| 134 | vcpriv = nsvc->priv; |
Pau Espin Pedrol | f6ef9ba | 2023-04-27 17:50:10 +0200 | [diff] [blame] | 135 | if (vcpriv->remote.u.sa.sa_family != rem_addr->u.sa.sa_family) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 136 | continue; |
Pau Espin Pedrol | f6ef9ba | 2023-04-27 17:50:10 +0200 | [diff] [blame] | 137 | if (osmo_sockaddr_cmp(&vcpriv->remote, rem_addr)) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 138 | continue; |
| 139 | |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 140 | return nsvc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 141 | } |
| 142 | |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 143 | return NULL; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static inline int nsip_sendmsg(struct gprs_ns2_vc_bind *bind, |
| 147 | struct msgb *msg, |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 148 | const struct osmo_sockaddr *dest) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 149 | { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 150 | struct priv_bind *priv = bind->priv; |
| 151 | |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 152 | return osmo_iofd_sendto_msgb(priv->iofd, msg, 0, dest); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 155 | /*! send the msg and free it afterwards. |
| 156 | * \param nsvc NS-VC on which the message shall be sent |
| 157 | * \param msg message to be sent |
| 158 | * \return number of bytes transmitted; negative on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 159 | static int nsip_vc_sendmsg(struct gprs_ns2_vc *nsvc, struct msgb *msg) |
| 160 | { |
| 161 | int rc; |
| 162 | struct gprs_ns2_vc_bind *bind = nsvc->bind; |
| 163 | struct priv_vc *priv = nsvc->priv; |
| 164 | |
| 165 | rc = nsip_sendmsg(bind, msg, &priv->remote); |
| 166 | |
| 167 | return rc; |
| 168 | } |
| 169 | |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 170 | static struct priv_vc *ns2_driver_alloc_vc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_vc *nsvc, const struct osmo_sockaddr *remote) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 171 | { |
| 172 | struct priv_vc *priv = talloc_zero(bind, struct priv_vc); |
| 173 | if (!priv) |
| 174 | return NULL; |
| 175 | |
| 176 | nsvc->priv = priv; |
| 177 | priv->remote = *remote; |
| 178 | |
| 179 | return priv; |
| 180 | } |
| 181 | |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 182 | static void handle_nsip_recvfrom(struct osmo_io_fd *iofd, int error, struct msgb *msg, |
| 183 | const struct osmo_sockaddr *saddr) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 184 | { |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 185 | int rc = 0; |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 186 | struct gprs_ns2_vc_bind *bind = osmo_iofd_get_data(iofd); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 187 | struct gprs_ns2_vc *nsvc; |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 188 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 189 | struct msgb *reject; |
| 190 | |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 191 | msg->l2h = msgb_data(msg); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 192 | |
| 193 | /* check if a vc is available */ |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 194 | nsvc = gprs_ns2_nsvc_by_sockaddr_bind(bind, saddr); |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 195 | if (!nsvc) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 196 | /* VC not found */ |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 197 | rc = ns2_create_vc(bind, msg, saddr, "newconnection", &reject, &nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 198 | switch (rc) { |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 199 | case NS2_CS_FOUND: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 200 | break; |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 201 | case NS2_CS_ERROR: |
| 202 | case NS2_CS_SKIPPED: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 203 | rc = 0; |
Alexander Couzens | 1301012 | 2020-09-24 00:54:51 +0200 | [diff] [blame] | 204 | goto out; |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 205 | case NS2_CS_REJECTED: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 206 | /* nsip_sendmsg will free reject */ |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 207 | rc = nsip_sendmsg(bind, reject, saddr); |
Alexander Couzens | 772ca61 | 2020-09-24 16:19:02 +0200 | [diff] [blame] | 208 | goto out; |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 209 | case NS2_CS_CREATED: |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 210 | ns2_driver_alloc_vc(bind, nsvc, saddr); |
Alexander Couzens | 20ed591 | 2021-08-06 18:07:26 +0200 | [diff] [blame] | 211 | /* only start the fsm for non SNS. SNS will take care of its own */ |
| 212 | if (nsvc->nse->dialect != GPRS_NS2_DIALECT_SNS) |
| 213 | ns2_vc_fsm_start(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 214 | break; |
| 215 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 218 | ns2_recv_vc(nsvc, msg); |
| 219 | return; |
Alexander Couzens | cce8828 | 2020-10-26 00:25:50 +0100 | [diff] [blame] | 220 | |
Alexander Couzens | 1301012 | 2020-09-24 00:54:51 +0200 | [diff] [blame] | 221 | out: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 222 | msgb_free(msg); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 225 | static void handle_nsip_sendto(struct osmo_io_fd *iofd, int res, |
| 226 | const struct msgb *msg, |
| 227 | const struct osmo_sockaddr *daddr) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 228 | { |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 229 | struct gprs_ns2_vc_bind *bind = osmo_iofd_get_data(iofd); |
| 230 | struct gprs_ns2_vc *nsvc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 231 | |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 232 | nsvc = gprs_ns2_nsvc_by_sockaddr_bind(bind, daddr); |
| 233 | if (!nsvc) |
| 234 | return; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 235 | |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 236 | if (OSMO_LIKELY(res >= 0)) { |
| 237 | RATE_CTR_INC_NS(nsvc, NS_CTR_PKTS_OUT); |
| 238 | RATE_CTR_ADD_NS(nsvc, NS_CTR_BYTES_OUT, res); |
| 239 | } else { |
| 240 | RATE_CTR_INC_NS(nsvc, NS_CTR_PKTS_OUT_DROP); |
| 241 | RATE_CTR_ADD_NS(nsvc, NS_CTR_BYTES_OUT_DROP, msgb_length(msg)); |
| 242 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 243 | } |
| 244 | |
Alexander Couzens | 4f60845 | 2020-10-11 18:41:24 +0200 | [diff] [blame] | 245 | /*! Find NS bind for a given socket address |
| 246 | * \param[in] nsi NS instance |
| 247 | * \param[in] sockaddr socket address to search for |
| 248 | * \return |
| 249 | */ |
| 250 | struct gprs_ns2_vc_bind *gprs_ns2_ip_bind_by_sockaddr(struct gprs_ns2_inst *nsi, |
| 251 | const struct osmo_sockaddr *sockaddr) |
| 252 | { |
| 253 | struct gprs_ns2_vc_bind *bind; |
Alexander Couzens | 9a4cf27 | 2020-10-11 20:48:04 +0200 | [diff] [blame] | 254 | const struct osmo_sockaddr *local; |
Alexander Couzens | 4f60845 | 2020-10-11 18:41:24 +0200 | [diff] [blame] | 255 | |
| 256 | OSMO_ASSERT(nsi); |
| 257 | OSMO_ASSERT(sockaddr); |
| 258 | |
| 259 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 260 | if (!gprs_ns2_is_ip_bind(bind)) |
| 261 | continue; |
| 262 | |
| 263 | local = gprs_ns2_ip_bind_sockaddr(bind); |
| 264 | if (!osmo_sockaddr_cmp(sockaddr, local)) |
| 265 | return bind; |
| 266 | } |
| 267 | |
| 268 | return NULL; |
| 269 | } |
| 270 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 271 | /*! Bind to an IPv4/IPv6 address |
| 272 | * \param[in] nsi NS Instance in which to create the NSVC |
| 273 | * \param[in] local the local address to bind to |
| 274 | * \param[in] dscp the DSCP/TOS bits used for transmitted data |
Alexander Couzens | c80a874 | 2021-02-03 11:27:52 +0100 | [diff] [blame] | 275 | * \param[out] result pointer to the created bind or if a bind with the name exists return the bind. |
| 276 | * \return 0 on success; negative on error. -EALREADY returned in case a bind with the name exists */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 277 | int gprs_ns2_ip_bind(struct gprs_ns2_inst *nsi, |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 278 | const char *name, |
Vadim Yanitskiy | a07f25e | 2020-10-09 21:47:01 +0700 | [diff] [blame] | 279 | const struct osmo_sockaddr *local, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 280 | int dscp, |
| 281 | struct gprs_ns2_vc_bind **result) |
| 282 | { |
Alexander Couzens | 6090b1d | 2020-10-11 18:41:37 +0200 | [diff] [blame] | 283 | struct gprs_ns2_vc_bind *bind; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 284 | struct priv_bind *priv; |
| 285 | int rc; |
| 286 | |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 287 | struct osmo_io_ops ioops = { |
| 288 | .sendto_cb = &handle_nsip_sendto, |
| 289 | .recvfrom_cb = &handle_nsip_recvfrom, |
| 290 | }; |
| 291 | |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 292 | if (local->u.sa.sa_family != AF_INET && local->u.sa.sa_family != AF_INET6) |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 293 | return -EINVAL; |
| 294 | |
Harald Welte | c96d716 | 2021-04-27 21:56:25 +0200 | [diff] [blame] | 295 | if (dscp < 0 || dscp > 63) |
| 296 | return -EINVAL; |
| 297 | |
Alexander Couzens | 6090b1d | 2020-10-11 18:41:37 +0200 | [diff] [blame] | 298 | bind = gprs_ns2_ip_bind_by_sockaddr(nsi, local); |
| 299 | if (bind) { |
Alexander Couzens | c80a874 | 2021-02-03 11:27:52 +0100 | [diff] [blame] | 300 | if (result) |
| 301 | *result = bind; |
Alexander Couzens | 6090b1d | 2020-10-11 18:41:37 +0200 | [diff] [blame] | 302 | return -EBUSY; |
| 303 | } |
| 304 | |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 305 | rc = ns2_bind_alloc(nsi, name, &bind); |
| 306 | if (rc < 0) |
| 307 | return rc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 308 | |
| 309 | bind->driver = &vc_driver_ip; |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 310 | bind->ll = GPRS_NS2_LL_UDP; |
Alexander Couzens | 1c8785d | 2020-12-17 06:58:53 +0100 | [diff] [blame] | 311 | /* expect 100 mbit at least. |
| 312 | * TODO: ask the network layer about the speed. But would require |
| 313 | * notification on change. */ |
| 314 | bind->transfer_capability = 100; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 315 | bind->send_vc = nsip_vc_sendmsg; |
| 316 | bind->free_vc = free_vc; |
Alexander Couzens | 22f3471 | 2020-10-02 02:34:39 +0200 | [diff] [blame] | 317 | bind->dump_vty = dump_vty; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 318 | |
| 319 | priv = bind->priv = talloc_zero(bind, struct priv_bind); |
| 320 | if (!priv) { |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 321 | gprs_ns2_free_bind(bind); |
Harald Welte | bdfb8b9 | 2021-01-31 11:44:57 +0100 | [diff] [blame] | 322 | return -ENOMEM; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 323 | } |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 324 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 325 | priv->addr = *local; |
Harald Welte | 485b3f7 | 2021-04-28 19:45:34 +0200 | [diff] [blame] | 326 | priv->dscp = dscp; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 327 | |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 328 | rc = osmo_sock_init_osa(SOCK_DGRAM, IPPROTO_UDP, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 329 | local, NULL, |
Harald Welte | 485b3f7 | 2021-04-28 19:45:34 +0200 | [diff] [blame] | 330 | OSMO_SOCK_F_BIND | OSMO_SOCK_F_DSCP(priv->dscp)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 331 | if (rc < 0) { |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 332 | gprs_ns2_free_bind(bind); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 333 | return rc; |
| 334 | } |
| 335 | |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 336 | priv->iofd = osmo_iofd_setup(bind, rc, "NS bind", OSMO_IO_FD_MODE_RECVFROM_SENDTO, &ioops, bind); |
| 337 | osmo_iofd_register(priv->iofd, rc); |
| 338 | osmo_iofd_set_alloc_info(priv->iofd, 4096, 128); |
| 339 | |
| 340 | osmo_iofd_read_enable(priv->iofd); |
| 341 | osmo_iofd_write_enable(priv->iofd); |
| 342 | |
Alexander Couzens | 4f1128f | 2021-01-20 17:42:48 +0100 | [diff] [blame] | 343 | /* IPv4: max fragmented payload can be (13 bit) * 8 byte => 65535. |
| 344 | * IPv6: max payload can be 65535 (RFC 2460). |
| 345 | * UDP header = 8 byte */ |
| 346 | bind->mtu = 65535 - 8; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 347 | if (result) |
| 348 | *result = bind; |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 353 | /*! Create new NS-VC to a given remote address |
| 354 | * \param[in] bind the bind we want to connect |
| 355 | * \param[in] nse NS entity to be used for the new NS-VC |
| 356 | * \param[in] remote remote address to connect to |
| 357 | * \return pointer to newly-allocated and connected NS-VC; NULL on error */ |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 358 | struct gprs_ns2_vc *ns2_ip_bind_connect(struct gprs_ns2_vc_bind *bind, |
| 359 | struct gprs_ns2_nse *nse, |
| 360 | const struct osmo_sockaddr *remote) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 361 | { |
| 362 | struct gprs_ns2_vc *nsvc; |
Alexander Couzens | d794806 | 2021-06-03 20:35:47 +0200 | [diff] [blame] | 363 | const struct osmo_sockaddr *local; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 364 | struct priv_vc *priv; |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 365 | enum gprs_ns2_vc_mode vc_mode; |
Alexander Couzens | d794806 | 2021-06-03 20:35:47 +0200 | [diff] [blame] | 366 | char idbuf[256], tmp[INET6_ADDRSTRLEN + 8]; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 367 | |
Alexander Couzens | 55bc869 | 2021-01-18 18:39:57 +0100 | [diff] [blame] | 368 | OSMO_ASSERT(gprs_ns2_is_ip_bind(bind)); |
| 369 | |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 370 | vc_mode = ns2_dialect_to_vc_mode(nse->dialect); |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 371 | if ((int) vc_mode == -1) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 372 | LOGNSE(nse, LOGL_ERROR, "Can not derive vc mode from dialect %d. Maybe libosmocore is too old.\n", |
| 373 | nse->dialect); |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 374 | return NULL; |
| 375 | } |
| 376 | |
Alexander Couzens | 7bb39e3 | 2021-02-16 23:06:53 +0100 | [diff] [blame] | 377 | /* duplicate */ |
| 378 | if (gprs_ns2_nsvc_by_sockaddr_bind(bind, remote)) |
| 379 | return NULL; |
| 380 | |
Alexander Couzens | d794806 | 2021-06-03 20:35:47 +0200 | [diff] [blame] | 381 | local = gprs_ns2_ip_bind_sockaddr(bind); |
| 382 | osmo_sockaddr_to_str_buf(tmp, sizeof(tmp), local); |
| 383 | snprintf(idbuf, sizeof(idbuf), "NSE%05u-NSVC-%s-%s-%s", nse->nsei, gprs_ns2_lltype_str(nse->ll), |
| 384 | tmp, osmo_sockaddr_to_str(remote)); |
| 385 | osmo_identifier_sanitize_buf(idbuf, NULL, '_'); |
| 386 | |
Harald Welte | 603f404 | 2020-11-29 17:39:19 +0100 | [diff] [blame] | 387 | nsvc = ns2_vc_alloc(bind, nse, true, vc_mode, idbuf); |
Alexander Couzens | c06aa71 | 2020-11-18 23:56:45 +0100 | [diff] [blame] | 388 | if (!nsvc) |
| 389 | return NULL; |
| 390 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 391 | nsvc->priv = talloc_zero(bind, struct priv_vc); |
| 392 | if (!nsvc->priv) { |
| 393 | gprs_ns2_free_nsvc(nsvc); |
| 394 | return NULL; |
| 395 | } |
| 396 | |
| 397 | priv = nsvc->priv; |
| 398 | priv->remote = *remote; |
| 399 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 400 | return nsvc; |
| 401 | } |
| 402 | |
Alexander Couzens | 979f5f5 | 2020-10-11 21:01:48 +0200 | [diff] [blame] | 403 | /*! Return the socket address of the local peer of a NS-VC. |
| 404 | * \param[in] nsvc NS-VC whose local peer we want to know |
| 405 | * \return address of the local peer; NULL in case of error */ |
| 406 | const struct osmo_sockaddr *gprs_ns2_ip_vc_local(const struct gprs_ns2_vc *nsvc) |
| 407 | { |
| 408 | struct priv_bind *priv; |
| 409 | |
Alexander Couzens | 979f5f5 | 2020-10-11 21:01:48 +0200 | [diff] [blame] | 410 | if (nsvc->bind->driver != &vc_driver_ip) |
| 411 | return NULL; |
| 412 | |
| 413 | priv = nsvc->bind->priv; |
| 414 | return &priv->addr; |
| 415 | } |
| 416 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 417 | /*! Return the socket address of the remote peer of a NS-VC. |
| 418 | * \param[in] nsvc NS-VC whose remote peer we want to know |
| 419 | * \return address of the remote peer; NULL in case of error */ |
Alexander Couzens | d33512b | 2020-10-11 21:42:11 +0200 | [diff] [blame] | 420 | const struct osmo_sockaddr *gprs_ns2_ip_vc_remote(const struct gprs_ns2_vc *nsvc) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 421 | { |
| 422 | struct priv_vc *priv; |
| 423 | |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 424 | if (nsvc->bind->driver != &vc_driver_ip) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 425 | return NULL; |
| 426 | |
| 427 | priv = nsvc->priv; |
| 428 | return &priv->remote; |
| 429 | } |
| 430 | |
Alexander Couzens | d420ea9 | 2020-10-12 01:11:05 +0200 | [diff] [blame] | 431 | /*! Compare the NS-VC with the given parameter |
| 432 | * \param[in] nsvc NS-VC to compare with |
| 433 | * \param[in] local The local address |
| 434 | * \param[in] remote The remote address |
| 435 | * \param[in] nsvci NS-VCI will only be used if the NS-VC in BLOCKRESET mode otherwise NS-VCI isn't applicable. |
| 436 | * \return true if the NS-VC has the same properties as given |
| 437 | */ |
| 438 | bool gprs_ns2_ip_vc_equal(const struct gprs_ns2_vc *nsvc, |
| 439 | const struct osmo_sockaddr *local, |
| 440 | const struct osmo_sockaddr *remote, |
| 441 | uint16_t nsvci) |
| 442 | { |
| 443 | struct priv_vc *vpriv; |
| 444 | struct priv_bind *bpriv; |
| 445 | |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 446 | if (nsvc->bind->driver != &vc_driver_ip) |
Alexander Couzens | d420ea9 | 2020-10-12 01:11:05 +0200 | [diff] [blame] | 447 | return false; |
| 448 | |
| 449 | vpriv = nsvc->priv; |
| 450 | bpriv = nsvc->bind->priv; |
| 451 | |
| 452 | if (osmo_sockaddr_cmp(local, &bpriv->addr)) |
| 453 | return false; |
| 454 | |
| 455 | if (osmo_sockaddr_cmp(remote, &vpriv->remote)) |
| 456 | return false; |
| 457 | |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 458 | if (nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET) |
Alexander Couzens | d420ea9 | 2020-10-12 01:11:05 +0200 | [diff] [blame] | 459 | if (nsvc->nsvci != nsvci) |
| 460 | return false; |
| 461 | |
| 462 | return true; |
| 463 | } |
| 464 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 465 | /*! Return the locally bound socket address of the bind. |
| 466 | * \param[in] bind The bind whose local address we want to know |
| 467 | * \return address of the local bind */ |
Alexander Couzens | 9a4cf27 | 2020-10-11 20:48:04 +0200 | [diff] [blame] | 468 | const struct osmo_sockaddr *gprs_ns2_ip_bind_sockaddr(struct gprs_ns2_vc_bind *bind) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 469 | { |
| 470 | struct priv_bind *priv; |
Alexander Couzens | 55bc869 | 2021-01-18 18:39:57 +0100 | [diff] [blame] | 471 | OSMO_ASSERT(gprs_ns2_is_ip_bind(bind)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 472 | |
| 473 | priv = bind->priv; |
| 474 | return &priv->addr; |
| 475 | } |
| 476 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 477 | /*! Is the given bind an IP bind? */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 478 | int gprs_ns2_is_ip_bind(struct gprs_ns2_vc_bind *bind) |
| 479 | { |
| 480 | return (bind->driver == &vc_driver_ip); |
| 481 | } |
| 482 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 483 | /*! Set the DSCP (TOS) bit value of the given bind. */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 484 | int gprs_ns2_ip_bind_set_dscp(struct gprs_ns2_vc_bind *bind, int dscp) |
| 485 | { |
| 486 | struct priv_bind *priv; |
| 487 | int rc = 0; |
| 488 | |
Harald Welte | 628f534 | 2021-04-28 19:41:07 +0200 | [diff] [blame] | 489 | if (dscp < 0 || dscp > 63) |
| 490 | return -EINVAL; |
| 491 | |
Alexander Couzens | 55bc869 | 2021-01-18 18:39:57 +0100 | [diff] [blame] | 492 | OSMO_ASSERT(gprs_ns2_is_ip_bind(bind)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 493 | priv = bind->priv; |
| 494 | |
| 495 | if (dscp != priv->dscp) { |
| 496 | priv->dscp = dscp; |
| 497 | |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 498 | rc = osmo_sock_set_dscp(osmo_iofd_get_fd(priv->iofd), dscp); |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 499 | if (rc < 0) { |
Harald Welte | 628f534 | 2021-04-28 19:41:07 +0200 | [diff] [blame] | 500 | LOGBIND(bind, LOGL_ERROR, "Failed to set the DSCP to %u with ret(%d) errno(%d)\n", |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 501 | dscp, rc, errno); |
| 502 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | return rc; |
| 506 | } |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 507 | |
Harald Welte | d99e4ee | 2021-04-28 19:57:12 +0200 | [diff] [blame] | 508 | /*! Set the socket priority of the given bind. */ |
| 509 | int gprs_ns2_ip_bind_set_priority(struct gprs_ns2_vc_bind *bind, uint8_t priority) |
| 510 | { |
| 511 | struct priv_bind *priv; |
| 512 | int rc = 0; |
| 513 | |
| 514 | OSMO_ASSERT(gprs_ns2_is_ip_bind(bind)); |
| 515 | priv = bind->priv; |
| 516 | |
| 517 | if (priority != priv->priority) { |
| 518 | priv->priority = priority; |
| 519 | |
Daniel Willmann | fa3a9ce | 2022-11-21 19:27:18 +0100 | [diff] [blame] | 520 | rc = osmo_sock_set_priority(osmo_iofd_get_fd(priv->iofd), priority); |
Harald Welte | d99e4ee | 2021-04-28 19:57:12 +0200 | [diff] [blame] | 521 | if (rc < 0) { |
| 522 | LOGBIND(bind, LOGL_ERROR, "Failed to set the priority to %u with ret(%d) errno(%d)\n", |
| 523 | priority, rc, errno); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | return rc; |
| 528 | } |
| 529 | |
| 530 | |
Alexander Couzens | e769f52 | 2020-12-07 07:37:07 +0100 | [diff] [blame] | 531 | /*! Count UDP binds compatible with remote */ |
| 532 | int ns2_ip_count_bind(struct gprs_ns2_inst *nsi, struct osmo_sockaddr *remote) |
| 533 | { |
| 534 | struct gprs_ns2_vc_bind *bind; |
| 535 | const struct osmo_sockaddr *sa; |
| 536 | int count = 0; |
| 537 | |
| 538 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 539 | if (!gprs_ns2_is_ip_bind(bind)) |
| 540 | continue; |
| 541 | |
| 542 | sa = gprs_ns2_ip_bind_sockaddr(bind); |
| 543 | if (!sa) |
| 544 | continue; |
| 545 | |
| 546 | if (sa->u.sa.sa_family == remote->u.sa.sa_family) |
| 547 | count++; |
| 548 | } |
| 549 | |
| 550 | return count; |
| 551 | } |
| 552 | |
| 553 | /* return the matching bind by index */ |
| 554 | struct gprs_ns2_vc_bind *ns2_ip_get_bind_by_index(struct gprs_ns2_inst *nsi, |
| 555 | struct osmo_sockaddr *remote, |
| 556 | int index) |
| 557 | { |
| 558 | struct gprs_ns2_vc_bind *bind; |
| 559 | const struct osmo_sockaddr *sa; |
| 560 | int i = 0; |
| 561 | |
| 562 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 563 | if (!gprs_ns2_is_ip_bind(bind)) |
| 564 | continue; |
| 565 | |
| 566 | sa = gprs_ns2_ip_bind_sockaddr(bind); |
| 567 | if (!sa) |
| 568 | continue; |
| 569 | |
| 570 | if (sa->u.sa.sa_family == remote->u.sa.sa_family) { |
| 571 | if (index == i) |
| 572 | return bind; |
| 573 | i++; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | return NULL; |
| 578 | } |
Alexander Couzens | c470476 | 2021-02-08 23:13:12 +0100 | [diff] [blame] | 579 | |
| 580 | /*! set the signalling and data weight for this bind |
| 581 | * \param[in] bind |
| 582 | * \param[in] signalling the signalling weight |
| 583 | * \param[in] data the data weight |
| 584 | */ |
| 585 | void gprs_ns2_ip_bind_set_sns_weight(struct gprs_ns2_vc_bind *bind, uint8_t signalling, uint8_t data) |
| 586 | { |
| 587 | OSMO_ASSERT(gprs_ns2_is_ip_bind(bind)); |
| 588 | bind->sns_sig_weight = signalling; |
| 589 | bind->sns_data_weight = data; |
| 590 | ns2_sns_update_weights(bind); |
| 591 | } |