blob: 6b4fa52e972bb8f216786cb4745f2dbb9f7f05ac [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
Harald Welte5bea72e2020-12-10 22:06:21 +0100320 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_HDLC));
Alexander Couzens841817e2020-11-19 00:41:29 +0100321 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
Alexander Couzens841817e2020-11-19 00:41:29 +0100489 if (result)
490 *result = bind;
491
492 /* FIXME: move fd handling into socket.c */
493 fr_link = osmo_fr_link_alloc(fr_network, fr_role, netif);
494 if (!fr_link) {
495 rc = -EINVAL;
496 goto err_priv;
497 }
498
499 fr_link->tx_cb = fr_tx_cb;
500 fr_link->tx_cb_data = bind;
501 priv->link = fr_link;
Harald Welte41b188b2020-12-10 22:00:23 +0100502
503 priv->ifindex = devname2ifindex(netif);
504 if (priv->ifindex < 0) {
505 LOGP(DLNS, LOGL_ERROR, "Can not get interface index for interface %s\n", netif);
506 goto err_fr;
507 }
508
509 priv->fd.fd = rc = open_socket(priv->ifindex);
Alexander Couzens841817e2020-11-19 00:41:29 +0100510 if (rc < 0)
511 goto err_fr;
512
513 priv->fd.when = OSMO_FD_READ;
514 rc = osmo_fd_register(&priv->fd);
515 if (rc < 0)
516 goto err_fd;
517
518 INIT_LLIST_HEAD(&bind->nsvc);
519 llist_add(&bind->list, &nsi->binding);
520
Harald Welte56f08a32020-12-01 23:07:32 +0100521#ifdef ENABLE_LIBMNL
522 if (!nsi->linkmon_mnl)
523 nsi->linkmon_mnl = osmo_mnl_init(nsi, NETLINK_ROUTE, RTMGRP_LINK, linkmon_mnl_cb, nsi);
524
525 /* we get a new full dump after every bind. which is a bit excessive. But that's just once
526 * at start-up, so we can get away with it */
527 if (nsi->linkmon_mnl)
528 linkmon_initial_dump(nsi->linkmon_mnl);
529#endif
530
Alexander Couzens841817e2020-11-19 00:41:29 +0100531 return rc;
532
533err_fd:
534 close(priv->fd.fd);
535err_fr:
536 osmo_fr_link_free(fr_link);
537err_priv:
538 talloc_free(priv);
539err_bind:
540 talloc_free(bind);
541
542 return rc;
543}
544
545/*! Return the network interface of the bind
546 * \param[in] bind The bind
547 * \return the network interface
548 */
549const char *gprs_ns2_fr_bind_netif(struct gprs_ns2_vc_bind *bind)
550{
551 struct priv_bind *priv;
552
553 if (bind->driver != &vc_driver_fr)
554 return NULL;
555
556 priv = bind->priv;
557 return priv->netif;
558}
559
560/*! Find NS bind for a given network interface
561 * \param[in] nsi NS instance
562 * \param[in] netif the network interface to search for
563 * \return the bind or NULL if not found
564 */
565struct gprs_ns2_vc_bind *gprs_ns2_fr_bind_by_netif(
566 struct gprs_ns2_inst *nsi,
567 const char *netif)
568{
569 struct gprs_ns2_vc_bind *bind;
570 const char *_netif;
571
572 OSMO_ASSERT(nsi);
573 OSMO_ASSERT(netif);
574
575 llist_for_each_entry(bind, &nsi->binding, list) {
576 if (!gprs_ns2_is_fr_bind(bind))
577 continue;
578
579 _netif = gprs_ns2_fr_bind_netif(bind);
580 if (!strncmp(_netif, netif, IF_NAMESIZE))
581 return bind;
582 }
583
584 return NULL;
585}
586
587/*! Create, connect and activate a new FR-based NS-VC
588 * \param[in] bind bind in which the new NS-VC is to be created
589 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
590 * \param[in] dlci Data Link connection identifier
591 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
592struct gprs_ns2_vc *gprs_ns2_fr_connect(struct gprs_ns2_vc_bind *bind,
593 uint16_t nsei,
594 uint16_t nsvci,
595 uint16_t dlci)
596{
597 bool created_nse = false;
598 struct gprs_ns2_vc *nsvc = NULL;
599 struct priv_vc *priv = NULL;
600 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
601 if (!nse) {
Alexander Couzensd923cff2020-12-01 01:03:52 +0100602 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_FR, NS2_DIALECT_STATIC_RESETBLOCK);
Alexander Couzens841817e2020-11-19 00:41:29 +0100603 if (!nse)
604 return NULL;
605 created_nse = true;
606 }
607
608 nsvc = gprs_ns2_fr_nsvc_by_dlci(bind, dlci);
609 if (nsvc) {
610 goto err_nse;
611 }
612
Alexander Couzensd923cff2020-12-01 01:03:52 +0100613 nsvc = ns2_vc_alloc(bind, nse, true, NS2_VC_MODE_BLOCKRESET);
Alexander Couzens841817e2020-11-19 00:41:29 +0100614 if (!nsvc)
615 goto err_nse;
616
617 nsvc->priv = priv = fr_alloc_vc(bind, nsvc, dlci);
618 if (!priv)
619 goto err;
620
621 nsvc->nsvci = nsvci;
622 nsvc->nsvci_is_valid = true;
Alexander Couzens841817e2020-11-19 00:41:29 +0100623
624 gprs_ns2_vc_fsm_start(nsvc);
625
626 return nsvc;
627
628err:
629 gprs_ns2_free_nsvc(nsvc);
630err_nse:
631 if (created_nse)
632 gprs_ns2_free_nse(nse);
633
634 return NULL;
635}
636
637/*! Return the nsvc by dlci.
638 * \param[in] bind
639 * \param[in] dlci Data Link connection identifier
640 * \return the nsvc or NULL if not found
641 */
642struct gprs_ns2_vc *gprs_ns2_fr_nsvc_by_dlci(struct gprs_ns2_vc_bind *bind,
643 uint16_t dlci)
644{
645 struct gprs_ns2_vc *nsvc;
646 struct priv_vc *vcpriv;
647
648 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
649 vcpriv = nsvc->priv;
650
651 if (dlci == vcpriv->dlci)
652 return nsvc;
653 }
654
655 return NULL;
656}
657
658/*! Return the dlci of the nsvc
659 * \param[in] nsvc
660 * \return the dlci or 0 on error. 0 is not a valid dlci.
661 */
662uint16_t gprs_ns2_fr_nsvc_dlci(struct gprs_ns2_vc *nsvc)
663{
664 struct priv_vc *vcpriv;
665
666 if (!nsvc->bind)
667 return 0;
668
669 if (nsvc->bind->driver != &vc_driver_fr)
670 return 0;
671
672 vcpriv = nsvc->priv;
673 return vcpriv->dlci;
674}