blob: 0e4e2d36f883d18815414fde945d932816b8c001 [file] [log] [blame]
Alexander Couzens841817e2020-11-19 00:41:29 +01001/*! \file gprs_ns2_fr.c
2 * NS-over-FR-over-GRE 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) 2009-2010,2014,2017 by Harald Welte <laforge@gnumonks.org>
8 * (C) 2020 sysmocom - s.f.m.c. GmbH
9 * Author: Alexander Couzens <lynxis@fe80.eu>
10 *
11 * All Rights Reserved
12 *
13 * SPDX-License-Identifier: GPL-2.0+
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 *
28 */
29
30#include <errno.h>
31#include <string.h>
32#include <unistd.h>
33
34#include <sys/socket.h>
35#include <netinet/in.h>
36#include <netinet/ip.h>
37#include <netinet/ip6.h>
38#include <arpa/inet.h>
39#include <net/if.h>
40
41#include <sys/ioctl.h>
42#include <netpacket/packet.h>
43#include <linux/if_ether.h>
44#include <linux/hdlc.h>
Alexander Couzens841817e2020-11-19 00:41:29 +010045
46#include <osmocom/gprs/frame_relay.h>
47#include <osmocom/core/byteswap.h>
48#include <osmocom/core/logging.h>
49#include <osmocom/core/msgb.h>
50#include <osmocom/core/select.h>
51#include <osmocom/core/socket.h>
52#include <osmocom/core/talloc.h>
53#include <osmocom/gprs/gprs_ns2.h>
54
Pau Espin Pedrold41800c2020-12-07 13:35:24 +010055#ifdef ENABLE_LIBMNL
56#include <osmocom/core/mnl.h>
57#endif
58
Harald Welte56f08a32020-12-01 23:07:32 +010059#include "config.h"
Alexander Couzens841817e2020-11-19 00:41:29 +010060#include "common_vty.h"
61#include "gprs_ns2_internal.h"
62
63#define GRE_PTYPE_FR 0x6559
64#define GRE_PTYPE_IPv4 0x0800
65#define GRE_PTYPE_IPv6 0x86dd
66#define GRE_PTYPE_KAR 0x0000 /* keepalive response */
67
68#ifndef IPPROTO_GRE
69# define IPPROTO_GRE 47
70#endif
71
72struct gre_hdr {
73 uint16_t flags;
74 uint16_t ptype;
75} __attribute__ ((packed));
76
77static void free_bind(struct gprs_ns2_vc_bind *bind);
78static int fr_dlci_rx_cb(void *cb_data, struct msgb *msg);
79
80struct gprs_ns2_vc_driver vc_driver_fr = {
81 .name = "GB frame relay",
82 .free_bind = free_bind,
83};
84
85struct priv_bind {
86 struct osmo_fd fd;
87 char netif[IF_NAMESIZE];
88 struct osmo_fr_link *link;
Harald Welte41b188b2020-12-10 22:00:23 +010089 int ifindex;
Harald Welte56f08a32020-12-01 23:07:32 +010090 bool if_running;
Alexander Couzens841817e2020-11-19 00:41:29 +010091};
92
93struct priv_vc {
94 struct osmo_sockaddr remote;
95 uint16_t dlci;
96 struct osmo_fr_dlc *dlc;
97};
98
99static void free_vc(struct gprs_ns2_vc *nsvc)
100{
101 OSMO_ASSERT(nsvc);
102
103 if (!nsvc->priv)
104 return;
105
106 talloc_free(nsvc->priv);
107 nsvc->priv = NULL;
108}
109
110static void dump_vty(const struct gprs_ns2_vc_bind *bind, struct vty *vty, bool _stats)
111{
112 struct priv_bind *priv;
113 struct gprs_ns2_vc *nsvc;
Harald Welte48bd76c2020-12-01 16:53:06 +0100114 struct osmo_fr_link *fr_link;
Alexander Couzens841817e2020-11-19 00:41:29 +0100115
116 if (!bind)
117 return;
118
119 priv = bind->priv;
Harald Welte48bd76c2020-12-01 16:53:06 +0100120 fr_link = priv->link;
Alexander Couzens841817e2020-11-19 00:41:29 +0100121
Harald Welte56f08a32020-12-01 23:07:32 +0100122 vty_out(vty, "FR bind: %s, role: %s, link: %s%s", priv->netif,
123 osmo_fr_role_str(fr_link->role), priv->if_running ? "UP" : "DOWN", VTY_NEWLINE);
Alexander Couzens841817e2020-11-19 00:41:29 +0100124
125 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
Harald Welte96ec84a2020-12-01 17:56:05 +0100126 vty_out(vty, " NSVCI %05u: %s%s", nsvc->nsvci, gprs_ns2_ll_str(nsvc), VTY_NEWLINE);
Alexander Couzens841817e2020-11-19 00:41:29 +0100127 }
128
129 priv = bind->priv;
130}
131
132/*! clean up all private driver state. Should be only called by gprs_ns2_free_bind() */
133static void free_bind(struct gprs_ns2_vc_bind *bind)
134{
135 struct priv_bind *priv;
136
137 if (!bind)
138 return;
139
140 priv = bind->priv;
141
142 OSMO_ASSERT(llist_empty(&bind->nsvc));
143
144 osmo_fr_link_free(priv->link);
145 osmo_fd_close(&priv->fd);
146 talloc_free(priv);
147}
148
149static struct priv_vc *fr_alloc_vc(struct gprs_ns2_vc_bind *bind,
150 struct gprs_ns2_vc *nsvc,
151 uint16_t dlci)
152{
153 struct priv_bind *privb = bind->priv;
154 struct priv_vc *priv = talloc_zero(bind, struct priv_vc);
155 if (!priv)
156 return NULL;
157
158 nsvc->priv = priv;
159 priv->dlci = dlci;
160 priv->dlc = osmo_fr_dlc_alloc(privb->link, dlci);
161 if (!priv->dlc) {
162 nsvc->priv = NULL;
163 talloc_free(priv);
164 return NULL;
165 }
166
167 priv->dlc->rx_cb_data = nsvc;
168 priv->dlc->rx_cb = fr_dlci_rx_cb;
169
170 return priv;
171}
172
173int gprs_ns2_find_vc_by_dlci(struct gprs_ns2_vc_bind *bind,
174 uint16_t dlci,
175 struct gprs_ns2_vc **result)
176{
177 struct gprs_ns2_vc *nsvc;
178 struct priv_vc *vcpriv;
179
180 if (!result)
181 return -EINVAL;
182
183 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
184 vcpriv = nsvc->priv;
185 if (vcpriv->dlci != dlci) {
186 *result = nsvc;
187 return 0;
188 }
189 }
190
191 return 1;
192}
193
194/* PDU from the network interface towards the fr layer (upwards) */
195static int handle_netif_read(struct osmo_fd *bfd)
196{
197 struct gprs_ns2_vc_bind *bind = bfd->data;
198 struct priv_bind *priv = bind->priv;
199 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Gb/NS/FR/GRE Rx");
Harald Welte41b188b2020-12-10 22:00:23 +0100200 struct sockaddr_ll sll;
201 socklen_t sll_len = sizeof(sll);
Alexander Couzens841817e2020-11-19 00:41:29 +0100202 int rc = 0;
203
204 if (!msg)
205 return -ENOMEM;
206
Harald Welte41b188b2020-12-10 22:00:23 +0100207 rc = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0, (struct sockaddr *)&sll, &sll_len);
Alexander Couzens841817e2020-11-19 00:41:29 +0100208 if (rc < 0) {
209 LOGP(DLNS, LOGL_ERROR, "recv error %s during NS-FR-GRE recv\n",
210 strerror(errno));
211 goto out_err;
212 } else if (rc == 0) {
213 goto out_err;
214 }
215
Harald Welte41b188b2020-12-10 22:00:23 +0100216 /* ignore any packets that we might have received for a different interface, between
217 * the socket() and the bind() call */
218 if (sll.sll_ifindex != priv->ifindex)
219 goto out_err;
220
Alexander Couzens841817e2020-11-19 00:41:29 +0100221 msgb_put(msg, rc);
222 msg->dst = priv->link;
223 return osmo_fr_rx(msg);
224
225out_err:
226 msgb_free(msg);
227 return rc;
228}
229
230/* PDU from the frame relay towards the NS-VC (upwards) */
231static int fr_dlci_rx_cb(void *cb_data, struct msgb *msg)
232{
233 int rc;
234 struct gprs_ns2_vc *nsvc = cb_data;
235
236 rc = ns2_recv_vc(nsvc, msg);
237
238 return rc;
239}
240
241static int handle_netif_write(struct osmo_fd *bfd)
242{
243 /* FIXME */
244 return -EIO;
245}
246
247static int fr_fd_cb(struct osmo_fd *bfd, unsigned int what)
248{
249 int rc = 0;
250
251 if (what & OSMO_FD_READ)
252 rc = handle_netif_read(bfd);
253 if (what & OSMO_FD_WRITE)
254 rc = handle_netif_write(bfd);
255
256 return rc;
257}
258
259/*! determine if given bind is for FR-GRE encapsulation. */
260int gprs_ns2_is_fr_bind(struct gprs_ns2_vc_bind *bind)
261{
262 return (bind->driver == &vc_driver_fr);
263}
264
265/* PDU from the NS-VC towards the frame relay layer (downwards) */
266static int fr_vc_sendmsg(struct gprs_ns2_vc *nsvc, struct msgb *msg)
267{
268 struct priv_vc *vcpriv = nsvc->priv;
269
270 msg->dst = vcpriv->dlc;
271 return osmo_fr_tx_dlc(msg);
272}
273
274/* PDU from the frame relay layer towards the network interface (downwards) */
275int fr_tx_cb(void *data, struct msgb *msg)
276{
277 struct gprs_ns2_vc_bind *bind = data;
278 struct priv_bind *priv = bind->priv;
279 int rc;
280
281 /* FIXME half writes */
282 rc = write(priv->fd.fd, msg->data, msg->len);
283 msgb_free(msg);
284
285 return rc;
286}
287
288static int devname2ifindex(const char *ifname)
289{
290 struct ifreq ifr;
291 int sk, rc;
292
293 sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
294 if (sk < 0)
295 return sk;
296
297
298 memset(&ifr, 0, sizeof(ifr));
299 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
300 ifr.ifr_name[sizeof(ifr.ifr_name)-1] = 0;
301
302 rc = ioctl(sk, SIOCGIFINDEX, &ifr);
303 close(sk);
304 if (rc < 0)
305 return rc;
306
307 return ifr.ifr_ifindex;
308}
309
Harald Welte41b188b2020-12-10 22:00:23 +0100310static int open_socket(int ifindex)
Alexander Couzens841817e2020-11-19 00:41:29 +0100311{
312 struct sockaddr_ll addr;
Harald Welte4ed0f4e2020-12-10 21:50:32 +0100313 int fd, rc;
Alexander Couzens841817e2020-11-19 00:41:29 +0100314
Alexander Couzens841817e2020-11-19 00:41:29 +0100315 memset(&addr, 0, sizeof(addr));
316 addr.sll_family = AF_PACKET;
317 addr.sll_protocol = htons(ETH_P_ALL);
318 addr.sll_ifindex = ifindex;
319
320 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
321 if (fd < 0) {
Harald Welte41b188b2020-12-10 22:00:23 +0100322 LOGP(DLNS, LOGL_ERROR, "Can not create AF_PACKET socket. Are you root or have CAP_RAW_SOCKET?\n");
Alexander Couzens841817e2020-11-19 00:41:29 +0100323 return fd;
324 }
325
Harald Welte41b188b2020-12-10 22:00:23 +0100326 /* there's a race condition between the above syscall and the bind() call below,
327 * causing other packets to be received in between */
328
Alexander Couzens841817e2020-11-19 00:41:29 +0100329 rc = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
330 if (rc < 0) {
Harald Welte41b188b2020-12-10 22:00:23 +0100331 LOGP(DLNS, LOGL_ERROR, "Can not bind AF_PACKET socket to ifindex %d\n", ifindex);
Alexander Couzens841817e2020-11-19 00:41:29 +0100332 close(fd);
333 return rc;
334 }
335
336 return fd;
337}
338
Harald Welte56f08a32020-12-01 23:07:32 +0100339#ifdef ENABLE_LIBMNL
340
341#include <osmocom/core/mnl.h>
Harald Welte56f08a32020-12-01 23:07:32 +0100342#include <linux/if_link.h>
343#include <linux/rtnetlink.h>
344
345#ifndef ARPHRD_FRAD
346#define ARPHRD_FRAD 770
347#endif
348
349/* validate the netlink attributes */
350static int data_attr_cb(const struct nlattr *attr, void *data)
351{
352 const struct nlattr **tb = data;
353 int type = mnl_attr_get_type(attr);
354
355 if (mnl_attr_type_valid(attr, IFLA_MAX) < 0)
356 return MNL_CB_OK;
357
358 switch (type) {
359 case IFLA_MTU:
360 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
361 return MNL_CB_ERROR;
362 break;
363 case IFLA_IFNAME:
364 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
365 return MNL_CB_ERROR;
366 break;
367 }
368 tb[type] = attr;
369 return MNL_CB_OK;
370}
371
372/* find the bind for the netdev (if any) */
373static struct gprs_ns2_vc_bind *bind4netdev(struct gprs_ns2_inst *nsi, const char *ifname)
374{
375 struct gprs_ns2_vc_bind *bind;
376
377 llist_for_each_entry(bind, &nsi->binding, list) {
378 struct priv_bind *bpriv = bind->priv;
379 if (!strcmp(bpriv->netif, ifname))
380 return bind;
381 }
382
383 return NULL;
384}
385
386/* handle a single netlink message received via libmnl */
387static int linkmon_mnl_cb(const struct nlmsghdr *nlh, void *data)
388{
389 struct osmo_mnl *omnl = data;
390 struct gprs_ns2_vc_bind *bind;
391 struct nlattr *tb[IFLA_MAX+1] = {};
392 struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
393 struct gprs_ns2_inst *nsi;
394 const char *ifname;
395 bool if_running;
396
397 OSMO_ASSERT(omnl);
398 OSMO_ASSERT(ifm);
399
400 nsi = omnl->priv;
401
402 if (ifm->ifi_type != ARPHRD_FRAD)
403 return MNL_CB_OK;
404
405 mnl_attr_parse(nlh, sizeof(*ifm), data_attr_cb, tb);
406
407 if (!tb[IFLA_IFNAME])
408 return MNL_CB_OK;
409 ifname = mnl_attr_get_str(tb[IFLA_IFNAME]);
410 if_running = !!(ifm->ifi_flags & IFF_RUNNING);
411
412 bind = bind4netdev(nsi, ifname);
413 if (bind) {
414 struct priv_bind *bpriv = bind->priv;
415 if (bpriv->if_running != if_running) {
416 /* update running state */
417 LOGP(DLNS, LOGL_NOTICE, "FR net-device '%s': Physical link state changed: %s\n",
418 ifname, if_running ? "UP" : "DOWN");
419 bpriv->if_running = if_running;
420 }
421 }
422
423 return MNL_CB_OK;
424}
425
426/* trigger one initial dump of all link information */
427static void linkmon_initial_dump(struct osmo_mnl *omnl)
428{
429 char buf[MNL_SOCKET_BUFFER_SIZE];
430 struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
431 struct rtgenmsg *rt;
432
433 nlh->nlmsg_type = RTM_GETLINK;
434 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
435 nlh->nlmsg_seq = time(NULL);
436 rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg));
437 rt->rtgen_family = AF_PACKET;
438
439 if (mnl_socket_sendto(omnl->mnls, nlh, nlh->nlmsg_len) < 0) {
440 LOGP(DLNS, LOGL_ERROR, "linkmon: Cannot send rtnetlink message: %s\n", strerror(errno));
441 }
442
443 /* the response[s] will be handled just like the events */
444}
445#endif /* LIBMNL */
446
447
Alexander Couzens841817e2020-11-19 00:41:29 +0100448/*! Create a new bind for NS over FR.
449 * \param[in] nsi NS instance in which to create the bind
450 * \param[in] netif Network interface to bind to
451 * \param[in] fr_network
452 * \param[in] fr_role
453 * \param[out] result pointer to created bind
454 * \return 0 on success; negative on error */
455int gprs_ns2_fr_bind(struct gprs_ns2_inst *nsi,
456 const char *netif,
457 struct osmo_fr_network *fr_network,
458 enum osmo_fr_role fr_role,
459 struct gprs_ns2_vc_bind **result)
460{
461 struct gprs_ns2_vc_bind *bind = talloc_zero(nsi, struct gprs_ns2_vc_bind);
462 struct priv_bind *priv;
463 struct osmo_fr_link *fr_link;
464 int rc = 0;
465
466 if (!bind)
467 return -ENOSPC;
468
469 bind->driver = &vc_driver_fr;
Alexander Couzensaac90162020-11-19 02:44:04 +0100470 bind->ll = GPRS_NS2_LL_FR;
Alexander Couzens841817e2020-11-19 00:41:29 +0100471 bind->send_vc = fr_vc_sendmsg;
472 bind->free_vc = free_vc;
473 bind->dump_vty = dump_vty;
474 bind->nsi = nsi;
475 priv = bind->priv = talloc_zero(bind, struct priv_bind);
476 if (!priv) {
477 rc = -ENOSPC;
478 goto err_bind;
479 }
480
481 priv->fd.cb = fr_fd_cb;
482 priv->fd.data = bind;
483 if (strlen(netif) > IF_NAMESIZE) {
484 rc = -EINVAL;
485 goto err_priv;
486 }
487 strncpy(priv->netif, netif, sizeof(priv->netif));
488
489 ns2_vty_bind_apply(bind);
490 if (result)
491 *result = bind;
492
493 /* FIXME: move fd handling into socket.c */
494 fr_link = osmo_fr_link_alloc(fr_network, fr_role, netif);
495 if (!fr_link) {
496 rc = -EINVAL;
497 goto err_priv;
498 }
499
500 fr_link->tx_cb = fr_tx_cb;
501 fr_link->tx_cb_data = bind;
502 priv->link = fr_link;
Harald Welte41b188b2020-12-10 22:00:23 +0100503
504 priv->ifindex = devname2ifindex(netif);
505 if (priv->ifindex < 0) {
506 LOGP(DLNS, LOGL_ERROR, "Can not get interface index for interface %s\n", netif);
507 goto err_fr;
508 }
509
510 priv->fd.fd = rc = open_socket(priv->ifindex);
Alexander Couzens841817e2020-11-19 00:41:29 +0100511 if (rc < 0)
512 goto err_fr;
513
514 priv->fd.when = OSMO_FD_READ;
515 rc = osmo_fd_register(&priv->fd);
516 if (rc < 0)
517 goto err_fd;
518
519 INIT_LLIST_HEAD(&bind->nsvc);
520 llist_add(&bind->list, &nsi->binding);
521
Harald Welte56f08a32020-12-01 23:07:32 +0100522#ifdef ENABLE_LIBMNL
523 if (!nsi->linkmon_mnl)
524 nsi->linkmon_mnl = osmo_mnl_init(nsi, NETLINK_ROUTE, RTMGRP_LINK, linkmon_mnl_cb, nsi);
525
526 /* we get a new full dump after every bind. which is a bit excessive. But that's just once
527 * at start-up, so we can get away with it */
528 if (nsi->linkmon_mnl)
529 linkmon_initial_dump(nsi->linkmon_mnl);
530#endif
531
Alexander Couzens841817e2020-11-19 00:41:29 +0100532 return rc;
533
534err_fd:
535 close(priv->fd.fd);
536err_fr:
537 osmo_fr_link_free(fr_link);
538err_priv:
539 talloc_free(priv);
540err_bind:
541 talloc_free(bind);
542
543 return rc;
544}
545
546/*! Return the network interface of the bind
547 * \param[in] bind The bind
548 * \return the network interface
549 */
550const char *gprs_ns2_fr_bind_netif(struct gprs_ns2_vc_bind *bind)
551{
552 struct priv_bind *priv;
553
554 if (bind->driver != &vc_driver_fr)
555 return NULL;
556
557 priv = bind->priv;
558 return priv->netif;
559}
560
561/*! Find NS bind for a given network interface
562 * \param[in] nsi NS instance
563 * \param[in] netif the network interface to search for
564 * \return the bind or NULL if not found
565 */
566struct gprs_ns2_vc_bind *gprs_ns2_fr_bind_by_netif(
567 struct gprs_ns2_inst *nsi,
568 const char *netif)
569{
570 struct gprs_ns2_vc_bind *bind;
571 const char *_netif;
572
573 OSMO_ASSERT(nsi);
574 OSMO_ASSERT(netif);
575
576 llist_for_each_entry(bind, &nsi->binding, list) {
577 if (!gprs_ns2_is_fr_bind(bind))
578 continue;
579
580 _netif = gprs_ns2_fr_bind_netif(bind);
581 if (!strncmp(_netif, netif, IF_NAMESIZE))
582 return bind;
583 }
584
585 return NULL;
586}
587
588/*! Create, connect and activate a new FR-based NS-VC
589 * \param[in] bind bind in which the new NS-VC is to be created
590 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
591 * \param[in] dlci Data Link connection identifier
592 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
593struct gprs_ns2_vc *gprs_ns2_fr_connect(struct gprs_ns2_vc_bind *bind,
594 uint16_t nsei,
595 uint16_t nsvci,
596 uint16_t dlci)
597{
598 bool created_nse = false;
599 struct gprs_ns2_vc *nsvc = NULL;
600 struct priv_vc *priv = NULL;
601 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
602 if (!nse) {
Alexander Couzensaac90162020-11-19 02:44:04 +0100603 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_FR);
Alexander Couzens841817e2020-11-19 00:41:29 +0100604 if (!nse)
605 return NULL;
606 created_nse = true;
607 }
608
609 nsvc = gprs_ns2_fr_nsvc_by_dlci(bind, dlci);
610 if (nsvc) {
611 goto err_nse;
612 }
613
614 nsvc = ns2_vc_alloc(bind, nse, true);
615 if (!nsvc)
616 goto err_nse;
617
618 nsvc->priv = priv = fr_alloc_vc(bind, nsvc, dlci);
619 if (!priv)
620 goto err;
621
622 nsvc->nsvci = nsvci;
623 nsvc->nsvci_is_valid = true;
Alexander Couzens841817e2020-11-19 00:41:29 +0100624
625 gprs_ns2_vc_fsm_start(nsvc);
626
627 return nsvc;
628
629err:
630 gprs_ns2_free_nsvc(nsvc);
631err_nse:
632 if (created_nse)
633 gprs_ns2_free_nse(nse);
634
635 return NULL;
636}
637
638/*! Return the nsvc by dlci.
639 * \param[in] bind
640 * \param[in] dlci Data Link connection identifier
641 * \return the nsvc or NULL if not found
642 */
643struct gprs_ns2_vc *gprs_ns2_fr_nsvc_by_dlci(struct gprs_ns2_vc_bind *bind,
644 uint16_t dlci)
645{
646 struct gprs_ns2_vc *nsvc;
647 struct priv_vc *vcpriv;
648
649 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
650 vcpriv = nsvc->priv;
651
652 if (dlci == vcpriv->dlci)
653 return nsvc;
654 }
655
656 return NULL;
657}
658
659/*! Return the dlci of the nsvc
660 * \param[in] nsvc
661 * \return the dlci or 0 on error. 0 is not a valid dlci.
662 */
663uint16_t gprs_ns2_fr_nsvc_dlci(struct gprs_ns2_vc *nsvc)
664{
665 struct priv_vc *vcpriv;
666
667 if (!nsvc->bind)
668 return 0;
669
670 if (nsvc->bind->driver != &vc_driver_fr)
671 return 0;
672
673 vcpriv = nsvc->priv;
674 return vcpriv->dlci;
675}