blob: a7455d7c570e06ab6c4a6d1b83f5f1292392242f [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{
Alexander Couzensea377242021-01-17 16:51:55 +0100104 if (!nsvc)
105 return;
Alexander Couzens841817e2020-11-19 00:41:29 +0100106
107 if (!nsvc->priv)
108 return;
109
Alexander Couzens55bc8692021-01-18 18:39:57 +0100110 OSMO_ASSERT(gprs_ns2_is_fr_bind(nsvc->bind));
Alexander Couzens841817e2020-11-19 00:41:29 +0100111 talloc_free(nsvc->priv);
112 nsvc->priv = NULL;
113}
114
115static void dump_vty(const struct gprs_ns2_vc_bind *bind, struct vty *vty, bool _stats)
116{
117 struct priv_bind *priv;
118 struct gprs_ns2_vc *nsvc;
Harald Welte48bd76c2020-12-01 16:53:06 +0100119 struct osmo_fr_link *fr_link;
Alexander Couzens841817e2020-11-19 00:41:29 +0100120
121 if (!bind)
122 return;
123
124 priv = bind->priv;
Harald Welte48bd76c2020-12-01 16:53:06 +0100125 fr_link = priv->link;
Alexander Couzens841817e2020-11-19 00:41:29 +0100126
Harald Welte56f08a32020-12-01 23:07:32 +0100127 vty_out(vty, "FR bind: %s, role: %s, link: %s%s", priv->netif,
128 osmo_fr_role_str(fr_link->role), priv->if_running ? "UP" : "DOWN", VTY_NEWLINE);
Alexander Couzens841817e2020-11-19 00:41:29 +0100129
130 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
Harald Welte96ec84a2020-12-01 17:56:05 +0100131 vty_out(vty, " NSVCI %05u: %s%s", nsvc->nsvci, gprs_ns2_ll_str(nsvc), VTY_NEWLINE);
Alexander Couzens841817e2020-11-19 00:41:29 +0100132 }
133
134 priv = bind->priv;
135}
136
137/*! clean up all private driver state. Should be only called by gprs_ns2_free_bind() */
138static void free_bind(struct gprs_ns2_vc_bind *bind)
139{
140 struct priv_bind *priv;
141
Alexander Couzens55bc8692021-01-18 18:39:57 +0100142 OSMO_ASSERT(gprs_ns2_is_fr_bind(bind));
Alexander Couzens841817e2020-11-19 00:41:29 +0100143 if (!bind)
144 return;
145
146 priv = bind->priv;
147
148 OSMO_ASSERT(llist_empty(&bind->nsvc));
149
150 osmo_fr_link_free(priv->link);
Alexander Couzens60021a42020-12-17 02:48:22 +0100151 osmo_fd_close(&priv->wqueue.bfd);
Alexander Couzens841817e2020-11-19 00:41:29 +0100152 talloc_free(priv);
153}
154
155static struct priv_vc *fr_alloc_vc(struct gprs_ns2_vc_bind *bind,
156 struct gprs_ns2_vc *nsvc,
157 uint16_t dlci)
158{
159 struct priv_bind *privb = bind->priv;
160 struct priv_vc *priv = talloc_zero(bind, struct priv_vc);
161 if (!priv)
162 return NULL;
163
Alexander Couzens55bc8692021-01-18 18:39:57 +0100164 OSMO_ASSERT(gprs_ns2_is_fr_bind(bind));
Alexander Couzens841817e2020-11-19 00:41:29 +0100165 nsvc->priv = priv;
166 priv->dlci = dlci;
167 priv->dlc = osmo_fr_dlc_alloc(privb->link, dlci);
168 if (!priv->dlc) {
169 nsvc->priv = NULL;
170 talloc_free(priv);
171 return NULL;
172 }
173
174 priv->dlc->rx_cb_data = nsvc;
175 priv->dlc->rx_cb = fr_dlci_rx_cb;
176
177 return priv;
178}
179
180int gprs_ns2_find_vc_by_dlci(struct gprs_ns2_vc_bind *bind,
181 uint16_t dlci,
182 struct gprs_ns2_vc **result)
183{
184 struct gprs_ns2_vc *nsvc;
185 struct priv_vc *vcpriv;
186
Alexander Couzens55bc8692021-01-18 18:39:57 +0100187 OSMO_ASSERT(gprs_ns2_is_fr_bind(bind));
Alexander Couzens841817e2020-11-19 00:41:29 +0100188 if (!result)
189 return -EINVAL;
190
191 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
192 vcpriv = nsvc->priv;
193 if (vcpriv->dlci != dlci) {
194 *result = nsvc;
195 return 0;
196 }
197 }
198
199 return 1;
200}
201
202/* PDU from the network interface towards the fr layer (upwards) */
203static int handle_netif_read(struct osmo_fd *bfd)
204{
205 struct gprs_ns2_vc_bind *bind = bfd->data;
206 struct priv_bind *priv = bind->priv;
207 struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Gb/NS/FR/GRE Rx");
Harald Welte41b188b2020-12-10 22:00:23 +0100208 struct sockaddr_ll sll;
209 socklen_t sll_len = sizeof(sll);
Alexander Couzens841817e2020-11-19 00:41:29 +0100210 int rc = 0;
211
212 if (!msg)
213 return -ENOMEM;
214
Harald Welte41b188b2020-12-10 22:00:23 +0100215 rc = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0, (struct sockaddr *)&sll, &sll_len);
Alexander Couzens841817e2020-11-19 00:41:29 +0100216 if (rc < 0) {
217 LOGP(DLNS, LOGL_ERROR, "recv error %s during NS-FR-GRE recv\n",
218 strerror(errno));
219 goto out_err;
220 } else if (rc == 0) {
221 goto out_err;
222 }
223
Harald Welte41b188b2020-12-10 22:00:23 +0100224 /* ignore any packets that we might have received for a different interface, between
225 * the socket() and the bind() call */
226 if (sll.sll_ifindex != priv->ifindex)
227 goto out_err;
228
Alexander Couzens841817e2020-11-19 00:41:29 +0100229 msgb_put(msg, rc);
230 msg->dst = priv->link;
231 return osmo_fr_rx(msg);
232
233out_err:
234 msgb_free(msg);
235 return rc;
236}
237
238/* PDU from the frame relay towards the NS-VC (upwards) */
239static int fr_dlci_rx_cb(void *cb_data, struct msgb *msg)
240{
241 int rc;
242 struct gprs_ns2_vc *nsvc = cb_data;
243
244 rc = ns2_recv_vc(nsvc, msg);
245
246 return rc;
247}
248
Alexander Couzens60021a42020-12-17 02:48:22 +0100249static int handle_netif_write(struct osmo_fd *ofd, struct msgb *msg)
Alexander Couzens841817e2020-11-19 00:41:29 +0100250{
Alexander Couzens60021a42020-12-17 02:48:22 +0100251 return write(ofd->fd, msgb_data(msg), msgb_length(msg));
Alexander Couzens841817e2020-11-19 00:41:29 +0100252}
253
254/*! determine if given bind is for FR-GRE encapsulation. */
255int gprs_ns2_is_fr_bind(struct gprs_ns2_vc_bind *bind)
256{
257 return (bind->driver == &vc_driver_fr);
258}
259
260/* PDU from the NS-VC towards the frame relay layer (downwards) */
261static int fr_vc_sendmsg(struct gprs_ns2_vc *nsvc, struct msgb *msg)
262{
263 struct priv_vc *vcpriv = nsvc->priv;
264
265 msg->dst = vcpriv->dlc;
266 return osmo_fr_tx_dlc(msg);
267}
268
269/* PDU from the frame relay layer towards the network interface (downwards) */
270int fr_tx_cb(void *data, struct msgb *msg)
271{
272 struct gprs_ns2_vc_bind *bind = data;
273 struct priv_bind *priv = bind->priv;
Alexander Couzens841817e2020-11-19 00:41:29 +0100274
Alexander Couzens60021a42020-12-17 02:48:22 +0100275 if (osmo_wqueue_enqueue(&priv->wqueue, msg)) {
276 LOGP(DLNS, LOGL_ERROR, "frame relay %s: failed to enqueue message\n",
277 priv->netif);
278 msgb_free(msg);
279 return -EINVAL;
280 }
Alexander Couzens841817e2020-11-19 00:41:29 +0100281
Alexander Couzens60021a42020-12-17 02:48:22 +0100282 return 0;
Alexander Couzens841817e2020-11-19 00:41:29 +0100283}
284
285static int devname2ifindex(const char *ifname)
286{
287 struct ifreq ifr;
288 int sk, rc;
289
290 sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
291 if (sk < 0)
292 return sk;
293
294
295 memset(&ifr, 0, sizeof(ifr));
Neels Hofmeyr475a0ac2020-12-17 18:10:34 +0100296 OSMO_STRLCPY_ARRAY(ifr.ifr_name, ifname);
Alexander Couzens841817e2020-11-19 00:41:29 +0100297
298 rc = ioctl(sk, SIOCGIFINDEX, &ifr);
299 close(sk);
300 if (rc < 0)
301 return rc;
302
303 return ifr.ifr_ifindex;
304}
305
Harald Welte41b188b2020-12-10 22:00:23 +0100306static int open_socket(int ifindex)
Alexander Couzens841817e2020-11-19 00:41:29 +0100307{
308 struct sockaddr_ll addr;
Harald Welte4ed0f4e2020-12-10 21:50:32 +0100309 int fd, rc;
Alexander Couzens841817e2020-11-19 00:41:29 +0100310
Alexander Couzens841817e2020-11-19 00:41:29 +0100311 memset(&addr, 0, sizeof(addr));
312 addr.sll_family = AF_PACKET;
313 addr.sll_protocol = htons(ETH_P_ALL);
314 addr.sll_ifindex = ifindex;
315
Harald Welte5bea72e2020-12-10 22:06:21 +0100316 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_HDLC));
Alexander Couzens841817e2020-11-19 00:41:29 +0100317 if (fd < 0) {
Harald Welte41b188b2020-12-10 22:00:23 +0100318 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 +0100319 return fd;
320 }
321
Harald Welte41b188b2020-12-10 22:00:23 +0100322 /* there's a race condition between the above syscall and the bind() call below,
323 * causing other packets to be received in between */
324
Alexander Couzens841817e2020-11-19 00:41:29 +0100325 rc = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
326 if (rc < 0) {
Harald Welte41b188b2020-12-10 22:00:23 +0100327 LOGP(DLNS, LOGL_ERROR, "Can not bind AF_PACKET socket to ifindex %d\n", ifindex);
Alexander Couzens841817e2020-11-19 00:41:29 +0100328 close(fd);
329 return rc;
330 }
331
332 return fd;
333}
334
Harald Welte56f08a32020-12-01 23:07:32 +0100335#ifdef ENABLE_LIBMNL
336
337#include <osmocom/core/mnl.h>
Harald Welte56f08a32020-12-01 23:07:32 +0100338#include <linux/if_link.h>
339#include <linux/rtnetlink.h>
340
341#ifndef ARPHRD_FRAD
342#define ARPHRD_FRAD 770
343#endif
344
345/* validate the netlink attributes */
346static int data_attr_cb(const struct nlattr *attr, void *data)
347{
348 const struct nlattr **tb = data;
349 int type = mnl_attr_get_type(attr);
350
351 if (mnl_attr_type_valid(attr, IFLA_MAX) < 0)
352 return MNL_CB_OK;
353
354 switch (type) {
355 case IFLA_MTU:
356 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
357 return MNL_CB_ERROR;
358 break;
359 case IFLA_IFNAME:
360 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
361 return MNL_CB_ERROR;
362 break;
363 }
364 tb[type] = attr;
365 return MNL_CB_OK;
366}
367
368/* find the bind for the netdev (if any) */
369static struct gprs_ns2_vc_bind *bind4netdev(struct gprs_ns2_inst *nsi, const char *ifname)
370{
371 struct gprs_ns2_vc_bind *bind;
372
373 llist_for_each_entry(bind, &nsi->binding, list) {
374 struct priv_bind *bpriv = bind->priv;
375 if (!strcmp(bpriv->netif, ifname))
376 return bind;
377 }
378
379 return NULL;
380}
381
382/* handle a single netlink message received via libmnl */
383static int linkmon_mnl_cb(const struct nlmsghdr *nlh, void *data)
384{
385 struct osmo_mnl *omnl = data;
386 struct gprs_ns2_vc_bind *bind;
387 struct nlattr *tb[IFLA_MAX+1] = {};
388 struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
389 struct gprs_ns2_inst *nsi;
390 const char *ifname;
391 bool if_running;
392
393 OSMO_ASSERT(omnl);
394 OSMO_ASSERT(ifm);
395
396 nsi = omnl->priv;
397
398 if (ifm->ifi_type != ARPHRD_FRAD)
399 return MNL_CB_OK;
400
401 mnl_attr_parse(nlh, sizeof(*ifm), data_attr_cb, tb);
402
403 if (!tb[IFLA_IFNAME])
404 return MNL_CB_OK;
405 ifname = mnl_attr_get_str(tb[IFLA_IFNAME]);
406 if_running = !!(ifm->ifi_flags & IFF_RUNNING);
407
408 bind = bind4netdev(nsi, ifname);
409 if (bind) {
410 struct priv_bind *bpriv = bind->priv;
411 if (bpriv->if_running != if_running) {
412 /* update running state */
413 LOGP(DLNS, LOGL_NOTICE, "FR net-device '%s': Physical link state changed: %s\n",
414 ifname, if_running ? "UP" : "DOWN");
415 bpriv->if_running = if_running;
416 }
417 }
418
419 return MNL_CB_OK;
420}
421
422/* trigger one initial dump of all link information */
423static void linkmon_initial_dump(struct osmo_mnl *omnl)
424{
425 char buf[MNL_SOCKET_BUFFER_SIZE];
426 struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
427 struct rtgenmsg *rt;
428
429 nlh->nlmsg_type = RTM_GETLINK;
430 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
431 nlh->nlmsg_seq = time(NULL);
432 rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg));
433 rt->rtgen_family = AF_PACKET;
434
435 if (mnl_socket_sendto(omnl->mnls, nlh, nlh->nlmsg_len) < 0) {
436 LOGP(DLNS, LOGL_ERROR, "linkmon: Cannot send rtnetlink message: %s\n", strerror(errno));
437 }
438
439 /* the response[s] will be handled just like the events */
440}
441#endif /* LIBMNL */
442
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100443static int set_ifupdown(const char *netif, bool up)
444{
445 int sock, rc;
446 struct ifreq req;
447
448 sock = socket(AF_INET, SOCK_DGRAM, 0);
449 if (sock < 0)
450 return sock;
451
452 memset(&req, 0, sizeof req);
Harald Welte7f01b682020-12-21 12:39:38 +0100453 OSMO_STRLCPY_ARRAY(req.ifr_name, netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100454
Alexander Couzensfdea03b2020-12-29 23:13:23 +0100455 rc = ioctl(sock, SIOCGIFFLAGS, &req);
Vadim Yanitskiy222e8442021-01-05 14:33:29 +0100456 if (rc < 0) {
457 close(sock);
Alexander Couzensfdea03b2020-12-29 23:13:23 +0100458 return rc;
Vadim Yanitskiy222e8442021-01-05 14:33:29 +0100459 }
Alexander Couzensfdea03b2020-12-29 23:13:23 +0100460
Vadim Yanitskiy222e8442021-01-05 14:33:29 +0100461 if ((req.ifr_flags & IFF_UP) == up) {
462 close(sock);
Alexander Couzensfdea03b2020-12-29 23:13:23 +0100463 return 0;
Vadim Yanitskiy222e8442021-01-05 14:33:29 +0100464 }
Alexander Couzensfdea03b2020-12-29 23:13:23 +0100465
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100466 if (up)
467 req.ifr_flags |= IFF_UP;
468
469 rc = ioctl(sock, SIOCSIFFLAGS, &req);
470 close(sock);
471 return rc;
472}
473
474static int setup_device(const char *netif)
475{
476 int sock, rc;
477 char buffer[128];
478 fr_proto *fr = (void*)buffer;
479 struct ifreq req;
480
481 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
482 if (sock < 0) {
483 LOGP(DLNS, LOGL_ERROR, "%s: Unable to create socket: %s\n",
484 netif, strerror(errno));
485 return sock;
486 }
487
488 memset(&req, 0, sizeof(struct ifreq));
489 memset(&buffer, 0, sizeof(buffer));
Harald Welteb8de1882020-12-21 12:40:45 +0100490 OSMO_STRLCPY_ARRAY(req.ifr_name, netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100491 req.ifr_settings.ifs_ifsu.sync = (void*)buffer;
492 req.ifr_settings.size = sizeof(buffer);
493 req.ifr_settings.type = IF_GET_PROTO;
494
Alexander Couzens8c33d4a2020-12-22 15:42:01 +0100495 /* EINVAL is returned when no protocol has been set */
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100496 rc = ioctl(sock, SIOCWANDEV, &req);
Alexander Couzens8c33d4a2020-12-22 15:42:01 +0100497 if (rc < 0 && errno != EINVAL) {
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100498 LOGP(DLNS, LOGL_ERROR, "%s: Unable to get FR protocol information: %s\n",
499 netif, strerror(errno));
500 goto err;
501 }
502
503 /* check if the device is good */
Alexander Couzens8c33d4a2020-12-22 15:42:01 +0100504 if (rc == 0 && req.ifr_settings.type == IF_PROTO_FR && fr->lmi == LMI_NONE) {
505 LOGP(DLNS, LOGL_NOTICE, "%s: has correct frame relay mode and lmi\n", netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100506 goto ifup;
507 }
508
509 /* modify the device to match */
510 rc = set_ifupdown(netif, false);
511 if (rc) {
Alexander Couzens8c33d4a2020-12-22 15:42:01 +0100512 LOGP(DLNS, LOGL_ERROR, "Unable to bring down the device %s: %s\n",
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100513 netif, strerror(errno));
514 goto err;
515 }
516
517 memset(&req, 0, sizeof(struct ifreq));
518 memset(fr, 0, sizeof(fr_proto));
Harald Welteb8de1882020-12-21 12:40:45 +0100519 OSMO_STRLCPY_ARRAY(req.ifr_name, netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100520 req.ifr_settings.type = IF_PROTO_FR;
521 req.ifr_settings.size = sizeof(fr_proto);
522 req.ifr_settings.ifs_ifsu.fr = fr;
523 fr->lmi = LMI_NONE;
524 /* even those settings aren't used, they must be in the range */
525 /* polling verification timer*/
526 fr->t391 = 10;
527 /* link integrity verification polling timer */
528 fr->t392 = 15;
529 /* full status polling counter*/
530 fr->n391 = 6;
531 /* error threshold */
532 fr->n392 = 3;
533 /* monitored events count */
534 fr->n393 = 4;
535
Alexander Couzens8c33d4a2020-12-22 15:42:01 +0100536 LOGP(DLNS, LOGL_INFO, "%s: Setting frame relay related parameters\n", netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100537 rc = ioctl(sock, SIOCWANDEV, &req);
538 if (rc) {
539 LOGP(DLNS, LOGL_ERROR, "%s: Unable to set FR protocol on information: %s\n",
540 netif, strerror(errno));
541 goto err;
542 }
543
544ifup:
545 rc = set_ifupdown(netif, true);
546 if (rc)
547 LOGP(DLNS, LOGL_ERROR, "Unable to bring up the device %s: %s\n",
548 netif, strerror(errno));
549err:
550 close(sock);
551 return rc;
552}
Harald Welte56f08a32020-12-01 23:07:32 +0100553
Alexander Couzens841817e2020-11-19 00:41:29 +0100554/*! Create a new bind for NS over FR.
555 * \param[in] nsi NS instance in which to create the bind
556 * \param[in] netif Network interface to bind to
557 * \param[in] fr_network
558 * \param[in] fr_role
559 * \param[out] result pointer to created bind
560 * \return 0 on success; negative on error */
561int gprs_ns2_fr_bind(struct gprs_ns2_inst *nsi,
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100562 const char *name,
Alexander Couzens841817e2020-11-19 00:41:29 +0100563 const char *netif,
564 struct osmo_fr_network *fr_network,
565 enum osmo_fr_role fr_role,
566 struct gprs_ns2_vc_bind **result)
567{
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100568 struct gprs_ns2_vc_bind *bind;
Alexander Couzens841817e2020-11-19 00:41:29 +0100569 struct priv_bind *priv;
570 struct osmo_fr_link *fr_link;
571 int rc = 0;
572
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100573 if (!name)
574 return -EINVAL;
575
576 if (gprs_ns2_bind_by_name(nsi, name))
577 return -EALREADY;
578
579 bind = talloc_zero(nsi, struct gprs_ns2_vc_bind);
Alexander Couzens841817e2020-11-19 00:41:29 +0100580 if (!bind)
581 return -ENOSPC;
582
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100583 bind->name = talloc_strdup(bind, name);
584 if (!bind->name) {
585 rc = -ENOSPC;
586 goto err_bind;
587 }
588
Alexander Couzens841817e2020-11-19 00:41:29 +0100589 bind->driver = &vc_driver_fr;
Alexander Couzensaac90162020-11-19 02:44:04 +0100590 bind->ll = GPRS_NS2_LL_FR;
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100591 /* 2 mbit */
592 bind->transfer_capability = 2;
Alexander Couzens841817e2020-11-19 00:41:29 +0100593 bind->send_vc = fr_vc_sendmsg;
594 bind->free_vc = free_vc;
595 bind->dump_vty = dump_vty;
596 bind->nsi = nsi;
597 priv = bind->priv = talloc_zero(bind, struct priv_bind);
598 if (!priv) {
599 rc = -ENOSPC;
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100600 goto err_name;
Alexander Couzens841817e2020-11-19 00:41:29 +0100601 }
602
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100603 if (strlen(netif) > IFNAMSIZ) {
Alexander Couzens841817e2020-11-19 00:41:29 +0100604 rc = -EINVAL;
605 goto err_priv;
606 }
Neels Hofmeyr8fef7612020-12-17 18:19:19 +0100607 OSMO_STRLCPY_ARRAY(priv->netif, netif);
Alexander Couzens841817e2020-11-19 00:41:29 +0100608
Alexander Couzens841817e2020-11-19 00:41:29 +0100609 if (result)
610 *result = bind;
611
612 /* FIXME: move fd handling into socket.c */
613 fr_link = osmo_fr_link_alloc(fr_network, fr_role, netif);
614 if (!fr_link) {
615 rc = -EINVAL;
616 goto err_priv;
617 }
618
619 fr_link->tx_cb = fr_tx_cb;
620 fr_link->tx_cb_data = bind;
621 priv->link = fr_link;
Harald Welte41b188b2020-12-10 22:00:23 +0100622
Alexander Couzens6f89c772020-12-17 03:06:39 +0100623 priv->ifindex = rc = devname2ifindex(netif);
624 if (rc < 0) {
Harald Welte41b188b2020-12-10 22:00:23 +0100625 LOGP(DLNS, LOGL_ERROR, "Can not get interface index for interface %s\n", netif);
626 goto err_fr;
627 }
628
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100629 /* set protocol frame relay and lmi */
630 rc = setup_device(priv->netif);
631 if(rc < 0) {
632 LOGP(DLNS, LOGL_ERROR, "Failed to setup the interface %s for frame relay and lmi\n", netif);
633 goto err_fr;
634 }
635
Alexander Couzens60021a42020-12-17 02:48:22 +0100636 rc = open_socket(priv->ifindex);
Alexander Couzens841817e2020-11-19 00:41:29 +0100637 if (rc < 0)
638 goto err_fr;
Alexander Couzens60021a42020-12-17 02:48:22 +0100639 osmo_wqueue_init(&priv->wqueue, 10);
640 priv->wqueue.write_cb = handle_netif_write;
641 priv->wqueue.read_cb = handle_netif_read;
642 osmo_fd_setup(&priv->wqueue.bfd, rc, OSMO_FD_READ, osmo_wqueue_bfd_cb, bind, 0);
643 rc = osmo_fd_register(&priv->wqueue.bfd);
Alexander Couzens841817e2020-11-19 00:41:29 +0100644 if (rc < 0)
645 goto err_fd;
646
647 INIT_LLIST_HEAD(&bind->nsvc);
648 llist_add(&bind->list, &nsi->binding);
649
Harald Welte56f08a32020-12-01 23:07:32 +0100650#ifdef ENABLE_LIBMNL
651 if (!nsi->linkmon_mnl)
652 nsi->linkmon_mnl = osmo_mnl_init(nsi, NETLINK_ROUTE, RTMGRP_LINK, linkmon_mnl_cb, nsi);
653
654 /* we get a new full dump after every bind. which is a bit excessive. But that's just once
655 * at start-up, so we can get away with it */
656 if (nsi->linkmon_mnl)
657 linkmon_initial_dump(nsi->linkmon_mnl);
658#endif
659
Alexander Couzens841817e2020-11-19 00:41:29 +0100660 return rc;
661
662err_fd:
Alexander Couzens60021a42020-12-17 02:48:22 +0100663 close(priv->wqueue.bfd.fd);
Alexander Couzens841817e2020-11-19 00:41:29 +0100664err_fr:
665 osmo_fr_link_free(fr_link);
666err_priv:
667 talloc_free(priv);
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100668err_name:
669 talloc_free((char *)bind->name);
Alexander Couzens841817e2020-11-19 00:41:29 +0100670err_bind:
671 talloc_free(bind);
672
673 return rc;
674}
675
Alexander Couzensc782cec2020-12-10 04:10:25 +0100676/*! Return the frame relay role of a bind
677 * \param[in] bind The bind
678 * \return the frame relay role or -EINVAL if bind is not frame relay
679 */
680enum osmo_fr_role gprs_ns2_fr_bind_role(struct gprs_ns2_vc_bind *bind)
681{
682 struct priv_bind *priv;
683
684 if (bind->driver != &vc_driver_fr)
685 return -EINVAL;
686
687 priv = bind->priv;
688 return priv->link->role;
689}
690
Alexander Couzens841817e2020-11-19 00:41:29 +0100691/*! Return the network interface of the bind
692 * \param[in] bind The bind
693 * \return the network interface
694 */
695const char *gprs_ns2_fr_bind_netif(struct gprs_ns2_vc_bind *bind)
696{
697 struct priv_bind *priv;
698
699 if (bind->driver != &vc_driver_fr)
700 return NULL;
701
702 priv = bind->priv;
703 return priv->netif;
704}
705
706/*! Find NS bind for a given network interface
707 * \param[in] nsi NS instance
708 * \param[in] netif the network interface to search for
709 * \return the bind or NULL if not found
710 */
711struct gprs_ns2_vc_bind *gprs_ns2_fr_bind_by_netif(
712 struct gprs_ns2_inst *nsi,
713 const char *netif)
714{
715 struct gprs_ns2_vc_bind *bind;
716 const char *_netif;
717
718 OSMO_ASSERT(nsi);
719 OSMO_ASSERT(netif);
720
721 llist_for_each_entry(bind, &nsi->binding, list) {
722 if (!gprs_ns2_is_fr_bind(bind))
723 continue;
724
725 _netif = gprs_ns2_fr_bind_netif(bind);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100726 if (!strncmp(_netif, netif, IFNAMSIZ))
Alexander Couzens841817e2020-11-19 00:41:29 +0100727 return bind;
728 }
729
730 return NULL;
731}
732
733/*! Create, connect and activate a new FR-based NS-VC
734 * \param[in] bind bind in which the new NS-VC is to be created
735 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
736 * \param[in] dlci Data Link connection identifier
737 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
738struct gprs_ns2_vc *gprs_ns2_fr_connect(struct gprs_ns2_vc_bind *bind,
Alexander Couzensebcbd722020-12-03 06:11:39 +0100739 struct gprs_ns2_nse *nse,
740 uint16_t nsvci,
741 uint16_t dlci)
742{
743 struct gprs_ns2_vc *nsvc = NULL;
744 struct priv_vc *priv = NULL;
Harald Welte603f4042020-11-29 17:39:19 +0100745 struct priv_bind *bpriv = bind->priv;
746 char idbuf[64];
Alexander Couzensebcbd722020-12-03 06:11:39 +0100747
Alexander Couzens55bc8692021-01-18 18:39:57 +0100748 OSMO_ASSERT(gprs_ns2_is_fr_bind(bind));
Alexander Couzensebcbd722020-12-03 06:11:39 +0100749 nsvc = gprs_ns2_fr_nsvc_by_dlci(bind, dlci);
750 if (nsvc) {
751 goto err;
752 }
753
Harald Welte603f4042020-11-29 17:39:19 +0100754 snprintf(idbuf, sizeof(idbuf), "%s-%s-DLCI%u-NSE%05u-NSVC%05u", gprs_ns2_lltype_str(nse->ll),
755 bpriv->netif, dlci, nse->nsei, nsvci);
756 nsvc = ns2_vc_alloc(bind, nse, true, NS2_VC_MODE_BLOCKRESET, idbuf);
Alexander Couzensebcbd722020-12-03 06:11:39 +0100757 if (!nsvc)
758 goto err;
759
760 nsvc->priv = priv = fr_alloc_vc(bind, nsvc, dlci);
761 if (!priv)
762 goto err;
763
764 nsvc->nsvci = nsvci;
765 nsvc->nsvci_is_valid = true;
766
767 gprs_ns2_vc_fsm_start(nsvc);
768
769 return nsvc;
770
771err:
772 gprs_ns2_free_nsvc(nsvc);
773 return NULL;
774}
775
776
777/*! Create, connect and activate a new FR-based NS-VC
778 * \param[in] bind bind in which the new NS-VC is to be created
779 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
780 * \param[in] dlci Data Link connection identifier
781 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
782struct gprs_ns2_vc *gprs_ns2_fr_connect2(struct gprs_ns2_vc_bind *bind,
Alexander Couzens841817e2020-11-19 00:41:29 +0100783 uint16_t nsei,
784 uint16_t nsvci,
785 uint16_t dlci)
786{
787 bool created_nse = false;
788 struct gprs_ns2_vc *nsvc = NULL;
Alexander Couzens55bc8692021-01-18 18:39:57 +0100789 struct gprs_ns2_nse *nse;
790
791 OSMO_ASSERT(gprs_ns2_is_fr_bind(bind));
792 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
Alexander Couzens841817e2020-11-19 00:41:29 +0100793 if (!nse) {
Alexander Couzensd923cff2020-12-01 01:03:52 +0100794 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_FR, NS2_DIALECT_STATIC_RESETBLOCK);
Alexander Couzens841817e2020-11-19 00:41:29 +0100795 if (!nse)
796 return NULL;
797 created_nse = true;
798 }
799
Harald Welte509047b2021-01-17 19:55:51 +0100800 nsvc = gprs_ns2_fr_connect(bind, nse, nsvci, dlci);
Alexander Couzens841817e2020-11-19 00:41:29 +0100801 if (!nsvc)
802 goto err_nse;
803
Alexander Couzens841817e2020-11-19 00:41:29 +0100804 return nsvc;
805
Alexander Couzens841817e2020-11-19 00:41:29 +0100806err_nse:
807 if (created_nse)
808 gprs_ns2_free_nse(nse);
809
810 return NULL;
811}
812
813/*! Return the nsvc by dlci.
814 * \param[in] bind
815 * \param[in] dlci Data Link connection identifier
816 * \return the nsvc or NULL if not found
817 */
818struct gprs_ns2_vc *gprs_ns2_fr_nsvc_by_dlci(struct gprs_ns2_vc_bind *bind,
819 uint16_t dlci)
820{
821 struct gprs_ns2_vc *nsvc;
822 struct priv_vc *vcpriv;
823
Alexander Couzens55bc8692021-01-18 18:39:57 +0100824 OSMO_ASSERT(gprs_ns2_is_fr_bind(bind));
Alexander Couzens841817e2020-11-19 00:41:29 +0100825 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
826 vcpriv = nsvc->priv;
827
828 if (dlci == vcpriv->dlci)
829 return nsvc;
830 }
831
832 return NULL;
833}
834
835/*! Return the dlci of the nsvc
836 * \param[in] nsvc
837 * \return the dlci or 0 on error. 0 is not a valid dlci.
838 */
Alexander Couzens22c26e02020-12-10 04:10:07 +0100839uint16_t gprs_ns2_fr_nsvc_dlci(const struct gprs_ns2_vc *nsvc)
Alexander Couzens841817e2020-11-19 00:41:29 +0100840{
841 struct priv_vc *vcpriv;
842
843 if (!nsvc->bind)
844 return 0;
845
846 if (nsvc->bind->driver != &vc_driver_fr)
847 return 0;
848
849 vcpriv = nsvc->priv;
850 return vcpriv->dlci;
851}