blob: 7ecf2760225b09eaac4bda7946a513c8a0660bb5 [file] [log] [blame]
Alexander Couzens6a161492020-07-12 13:45:50 +02001/*! \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
40static void free_bind(struct gprs_ns2_vc_bind *bind);
41
42
43struct gprs_ns2_vc_driver vc_driver_ip = {
44 .name = "GB UDP IPv4/IPv6",
45 .free_bind = free_bind,
46};
47
48struct priv_bind {
49 struct osmo_fd fd;
50 struct osmo_sockaddr addr;
51 int dscp;
Harald Welted99e4ee2021-04-28 19:57:12 +020052 uint8_t priority;
Alexander Couzens6a161492020-07-12 13:45:50 +020053};
54
55struct 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() */
60static void free_bind(struct gprs_ns2_vc_bind *bind)
61{
62 struct priv_bind *priv;
63
64 if (!bind)
65 return;
66
Alexander Couzens55bc8692021-01-18 18:39:57 +010067 OSMO_ASSERT(gprs_ns2_is_ip_bind(bind));
68
Alexander Couzens6a161492020-07-12 13:45:50 +020069 priv = bind->priv;
70
71 osmo_fd_close(&priv->fd);
72 talloc_free(priv);
73}
74
75static void free_vc(struct gprs_ns2_vc *nsvc)
76{
Alexander Couzensea377242021-01-17 16:51:55 +010077 if (!nsvc)
78 return;
79
Alexander Couzens6a161492020-07-12 13:45:50 +020080 if (!nsvc->priv)
81 return;
82
Alexander Couzens55bc8692021-01-18 18:39:57 +010083 OSMO_ASSERT(gprs_ns2_is_ip_bind(nsvc->bind));
Alexander Couzens6a161492020-07-12 13:45:50 +020084 talloc_free(nsvc->priv);
85 nsvc->priv = NULL;
86}
87
Alexander Couzens22f34712020-10-02 02:34:39 +020088static void dump_vty(const struct gprs_ns2_vc_bind *bind,
Alexander Couzens75b61882021-03-21 16:18:17 +010089 struct vty *vty, bool stats)
Alexander Couzens22f34712020-10-02 02:34:39 +020090{
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 Welted99e4ee2021-04-28 19:57:12 +0200107 vty_out(vty, "UDP bind: %s:%d DSCP: %d Priority: %u%s", sockstr.ip, sockstr.port,
108 priv->dscp, priv->priority, VTY_NEWLINE);
Alexander Couzensc4704762021-02-08 23:13:12 +0100109 vty_out(vty, " IP-SNS signalling weight: %u data weight: %u%s",
110 bind->sns_sig_weight, bind->sns_data_weight, VTY_NEWLINE);
Alexander Couzens8bd63b62021-03-21 17:45:22 +0100111 vty_out(vty, " %lu NS-VC:%s", nsvcs, VTY_NEWLINE);
Alexander Couzens22f34712020-10-02 02:34:39 +0200112
113 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
Alexander Couzens75b61882021-03-21 16:18:17 +0100114 ns2_vty_dump_nsvc(vty, nsvc, stats);
Alexander Couzens22f34712020-10-02 02:34:39 +0200115 }
116}
117
118
Harald Welte5bef2cc2020-09-18 22:33:24 +0200119/*! Find a NS-VC by its remote socket address.
120 * \param[in] bind in which to search
Pau Espin Pedrolf6ef9ba2023-04-27 17:50:10 +0200121 * \param[in] rem_addr remote peer socket address to search
Alexander Couzens38b19e82020-09-23 23:56:37 +0200122 * \returns NS-VC matching sockaddr; NULL if none found */
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700123struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_bind(struct gprs_ns2_vc_bind *bind,
Pau Espin Pedrolf6ef9ba2023-04-27 17:50:10 +0200124 const struct osmo_sockaddr *rem_addr)
Alexander Couzens6a161492020-07-12 13:45:50 +0200125{
126 struct gprs_ns2_vc *nsvc;
127 struct priv_vc *vcpriv;
128
Alexander Couzens55bc8692021-01-18 18:39:57 +0100129 OSMO_ASSERT(gprs_ns2_is_ip_bind(bind));
130
Alexander Couzens6a161492020-07-12 13:45:50 +0200131 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
132 vcpriv = nsvc->priv;
Pau Espin Pedrolf6ef9ba2023-04-27 17:50:10 +0200133 if (vcpriv->remote.u.sa.sa_family != rem_addr->u.sa.sa_family)
Alexander Couzens6a161492020-07-12 13:45:50 +0200134 continue;
Pau Espin Pedrolf6ef9ba2023-04-27 17:50:10 +0200135 if (osmo_sockaddr_cmp(&vcpriv->remote, rem_addr))
Alexander Couzens6a161492020-07-12 13:45:50 +0200136 continue;
137
Alexander Couzens38b19e82020-09-23 23:56:37 +0200138 return nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200139 }
140
Alexander Couzens38b19e82020-09-23 23:56:37 +0200141 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200142}
143
144static 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 Welte5bef2cc2020-09-18 22:33:24 +0200159/*! 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 Couzens6a161492020-07-12 13:45:50 +0200163static 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 Weltef2949742021-01-20 14:54:14 +0100175static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error, struct osmo_sockaddr *saddr,
176 const struct gprs_ns2_vc_bind *bind)
Alexander Couzens6a161492020-07-12 13:45:50 +0200177{
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100178 struct msgb *msg = ns2_msgb_alloc();
Alexander Couzens6a161492020-07-12 13:45:50 +0200179 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 Weltef2949742021-01-20 14:54:14 +0100190 LOGBIND(bind, LOGL_ERROR, "recv error %s during NSIP recvfrom %s\n",
191 strerror(errno), osmo_sock_get_name2(bfd->fd));
Alexander Couzens6a161492020-07-12 13:45:50 +0200192 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
207static 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
219static int handle_nsip_read(struct osmo_fd *bfd)
220{
Alexander Couzenscce88282020-10-26 00:25:50 +0100221 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200222 int error = 0;
223 struct gprs_ns2_vc_bind *bind = bfd->data;
224 struct osmo_sockaddr saddr;
225 struct gprs_ns2_vc *nsvc;
Harald Weltef2949742021-01-20 14:54:14 +0100226 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr, bind);
Alexander Couzens6a161492020-07-12 13:45:50 +0200227 struct msgb *reject;
228
229 if (!msg)
230 return -EINVAL;
231
232 /* check if a vc is available */
Alexander Couzens38b19e82020-09-23 23:56:37 +0200233 nsvc = gprs_ns2_nsvc_by_sockaddr_bind(bind, &saddr);
234 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200235 /* VC not found */
Harald Welte7d0daac2021-03-02 23:08:43 +0100236 rc = ns2_create_vc(bind, msg, &saddr, "newconnection", &reject, &nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200237 switch (rc) {
Alexander Couzensba5a9922021-01-25 16:03:23 +0100238 case NS2_CS_FOUND:
Alexander Couzens6a161492020-07-12 13:45:50 +0200239 break;
Alexander Couzensba5a9922021-01-25 16:03:23 +0100240 case NS2_CS_ERROR:
241 case NS2_CS_SKIPPED:
Alexander Couzens6a161492020-07-12 13:45:50 +0200242 rc = 0;
Alexander Couzens13010122020-09-24 00:54:51 +0200243 goto out;
Alexander Couzensba5a9922021-01-25 16:03:23 +0100244 case NS2_CS_REJECTED:
Alexander Couzens6a161492020-07-12 13:45:50 +0200245 /* nsip_sendmsg will free reject */
Alexander Couzens772ca612020-09-24 16:19:02 +0200246 rc = nsip_sendmsg(bind, reject, &saddr);
247 goto out;
Alexander Couzensba5a9922021-01-25 16:03:23 +0100248 case NS2_CS_CREATED:
Alexander Couzens6a161492020-07-12 13:45:50 +0200249 ns2_driver_alloc_vc(bind, nsvc, &saddr);
Alexander Couzens20ed5912021-08-06 18:07:26 +0200250 /* 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 Couzens6a161492020-07-12 13:45:50 +0200253 break;
254 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200255 }
256
Alexander Couzenscce88282020-10-26 00:25:50 +0100257 return ns2_recv_vc(nsvc, msg);
258
Alexander Couzens13010122020-09-24 00:54:51 +0200259out:
Alexander Couzens6a161492020-07-12 13:45:50 +0200260 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200261 return rc;
262}
263
264static 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
270static 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 Couzens4f608452020-10-11 18:41:24 +0200282/*! 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 */
287struct 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 Couzens9a4cf272020-10-11 20:48:04 +0200291 const struct osmo_sockaddr *local;
Alexander Couzens4f608452020-10-11 18:41:24 +0200292
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 Welte5bef2cc2020-09-18 22:33:24 +0200308/*! 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 Couzensc80a8742021-02-03 11:27:52 +0100312 * \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 Couzens6a161492020-07-12 13:45:50 +0200314int gprs_ns2_ip_bind(struct gprs_ns2_inst *nsi,
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100315 const char *name,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700316 const struct osmo_sockaddr *local,
Alexander Couzens6a161492020-07-12 13:45:50 +0200317 int dscp,
318 struct gprs_ns2_vc_bind **result)
319{
Alexander Couzens6090b1d2020-10-11 18:41:37 +0200320 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6a161492020-07-12 13:45:50 +0200321 struct priv_bind *priv;
322 int rc;
323
Harald Weltec3aa8f92021-01-31 11:41:34 +0100324 if (local->u.sa.sa_family != AF_INET && local->u.sa.sa_family != AF_INET6)
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100325 return -EINVAL;
326
Harald Weltec96d7162021-04-27 21:56:25 +0200327 if (dscp < 0 || dscp > 63)
328 return -EINVAL;
329
Alexander Couzens6090b1d2020-10-11 18:41:37 +0200330 bind = gprs_ns2_ip_bind_by_sockaddr(nsi, local);
331 if (bind) {
Alexander Couzensc80a8742021-02-03 11:27:52 +0100332 if (result)
333 *result = bind;
Alexander Couzens6090b1d2020-10-11 18:41:37 +0200334 return -EBUSY;
335 }
336
Harald Weltec3aa8f92021-01-31 11:41:34 +0100337 rc = ns2_bind_alloc(nsi, name, &bind);
338 if (rc < 0)
339 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200340
341 bind->driver = &vc_driver_ip;
Alexander Couzensaac90162020-11-19 02:44:04 +0100342 bind->ll = GPRS_NS2_LL_UDP;
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100343 /* 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 Couzens6a161492020-07-12 13:45:50 +0200347 bind->send_vc = nsip_vc_sendmsg;
348 bind->free_vc = free_vc;
Alexander Couzens22f34712020-10-02 02:34:39 +0200349 bind->dump_vty = dump_vty;
Alexander Couzens6a161492020-07-12 13:45:50 +0200350
351 priv = bind->priv = talloc_zero(bind, struct priv_bind);
352 if (!priv) {
Harald Weltec3aa8f92021-01-31 11:41:34 +0100353 gprs_ns2_free_bind(bind);
Harald Weltebdfb8b92021-01-31 11:44:57 +0100354 return -ENOMEM;
Alexander Couzens6a161492020-07-12 13:45:50 +0200355 }
356 priv->fd.cb = nsip_fd_cb;
357 priv->fd.data = bind;
358 priv->addr = *local;
Harald Welte485b3f72021-04-28 19:45:34 +0200359 priv->dscp = dscp;
Alexander Couzens6a161492020-07-12 13:45:50 +0200360
Alexander Couzens6a161492020-07-12 13:45:50 +0200361 rc = osmo_sock_init_osa_ofd(&priv->fd, SOCK_DGRAM, IPPROTO_UDP,
362 local, NULL,
Harald Welte485b3f72021-04-28 19:45:34 +0200363 OSMO_SOCK_F_BIND | OSMO_SOCK_F_DSCP(priv->dscp));
Alexander Couzens6a161492020-07-12 13:45:50 +0200364 if (rc < 0) {
Harald Weltec3aa8f92021-01-31 11:41:34 +0100365 gprs_ns2_free_bind(bind);
Alexander Couzens6a161492020-07-12 13:45:50 +0200366 return rc;
367 }
368
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100369 /* 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 Couzens6a161492020-07-12 13:45:50 +0200373 if (result)
374 *result = bind;
375
376 return 0;
377}
378
Harald Welte5bef2cc2020-09-18 22:33:24 +0200379/*! 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 Couzens8dfc24c2021-01-25 16:09:23 +0100384struct 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 Couzens6a161492020-07-12 13:45:50 +0200387{
388 struct gprs_ns2_vc *nsvc;
Alexander Couzensd7948062021-06-03 20:35:47 +0200389 const struct osmo_sockaddr *local;
Alexander Couzens6a161492020-07-12 13:45:50 +0200390 struct priv_vc *priv;
Alexander Couzensd923cff2020-12-01 01:03:52 +0100391 enum gprs_ns2_vc_mode vc_mode;
Alexander Couzensd7948062021-06-03 20:35:47 +0200392 char idbuf[256], tmp[INET6_ADDRSTRLEN + 8];
Alexander Couzens6a161492020-07-12 13:45:50 +0200393
Alexander Couzens55bc8692021-01-18 18:39:57 +0100394 OSMO_ASSERT(gprs_ns2_is_ip_bind(bind));
395
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100396 vc_mode = ns2_dialect_to_vc_mode(nse->dialect);
Alexander Couzensd923cff2020-12-01 01:03:52 +0100397 if ((int) vc_mode == -1) {
Harald Weltef2949742021-01-20 14:54:14 +0100398 LOGNSE(nse, LOGL_ERROR, "Can not derive vc mode from dialect %d. Maybe libosmocore is too old.\n",
399 nse->dialect);
Alexander Couzensd923cff2020-12-01 01:03:52 +0100400 return NULL;
401 }
402
Alexander Couzens7bb39e32021-02-16 23:06:53 +0100403 /* duplicate */
404 if (gprs_ns2_nsvc_by_sockaddr_bind(bind, remote))
405 return NULL;
406
Alexander Couzensd7948062021-06-03 20:35:47 +0200407 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 Welte603f4042020-11-29 17:39:19 +0100413 nsvc = ns2_vc_alloc(bind, nse, true, vc_mode, idbuf);
Alexander Couzensc06aa712020-11-18 23:56:45 +0100414 if (!nsvc)
415 return NULL;
416
Alexander Couzens6a161492020-07-12 13:45:50 +0200417 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 Couzens6a161492020-07-12 13:45:50 +0200426 return nsvc;
427}
428
Alexander Couzens979f5f52020-10-11 21:01:48 +0200429/*! 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 */
432const struct osmo_sockaddr *gprs_ns2_ip_vc_local(const struct gprs_ns2_vc *nsvc)
433{
434 struct priv_bind *priv;
435
Alexander Couzens979f5f52020-10-11 21:01:48 +0200436 if (nsvc->bind->driver != &vc_driver_ip)
437 return NULL;
438
439 priv = nsvc->bind->priv;
440 return &priv->addr;
441}
442
Harald Welte5bef2cc2020-09-18 22:33:24 +0200443/*! 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 Couzensd33512b2020-10-11 21:42:11 +0200446const struct osmo_sockaddr *gprs_ns2_ip_vc_remote(const struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200447{
448 struct priv_vc *priv;
449
Alexander Couzensaac90162020-11-19 02:44:04 +0100450 if (nsvc->bind->driver != &vc_driver_ip)
Alexander Couzens6a161492020-07-12 13:45:50 +0200451 return NULL;
452
453 priv = nsvc->priv;
454 return &priv->remote;
455}
456
Alexander Couzensd420ea92020-10-12 01:11:05 +0200457/*! 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 */
464bool 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 Couzensaac90162020-11-19 02:44:04 +0100472 if (nsvc->bind->driver != &vc_driver_ip)
Alexander Couzensd420ea92020-10-12 01:11:05 +0200473 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 Couzens138b96f2021-01-25 16:23:29 +0100484 if (nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET)
Alexander Couzensd420ea92020-10-12 01:11:05 +0200485 if (nsvc->nsvci != nsvci)
486 return false;
487
488 return true;
489}
490
Harald Welte5bef2cc2020-09-18 22:33:24 +0200491/*! 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 Couzens9a4cf272020-10-11 20:48:04 +0200494const struct osmo_sockaddr *gprs_ns2_ip_bind_sockaddr(struct gprs_ns2_vc_bind *bind)
Alexander Couzens6a161492020-07-12 13:45:50 +0200495{
496 struct priv_bind *priv;
Alexander Couzens55bc8692021-01-18 18:39:57 +0100497 OSMO_ASSERT(gprs_ns2_is_ip_bind(bind));
Alexander Couzens6a161492020-07-12 13:45:50 +0200498
499 priv = bind->priv;
500 return &priv->addr;
501}
502
Harald Welte5bef2cc2020-09-18 22:33:24 +0200503/*! Is the given bind an IP bind? */
Alexander Couzens6a161492020-07-12 13:45:50 +0200504int gprs_ns2_is_ip_bind(struct gprs_ns2_vc_bind *bind)
505{
506 return (bind->driver == &vc_driver_ip);
507}
508
Harald Welte5bef2cc2020-09-18 22:33:24 +0200509/*! Set the DSCP (TOS) bit value of the given bind. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200510int 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 Welte628f5342021-04-28 19:41:07 +0200515 if (dscp < 0 || dscp > 63)
516 return -EINVAL;
517
Alexander Couzens55bc8692021-01-18 18:39:57 +0100518 OSMO_ASSERT(gprs_ns2_is_ip_bind(bind));
Alexander Couzens6a161492020-07-12 13:45:50 +0200519 priv = bind->priv;
520
521 if (dscp != priv->dscp) {
522 priv->dscp = dscp;
523
Harald Welte628f5342021-04-28 19:41:07 +0200524 rc = osmo_sock_set_dscp(priv->fd.fd, dscp);
Harald Weltef2949742021-01-20 14:54:14 +0100525 if (rc < 0) {
Harald Welte628f5342021-04-28 19:41:07 +0200526 LOGBIND(bind, LOGL_ERROR, "Failed to set the DSCP to %u with ret(%d) errno(%d)\n",
Harald Weltef2949742021-01-20 14:54:14 +0100527 dscp, rc, errno);
528 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200529 }
530
531 return rc;
532}
Alexander Couzense769f522020-12-07 07:37:07 +0100533
Harald Welted99e4ee2021-04-28 19:57:12 +0200534/*! Set the socket priority of the given bind. */
535int 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 Couzense769f522020-12-07 07:37:07 +0100557/*! Count UDP binds compatible with remote */
558int 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 */
580struct 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 Couzensc4704762021-02-08 23:13:12 +0100605
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 */
611void 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}