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; |
| 52 | }; |
| 53 | |
| 54 | struct priv_vc { |
| 55 | struct osmo_sockaddr remote; |
| 56 | }; |
| 57 | |
| 58 | /*! clean up all private driver state. Should be only called by gprs_ns2_free_bind() */ |
| 59 | static void free_bind(struct gprs_ns2_vc_bind *bind) |
| 60 | { |
| 61 | struct priv_bind *priv; |
| 62 | |
| 63 | if (!bind) |
| 64 | return; |
| 65 | |
| 66 | priv = bind->priv; |
| 67 | |
| 68 | osmo_fd_close(&priv->fd); |
| 69 | talloc_free(priv); |
| 70 | } |
| 71 | |
| 72 | static void free_vc(struct gprs_ns2_vc *nsvc) |
| 73 | { |
| 74 | if (!nsvc->priv) |
| 75 | return; |
| 76 | |
| 77 | talloc_free(nsvc->priv); |
| 78 | nsvc->priv = NULL; |
| 79 | } |
| 80 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 81 | /*! Find a NS-VC by its remote socket address. |
| 82 | * \param[in] bind in which to search |
| 83 | * \param[in] saddr remote peer socket adddress to search |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 84 | * \returns NS-VC matching sockaddr; NULL if none found */ |
| 85 | struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_bind(struct gprs_ns2_vc_bind *bind, struct osmo_sockaddr *saddr) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 86 | { |
| 87 | struct gprs_ns2_vc *nsvc; |
| 88 | struct priv_vc *vcpriv; |
| 89 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 90 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
| 91 | vcpriv = nsvc->priv; |
| 92 | if (vcpriv->remote.u.sa.sa_family != saddr->u.sa.sa_family) |
| 93 | continue; |
| 94 | if (osmo_sockaddr_cmp(&vcpriv->remote, saddr)) |
| 95 | continue; |
| 96 | |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 97 | return nsvc; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 100 | return NULL; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static inline int nsip_sendmsg(struct gprs_ns2_vc_bind *bind, |
| 104 | struct msgb *msg, |
| 105 | struct osmo_sockaddr *dest) |
| 106 | { |
| 107 | int rc; |
| 108 | struct priv_bind *priv = bind->priv; |
| 109 | |
| 110 | rc = sendto(priv->fd.fd, msg->data, msg->len, 0, |
| 111 | &dest->u.sa, sizeof(*dest)); |
| 112 | |
| 113 | msgb_free(msg); |
| 114 | |
| 115 | return rc; |
| 116 | } |
| 117 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 118 | /*! send the msg and free it afterwards. |
| 119 | * \param nsvc NS-VC on which the message shall be sent |
| 120 | * \param msg message to be sent |
| 121 | * \return number of bytes transmitted; negative on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 122 | static int nsip_vc_sendmsg(struct gprs_ns2_vc *nsvc, struct msgb *msg) |
| 123 | { |
| 124 | int rc; |
| 125 | struct gprs_ns2_vc_bind *bind = nsvc->bind; |
| 126 | struct priv_vc *priv = nsvc->priv; |
| 127 | |
| 128 | rc = nsip_sendmsg(bind, msg, &priv->remote); |
| 129 | |
| 130 | return rc; |
| 131 | } |
| 132 | |
| 133 | /* Read a single NS-over-IP message */ |
| 134 | static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error, |
| 135 | struct osmo_sockaddr *saddr) |
| 136 | { |
| 137 | struct msgb *msg = gprs_ns2_msgb_alloc(); |
| 138 | int ret = 0; |
| 139 | socklen_t saddr_len = sizeof(*saddr); |
| 140 | |
| 141 | if (!msg) { |
| 142 | *error = -ENOMEM; |
| 143 | return NULL; |
| 144 | } |
| 145 | |
| 146 | ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0, |
| 147 | &saddr->u.sa, &saddr_len); |
| 148 | if (ret < 0) { |
| 149 | LOGP(DLNS, LOGL_ERROR, "recv error %s during NSIP recvfrom %s\n", |
| 150 | strerror(errno), osmo_sock_get_name2(bfd->fd)); |
| 151 | msgb_free(msg); |
| 152 | *error = ret; |
| 153 | return NULL; |
| 154 | } else if (ret == 0) { |
| 155 | msgb_free(msg); |
| 156 | *error = ret; |
| 157 | return NULL; |
| 158 | } |
| 159 | |
| 160 | msg->l2h = msg->data; |
| 161 | msgb_put(msg, ret); |
| 162 | |
| 163 | return msg; |
| 164 | } |
| 165 | |
| 166 | static struct priv_vc *ns2_driver_alloc_vc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_vc *nsvc, struct osmo_sockaddr *remote) |
| 167 | { |
| 168 | struct priv_vc *priv = talloc_zero(bind, struct priv_vc); |
| 169 | if (!priv) |
| 170 | return NULL; |
| 171 | |
| 172 | nsvc->priv = priv; |
| 173 | priv->remote = *remote; |
| 174 | |
| 175 | return priv; |
| 176 | } |
| 177 | |
| 178 | static int handle_nsip_read(struct osmo_fd *bfd) |
| 179 | { |
| 180 | int rc; |
| 181 | int error = 0; |
| 182 | struct gprs_ns2_vc_bind *bind = bfd->data; |
| 183 | struct osmo_sockaddr saddr; |
| 184 | struct gprs_ns2_vc *nsvc; |
| 185 | struct msgb *msg = read_nsip_msg(bfd, &error, &saddr); |
| 186 | struct msgb *reject; |
| 187 | |
| 188 | if (!msg) |
| 189 | return -EINVAL; |
| 190 | |
| 191 | /* check if a vc is available */ |
Alexander Couzens | 38b19e8 | 2020-09-23 23:56:37 +0200 | [diff] [blame] | 192 | nsvc = gprs_ns2_nsvc_by_sockaddr_bind(bind, &saddr); |
| 193 | if (!nsvc) { |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 194 | /* VC not found */ |
| 195 | rc = ns2_create_vc(bind, msg, "newconnection", &reject, &nsvc); |
| 196 | switch (rc) { |
| 197 | case GPRS_NS2_CS_FOUND: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 198 | break; |
| 199 | case GPRS_NS2_CS_ERROR: |
| 200 | case GPRS_NS2_CS_SKIPPED: |
| 201 | rc = 0; |
Alexander Couzens | 1301012 | 2020-09-24 00:54:51 +0200 | [diff] [blame] | 202 | goto out; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 203 | case GPRS_NS2_CS_REJECTED: |
| 204 | /* nsip_sendmsg will free reject */ |
Alexander Couzens | 772ca61 | 2020-09-24 16:19:02 +0200 | [diff] [blame] | 205 | rc = nsip_sendmsg(bind, reject, &saddr); |
| 206 | goto out; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 207 | case GPRS_NS2_CS_CREATED: |
| 208 | ns2_driver_alloc_vc(bind, nsvc, &saddr); |
| 209 | gprs_ns2_vc_fsm_start(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 210 | break; |
| 211 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Alexander Couzens | 1301012 | 2020-09-24 00:54:51 +0200 | [diff] [blame] | 214 | rc = ns2_recv_vc(nsvc, msg); |
| 215 | out: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 216 | msgb_free(msg); |
| 217 | |
| 218 | return rc; |
| 219 | } |
| 220 | |
| 221 | static int handle_nsip_write(struct osmo_fd *bfd) |
| 222 | { |
| 223 | /* FIXME: actually send the data here instead of nsip_sendmsg() */ |
| 224 | return -EIO; |
| 225 | } |
| 226 | |
| 227 | static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what) |
| 228 | { |
| 229 | int rc = 0; |
| 230 | |
| 231 | if (what & OSMO_FD_READ) |
| 232 | rc = handle_nsip_read(bfd); |
| 233 | if (what & OSMO_FD_WRITE) |
| 234 | rc = handle_nsip_write(bfd); |
| 235 | |
| 236 | return rc; |
| 237 | } |
| 238 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 239 | /*! Bind to an IPv4/IPv6 address |
| 240 | * \param[in] nsi NS Instance in which to create the NSVC |
| 241 | * \param[in] local the local address to bind to |
| 242 | * \param[in] dscp the DSCP/TOS bits used for transmitted data |
| 243 | * \param[out] result if set, returns the bind object |
| 244 | * \return 0 on success; negative in case of error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 245 | int gprs_ns2_ip_bind(struct gprs_ns2_inst *nsi, |
| 246 | struct osmo_sockaddr *local, |
| 247 | int dscp, |
| 248 | struct gprs_ns2_vc_bind **result) |
| 249 | { |
| 250 | struct gprs_ns2_vc_bind *bind = talloc_zero(nsi, struct gprs_ns2_vc_bind); |
| 251 | struct priv_bind *priv; |
| 252 | int rc; |
| 253 | |
| 254 | if (!bind) |
| 255 | return -ENOSPC; |
| 256 | |
| 257 | if (local->u.sa.sa_family != AF_INET && local->u.sa.sa_family != AF_INET6) { |
| 258 | talloc_free(bind); |
| 259 | return -EINVAL; |
| 260 | } |
| 261 | |
| 262 | bind->driver = &vc_driver_ip; |
| 263 | bind->send_vc = nsip_vc_sendmsg; |
| 264 | bind->free_vc = free_vc; |
| 265 | bind->nsi = nsi; |
| 266 | |
| 267 | priv = bind->priv = talloc_zero(bind, struct priv_bind); |
| 268 | if (!priv) { |
| 269 | talloc_free(bind); |
| 270 | return -ENOSPC; |
| 271 | } |
| 272 | priv->fd.cb = nsip_fd_cb; |
| 273 | priv->fd.data = bind; |
| 274 | priv->addr = *local; |
| 275 | INIT_LLIST_HEAD(&bind->nsvc); |
| 276 | |
| 277 | llist_add(&bind->list, &nsi->binding); |
| 278 | |
| 279 | rc = osmo_sock_init_osa_ofd(&priv->fd, SOCK_DGRAM, IPPROTO_UDP, |
| 280 | local, NULL, |
| 281 | OSMO_SOCK_F_BIND); |
| 282 | if (rc < 0) { |
| 283 | talloc_free(priv); |
| 284 | talloc_free(bind); |
| 285 | return rc; |
| 286 | } |
| 287 | |
| 288 | if (dscp > 0) { |
| 289 | priv->dscp = dscp; |
| 290 | |
| 291 | rc = setsockopt(priv->fd.fd, IPPROTO_IP, IP_TOS, |
| 292 | &dscp, sizeof(dscp)); |
| 293 | if (rc < 0) |
| 294 | LOGP(DLNS, LOGL_ERROR, |
| 295 | "Failed to set the DSCP to %d with ret(%d) errno(%d)\n", |
| 296 | dscp, rc, errno); |
| 297 | } |
| 298 | |
| 299 | ns2_vty_bind_apply(bind); |
| 300 | |
| 301 | if (result) |
| 302 | *result = bind; |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 307 | /*! Create new NS-VC to a given remote address |
| 308 | * \param[in] bind the bind we want to connect |
| 309 | * \param[in] nse NS entity to be used for the new NS-VC |
| 310 | * \param[in] remote remote address to connect to |
| 311 | * \return pointer to newly-allocated and connected NS-VC; NULL on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 312 | struct gprs_ns2_vc *gprs_ns2_ip_bind_connect(struct gprs_ns2_vc_bind *bind, |
| 313 | struct gprs_ns2_nse *nse, |
| 314 | struct osmo_sockaddr *remote) |
| 315 | { |
| 316 | struct gprs_ns2_vc *nsvc; |
| 317 | struct priv_vc *priv; |
| 318 | |
| 319 | nsvc = ns2_vc_alloc(bind, nse, true); |
| 320 | nsvc->priv = talloc_zero(bind, struct priv_vc); |
| 321 | if (!nsvc->priv) { |
| 322 | gprs_ns2_free_nsvc(nsvc); |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | priv = nsvc->priv; |
| 327 | priv->remote = *remote; |
| 328 | |
| 329 | nsvc->ll = GPRS_NS_LL_UDP; |
| 330 | |
| 331 | return nsvc; |
| 332 | } |
| 333 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 334 | /*! Return the socket address of the remote peer of a NS-VC. |
| 335 | * \param[in] nsvc NS-VC whose remote peer we want to know |
| 336 | * \return address of the remote peer; NULL in case of error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 337 | struct osmo_sockaddr *gprs_ns2_ip_vc_sockaddr(struct gprs_ns2_vc *nsvc) |
| 338 | { |
| 339 | struct priv_vc *priv; |
| 340 | |
| 341 | if (nsvc->ll != GPRS_NS_LL_UDP) |
| 342 | return NULL; |
| 343 | |
| 344 | priv = nsvc->priv; |
| 345 | return &priv->remote; |
| 346 | } |
| 347 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 348 | /*! Return the locally bound socket address of the bind. |
| 349 | * \param[in] bind The bind whose local address we want to know |
| 350 | * \return address of the local bind */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 351 | struct osmo_sockaddr *gprs_ns2_ip_bind_sockaddr(struct gprs_ns2_vc_bind *bind) |
| 352 | { |
| 353 | struct priv_bind *priv; |
| 354 | |
| 355 | priv = bind->priv; |
| 356 | return &priv->addr; |
| 357 | } |
| 358 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 359 | /*! Is the given bind an IP bind? */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 360 | int gprs_ns2_is_ip_bind(struct gprs_ns2_vc_bind *bind) |
| 361 | { |
| 362 | return (bind->driver == &vc_driver_ip); |
| 363 | } |
| 364 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 365 | /*! Set the DSCP (TOS) bit value of the given bind. */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 366 | int gprs_ns2_ip_bind_set_dscp(struct gprs_ns2_vc_bind *bind, int dscp) |
| 367 | { |
| 368 | struct priv_bind *priv; |
| 369 | int rc = 0; |
| 370 | |
| 371 | priv = bind->priv; |
| 372 | |
| 373 | if (dscp != priv->dscp) { |
| 374 | priv->dscp = dscp; |
| 375 | |
| 376 | rc = setsockopt(priv->fd.fd, IPPROTO_IP, IP_TOS, |
| 377 | &dscp, sizeof(dscp)); |
| 378 | if (rc < 0) |
| 379 | LOGP(DLNS, LOGL_ERROR, |
| 380 | "Failed to set the DSCP to %d with ret(%d) errno(%d)\n", |
| 381 | dscp, rc, errno); |
| 382 | } |
| 383 | |
| 384 | return rc; |
| 385 | } |