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