blob: 274ed9690b31227f94314e9749187b6eecae261a [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) {
Harald Weltef2949742021-01-20 14:54:14 +0100217 LOGBIND(bind, LOGL_ERROR, "recv error %s during NS-FR-GRE recv\n", strerror(errno));
Alexander Couzens841817e2020-11-19 00:41:29 +0100218 goto out_err;
219 } else if (rc == 0) {
220 goto out_err;
221 }
222
Harald Welte41b188b2020-12-10 22:00:23 +0100223 /* ignore any packets that we might have received for a different interface, between
224 * the socket() and the bind() call */
225 if (sll.sll_ifindex != priv->ifindex)
226 goto out_err;
227
Alexander Couzens841817e2020-11-19 00:41:29 +0100228 msgb_put(msg, rc);
229 msg->dst = priv->link;
230 return osmo_fr_rx(msg);
231
232out_err:
233 msgb_free(msg);
234 return rc;
235}
236
237/* PDU from the frame relay towards the NS-VC (upwards) */
238static int fr_dlci_rx_cb(void *cb_data, struct msgb *msg)
239{
240 int rc;
241 struct gprs_ns2_vc *nsvc = cb_data;
242
243 rc = ns2_recv_vc(nsvc, msg);
244
245 return rc;
246}
247
Alexander Couzens60021a42020-12-17 02:48:22 +0100248static int handle_netif_write(struct osmo_fd *ofd, struct msgb *msg)
Alexander Couzens841817e2020-11-19 00:41:29 +0100249{
Alexander Couzens60021a42020-12-17 02:48:22 +0100250 return write(ofd->fd, msgb_data(msg), msgb_length(msg));
Alexander Couzens841817e2020-11-19 00:41:29 +0100251}
252
253/*! determine if given bind is for FR-GRE encapsulation. */
254int gprs_ns2_is_fr_bind(struct gprs_ns2_vc_bind *bind)
255{
256 return (bind->driver == &vc_driver_fr);
257}
258
259/* PDU from the NS-VC towards the frame relay layer (downwards) */
260static int fr_vc_sendmsg(struct gprs_ns2_vc *nsvc, struct msgb *msg)
261{
262 struct priv_vc *vcpriv = nsvc->priv;
263
264 msg->dst = vcpriv->dlc;
265 return osmo_fr_tx_dlc(msg);
266}
267
268/* PDU from the frame relay layer towards the network interface (downwards) */
269int fr_tx_cb(void *data, struct msgb *msg)
270{
271 struct gprs_ns2_vc_bind *bind = data;
272 struct priv_bind *priv = bind->priv;
Alexander Couzens841817e2020-11-19 00:41:29 +0100273
Alexander Couzens60021a42020-12-17 02:48:22 +0100274 if (osmo_wqueue_enqueue(&priv->wqueue, msg)) {
Harald Weltef2949742021-01-20 14:54:14 +0100275 LOGBIND(bind, LOGL_ERROR, "frame relay %s: failed to enqueue message\n", priv->netif);
Alexander Couzens60021a42020-12-17 02:48:22 +0100276 msgb_free(msg);
277 return -EINVAL;
278 }
Alexander Couzens841817e2020-11-19 00:41:29 +0100279
Alexander Couzens60021a42020-12-17 02:48:22 +0100280 return 0;
Alexander Couzens841817e2020-11-19 00:41:29 +0100281}
282
283static int devname2ifindex(const char *ifname)
284{
285 struct ifreq ifr;
286 int sk, rc;
287
288 sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
289 if (sk < 0)
290 return sk;
291
292
293 memset(&ifr, 0, sizeof(ifr));
Neels Hofmeyr475a0ac2020-12-17 18:10:34 +0100294 OSMO_STRLCPY_ARRAY(ifr.ifr_name, ifname);
Alexander Couzens841817e2020-11-19 00:41:29 +0100295
296 rc = ioctl(sk, SIOCGIFINDEX, &ifr);
297 close(sk);
298 if (rc < 0)
299 return rc;
300
301 return ifr.ifr_ifindex;
302}
303
Harald Weltef2949742021-01-20 14:54:14 +0100304static int open_socket(int ifindex, const struct gprs_ns2_vc_bind *nsbind)
Alexander Couzens841817e2020-11-19 00:41:29 +0100305{
306 struct sockaddr_ll addr;
Harald Welte4ed0f4e2020-12-10 21:50:32 +0100307 int fd, rc;
Alexander Couzens841817e2020-11-19 00:41:29 +0100308
Alexander Couzens841817e2020-11-19 00:41:29 +0100309 memset(&addr, 0, sizeof(addr));
310 addr.sll_family = AF_PACKET;
311 addr.sll_protocol = htons(ETH_P_ALL);
312 addr.sll_ifindex = ifindex;
313
Harald Welte5bea72e2020-12-10 22:06:21 +0100314 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_HDLC));
Alexander Couzens841817e2020-11-19 00:41:29 +0100315 if (fd < 0) {
Harald Weltef2949742021-01-20 14:54:14 +0100316 LOGBIND(nsbind, LOGL_ERROR, "Can not create AF_PACKET socket. Are you root or have CAP_NET_RAW?\n");
Alexander Couzens841817e2020-11-19 00:41:29 +0100317 return fd;
318 }
319
Harald Welte41b188b2020-12-10 22:00:23 +0100320 /* there's a race condition between the above syscall and the bind() call below,
321 * causing other packets to be received in between */
322
Alexander Couzens841817e2020-11-19 00:41:29 +0100323 rc = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
324 if (rc < 0) {
Harald Weltef2949742021-01-20 14:54:14 +0100325 LOGBIND(nsbind, LOGL_ERROR, "Can not bind AF_PACKET socket to ifindex %d\n", ifindex);
Alexander Couzens841817e2020-11-19 00:41:29 +0100326 close(fd);
327 return rc;
328 }
329
330 return fd;
331}
332
Harald Welte56f08a32020-12-01 23:07:32 +0100333#ifdef ENABLE_LIBMNL
334
335#include <osmocom/core/mnl.h>
Harald Welte56f08a32020-12-01 23:07:32 +0100336#include <linux/if_link.h>
337#include <linux/rtnetlink.h>
338
339#ifndef ARPHRD_FRAD
340#define ARPHRD_FRAD 770
341#endif
342
343/* validate the netlink attributes */
344static int data_attr_cb(const struct nlattr *attr, void *data)
345{
346 const struct nlattr **tb = data;
347 int type = mnl_attr_get_type(attr);
348
349 if (mnl_attr_type_valid(attr, IFLA_MAX) < 0)
350 return MNL_CB_OK;
351
352 switch (type) {
353 case IFLA_MTU:
354 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
355 return MNL_CB_ERROR;
356 break;
357 case IFLA_IFNAME:
358 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
359 return MNL_CB_ERROR;
360 break;
361 }
362 tb[type] = attr;
363 return MNL_CB_OK;
364}
365
366/* find the bind for the netdev (if any) */
367static struct gprs_ns2_vc_bind *bind4netdev(struct gprs_ns2_inst *nsi, const char *ifname)
368{
369 struct gprs_ns2_vc_bind *bind;
370
371 llist_for_each_entry(bind, &nsi->binding, list) {
372 struct priv_bind *bpriv = bind->priv;
373 if (!strcmp(bpriv->netif, ifname))
374 return bind;
375 }
376
377 return NULL;
378}
379
380/* handle a single netlink message received via libmnl */
381static int linkmon_mnl_cb(const struct nlmsghdr *nlh, void *data)
382{
383 struct osmo_mnl *omnl = data;
384 struct gprs_ns2_vc_bind *bind;
385 struct nlattr *tb[IFLA_MAX+1] = {};
386 struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
387 struct gprs_ns2_inst *nsi;
388 const char *ifname;
389 bool if_running;
390
391 OSMO_ASSERT(omnl);
392 OSMO_ASSERT(ifm);
393
394 nsi = omnl->priv;
395
396 if (ifm->ifi_type != ARPHRD_FRAD)
397 return MNL_CB_OK;
398
399 mnl_attr_parse(nlh, sizeof(*ifm), data_attr_cb, tb);
400
401 if (!tb[IFLA_IFNAME])
402 return MNL_CB_OK;
403 ifname = mnl_attr_get_str(tb[IFLA_IFNAME]);
404 if_running = !!(ifm->ifi_flags & IFF_RUNNING);
405
406 bind = bind4netdev(nsi, ifname);
407 if (bind) {
408 struct priv_bind *bpriv = bind->priv;
409 if (bpriv->if_running != if_running) {
410 /* update running state */
Harald Weltef2949742021-01-20 14:54:14 +0100411 LOGBIND(bind, LOGL_NOTICE, "FR net-device '%s': Physical link state changed: %s\n",
412 ifname, if_running ? "UP" : "DOWN");
Harald Welte56f08a32020-12-01 23:07:32 +0100413 bpriv->if_running = if_running;
414 }
415 }
416
417 return MNL_CB_OK;
418}
419
420/* trigger one initial dump of all link information */
421static void linkmon_initial_dump(struct osmo_mnl *omnl)
422{
423 char buf[MNL_SOCKET_BUFFER_SIZE];
424 struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
425 struct rtgenmsg *rt;
426
427 nlh->nlmsg_type = RTM_GETLINK;
428 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
429 nlh->nlmsg_seq = time(NULL);
430 rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg));
431 rt->rtgen_family = AF_PACKET;
432
433 if (mnl_socket_sendto(omnl->mnls, nlh, nlh->nlmsg_len) < 0) {
434 LOGP(DLNS, LOGL_ERROR, "linkmon: Cannot send rtnetlink message: %s\n", strerror(errno));
435 }
436
437 /* the response[s] will be handled just like the events */
438}
439#endif /* LIBMNL */
440
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100441static int set_ifupdown(const char *netif, bool up)
442{
443 int sock, rc;
444 struct ifreq req;
445
446 sock = socket(AF_INET, SOCK_DGRAM, 0);
447 if (sock < 0)
448 return sock;
449
450 memset(&req, 0, sizeof req);
Harald Welte7f01b682020-12-21 12:39:38 +0100451 OSMO_STRLCPY_ARRAY(req.ifr_name, netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100452
Alexander Couzensfdea03b2020-12-29 23:13:23 +0100453 rc = ioctl(sock, SIOCGIFFLAGS, &req);
Vadim Yanitskiy222e8442021-01-05 14:33:29 +0100454 if (rc < 0) {
455 close(sock);
Alexander Couzensfdea03b2020-12-29 23:13:23 +0100456 return rc;
Vadim Yanitskiy222e8442021-01-05 14:33:29 +0100457 }
Alexander Couzensfdea03b2020-12-29 23:13:23 +0100458
Vadim Yanitskiy222e8442021-01-05 14:33:29 +0100459 if ((req.ifr_flags & IFF_UP) == up) {
460 close(sock);
Alexander Couzensfdea03b2020-12-29 23:13:23 +0100461 return 0;
Vadim Yanitskiy222e8442021-01-05 14:33:29 +0100462 }
Alexander Couzensfdea03b2020-12-29 23:13:23 +0100463
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100464 if (up)
465 req.ifr_flags |= IFF_UP;
466
467 rc = ioctl(sock, SIOCSIFFLAGS, &req);
468 close(sock);
469 return rc;
470}
471
Harald Weltef2949742021-01-20 14:54:14 +0100472static int setup_device(const char *netif, const struct gprs_ns2_vc_bind *bind)
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100473{
474 int sock, rc;
475 char buffer[128];
476 fr_proto *fr = (void*)buffer;
477 struct ifreq req;
478
479 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
480 if (sock < 0) {
Harald Weltef2949742021-01-20 14:54:14 +0100481 LOGBIND(bind, LOGL_ERROR, "%s: Unable to create socket: %s\n",
482 netif, strerror(errno));
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100483 return sock;
484 }
485
486 memset(&req, 0, sizeof(struct ifreq));
487 memset(&buffer, 0, sizeof(buffer));
Harald Welteb8de1882020-12-21 12:40:45 +0100488 OSMO_STRLCPY_ARRAY(req.ifr_name, netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100489 req.ifr_settings.ifs_ifsu.sync = (void*)buffer;
490 req.ifr_settings.size = sizeof(buffer);
491 req.ifr_settings.type = IF_GET_PROTO;
492
Alexander Couzens8c33d4a2020-12-22 15:42:01 +0100493 /* EINVAL is returned when no protocol has been set */
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100494 rc = ioctl(sock, SIOCWANDEV, &req);
Alexander Couzens8c33d4a2020-12-22 15:42:01 +0100495 if (rc < 0 && errno != EINVAL) {
Harald Weltef2949742021-01-20 14:54:14 +0100496 LOGBIND(bind, LOGL_ERROR, "%s: Unable to get FR protocol information: %s\n",
497 netif, strerror(errno));
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100498 goto err;
499 }
500
501 /* check if the device is good */
Alexander Couzens8c33d4a2020-12-22 15:42:01 +0100502 if (rc == 0 && req.ifr_settings.type == IF_PROTO_FR && fr->lmi == LMI_NONE) {
Harald Weltef2949742021-01-20 14:54:14 +0100503 LOGBIND(bind, LOGL_NOTICE, "%s: has correct frame relay mode and lmi\n", netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100504 goto ifup;
505 }
506
507 /* modify the device to match */
508 rc = set_ifupdown(netif, false);
509 if (rc) {
Harald Weltef2949742021-01-20 14:54:14 +0100510 LOGBIND(bind, LOGL_ERROR, "Unable to bring down the device %s: %s\n",
511 netif, strerror(errno));
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100512 goto err;
513 }
514
515 memset(&req, 0, sizeof(struct ifreq));
516 memset(fr, 0, sizeof(fr_proto));
Harald Welteb8de1882020-12-21 12:40:45 +0100517 OSMO_STRLCPY_ARRAY(req.ifr_name, netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100518 req.ifr_settings.type = IF_PROTO_FR;
519 req.ifr_settings.size = sizeof(fr_proto);
520 req.ifr_settings.ifs_ifsu.fr = fr;
521 fr->lmi = LMI_NONE;
522 /* even those settings aren't used, they must be in the range */
523 /* polling verification timer*/
524 fr->t391 = 10;
525 /* link integrity verification polling timer */
526 fr->t392 = 15;
527 /* full status polling counter*/
528 fr->n391 = 6;
529 /* error threshold */
530 fr->n392 = 3;
531 /* monitored events count */
532 fr->n393 = 4;
533
Harald Weltef2949742021-01-20 14:54:14 +0100534 LOGBIND(bind, LOGL_INFO, "%s: Setting frame relay related parameters\n", netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100535 rc = ioctl(sock, SIOCWANDEV, &req);
536 if (rc) {
Harald Weltef2949742021-01-20 14:54:14 +0100537 LOGBIND(bind, LOGL_ERROR, "%s: Unable to set FR protocol on information: %s\n",
538 netif, strerror(errno));
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100539 goto err;
540 }
541
542ifup:
543 rc = set_ifupdown(netif, true);
544 if (rc)
Harald Weltef2949742021-01-20 14:54:14 +0100545 LOGBIND(bind, LOGL_ERROR, "Unable to bring up the device %s: %s\n",
546 netif, strerror(errno));
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100547err:
548 close(sock);
549 return rc;
550}
Harald Welte56f08a32020-12-01 23:07:32 +0100551
Alexander Couzens841817e2020-11-19 00:41:29 +0100552/*! Create a new bind for NS over FR.
553 * \param[in] nsi NS instance in which to create the bind
554 * \param[in] netif Network interface to bind to
555 * \param[in] fr_network
556 * \param[in] fr_role
557 * \param[out] result pointer to created bind
558 * \return 0 on success; negative on error */
559int gprs_ns2_fr_bind(struct gprs_ns2_inst *nsi,
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100560 const char *name,
Alexander Couzens841817e2020-11-19 00:41:29 +0100561 const char *netif,
562 struct osmo_fr_network *fr_network,
563 enum osmo_fr_role fr_role,
564 struct gprs_ns2_vc_bind **result)
565{
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100566 struct gprs_ns2_vc_bind *bind;
Alexander Couzens841817e2020-11-19 00:41:29 +0100567 struct priv_bind *priv;
568 struct osmo_fr_link *fr_link;
569 int rc = 0;
570
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100571 if (!name)
572 return -EINVAL;
573
574 if (gprs_ns2_bind_by_name(nsi, name))
575 return -EALREADY;
576
577 bind = talloc_zero(nsi, struct gprs_ns2_vc_bind);
Alexander Couzens841817e2020-11-19 00:41:29 +0100578 if (!bind)
579 return -ENOSPC;
580
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100581 bind->name = talloc_strdup(bind, name);
582 if (!bind->name) {
583 rc = -ENOSPC;
584 goto err_bind;
585 }
586
Alexander Couzens841817e2020-11-19 00:41:29 +0100587 bind->driver = &vc_driver_fr;
Alexander Couzensaac90162020-11-19 02:44:04 +0100588 bind->ll = GPRS_NS2_LL_FR;
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100589 /* 2 mbit */
590 bind->transfer_capability = 2;
Alexander Couzens841817e2020-11-19 00:41:29 +0100591 bind->send_vc = fr_vc_sendmsg;
592 bind->free_vc = free_vc;
593 bind->dump_vty = dump_vty;
594 bind->nsi = nsi;
595 priv = bind->priv = talloc_zero(bind, struct priv_bind);
596 if (!priv) {
597 rc = -ENOSPC;
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100598 goto err_name;
Alexander Couzens841817e2020-11-19 00:41:29 +0100599 }
600
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100601 if (strlen(netif) > IFNAMSIZ) {
Alexander Couzens841817e2020-11-19 00:41:29 +0100602 rc = -EINVAL;
603 goto err_priv;
604 }
Neels Hofmeyr8fef7612020-12-17 18:19:19 +0100605 OSMO_STRLCPY_ARRAY(priv->netif, netif);
Alexander Couzens841817e2020-11-19 00:41:29 +0100606
Alexander Couzens841817e2020-11-19 00:41:29 +0100607 if (result)
608 *result = bind;
609
610 /* FIXME: move fd handling into socket.c */
611 fr_link = osmo_fr_link_alloc(fr_network, fr_role, netif);
612 if (!fr_link) {
613 rc = -EINVAL;
614 goto err_priv;
615 }
616
617 fr_link->tx_cb = fr_tx_cb;
618 fr_link->tx_cb_data = bind;
619 priv->link = fr_link;
Harald Welte41b188b2020-12-10 22:00:23 +0100620
Alexander Couzens6f89c772020-12-17 03:06:39 +0100621 priv->ifindex = rc = devname2ifindex(netif);
622 if (rc < 0) {
Harald Weltef2949742021-01-20 14:54:14 +0100623 LOGBIND(bind, LOGL_ERROR, "Can not get interface index for interface %s\n", netif);
Harald Welte41b188b2020-12-10 22:00:23 +0100624 goto err_fr;
625 }
626
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100627 /* set protocol frame relay and lmi */
Harald Weltef2949742021-01-20 14:54:14 +0100628 rc = setup_device(priv->netif, bind);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100629 if(rc < 0) {
Harald Weltef2949742021-01-20 14:54:14 +0100630 LOGBIND(bind, LOGL_ERROR, "Failed to setup the interface %s for frame relay and lmi\n", netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100631 goto err_fr;
632 }
633
Harald Weltef2949742021-01-20 14:54:14 +0100634 rc = open_socket(priv->ifindex, bind);
Alexander Couzens841817e2020-11-19 00:41:29 +0100635 if (rc < 0)
636 goto err_fr;
Alexander Couzens60021a42020-12-17 02:48:22 +0100637 osmo_wqueue_init(&priv->wqueue, 10);
638 priv->wqueue.write_cb = handle_netif_write;
639 priv->wqueue.read_cb = handle_netif_read;
640 osmo_fd_setup(&priv->wqueue.bfd, rc, OSMO_FD_READ, osmo_wqueue_bfd_cb, bind, 0);
641 rc = osmo_fd_register(&priv->wqueue.bfd);
Alexander Couzens841817e2020-11-19 00:41:29 +0100642 if (rc < 0)
643 goto err_fd;
644
645 INIT_LLIST_HEAD(&bind->nsvc);
646 llist_add(&bind->list, &nsi->binding);
647
Harald Welte56f08a32020-12-01 23:07:32 +0100648#ifdef ENABLE_LIBMNL
649 if (!nsi->linkmon_mnl)
650 nsi->linkmon_mnl = osmo_mnl_init(nsi, NETLINK_ROUTE, RTMGRP_LINK, linkmon_mnl_cb, nsi);
651
652 /* we get a new full dump after every bind. which is a bit excessive. But that's just once
653 * at start-up, so we can get away with it */
654 if (nsi->linkmon_mnl)
655 linkmon_initial_dump(nsi->linkmon_mnl);
656#endif
657
Alexander Couzens841817e2020-11-19 00:41:29 +0100658 return rc;
659
660err_fd:
Alexander Couzens60021a42020-12-17 02:48:22 +0100661 close(priv->wqueue.bfd.fd);
Alexander Couzens841817e2020-11-19 00:41:29 +0100662err_fr:
663 osmo_fr_link_free(fr_link);
664err_priv:
665 talloc_free(priv);
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100666err_name:
667 talloc_free((char *)bind->name);
Alexander Couzens841817e2020-11-19 00:41:29 +0100668err_bind:
669 talloc_free(bind);
670
671 return rc;
672}
673
Alexander Couzensc782cec2020-12-10 04:10:25 +0100674/*! Return the frame relay role of a bind
675 * \param[in] bind The bind
676 * \return the frame relay role or -EINVAL if bind is not frame relay
677 */
678enum osmo_fr_role gprs_ns2_fr_bind_role(struct gprs_ns2_vc_bind *bind)
679{
680 struct priv_bind *priv;
681
682 if (bind->driver != &vc_driver_fr)
683 return -EINVAL;
684
685 priv = bind->priv;
686 return priv->link->role;
687}
688
Alexander Couzens841817e2020-11-19 00:41:29 +0100689/*! Return the network interface of the bind
690 * \param[in] bind The bind
691 * \return the network interface
692 */
693const char *gprs_ns2_fr_bind_netif(struct gprs_ns2_vc_bind *bind)
694{
695 struct priv_bind *priv;
696
697 if (bind->driver != &vc_driver_fr)
698 return NULL;
699
700 priv = bind->priv;
701 return priv->netif;
702}
703
704/*! Find NS bind for a given network interface
705 * \param[in] nsi NS instance
706 * \param[in] netif the network interface to search for
707 * \return the bind or NULL if not found
708 */
709struct gprs_ns2_vc_bind *gprs_ns2_fr_bind_by_netif(
710 struct gprs_ns2_inst *nsi,
711 const char *netif)
712{
713 struct gprs_ns2_vc_bind *bind;
714 const char *_netif;
715
716 OSMO_ASSERT(nsi);
717 OSMO_ASSERT(netif);
718
719 llist_for_each_entry(bind, &nsi->binding, list) {
720 if (!gprs_ns2_is_fr_bind(bind))
721 continue;
722
723 _netif = gprs_ns2_fr_bind_netif(bind);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100724 if (!strncmp(_netif, netif, IFNAMSIZ))
Alexander Couzens841817e2020-11-19 00:41:29 +0100725 return bind;
726 }
727
728 return NULL;
729}
730
731/*! Create, connect and activate a new FR-based NS-VC
732 * \param[in] bind bind in which the new NS-VC is to be created
733 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
734 * \param[in] dlci Data Link connection identifier
735 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
736struct gprs_ns2_vc *gprs_ns2_fr_connect(struct gprs_ns2_vc_bind *bind,
Alexander Couzensebcbd722020-12-03 06:11:39 +0100737 struct gprs_ns2_nse *nse,
738 uint16_t nsvci,
739 uint16_t dlci)
740{
741 struct gprs_ns2_vc *nsvc = NULL;
742 struct priv_vc *priv = NULL;
Harald Welte603f4042020-11-29 17:39:19 +0100743 struct priv_bind *bpriv = bind->priv;
744 char idbuf[64];
Alexander Couzensebcbd722020-12-03 06:11:39 +0100745
Alexander Couzens55bc8692021-01-18 18:39:57 +0100746 OSMO_ASSERT(gprs_ns2_is_fr_bind(bind));
Alexander Couzensebcbd722020-12-03 06:11:39 +0100747 nsvc = gprs_ns2_fr_nsvc_by_dlci(bind, dlci);
748 if (nsvc) {
749 goto err;
750 }
751
Harald Welte603f4042020-11-29 17:39:19 +0100752 snprintf(idbuf, sizeof(idbuf), "%s-%s-DLCI%u-NSE%05u-NSVC%05u", gprs_ns2_lltype_str(nse->ll),
753 bpriv->netif, dlci, nse->nsei, nsvci);
754 nsvc = ns2_vc_alloc(bind, nse, true, NS2_VC_MODE_BLOCKRESET, idbuf);
Alexander Couzensebcbd722020-12-03 06:11:39 +0100755 if (!nsvc)
756 goto err;
757
758 nsvc->priv = priv = fr_alloc_vc(bind, nsvc, dlci);
759 if (!priv)
760 goto err;
761
762 nsvc->nsvci = nsvci;
763 nsvc->nsvci_is_valid = true;
764
765 gprs_ns2_vc_fsm_start(nsvc);
766
767 return nsvc;
768
769err:
770 gprs_ns2_free_nsvc(nsvc);
771 return NULL;
772}
773
774
775/*! Create, connect and activate a new FR-based NS-VC
776 * \param[in] bind bind in which the new NS-VC is to be created
777 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
778 * \param[in] dlci Data Link connection identifier
779 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
780struct gprs_ns2_vc *gprs_ns2_fr_connect2(struct gprs_ns2_vc_bind *bind,
Alexander Couzens841817e2020-11-19 00:41:29 +0100781 uint16_t nsei,
782 uint16_t nsvci,
783 uint16_t dlci)
784{
785 bool created_nse = false;
786 struct gprs_ns2_vc *nsvc = NULL;
Alexander Couzens55bc8692021-01-18 18:39:57 +0100787 struct gprs_ns2_nse *nse;
788
789 OSMO_ASSERT(gprs_ns2_is_fr_bind(bind));
790 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
Alexander Couzens841817e2020-11-19 00:41:29 +0100791 if (!nse) {
Alexander Couzensd923cff2020-12-01 01:03:52 +0100792 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_FR, NS2_DIALECT_STATIC_RESETBLOCK);
Alexander Couzens841817e2020-11-19 00:41:29 +0100793 if (!nse)
794 return NULL;
795 created_nse = true;
796 }
797
Harald Welte509047b2021-01-17 19:55:51 +0100798 nsvc = gprs_ns2_fr_connect(bind, nse, nsvci, dlci);
Alexander Couzens841817e2020-11-19 00:41:29 +0100799 if (!nsvc)
800 goto err_nse;
801
Alexander Couzens841817e2020-11-19 00:41:29 +0100802 return nsvc;
803
Alexander Couzens841817e2020-11-19 00:41:29 +0100804err_nse:
805 if (created_nse)
806 gprs_ns2_free_nse(nse);
807
808 return NULL;
809}
810
811/*! Return the nsvc by dlci.
812 * \param[in] bind
813 * \param[in] dlci Data Link connection identifier
814 * \return the nsvc or NULL if not found
815 */
816struct gprs_ns2_vc *gprs_ns2_fr_nsvc_by_dlci(struct gprs_ns2_vc_bind *bind,
817 uint16_t dlci)
818{
819 struct gprs_ns2_vc *nsvc;
820 struct priv_vc *vcpriv;
821
Alexander Couzens55bc8692021-01-18 18:39:57 +0100822 OSMO_ASSERT(gprs_ns2_is_fr_bind(bind));
Alexander Couzens841817e2020-11-19 00:41:29 +0100823 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
824 vcpriv = nsvc->priv;
825
826 if (dlci == vcpriv->dlci)
827 return nsvc;
828 }
829
830 return NULL;
831}
832
833/*! Return the dlci of the nsvc
834 * \param[in] nsvc
835 * \return the dlci or 0 on error. 0 is not a valid dlci.
836 */
Alexander Couzens22c26e02020-12-10 04:10:07 +0100837uint16_t gprs_ns2_fr_nsvc_dlci(const struct gprs_ns2_vc *nsvc)
Alexander Couzens841817e2020-11-19 00:41:29 +0100838{
839 struct priv_vc *vcpriv;
840
841 if (!nsvc->bind)
842 return 0;
843
844 if (nsvc->bind->driver != &vc_driver_fr)
845 return 0;
846
847 vcpriv = nsvc->priv;
848 return vcpriv->dlci;
849}