blob: 8bfa184d2501c6856770dee24d5e07fa77f817e4 [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;
52};
53
54struct 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() */
59static void free_bind(struct gprs_ns2_vc_bind *bind)
60{
61 struct priv_bind *priv;
62
63 if (!bind)
64 return;
65
Alexander Couzens55bc8692021-01-18 18:39:57 +010066 OSMO_ASSERT(gprs_ns2_is_ip_bind(bind));
67
Alexander Couzens6a161492020-07-12 13:45:50 +020068 priv = bind->priv;
69
70 osmo_fd_close(&priv->fd);
71 talloc_free(priv);
72}
73
74static void free_vc(struct gprs_ns2_vc *nsvc)
75{
Alexander Couzensea377242021-01-17 16:51:55 +010076 if (!nsvc)
77 return;
78
Alexander Couzens6a161492020-07-12 13:45:50 +020079 if (!nsvc->priv)
80 return;
81
Alexander Couzens55bc8692021-01-18 18:39:57 +010082 OSMO_ASSERT(gprs_ns2_is_ip_bind(nsvc->bind));
Alexander Couzens6a161492020-07-12 13:45:50 +020083 talloc_free(nsvc->priv);
84 nsvc->priv = NULL;
85}
86
Alexander Couzens22f34712020-10-02 02:34:39 +020087static void dump_vty(const struct gprs_ns2_vc_bind *bind,
88 struct vty *vty, bool _stats)
89{
90 struct priv_bind *priv;
91 struct gprs_ns2_vc *nsvc;
92 struct osmo_sockaddr_str sockstr = {};
93 unsigned long nsvcs = 0;
94
95 if (!bind)
96 return;
97
98 priv = bind->priv;
99 if (osmo_sockaddr_str_from_sockaddr(&sockstr, &priv->addr.u.sas))
100 strcpy(sockstr.ip, "invalid");
101
102 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
103 nsvcs++;
104 }
105
Harald Welte1e72df02020-12-01 18:20:25 +0100106 vty_out(vty, "UDP bind: %s:%d DSCP: %d%s", sockstr.ip, sockstr.port, priv->dscp, VTY_NEWLINE);
Alexander Couzens22f34712020-10-02 02:34:39 +0200107 vty_out(vty, " %lu NS-VC: %s", nsvcs, VTY_NEWLINE);
108
109 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
Harald Welte96ec84a2020-12-01 17:56:05 +0100110 vty_out(vty, " NSVCI %05u: %s%s", nsvc->nsvci, gprs_ns2_ll_str(nsvc), VTY_NEWLINE);
Alexander Couzens22f34712020-10-02 02:34:39 +0200111 }
112}
113
114
Harald Welte5bef2cc2020-09-18 22:33:24 +0200115/*! Find a NS-VC by its remote socket address.
116 * \param[in] bind in which to search
117 * \param[in] saddr remote peer socket adddress to search
Alexander Couzens38b19e82020-09-23 23:56:37 +0200118 * \returns NS-VC matching sockaddr; NULL if none found */
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700119struct gprs_ns2_vc *gprs_ns2_nsvc_by_sockaddr_bind(struct gprs_ns2_vc_bind *bind,
120 const struct osmo_sockaddr *saddr)
Alexander Couzens6a161492020-07-12 13:45:50 +0200121{
122 struct gprs_ns2_vc *nsvc;
123 struct priv_vc *vcpriv;
124
Alexander Couzens55bc8692021-01-18 18:39:57 +0100125 OSMO_ASSERT(gprs_ns2_is_ip_bind(bind));
126
Alexander Couzens6a161492020-07-12 13:45:50 +0200127 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
128 vcpriv = nsvc->priv;
129 if (vcpriv->remote.u.sa.sa_family != saddr->u.sa.sa_family)
130 continue;
131 if (osmo_sockaddr_cmp(&vcpriv->remote, saddr))
132 continue;
133
Alexander Couzens38b19e82020-09-23 23:56:37 +0200134 return nsvc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200135 }
136
Alexander Couzens38b19e82020-09-23 23:56:37 +0200137 return NULL;
Alexander Couzens6a161492020-07-12 13:45:50 +0200138}
139
140static inline int nsip_sendmsg(struct gprs_ns2_vc_bind *bind,
141 struct msgb *msg,
142 struct osmo_sockaddr *dest)
143{
144 int rc;
145 struct priv_bind *priv = bind->priv;
146
147 rc = sendto(priv->fd.fd, msg->data, msg->len, 0,
148 &dest->u.sa, sizeof(*dest));
149
150 msgb_free(msg);
151
152 return rc;
153}
154
Harald Welte5bef2cc2020-09-18 22:33:24 +0200155/*! 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 Couzens6a161492020-07-12 13:45:50 +0200159static 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
170/* Read a single NS-over-IP message */
Harald Weltef2949742021-01-20 14:54:14 +0100171static struct msgb *read_nsip_msg(struct osmo_fd *bfd, int *error, struct osmo_sockaddr *saddr,
172 const struct gprs_ns2_vc_bind *bind)
Alexander Couzens6a161492020-07-12 13:45:50 +0200173{
174 struct msgb *msg = gprs_ns2_msgb_alloc();
175 int ret = 0;
176 socklen_t saddr_len = sizeof(*saddr);
177
178 if (!msg) {
179 *error = -ENOMEM;
180 return NULL;
181 }
182
183 ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE - NS_ALLOC_HEADROOM, 0,
184 &saddr->u.sa, &saddr_len);
185 if (ret < 0) {
Harald Weltef2949742021-01-20 14:54:14 +0100186 LOGBIND(bind, LOGL_ERROR, "recv error %s during NSIP recvfrom %s\n",
187 strerror(errno), osmo_sock_get_name2(bfd->fd));
Alexander Couzens6a161492020-07-12 13:45:50 +0200188 msgb_free(msg);
189 *error = ret;
190 return NULL;
191 } else if (ret == 0) {
192 msgb_free(msg);
193 *error = ret;
194 return NULL;
195 }
196
197 msg->l2h = msg->data;
198 msgb_put(msg, ret);
199
200 return msg;
201}
202
203static struct priv_vc *ns2_driver_alloc_vc(struct gprs_ns2_vc_bind *bind, struct gprs_ns2_vc *nsvc, struct osmo_sockaddr *remote)
204{
205 struct priv_vc *priv = talloc_zero(bind, struct priv_vc);
206 if (!priv)
207 return NULL;
208
209 nsvc->priv = priv;
210 priv->remote = *remote;
211
212 return priv;
213}
214
215static int handle_nsip_read(struct osmo_fd *bfd)
216{
Alexander Couzenscce88282020-10-26 00:25:50 +0100217 int rc = 0;
Alexander Couzens6a161492020-07-12 13:45:50 +0200218 int error = 0;
219 struct gprs_ns2_vc_bind *bind = bfd->data;
220 struct osmo_sockaddr saddr;
221 struct gprs_ns2_vc *nsvc;
Harald Weltef2949742021-01-20 14:54:14 +0100222 struct msgb *msg = read_nsip_msg(bfd, &error, &saddr, bind);
Alexander Couzens6a161492020-07-12 13:45:50 +0200223 struct msgb *reject;
224
225 if (!msg)
226 return -EINVAL;
227
228 /* check if a vc is available */
Alexander Couzens38b19e82020-09-23 23:56:37 +0200229 nsvc = gprs_ns2_nsvc_by_sockaddr_bind(bind, &saddr);
230 if (!nsvc) {
Alexander Couzens6a161492020-07-12 13:45:50 +0200231 /* VC not found */
232 rc = ns2_create_vc(bind, msg, "newconnection", &reject, &nsvc);
233 switch (rc) {
234 case GPRS_NS2_CS_FOUND:
Alexander Couzens6a161492020-07-12 13:45:50 +0200235 break;
236 case GPRS_NS2_CS_ERROR:
237 case GPRS_NS2_CS_SKIPPED:
238 rc = 0;
Alexander Couzens13010122020-09-24 00:54:51 +0200239 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +0200240 case GPRS_NS2_CS_REJECTED:
241 /* nsip_sendmsg will free reject */
Alexander Couzens772ca612020-09-24 16:19:02 +0200242 rc = nsip_sendmsg(bind, reject, &saddr);
243 goto out;
Alexander Couzens6a161492020-07-12 13:45:50 +0200244 case GPRS_NS2_CS_CREATED:
245 ns2_driver_alloc_vc(bind, nsvc, &saddr);
246 gprs_ns2_vc_fsm_start(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200247 break;
248 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200249 }
250
Alexander Couzenscce88282020-10-26 00:25:50 +0100251 return ns2_recv_vc(nsvc, msg);
252
Alexander Couzens13010122020-09-24 00:54:51 +0200253out:
Alexander Couzens6a161492020-07-12 13:45:50 +0200254 msgb_free(msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200255 return rc;
256}
257
258static int handle_nsip_write(struct osmo_fd *bfd)
259{
260 /* FIXME: actually send the data here instead of nsip_sendmsg() */
261 return -EIO;
262}
263
264static int nsip_fd_cb(struct osmo_fd *bfd, unsigned int what)
265{
266 int rc = 0;
267
268 if (what & OSMO_FD_READ)
269 rc = handle_nsip_read(bfd);
270 if (what & OSMO_FD_WRITE)
271 rc = handle_nsip_write(bfd);
272
273 return rc;
274}
275
Alexander Couzens4f608452020-10-11 18:41:24 +0200276/*! Find NS bind for a given socket address
277 * \param[in] nsi NS instance
278 * \param[in] sockaddr socket address to search for
279 * \return
280 */
281struct gprs_ns2_vc_bind *gprs_ns2_ip_bind_by_sockaddr(struct gprs_ns2_inst *nsi,
282 const struct osmo_sockaddr *sockaddr)
283{
284 struct gprs_ns2_vc_bind *bind;
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200285 const struct osmo_sockaddr *local;
Alexander Couzens4f608452020-10-11 18:41:24 +0200286
287 OSMO_ASSERT(nsi);
288 OSMO_ASSERT(sockaddr);
289
290 llist_for_each_entry(bind, &nsi->binding, list) {
291 if (!gprs_ns2_is_ip_bind(bind))
292 continue;
293
294 local = gprs_ns2_ip_bind_sockaddr(bind);
295 if (!osmo_sockaddr_cmp(sockaddr, local))
296 return bind;
297 }
298
299 return NULL;
300}
301
Harald Welte5bef2cc2020-09-18 22:33:24 +0200302/*! Bind to an IPv4/IPv6 address
303 * \param[in] nsi NS Instance in which to create the NSVC
304 * \param[in] local the local address to bind to
305 * \param[in] dscp the DSCP/TOS bits used for transmitted data
306 * \param[out] result if set, returns the bind object
307 * \return 0 on success; negative in case of error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200308int gprs_ns2_ip_bind(struct gprs_ns2_inst *nsi,
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100309 const char *name,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700310 const struct osmo_sockaddr *local,
Alexander Couzens6a161492020-07-12 13:45:50 +0200311 int dscp,
312 struct gprs_ns2_vc_bind **result)
313{
Alexander Couzens6090b1d2020-10-11 18:41:37 +0200314 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6a161492020-07-12 13:45:50 +0200315 struct priv_bind *priv;
316 int rc;
317
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100318 if (!name)
319 return -EINVAL;
320
321 if (gprs_ns2_bind_by_name(nsi, name))
322 return -EALREADY;
323
Alexander Couzens6090b1d2020-10-11 18:41:37 +0200324 bind = gprs_ns2_ip_bind_by_sockaddr(nsi, local);
325 if (bind) {
326 *result = bind;
327 return -EBUSY;
328 }
329
330 bind = talloc_zero(nsi, struct gprs_ns2_vc_bind);
Alexander Couzens6a161492020-07-12 13:45:50 +0200331 if (!bind)
332 return -ENOSPC;
333
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100334 bind->name = talloc_strdup(bind, name);
335 if (!bind->name) {
336 talloc_free(bind);
337 return -ENOSPC;
338 }
339
Alexander Couzens6a161492020-07-12 13:45:50 +0200340 if (local->u.sa.sa_family != AF_INET && local->u.sa.sa_family != AF_INET6) {
341 talloc_free(bind);
342 return -EINVAL;
343 }
344
345 bind->driver = &vc_driver_ip;
Alexander Couzensaac90162020-11-19 02:44:04 +0100346 bind->ll = GPRS_NS2_LL_UDP;
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100347 /* expect 100 mbit at least.
348 * TODO: ask the network layer about the speed. But would require
349 * notification on change. */
350 bind->transfer_capability = 100;
Alexander Couzens6a161492020-07-12 13:45:50 +0200351 bind->send_vc = nsip_vc_sendmsg;
352 bind->free_vc = free_vc;
Alexander Couzens22f34712020-10-02 02:34:39 +0200353 bind->dump_vty = dump_vty;
Alexander Couzens6a161492020-07-12 13:45:50 +0200354 bind->nsi = nsi;
355
356 priv = bind->priv = talloc_zero(bind, struct priv_bind);
357 if (!priv) {
358 talloc_free(bind);
359 return -ENOSPC;
360 }
361 priv->fd.cb = nsip_fd_cb;
362 priv->fd.data = bind;
363 priv->addr = *local;
364 INIT_LLIST_HEAD(&bind->nsvc);
365
Alexander Couzens6a161492020-07-12 13:45:50 +0200366 rc = osmo_sock_init_osa_ofd(&priv->fd, SOCK_DGRAM, IPPROTO_UDP,
367 local, NULL,
368 OSMO_SOCK_F_BIND);
369 if (rc < 0) {
370 talloc_free(priv);
371 talloc_free(bind);
372 return rc;
373 }
374
375 if (dscp > 0) {
376 priv->dscp = dscp;
377
378 rc = setsockopt(priv->fd.fd, IPPROTO_IP, IP_TOS,
379 &dscp, sizeof(dscp));
380 if (rc < 0)
Harald Weltef2949742021-01-20 14:54:14 +0100381 LOGBIND(bind, LOGL_ERROR, "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
Alexander Couzens6a161492020-07-12 13:45:50 +0200382 dscp, rc, errno);
383 }
384
Pau Espin Pedrolaa83d412020-10-09 14:20:13 +0200385 llist_add(&bind->list, &nsi->binding);
Alexander Couzens6a161492020-07-12 13:45:50 +0200386 if (result)
387 *result = bind;
388
389 return 0;
390}
391
Harald Welte5bef2cc2020-09-18 22:33:24 +0200392/*! Create new NS-VC to a given remote address
393 * \param[in] bind the bind we want to connect
394 * \param[in] nse NS entity to be used for the new NS-VC
395 * \param[in] remote remote address to connect to
396 * \return pointer to newly-allocated and connected NS-VC; NULL on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200397struct gprs_ns2_vc *gprs_ns2_ip_bind_connect(struct gprs_ns2_vc_bind *bind,
398 struct gprs_ns2_nse *nse,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700399 const struct osmo_sockaddr *remote)
Alexander Couzens6a161492020-07-12 13:45:50 +0200400{
401 struct gprs_ns2_vc *nsvc;
402 struct priv_vc *priv;
Alexander Couzensd923cff2020-12-01 01:03:52 +0100403 enum gprs_ns2_vc_mode vc_mode;
Harald Welte603f4042020-11-29 17:39:19 +0100404 char *sockaddr_str;
405 char idbuf[64];
Alexander Couzens6a161492020-07-12 13:45:50 +0200406
Alexander Couzens55bc8692021-01-18 18:39:57 +0100407 OSMO_ASSERT(gprs_ns2_is_ip_bind(bind));
408
Alexander Couzensd923cff2020-12-01 01:03:52 +0100409 vc_mode = gprs_ns2_dialect_to_vc_mode(nse->dialect);
410 if ((int) vc_mode == -1) {
Harald Weltef2949742021-01-20 14:54:14 +0100411 LOGNSE(nse, LOGL_ERROR, "Can not derive vc mode from dialect %d. Maybe libosmocore is too old.\n",
412 nse->dialect);
Alexander Couzensd923cff2020-12-01 01:03:52 +0100413 return NULL;
414 }
415
Harald Welte603f4042020-11-29 17:39:19 +0100416 sockaddr_str = (char *)osmo_sockaddr_to_str(remote);
417 osmo_identifier_sanitize_buf(sockaddr_str, NULL, '_');
418 snprintf(idbuf, sizeof(idbuf), "%s-NSE%05u-remote-%s", gprs_ns2_lltype_str(nse->ll),
419 nse->nsei, sockaddr_str);
420 nsvc = ns2_vc_alloc(bind, nse, true, vc_mode, idbuf);
Alexander Couzensc06aa712020-11-18 23:56:45 +0100421 if (!nsvc)
422 return NULL;
423
Alexander Couzens6a161492020-07-12 13:45:50 +0200424 nsvc->priv = talloc_zero(bind, struct priv_vc);
425 if (!nsvc->priv) {
426 gprs_ns2_free_nsvc(nsvc);
427 return NULL;
428 }
429
430 priv = nsvc->priv;
431 priv->remote = *remote;
432
Alexander Couzens6a161492020-07-12 13:45:50 +0200433 return nsvc;
434}
435
Alexander Couzens979f5f52020-10-11 21:01:48 +0200436/*! Return the socket address of the local peer of a NS-VC.
437 * \param[in] nsvc NS-VC whose local peer we want to know
438 * \return address of the local peer; NULL in case of error */
439const struct osmo_sockaddr *gprs_ns2_ip_vc_local(const struct gprs_ns2_vc *nsvc)
440{
441 struct priv_bind *priv;
442
Alexander Couzens979f5f52020-10-11 21:01:48 +0200443 if (nsvc->bind->driver != &vc_driver_ip)
444 return NULL;
445
446 priv = nsvc->bind->priv;
447 return &priv->addr;
448}
449
Harald Welte5bef2cc2020-09-18 22:33:24 +0200450/*! Return the socket address of the remote peer of a NS-VC.
451 * \param[in] nsvc NS-VC whose remote peer we want to know
452 * \return address of the remote peer; NULL in case of error */
Alexander Couzensd33512b2020-10-11 21:42:11 +0200453const struct osmo_sockaddr *gprs_ns2_ip_vc_remote(const struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200454{
455 struct priv_vc *priv;
456
Alexander Couzensaac90162020-11-19 02:44:04 +0100457 if (nsvc->bind->driver != &vc_driver_ip)
Alexander Couzens6a161492020-07-12 13:45:50 +0200458 return NULL;
459
460 priv = nsvc->priv;
461 return &priv->remote;
462}
463
Alexander Couzensd420ea92020-10-12 01:11:05 +0200464/*! Compare the NS-VC with the given parameter
465 * \param[in] nsvc NS-VC to compare with
466 * \param[in] local The local address
467 * \param[in] remote The remote address
468 * \param[in] nsvci NS-VCI will only be used if the NS-VC in BLOCKRESET mode otherwise NS-VCI isn't applicable.
469 * \return true if the NS-VC has the same properties as given
470 */
471bool gprs_ns2_ip_vc_equal(const struct gprs_ns2_vc *nsvc,
472 const struct osmo_sockaddr *local,
473 const struct osmo_sockaddr *remote,
474 uint16_t nsvci)
475{
476 struct priv_vc *vpriv;
477 struct priv_bind *bpriv;
478
Alexander Couzensaac90162020-11-19 02:44:04 +0100479 if (nsvc->bind->driver != &vc_driver_ip)
Alexander Couzensd420ea92020-10-12 01:11:05 +0200480 return false;
481
482 vpriv = nsvc->priv;
483 bpriv = nsvc->bind->priv;
484
485 if (osmo_sockaddr_cmp(local, &bpriv->addr))
486 return false;
487
488 if (osmo_sockaddr_cmp(remote, &vpriv->remote))
489 return false;
490
491 if (nsvc->mode == NS2_VC_MODE_BLOCKRESET)
492 if (nsvc->nsvci != nsvci)
493 return false;
494
495 return true;
496}
497
Harald Welte5bef2cc2020-09-18 22:33:24 +0200498/*! Return the locally bound socket address of the bind.
499 * \param[in] bind The bind whose local address we want to know
500 * \return address of the local bind */
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200501const struct osmo_sockaddr *gprs_ns2_ip_bind_sockaddr(struct gprs_ns2_vc_bind *bind)
Alexander Couzens6a161492020-07-12 13:45:50 +0200502{
503 struct priv_bind *priv;
Alexander Couzens55bc8692021-01-18 18:39:57 +0100504 OSMO_ASSERT(gprs_ns2_is_ip_bind(bind));
Alexander Couzens6a161492020-07-12 13:45:50 +0200505
506 priv = bind->priv;
507 return &priv->addr;
508}
509
Harald Welte5bef2cc2020-09-18 22:33:24 +0200510/*! Is the given bind an IP bind? */
Alexander Couzens6a161492020-07-12 13:45:50 +0200511int gprs_ns2_is_ip_bind(struct gprs_ns2_vc_bind *bind)
512{
513 return (bind->driver == &vc_driver_ip);
514}
515
Harald Welte5bef2cc2020-09-18 22:33:24 +0200516/*! Set the DSCP (TOS) bit value of the given bind. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200517int gprs_ns2_ip_bind_set_dscp(struct gprs_ns2_vc_bind *bind, int dscp)
518{
519 struct priv_bind *priv;
520 int rc = 0;
521
Alexander Couzens55bc8692021-01-18 18:39:57 +0100522 OSMO_ASSERT(gprs_ns2_is_ip_bind(bind));
Alexander Couzens6a161492020-07-12 13:45:50 +0200523 priv = bind->priv;
524
525 if (dscp != priv->dscp) {
526 priv->dscp = dscp;
527
528 rc = setsockopt(priv->fd.fd, IPPROTO_IP, IP_TOS,
529 &dscp, sizeof(dscp));
Harald Weltef2949742021-01-20 14:54:14 +0100530 if (rc < 0) {
531 LOGBIND(bind, LOGL_ERROR, "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
532 dscp, rc, errno);
533 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200534 }
535
536 return rc;
537}
Alexander Couzense769f522020-12-07 07:37:07 +0100538
539/*! Count UDP binds compatible with remote */
540int ns2_ip_count_bind(struct gprs_ns2_inst *nsi, struct osmo_sockaddr *remote)
541{
542 struct gprs_ns2_vc_bind *bind;
543 const struct osmo_sockaddr *sa;
544 int count = 0;
545
546 llist_for_each_entry(bind, &nsi->binding, list) {
547 if (!gprs_ns2_is_ip_bind(bind))
548 continue;
549
550 sa = gprs_ns2_ip_bind_sockaddr(bind);
551 if (!sa)
552 continue;
553
554 if (sa->u.sa.sa_family == remote->u.sa.sa_family)
555 count++;
556 }
557
558 return count;
559}
560
561/* return the matching bind by index */
562struct gprs_ns2_vc_bind *ns2_ip_get_bind_by_index(struct gprs_ns2_inst *nsi,
563 struct osmo_sockaddr *remote,
564 int index)
565{
566 struct gprs_ns2_vc_bind *bind;
567 const struct osmo_sockaddr *sa;
568 int i = 0;
569
570 llist_for_each_entry(bind, &nsi->binding, list) {
571 if (!gprs_ns2_is_ip_bind(bind))
572 continue;
573
574 sa = gprs_ns2_ip_bind_sockaddr(bind);
575 if (!sa)
576 continue;
577
578 if (sa->u.sa.sa_family == remote->u.sa.sa_family) {
579 if (index == i)
580 return bind;
581 i++;
582 }
583 }
584
585 return NULL;
586}