blob: 1a6b92f7eba24dd739aa1ebc454b2026811095b5 [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>
Alexander Couzens5c96f5d2020-12-17 04:45:03 +010039#include <linux/if.h>
Alexander Couzens841817e2020-11-19 00:41:29 +010040
41#include <sys/ioctl.h>
42#include <netpacket/packet.h>
43#include <linux/if_ether.h>
44#include <linux/hdlc.h>
Alexander Couzens5c96f5d2020-12-17 04:45:03 +010045#include <linux/hdlc/ioctl.h>
46#include <linux/sockios.h>
Alexander Couzens841817e2020-11-19 00:41:29 +010047
48#include <osmocom/gprs/frame_relay.h>
49#include <osmocom/core/byteswap.h>
50#include <osmocom/core/logging.h>
51#include <osmocom/core/msgb.h>
52#include <osmocom/core/select.h>
53#include <osmocom/core/socket.h>
54#include <osmocom/core/talloc.h>
Alexander Couzens60021a42020-12-17 02:48:22 +010055#include <osmocom/core/write_queue.h>
Alexander Couzens841817e2020-11-19 00:41:29 +010056#include <osmocom/gprs/gprs_ns2.h>
57
Pau Espin Pedrold41800c2020-12-07 13:35:24 +010058#ifdef ENABLE_LIBMNL
59#include <osmocom/core/mnl.h>
60#endif
61
Harald Welte56f08a32020-12-01 23:07:32 +010062#include "config.h"
Alexander Couzens841817e2020-11-19 00:41:29 +010063#include "common_vty.h"
64#include "gprs_ns2_internal.h"
65
66#define GRE_PTYPE_FR 0x6559
67#define GRE_PTYPE_IPv4 0x0800
68#define GRE_PTYPE_IPv6 0x86dd
69#define GRE_PTYPE_KAR 0x0000 /* keepalive response */
70
71#ifndef IPPROTO_GRE
72# define IPPROTO_GRE 47
73#endif
74
75struct gre_hdr {
76 uint16_t flags;
77 uint16_t ptype;
78} __attribute__ ((packed));
79
80static void free_bind(struct gprs_ns2_vc_bind *bind);
81static int fr_dlci_rx_cb(void *cb_data, struct msgb *msg);
82
83struct gprs_ns2_vc_driver vc_driver_fr = {
84 .name = "GB frame relay",
85 .free_bind = free_bind,
86};
87
88struct priv_bind {
Alexander Couzens5c96f5d2020-12-17 04:45:03 +010089 char netif[IFNAMSIZ];
Alexander Couzens841817e2020-11-19 00:41:29 +010090 struct osmo_fr_link *link;
Alexander Couzens60021a42020-12-17 02:48:22 +010091 struct osmo_wqueue wqueue;
Harald Welte41b188b2020-12-10 22:00:23 +010092 int ifindex;
Harald Welte56f08a32020-12-01 23:07:32 +010093 bool if_running;
Alexander Couzens841817e2020-11-19 00:41:29 +010094};
95
96struct priv_vc {
97 struct osmo_sockaddr remote;
98 uint16_t dlci;
99 struct osmo_fr_dlc *dlc;
100};
101
102static void free_vc(struct gprs_ns2_vc *nsvc)
103{
104 OSMO_ASSERT(nsvc);
105
106 if (!nsvc->priv)
107 return;
108
109 talloc_free(nsvc->priv);
110 nsvc->priv = NULL;
111}
112
113static void dump_vty(const struct gprs_ns2_vc_bind *bind, struct vty *vty, bool _stats)
114{
115 struct priv_bind *priv;
116 struct gprs_ns2_vc *nsvc;
Harald Welte48bd76c2020-12-01 16:53:06 +0100117 struct osmo_fr_link *fr_link;
Alexander Couzens841817e2020-11-19 00:41:29 +0100118
119 if (!bind)
120 return;
121
122 priv = bind->priv;
Harald Welte48bd76c2020-12-01 16:53:06 +0100123 fr_link = priv->link;
Alexander Couzens841817e2020-11-19 00:41:29 +0100124
Harald Welte56f08a32020-12-01 23:07:32 +0100125 vty_out(vty, "FR bind: %s, role: %s, link: %s%s", priv->netif,
126 osmo_fr_role_str(fr_link->role), priv->if_running ? "UP" : "DOWN", VTY_NEWLINE);
Alexander Couzens841817e2020-11-19 00:41:29 +0100127
128 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
Harald Welte96ec84a2020-12-01 17:56:05 +0100129 vty_out(vty, " NSVCI %05u: %s%s", nsvc->nsvci, gprs_ns2_ll_str(nsvc), VTY_NEWLINE);
Alexander Couzens841817e2020-11-19 00:41:29 +0100130 }
131
132 priv = bind->priv;
133}
134
135/*! clean up all private driver state. Should be only called by gprs_ns2_free_bind() */
136static void free_bind(struct gprs_ns2_vc_bind *bind)
137{
138 struct priv_bind *priv;
139
140 if (!bind)
141 return;
142
143 priv = bind->priv;
144
145 OSMO_ASSERT(llist_empty(&bind->nsvc));
146
147 osmo_fr_link_free(priv->link);
Alexander Couzens60021a42020-12-17 02:48:22 +0100148 osmo_fd_close(&priv->wqueue.bfd);
Alexander Couzens841817e2020-11-19 00:41:29 +0100149 talloc_free(priv);
150}
151
152static struct priv_vc *fr_alloc_vc(struct gprs_ns2_vc_bind *bind,
153 struct gprs_ns2_vc *nsvc,
154 uint16_t dlci)
155{
156 struct priv_bind *privb = bind->priv;
157 struct priv_vc *priv = talloc_zero(bind, struct priv_vc);
158 if (!priv)
159 return NULL;
160
161 nsvc->priv = priv;
162 priv->dlci = dlci;
163 priv->dlc = osmo_fr_dlc_alloc(privb->link, dlci);
164 if (!priv->dlc) {
165 nsvc->priv = NULL;
166 talloc_free(priv);
167 return NULL;
168 }
169
170 priv->dlc->rx_cb_data = nsvc;
171 priv->dlc->rx_cb = fr_dlci_rx_cb;
172
173 return priv;
174}
175
176int gprs_ns2_find_vc_by_dlci(struct gprs_ns2_vc_bind *bind,
177 uint16_t dlci,
178 struct gprs_ns2_vc **result)
179{
180 struct gprs_ns2_vc *nsvc;
181 struct priv_vc *vcpriv;
182
183 if (!result)
184 return -EINVAL;
185
186 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
187 vcpriv = nsvc->priv;
188 if (vcpriv->dlci != dlci) {
189 *result = nsvc;
190 return 0;
191 }
192 }
193
194 return 1;
195}
196
197/* PDU from the network interface towards the fr layer (upwards) */
198static int handle_netif_read(struct osmo_fd *bfd)
199{
200 struct gprs_ns2_vc_bind *bind = bfd->data;
201 struct priv_bind *priv = bind->priv;
202 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Gb/NS/FR/GRE Rx");
Harald Welte41b188b2020-12-10 22:00:23 +0100203 struct sockaddr_ll sll;
204 socklen_t sll_len = sizeof(sll);
Alexander Couzens841817e2020-11-19 00:41:29 +0100205 int rc = 0;
206
207 if (!msg)
208 return -ENOMEM;
209
Harald Welte41b188b2020-12-10 22:00:23 +0100210 rc = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0, (struct sockaddr *)&sll, &sll_len);
Alexander Couzens841817e2020-11-19 00:41:29 +0100211 if (rc < 0) {
212 LOGP(DLNS, LOGL_ERROR, "recv error %s during NS-FR-GRE recv\n",
213 strerror(errno));
214 goto out_err;
215 } else if (rc == 0) {
216 goto out_err;
217 }
218
Harald Welte41b188b2020-12-10 22:00:23 +0100219 /* ignore any packets that we might have received for a different interface, between
220 * the socket() and the bind() call */
221 if (sll.sll_ifindex != priv->ifindex)
222 goto out_err;
223
Alexander Couzens841817e2020-11-19 00:41:29 +0100224 msgb_put(msg, rc);
225 msg->dst = priv->link;
226 return osmo_fr_rx(msg);
227
228out_err:
229 msgb_free(msg);
230 return rc;
231}
232
233/* PDU from the frame relay towards the NS-VC (upwards) */
234static int fr_dlci_rx_cb(void *cb_data, struct msgb *msg)
235{
236 int rc;
237 struct gprs_ns2_vc *nsvc = cb_data;
238
239 rc = ns2_recv_vc(nsvc, msg);
240
241 return rc;
242}
243
Alexander Couzens60021a42020-12-17 02:48:22 +0100244static int handle_netif_write(struct osmo_fd *ofd, struct msgb *msg)
Alexander Couzens841817e2020-11-19 00:41:29 +0100245{
Alexander Couzens60021a42020-12-17 02:48:22 +0100246 return write(ofd->fd, msgb_data(msg), msgb_length(msg));
Alexander Couzens841817e2020-11-19 00:41:29 +0100247}
248
249/*! determine if given bind is for FR-GRE encapsulation. */
250int gprs_ns2_is_fr_bind(struct gprs_ns2_vc_bind *bind)
251{
252 return (bind->driver == &vc_driver_fr);
253}
254
255/* PDU from the NS-VC towards the frame relay layer (downwards) */
256static int fr_vc_sendmsg(struct gprs_ns2_vc *nsvc, struct msgb *msg)
257{
258 struct priv_vc *vcpriv = nsvc->priv;
259
260 msg->dst = vcpriv->dlc;
261 return osmo_fr_tx_dlc(msg);
262}
263
264/* PDU from the frame relay layer towards the network interface (downwards) */
265int fr_tx_cb(void *data, struct msgb *msg)
266{
267 struct gprs_ns2_vc_bind *bind = data;
268 struct priv_bind *priv = bind->priv;
Alexander Couzens841817e2020-11-19 00:41:29 +0100269
Alexander Couzens60021a42020-12-17 02:48:22 +0100270 if (osmo_wqueue_enqueue(&priv->wqueue, msg)) {
271 LOGP(DLNS, LOGL_ERROR, "frame relay %s: failed to enqueue message\n",
272 priv->netif);
273 msgb_free(msg);
274 return -EINVAL;
275 }
Alexander Couzens841817e2020-11-19 00:41:29 +0100276
Alexander Couzens60021a42020-12-17 02:48:22 +0100277 return 0;
Alexander Couzens841817e2020-11-19 00:41:29 +0100278}
279
280static int devname2ifindex(const char *ifname)
281{
282 struct ifreq ifr;
283 int sk, rc;
284
285 sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
286 if (sk < 0)
287 return sk;
288
289
290 memset(&ifr, 0, sizeof(ifr));
Neels Hofmeyr475a0ac2020-12-17 18:10:34 +0100291 OSMO_STRLCPY_ARRAY(ifr.ifr_name, ifname);
Alexander Couzens841817e2020-11-19 00:41:29 +0100292
293 rc = ioctl(sk, SIOCGIFINDEX, &ifr);
294 close(sk);
295 if (rc < 0)
296 return rc;
297
298 return ifr.ifr_ifindex;
299}
300
Harald Welte41b188b2020-12-10 22:00:23 +0100301static int open_socket(int ifindex)
Alexander Couzens841817e2020-11-19 00:41:29 +0100302{
303 struct sockaddr_ll addr;
Harald Welte4ed0f4e2020-12-10 21:50:32 +0100304 int fd, rc;
Alexander Couzens841817e2020-11-19 00:41:29 +0100305
Alexander Couzens841817e2020-11-19 00:41:29 +0100306 memset(&addr, 0, sizeof(addr));
307 addr.sll_family = AF_PACKET;
308 addr.sll_protocol = htons(ETH_P_ALL);
309 addr.sll_ifindex = ifindex;
310
Harald Welte5bea72e2020-12-10 22:06:21 +0100311 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_HDLC));
Alexander Couzens841817e2020-11-19 00:41:29 +0100312 if (fd < 0) {
Harald Welte41b188b2020-12-10 22:00:23 +0100313 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 +0100314 return fd;
315 }
316
Harald Welte41b188b2020-12-10 22:00:23 +0100317 /* there's a race condition between the above syscall and the bind() call below,
318 * causing other packets to be received in between */
319
Alexander Couzens841817e2020-11-19 00:41:29 +0100320 rc = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
321 if (rc < 0) {
Harald Welte41b188b2020-12-10 22:00:23 +0100322 LOGP(DLNS, LOGL_ERROR, "Can not bind AF_PACKET socket to ifindex %d\n", ifindex);
Alexander Couzens841817e2020-11-19 00:41:29 +0100323 close(fd);
324 return rc;
325 }
326
327 return fd;
328}
329
Harald Welte56f08a32020-12-01 23:07:32 +0100330#ifdef ENABLE_LIBMNL
331
332#include <osmocom/core/mnl.h>
Harald Welte56f08a32020-12-01 23:07:32 +0100333#include <linux/if_link.h>
334#include <linux/rtnetlink.h>
335
336#ifndef ARPHRD_FRAD
337#define ARPHRD_FRAD 770
338#endif
339
340/* validate the netlink attributes */
341static int data_attr_cb(const struct nlattr *attr, void *data)
342{
343 const struct nlattr **tb = data;
344 int type = mnl_attr_get_type(attr);
345
346 if (mnl_attr_type_valid(attr, IFLA_MAX) < 0)
347 return MNL_CB_OK;
348
349 switch (type) {
350 case IFLA_MTU:
351 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
352 return MNL_CB_ERROR;
353 break;
354 case IFLA_IFNAME:
355 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
356 return MNL_CB_ERROR;
357 break;
358 }
359 tb[type] = attr;
360 return MNL_CB_OK;
361}
362
363/* find the bind for the netdev (if any) */
364static struct gprs_ns2_vc_bind *bind4netdev(struct gprs_ns2_inst *nsi, const char *ifname)
365{
366 struct gprs_ns2_vc_bind *bind;
367
368 llist_for_each_entry(bind, &nsi->binding, list) {
369 struct priv_bind *bpriv = bind->priv;
370 if (!strcmp(bpriv->netif, ifname))
371 return bind;
372 }
373
374 return NULL;
375}
376
377/* handle a single netlink message received via libmnl */
378static int linkmon_mnl_cb(const struct nlmsghdr *nlh, void *data)
379{
380 struct osmo_mnl *omnl = data;
381 struct gprs_ns2_vc_bind *bind;
382 struct nlattr *tb[IFLA_MAX+1] = {};
383 struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
384 struct gprs_ns2_inst *nsi;
385 const char *ifname;
386 bool if_running;
387
388 OSMO_ASSERT(omnl);
389 OSMO_ASSERT(ifm);
390
391 nsi = omnl->priv;
392
393 if (ifm->ifi_type != ARPHRD_FRAD)
394 return MNL_CB_OK;
395
396 mnl_attr_parse(nlh, sizeof(*ifm), data_attr_cb, tb);
397
398 if (!tb[IFLA_IFNAME])
399 return MNL_CB_OK;
400 ifname = mnl_attr_get_str(tb[IFLA_IFNAME]);
401 if_running = !!(ifm->ifi_flags & IFF_RUNNING);
402
403 bind = bind4netdev(nsi, ifname);
404 if (bind) {
405 struct priv_bind *bpriv = bind->priv;
406 if (bpriv->if_running != if_running) {
407 /* update running state */
408 LOGP(DLNS, LOGL_NOTICE, "FR net-device '%s': Physical link state changed: %s\n",
409 ifname, if_running ? "UP" : "DOWN");
410 bpriv->if_running = if_running;
411 }
412 }
413
414 return MNL_CB_OK;
415}
416
417/* trigger one initial dump of all link information */
418static void linkmon_initial_dump(struct osmo_mnl *omnl)
419{
420 char buf[MNL_SOCKET_BUFFER_SIZE];
421 struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
422 struct rtgenmsg *rt;
423
424 nlh->nlmsg_type = RTM_GETLINK;
425 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
426 nlh->nlmsg_seq = time(NULL);
427 rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg));
428 rt->rtgen_family = AF_PACKET;
429
430 if (mnl_socket_sendto(omnl->mnls, nlh, nlh->nlmsg_len) < 0) {
431 LOGP(DLNS, LOGL_ERROR, "linkmon: Cannot send rtnetlink message: %s\n", strerror(errno));
432 }
433
434 /* the response[s] will be handled just like the events */
435}
436#endif /* LIBMNL */
437
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100438static int set_ifupdown(const char *netif, bool up)
439{
440 int sock, rc;
441 struct ifreq req;
442
443 sock = socket(AF_INET, SOCK_DGRAM, 0);
444 if (sock < 0)
445 return sock;
446
447 memset(&req, 0, sizeof req);
Harald Welte7f01b682020-12-21 12:39:38 +0100448 OSMO_STRLCPY_ARRAY(req.ifr_name, netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100449
450 if (up)
451 req.ifr_flags |= IFF_UP;
452
453 rc = ioctl(sock, SIOCSIFFLAGS, &req);
454 close(sock);
455 return rc;
456}
457
458static int setup_device(const char *netif)
459{
460 int sock, rc;
461 char buffer[128];
462 fr_proto *fr = (void*)buffer;
463 struct ifreq req;
464
465 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
466 if (sock < 0) {
467 LOGP(DLNS, LOGL_ERROR, "%s: Unable to create socket: %s\n",
468 netif, strerror(errno));
469 return sock;
470 }
471
472 memset(&req, 0, sizeof(struct ifreq));
473 memset(&buffer, 0, sizeof(buffer));
Harald Welteb8de1882020-12-21 12:40:45 +0100474 OSMO_STRLCPY_ARRAY(req.ifr_name, netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100475 req.ifr_settings.ifs_ifsu.sync = (void*)buffer;
476 req.ifr_settings.size = sizeof(buffer);
477 req.ifr_settings.type = IF_GET_PROTO;
478
479 rc = ioctl(sock, SIOCWANDEV, &req);
480 if (rc < 0) {
481 LOGP(DLNS, LOGL_ERROR, "%s: Unable to get FR protocol information: %s\n",
482 netif, strerror(errno));
483 goto err;
484 }
485
486 /* check if the device is good */
487 if (req.ifr_settings.type != IF_PROTO_FR && fr->lmi != LMI_NONE) {
488 LOGP(DLNS, LOGL_INFO, "%s: has correct frame relay mode and lmi\n", netif);
489 goto ifup;
490 }
491
492 /* modify the device to match */
493 rc = set_ifupdown(netif, false);
494 if (rc) {
495 LOGP(DLNS, LOGL_ERROR, "Unable to bring up the device %s: %s\n",
496 netif, strerror(errno));
497 goto err;
498 }
499
500 memset(&req, 0, sizeof(struct ifreq));
501 memset(fr, 0, sizeof(fr_proto));
Harald Welteb8de1882020-12-21 12:40:45 +0100502 OSMO_STRLCPY_ARRAY(req.ifr_name, netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100503 req.ifr_settings.type = IF_PROTO_FR;
504 req.ifr_settings.size = sizeof(fr_proto);
505 req.ifr_settings.ifs_ifsu.fr = fr;
506 fr->lmi = LMI_NONE;
507 /* even those settings aren't used, they must be in the range */
508 /* polling verification timer*/
509 fr->t391 = 10;
510 /* link integrity verification polling timer */
511 fr->t392 = 15;
512 /* full status polling counter*/
513 fr->n391 = 6;
514 /* error threshold */
515 fr->n392 = 3;
516 /* monitored events count */
517 fr->n393 = 4;
518
519 rc = ioctl(sock, SIOCWANDEV, &req);
520 if (rc) {
521 LOGP(DLNS, LOGL_ERROR, "%s: Unable to set FR protocol on information: %s\n",
522 netif, strerror(errno));
523 goto err;
524 }
525
526ifup:
527 rc = set_ifupdown(netif, true);
528 if (rc)
529 LOGP(DLNS, LOGL_ERROR, "Unable to bring up the device %s: %s\n",
530 netif, strerror(errno));
531err:
532 close(sock);
533 return rc;
534}
Harald Welte56f08a32020-12-01 23:07:32 +0100535
Alexander Couzens841817e2020-11-19 00:41:29 +0100536/*! Create a new bind for NS over FR.
537 * \param[in] nsi NS instance in which to create the bind
538 * \param[in] netif Network interface to bind to
539 * \param[in] fr_network
540 * \param[in] fr_role
541 * \param[out] result pointer to created bind
542 * \return 0 on success; negative on error */
543int gprs_ns2_fr_bind(struct gprs_ns2_inst *nsi,
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100544 const char *name,
Alexander Couzens841817e2020-11-19 00:41:29 +0100545 const char *netif,
546 struct osmo_fr_network *fr_network,
547 enum osmo_fr_role fr_role,
548 struct gprs_ns2_vc_bind **result)
549{
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100550 struct gprs_ns2_vc_bind *bind;
Alexander Couzens841817e2020-11-19 00:41:29 +0100551 struct priv_bind *priv;
552 struct osmo_fr_link *fr_link;
553 int rc = 0;
554
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100555 if (!name)
556 return -EINVAL;
557
558 if (gprs_ns2_bind_by_name(nsi, name))
559 return -EALREADY;
560
561 bind = talloc_zero(nsi, struct gprs_ns2_vc_bind);
Alexander Couzens841817e2020-11-19 00:41:29 +0100562 if (!bind)
563 return -ENOSPC;
564
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100565 bind->name = talloc_strdup(bind, name);
566 if (!bind->name) {
567 rc = -ENOSPC;
568 goto err_bind;
569 }
570
Alexander Couzens841817e2020-11-19 00:41:29 +0100571 bind->driver = &vc_driver_fr;
Alexander Couzensaac90162020-11-19 02:44:04 +0100572 bind->ll = GPRS_NS2_LL_FR;
Alexander Couzens841817e2020-11-19 00:41:29 +0100573 bind->send_vc = fr_vc_sendmsg;
574 bind->free_vc = free_vc;
575 bind->dump_vty = dump_vty;
576 bind->nsi = nsi;
577 priv = bind->priv = talloc_zero(bind, struct priv_bind);
578 if (!priv) {
579 rc = -ENOSPC;
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100580 goto err_name;
Alexander Couzens841817e2020-11-19 00:41:29 +0100581 }
582
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100583 if (strlen(netif) > IFNAMSIZ) {
Alexander Couzens841817e2020-11-19 00:41:29 +0100584 rc = -EINVAL;
585 goto err_priv;
586 }
Neels Hofmeyr8fef7612020-12-17 18:19:19 +0100587 OSMO_STRLCPY_ARRAY(priv->netif, netif);
Alexander Couzens841817e2020-11-19 00:41:29 +0100588
Alexander Couzens841817e2020-11-19 00:41:29 +0100589 if (result)
590 *result = bind;
591
592 /* FIXME: move fd handling into socket.c */
593 fr_link = osmo_fr_link_alloc(fr_network, fr_role, netif);
594 if (!fr_link) {
595 rc = -EINVAL;
596 goto err_priv;
597 }
598
599 fr_link->tx_cb = fr_tx_cb;
600 fr_link->tx_cb_data = bind;
601 priv->link = fr_link;
Harald Welte41b188b2020-12-10 22:00:23 +0100602
Alexander Couzens6f89c772020-12-17 03:06:39 +0100603 priv->ifindex = rc = devname2ifindex(netif);
604 if (rc < 0) {
Harald Welte41b188b2020-12-10 22:00:23 +0100605 LOGP(DLNS, LOGL_ERROR, "Can not get interface index for interface %s\n", netif);
606 goto err_fr;
607 }
608
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100609 /* set protocol frame relay and lmi */
610 rc = setup_device(priv->netif);
611 if(rc < 0) {
612 LOGP(DLNS, LOGL_ERROR, "Failed to setup the interface %s for frame relay and lmi\n", netif);
613 goto err_fr;
614 }
615
Alexander Couzens60021a42020-12-17 02:48:22 +0100616 rc = open_socket(priv->ifindex);
Alexander Couzens841817e2020-11-19 00:41:29 +0100617 if (rc < 0)
618 goto err_fr;
Alexander Couzens60021a42020-12-17 02:48:22 +0100619 osmo_wqueue_init(&priv->wqueue, 10);
620 priv->wqueue.write_cb = handle_netif_write;
621 priv->wqueue.read_cb = handle_netif_read;
622 osmo_fd_setup(&priv->wqueue.bfd, rc, OSMO_FD_READ, osmo_wqueue_bfd_cb, bind, 0);
623 rc = osmo_fd_register(&priv->wqueue.bfd);
Alexander Couzens841817e2020-11-19 00:41:29 +0100624 if (rc < 0)
625 goto err_fd;
626
627 INIT_LLIST_HEAD(&bind->nsvc);
628 llist_add(&bind->list, &nsi->binding);
629
Harald Welte56f08a32020-12-01 23:07:32 +0100630#ifdef ENABLE_LIBMNL
631 if (!nsi->linkmon_mnl)
632 nsi->linkmon_mnl = osmo_mnl_init(nsi, NETLINK_ROUTE, RTMGRP_LINK, linkmon_mnl_cb, nsi);
633
634 /* we get a new full dump after every bind. which is a bit excessive. But that's just once
635 * at start-up, so we can get away with it */
636 if (nsi->linkmon_mnl)
637 linkmon_initial_dump(nsi->linkmon_mnl);
638#endif
639
Alexander Couzens841817e2020-11-19 00:41:29 +0100640 return rc;
641
642err_fd:
Alexander Couzens60021a42020-12-17 02:48:22 +0100643 close(priv->wqueue.bfd.fd);
Alexander Couzens841817e2020-11-19 00:41:29 +0100644err_fr:
645 osmo_fr_link_free(fr_link);
646err_priv:
647 talloc_free(priv);
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100648err_name:
649 talloc_free((char *)bind->name);
Alexander Couzens841817e2020-11-19 00:41:29 +0100650err_bind:
651 talloc_free(bind);
652
653 return rc;
654}
655
Alexander Couzensc782cec2020-12-10 04:10:25 +0100656/*! Return the frame relay role of a bind
657 * \param[in] bind The bind
658 * \return the frame relay role or -EINVAL if bind is not frame relay
659 */
660enum osmo_fr_role gprs_ns2_fr_bind_role(struct gprs_ns2_vc_bind *bind)
661{
662 struct priv_bind *priv;
663
664 if (bind->driver != &vc_driver_fr)
665 return -EINVAL;
666
667 priv = bind->priv;
668 return priv->link->role;
669}
670
Alexander Couzens841817e2020-11-19 00:41:29 +0100671/*! Return the network interface of the bind
672 * \param[in] bind The bind
673 * \return the network interface
674 */
675const char *gprs_ns2_fr_bind_netif(struct gprs_ns2_vc_bind *bind)
676{
677 struct priv_bind *priv;
678
679 if (bind->driver != &vc_driver_fr)
680 return NULL;
681
682 priv = bind->priv;
683 return priv->netif;
684}
685
686/*! Find NS bind for a given network interface
687 * \param[in] nsi NS instance
688 * \param[in] netif the network interface to search for
689 * \return the bind or NULL if not found
690 */
691struct gprs_ns2_vc_bind *gprs_ns2_fr_bind_by_netif(
692 struct gprs_ns2_inst *nsi,
693 const char *netif)
694{
695 struct gprs_ns2_vc_bind *bind;
696 const char *_netif;
697
698 OSMO_ASSERT(nsi);
699 OSMO_ASSERT(netif);
700
701 llist_for_each_entry(bind, &nsi->binding, list) {
702 if (!gprs_ns2_is_fr_bind(bind))
703 continue;
704
705 _netif = gprs_ns2_fr_bind_netif(bind);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100706 if (!strncmp(_netif, netif, IFNAMSIZ))
Alexander Couzens841817e2020-11-19 00:41:29 +0100707 return bind;
708 }
709
710 return NULL;
711}
712
713/*! Create, connect and activate a new FR-based NS-VC
714 * \param[in] bind bind in which the new NS-VC is to be created
715 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
716 * \param[in] dlci Data Link connection identifier
717 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
718struct gprs_ns2_vc *gprs_ns2_fr_connect(struct gprs_ns2_vc_bind *bind,
Alexander Couzensebcbd722020-12-03 06:11:39 +0100719 struct gprs_ns2_nse *nse,
720 uint16_t nsvci,
721 uint16_t dlci)
722{
723 struct gprs_ns2_vc *nsvc = NULL;
724 struct priv_vc *priv = NULL;
725
726 nsvc = gprs_ns2_fr_nsvc_by_dlci(bind, dlci);
727 if (nsvc) {
728 goto err;
729 }
730
731 nsvc = ns2_vc_alloc(bind, nse, true, NS2_VC_MODE_BLOCKRESET);
732 if (!nsvc)
733 goto err;
734
735 nsvc->priv = priv = fr_alloc_vc(bind, nsvc, dlci);
736 if (!priv)
737 goto err;
738
739 nsvc->nsvci = nsvci;
740 nsvc->nsvci_is_valid = true;
741
742 gprs_ns2_vc_fsm_start(nsvc);
743
744 return nsvc;
745
746err:
747 gprs_ns2_free_nsvc(nsvc);
748 return NULL;
749}
750
751
752/*! Create, connect and activate a new FR-based NS-VC
753 * \param[in] bind bind in which the new NS-VC is to be created
754 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
755 * \param[in] dlci Data Link connection identifier
756 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
757struct gprs_ns2_vc *gprs_ns2_fr_connect2(struct gprs_ns2_vc_bind *bind,
Alexander Couzens841817e2020-11-19 00:41:29 +0100758 uint16_t nsei,
759 uint16_t nsvci,
760 uint16_t dlci)
761{
762 bool created_nse = false;
763 struct gprs_ns2_vc *nsvc = NULL;
764 struct priv_vc *priv = NULL;
765 struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
766 if (!nse) {
Alexander Couzensd923cff2020-12-01 01:03:52 +0100767 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_FR, NS2_DIALECT_STATIC_RESETBLOCK);
Alexander Couzens841817e2020-11-19 00:41:29 +0100768 if (!nse)
769 return NULL;
770 created_nse = true;
771 }
772
773 nsvc = gprs_ns2_fr_nsvc_by_dlci(bind, dlci);
774 if (nsvc) {
775 goto err_nse;
776 }
777
Alexander Couzensd923cff2020-12-01 01:03:52 +0100778 nsvc = ns2_vc_alloc(bind, nse, true, NS2_VC_MODE_BLOCKRESET);
Alexander Couzens841817e2020-11-19 00:41:29 +0100779 if (!nsvc)
780 goto err_nse;
781
782 nsvc->priv = priv = fr_alloc_vc(bind, nsvc, dlci);
783 if (!priv)
784 goto err;
785
786 nsvc->nsvci = nsvci;
787 nsvc->nsvci_is_valid = true;
Alexander Couzens841817e2020-11-19 00:41:29 +0100788
789 gprs_ns2_vc_fsm_start(nsvc);
790
791 return nsvc;
792
793err:
794 gprs_ns2_free_nsvc(nsvc);
795err_nse:
796 if (created_nse)
797 gprs_ns2_free_nse(nse);
798
799 return NULL;
800}
801
802/*! Return the nsvc by dlci.
803 * \param[in] bind
804 * \param[in] dlci Data Link connection identifier
805 * \return the nsvc or NULL if not found
806 */
807struct gprs_ns2_vc *gprs_ns2_fr_nsvc_by_dlci(struct gprs_ns2_vc_bind *bind,
808 uint16_t dlci)
809{
810 struct gprs_ns2_vc *nsvc;
811 struct priv_vc *vcpriv;
812
813 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
814 vcpriv = nsvc->priv;
815
816 if (dlci == vcpriv->dlci)
817 return nsvc;
818 }
819
820 return NULL;
821}
822
823/*! Return the dlci of the nsvc
824 * \param[in] nsvc
825 * \return the dlci or 0 on error. 0 is not a valid dlci.
826 */
Alexander Couzens22c26e02020-12-10 04:10:07 +0100827uint16_t gprs_ns2_fr_nsvc_dlci(const struct gprs_ns2_vc *nsvc)
Alexander Couzens841817e2020-11-19 00:41:29 +0100828{
829 struct priv_vc *vcpriv;
830
831 if (!nsvc->bind)
832 return 0;
833
834 if (nsvc->bind->driver != &vc_driver_fr)
835 return 0;
836
837 vcpriv = nsvc->priv;
838 return vcpriv->dlci;
839}