blob: 9277f9aac65a55dc62f016dc2dadb5676de3e217 [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{
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100174 struct msgb *msg = ns2_msgb_alloc();
Alexander Couzens6a161492020-07-12 13:45:50 +0200175 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) {
Alexander Couzensba5a9922021-01-25 16:03:23 +0100234 case NS2_CS_FOUND:
Alexander Couzens6a161492020-07-12 13:45:50 +0200235 break;
Alexander Couzensba5a9922021-01-25 16:03:23 +0100236 case NS2_CS_ERROR:
237 case NS2_CS_SKIPPED:
Alexander Couzens6a161492020-07-12 13:45:50 +0200238 rc = 0;
Alexander Couzens13010122020-09-24 00:54:51 +0200239 goto out;
Alexander Couzensba5a9922021-01-25 16:03:23 +0100240 case NS2_CS_REJECTED:
Alexander Couzens6a161492020-07-12 13:45:50 +0200241 /* nsip_sendmsg will free reject */
Alexander Couzens772ca612020-09-24 16:19:02 +0200242 rc = nsip_sendmsg(bind, reject, &saddr);
243 goto out;
Alexander Couzensba5a9922021-01-25 16:03:23 +0100244 case NS2_CS_CREATED:
Alexander Couzens6a161492020-07-12 13:45:50 +0200245 ns2_driver_alloc_vc(bind, nsvc, &saddr);
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100246 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
Harald Weltec3aa8f92021-01-31 11:41:34 +0100318 if (local->u.sa.sa_family != AF_INET && local->u.sa.sa_family != AF_INET6)
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100319 return -EINVAL;
320
Alexander Couzens6090b1d2020-10-11 18:41:37 +0200321 bind = gprs_ns2_ip_bind_by_sockaddr(nsi, local);
322 if (bind) {
323 *result = bind;
324 return -EBUSY;
325 }
326
Harald Weltec3aa8f92021-01-31 11:41:34 +0100327 rc = ns2_bind_alloc(nsi, name, &bind);
328 if (rc < 0)
329 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200330
331 bind->driver = &vc_driver_ip;
Alexander Couzensaac90162020-11-19 02:44:04 +0100332 bind->ll = GPRS_NS2_LL_UDP;
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100333 /* expect 100 mbit at least.
334 * TODO: ask the network layer about the speed. But would require
335 * notification on change. */
336 bind->transfer_capability = 100;
Alexander Couzens6a161492020-07-12 13:45:50 +0200337 bind->send_vc = nsip_vc_sendmsg;
338 bind->free_vc = free_vc;
Alexander Couzens22f34712020-10-02 02:34:39 +0200339 bind->dump_vty = dump_vty;
Alexander Couzens6a161492020-07-12 13:45:50 +0200340
341 priv = bind->priv = talloc_zero(bind, struct priv_bind);
342 if (!priv) {
Harald Weltec3aa8f92021-01-31 11:41:34 +0100343 gprs_ns2_free_bind(bind);
Harald Weltebdfb8b92021-01-31 11:44:57 +0100344 return -ENOMEM;
Alexander Couzens6a161492020-07-12 13:45:50 +0200345 }
346 priv->fd.cb = nsip_fd_cb;
347 priv->fd.data = bind;
348 priv->addr = *local;
Alexander Couzens6a161492020-07-12 13:45:50 +0200349
Alexander Couzens6a161492020-07-12 13:45:50 +0200350 rc = osmo_sock_init_osa_ofd(&priv->fd, SOCK_DGRAM, IPPROTO_UDP,
351 local, NULL,
352 OSMO_SOCK_F_BIND);
353 if (rc < 0) {
Harald Weltec3aa8f92021-01-31 11:41:34 +0100354 gprs_ns2_free_bind(bind);
Alexander Couzens6a161492020-07-12 13:45:50 +0200355 return rc;
356 }
357
358 if (dscp > 0) {
359 priv->dscp = dscp;
360
361 rc = setsockopt(priv->fd.fd, IPPROTO_IP, IP_TOS,
362 &dscp, sizeof(dscp));
363 if (rc < 0)
Harald Weltef2949742021-01-20 14:54:14 +0100364 LOGBIND(bind, LOGL_ERROR, "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
Alexander Couzens6a161492020-07-12 13:45:50 +0200365 dscp, rc, errno);
366 }
367
Alexander Couzens6a161492020-07-12 13:45:50 +0200368 if (result)
369 *result = bind;
370
371 return 0;
372}
373
Harald Welte5bef2cc2020-09-18 22:33:24 +0200374/*! Create new NS-VC to a given remote address
375 * \param[in] bind the bind we want to connect
376 * \param[in] nse NS entity to be used for the new NS-VC
377 * \param[in] remote remote address to connect to
378 * \return pointer to newly-allocated and connected NS-VC; NULL on error */
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100379struct gprs_ns2_vc *ns2_ip_bind_connect(struct gprs_ns2_vc_bind *bind,
380 struct gprs_ns2_nse *nse,
381 const struct osmo_sockaddr *remote)
Alexander Couzens6a161492020-07-12 13:45:50 +0200382{
383 struct gprs_ns2_vc *nsvc;
384 struct priv_vc *priv;
Alexander Couzensd923cff2020-12-01 01:03:52 +0100385 enum gprs_ns2_vc_mode vc_mode;
Harald Welte603f4042020-11-29 17:39:19 +0100386 char *sockaddr_str;
387 char idbuf[64];
Alexander Couzens6a161492020-07-12 13:45:50 +0200388
Alexander Couzens55bc8692021-01-18 18:39:57 +0100389 OSMO_ASSERT(gprs_ns2_is_ip_bind(bind));
390
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100391 vc_mode = ns2_dialect_to_vc_mode(nse->dialect);
Alexander Couzensd923cff2020-12-01 01:03:52 +0100392 if ((int) vc_mode == -1) {
Harald Weltef2949742021-01-20 14:54:14 +0100393 LOGNSE(nse, LOGL_ERROR, "Can not derive vc mode from dialect %d. Maybe libosmocore is too old.\n",
394 nse->dialect);
Alexander Couzensd923cff2020-12-01 01:03:52 +0100395 return NULL;
396 }
397
Harald Welte603f4042020-11-29 17:39:19 +0100398 sockaddr_str = (char *)osmo_sockaddr_to_str(remote);
399 osmo_identifier_sanitize_buf(sockaddr_str, NULL, '_');
400 snprintf(idbuf, sizeof(idbuf), "%s-NSE%05u-remote-%s", gprs_ns2_lltype_str(nse->ll),
401 nse->nsei, sockaddr_str);
402 nsvc = ns2_vc_alloc(bind, nse, true, vc_mode, idbuf);
Alexander Couzensc06aa712020-11-18 23:56:45 +0100403 if (!nsvc)
404 return NULL;
405
Alexander Couzens6a161492020-07-12 13:45:50 +0200406 nsvc->priv = talloc_zero(bind, struct priv_vc);
407 if (!nsvc->priv) {
408 gprs_ns2_free_nsvc(nsvc);
409 return NULL;
410 }
411
412 priv = nsvc->priv;
413 priv->remote = *remote;
414
Alexander Couzens6a161492020-07-12 13:45:50 +0200415 return nsvc;
416}
417
Alexander Couzens979f5f52020-10-11 21:01:48 +0200418/*! Return the socket address of the local peer of a NS-VC.
419 * \param[in] nsvc NS-VC whose local peer we want to know
420 * \return address of the local peer; NULL in case of error */
421const struct osmo_sockaddr *gprs_ns2_ip_vc_local(const struct gprs_ns2_vc *nsvc)
422{
423 struct priv_bind *priv;
424
Alexander Couzens979f5f52020-10-11 21:01:48 +0200425 if (nsvc->bind->driver != &vc_driver_ip)
426 return NULL;
427
428 priv = nsvc->bind->priv;
429 return &priv->addr;
430}
431
Harald Welte5bef2cc2020-09-18 22:33:24 +0200432/*! Return the socket address of the remote peer of a NS-VC.
433 * \param[in] nsvc NS-VC whose remote peer we want to know
434 * \return address of the remote peer; NULL in case of error */
Alexander Couzensd33512b2020-10-11 21:42:11 +0200435const struct osmo_sockaddr *gprs_ns2_ip_vc_remote(const struct gprs_ns2_vc *nsvc)
Alexander Couzens6a161492020-07-12 13:45:50 +0200436{
437 struct priv_vc *priv;
438
Alexander Couzensaac90162020-11-19 02:44:04 +0100439 if (nsvc->bind->driver != &vc_driver_ip)
Alexander Couzens6a161492020-07-12 13:45:50 +0200440 return NULL;
441
442 priv = nsvc->priv;
443 return &priv->remote;
444}
445
Alexander Couzensd420ea92020-10-12 01:11:05 +0200446/*! Compare the NS-VC with the given parameter
447 * \param[in] nsvc NS-VC to compare with
448 * \param[in] local The local address
449 * \param[in] remote The remote address
450 * \param[in] nsvci NS-VCI will only be used if the NS-VC in BLOCKRESET mode otherwise NS-VCI isn't applicable.
451 * \return true if the NS-VC has the same properties as given
452 */
453bool gprs_ns2_ip_vc_equal(const struct gprs_ns2_vc *nsvc,
454 const struct osmo_sockaddr *local,
455 const struct osmo_sockaddr *remote,
456 uint16_t nsvci)
457{
458 struct priv_vc *vpriv;
459 struct priv_bind *bpriv;
460
Alexander Couzensaac90162020-11-19 02:44:04 +0100461 if (nsvc->bind->driver != &vc_driver_ip)
Alexander Couzensd420ea92020-10-12 01:11:05 +0200462 return false;
463
464 vpriv = nsvc->priv;
465 bpriv = nsvc->bind->priv;
466
467 if (osmo_sockaddr_cmp(local, &bpriv->addr))
468 return false;
469
470 if (osmo_sockaddr_cmp(remote, &vpriv->remote))
471 return false;
472
Alexander Couzens138b96f2021-01-25 16:23:29 +0100473 if (nsvc->mode == GPRS_NS2_VC_MODE_BLOCKRESET)
Alexander Couzensd420ea92020-10-12 01:11:05 +0200474 if (nsvc->nsvci != nsvci)
475 return false;
476
477 return true;
478}
479
Harald Welte5bef2cc2020-09-18 22:33:24 +0200480/*! Return the locally bound socket address of the bind.
481 * \param[in] bind The bind whose local address we want to know
482 * \return address of the local bind */
Alexander Couzens9a4cf272020-10-11 20:48:04 +0200483const struct osmo_sockaddr *gprs_ns2_ip_bind_sockaddr(struct gprs_ns2_vc_bind *bind)
Alexander Couzens6a161492020-07-12 13:45:50 +0200484{
485 struct priv_bind *priv;
Alexander Couzens55bc8692021-01-18 18:39:57 +0100486 OSMO_ASSERT(gprs_ns2_is_ip_bind(bind));
Alexander Couzens6a161492020-07-12 13:45:50 +0200487
488 priv = bind->priv;
489 return &priv->addr;
490}
491
Harald Welte5bef2cc2020-09-18 22:33:24 +0200492/*! Is the given bind an IP bind? */
Alexander Couzens6a161492020-07-12 13:45:50 +0200493int gprs_ns2_is_ip_bind(struct gprs_ns2_vc_bind *bind)
494{
495 return (bind->driver == &vc_driver_ip);
496}
497
Harald Welte5bef2cc2020-09-18 22:33:24 +0200498/*! Set the DSCP (TOS) bit value of the given bind. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200499int gprs_ns2_ip_bind_set_dscp(struct gprs_ns2_vc_bind *bind, int dscp)
500{
501 struct priv_bind *priv;
502 int rc = 0;
503
Alexander Couzens55bc8692021-01-18 18:39:57 +0100504 OSMO_ASSERT(gprs_ns2_is_ip_bind(bind));
Alexander Couzens6a161492020-07-12 13:45:50 +0200505 priv = bind->priv;
506
507 if (dscp != priv->dscp) {
508 priv->dscp = dscp;
509
510 rc = setsockopt(priv->fd.fd, IPPROTO_IP, IP_TOS,
511 &dscp, sizeof(dscp));
Harald Weltef2949742021-01-20 14:54:14 +0100512 if (rc < 0) {
513 LOGBIND(bind, LOGL_ERROR, "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
514 dscp, rc, errno);
515 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200516 }
517
518 return rc;
519}
Alexander Couzense769f522020-12-07 07:37:07 +0100520
521/*! Count UDP binds compatible with remote */
522int ns2_ip_count_bind(struct gprs_ns2_inst *nsi, struct osmo_sockaddr *remote)
523{
524 struct gprs_ns2_vc_bind *bind;
525 const struct osmo_sockaddr *sa;
526 int count = 0;
527
528 llist_for_each_entry(bind, &nsi->binding, list) {
529 if (!gprs_ns2_is_ip_bind(bind))
530 continue;
531
532 sa = gprs_ns2_ip_bind_sockaddr(bind);
533 if (!sa)
534 continue;
535
536 if (sa->u.sa.sa_family == remote->u.sa.sa_family)
537 count++;
538 }
539
540 return count;
541}
542
543/* return the matching bind by index */
544struct gprs_ns2_vc_bind *ns2_ip_get_bind_by_index(struct gprs_ns2_inst *nsi,
545 struct osmo_sockaddr *remote,
546 int index)
547{
548 struct gprs_ns2_vc_bind *bind;
549 const struct osmo_sockaddr *sa;
550 int i = 0;
551
552 llist_for_each_entry(bind, &nsi->binding, list) {
553 if (!gprs_ns2_is_ip_bind(bind))
554 continue;
555
556 sa = gprs_ns2_ip_bind_sockaddr(bind);
557 if (!sa)
558 continue;
559
560 if (sa->u.sa.sa_family == remote->u.sa.sa_family) {
561 if (index == i)
562 return bind;
563 i++;
564 }
565 }
566
567 return NULL;
568}