blob: f73033b43ec09fb8ae852ea063e8d462317b00c5 [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{
Harald Welte335c5502021-01-30 11:36:20 +0100250 int rc = write(ofd->fd, msgb_data(msg), msgb_length(msg));
251 /* write_queue expects a "-errno" type return value in case of failure */
252 if (rc == -1)
253 return -errno;
254 else
255 return rc;
Alexander Couzens841817e2020-11-19 00:41:29 +0100256}
257
258/*! determine if given bind is for FR-GRE encapsulation. */
259int gprs_ns2_is_fr_bind(struct gprs_ns2_vc_bind *bind)
260{
261 return (bind->driver == &vc_driver_fr);
262}
263
264/* PDU from the NS-VC towards the frame relay layer (downwards) */
265static int fr_vc_sendmsg(struct gprs_ns2_vc *nsvc, struct msgb *msg)
266{
267 struct priv_vc *vcpriv = nsvc->priv;
268
269 msg->dst = vcpriv->dlc;
270 return osmo_fr_tx_dlc(msg);
271}
272
273/* PDU from the frame relay layer towards the network interface (downwards) */
274int fr_tx_cb(void *data, struct msgb *msg)
275{
276 struct gprs_ns2_vc_bind *bind = data;
277 struct priv_bind *priv = bind->priv;
Alexander Couzens841817e2020-11-19 00:41:29 +0100278
Alexander Couzens60021a42020-12-17 02:48:22 +0100279 if (osmo_wqueue_enqueue(&priv->wqueue, msg)) {
Harald Weltef2949742021-01-20 14:54:14 +0100280 LOGBIND(bind, LOGL_ERROR, "frame relay %s: failed to enqueue message\n", priv->netif);
Alexander Couzens60021a42020-12-17 02:48:22 +0100281 msgb_free(msg);
282 return -EINVAL;
283 }
Alexander Couzens841817e2020-11-19 00:41:29 +0100284
Alexander Couzens60021a42020-12-17 02:48:22 +0100285 return 0;
Alexander Couzens841817e2020-11-19 00:41:29 +0100286}
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));
Neels Hofmeyr475a0ac2020-12-17 18:10:34 +0100299 OSMO_STRLCPY_ARRAY(ifr.ifr_name, ifname);
Alexander Couzens841817e2020-11-19 00:41:29 +0100300
301 rc = ioctl(sk, SIOCGIFINDEX, &ifr);
302 close(sk);
303 if (rc < 0)
304 return rc;
305
306 return ifr.ifr_ifindex;
307}
308
Harald Weltef2949742021-01-20 14:54:14 +0100309static int open_socket(int ifindex, const struct gprs_ns2_vc_bind *nsbind)
Alexander Couzens841817e2020-11-19 00:41:29 +0100310{
311 struct sockaddr_ll addr;
Harald Welte4ed0f4e2020-12-10 21:50:32 +0100312 int fd, rc;
Alexander Couzens841817e2020-11-19 00:41:29 +0100313
Alexander Couzens841817e2020-11-19 00:41:29 +0100314 memset(&addr, 0, sizeof(addr));
315 addr.sll_family = AF_PACKET;
316 addr.sll_protocol = htons(ETH_P_ALL);
317 addr.sll_ifindex = ifindex;
318
Harald Welte5bea72e2020-12-10 22:06:21 +0100319 fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_HDLC));
Alexander Couzens841817e2020-11-19 00:41:29 +0100320 if (fd < 0) {
Harald Weltef2949742021-01-20 14:54:14 +0100321 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 +0100322 return fd;
323 }
324
Harald Welte41b188b2020-12-10 22:00:23 +0100325 /* there's a race condition between the above syscall and the bind() call below,
326 * causing other packets to be received in between */
327
Alexander Couzens841817e2020-11-19 00:41:29 +0100328 rc = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
329 if (rc < 0) {
Harald Weltef2949742021-01-20 14:54:14 +0100330 LOGBIND(nsbind, LOGL_ERROR, "Can not bind AF_PACKET socket to ifindex %d\n", ifindex);
Alexander Couzens841817e2020-11-19 00:41:29 +0100331 close(fd);
332 return rc;
333 }
334
335 return fd;
336}
337
Harald Welte56f08a32020-12-01 23:07:32 +0100338#ifdef ENABLE_LIBMNL
339
340#include <osmocom/core/mnl.h>
Harald Welte56f08a32020-12-01 23:07:32 +0100341#include <linux/if_link.h>
342#include <linux/rtnetlink.h>
343
344#ifndef ARPHRD_FRAD
345#define ARPHRD_FRAD 770
346#endif
347
348/* validate the netlink attributes */
349static int data_attr_cb(const struct nlattr *attr, void *data)
350{
351 const struct nlattr **tb = data;
352 int type = mnl_attr_get_type(attr);
353
354 if (mnl_attr_type_valid(attr, IFLA_MAX) < 0)
355 return MNL_CB_OK;
356
357 switch (type) {
358 case IFLA_MTU:
359 if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0)
360 return MNL_CB_ERROR;
361 break;
362 case IFLA_IFNAME:
363 if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0)
364 return MNL_CB_ERROR;
365 break;
366 }
367 tb[type] = attr;
368 return MNL_CB_OK;
369}
370
371/* find the bind for the netdev (if any) */
372static struct gprs_ns2_vc_bind *bind4netdev(struct gprs_ns2_inst *nsi, const char *ifname)
373{
374 struct gprs_ns2_vc_bind *bind;
375
376 llist_for_each_entry(bind, &nsi->binding, list) {
377 struct priv_bind *bpriv = bind->priv;
378 if (!strcmp(bpriv->netif, ifname))
379 return bind;
380 }
381
382 return NULL;
383}
384
385/* handle a single netlink message received via libmnl */
386static int linkmon_mnl_cb(const struct nlmsghdr *nlh, void *data)
387{
388 struct osmo_mnl *omnl = data;
389 struct gprs_ns2_vc_bind *bind;
390 struct nlattr *tb[IFLA_MAX+1] = {};
391 struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
392 struct gprs_ns2_inst *nsi;
393 const char *ifname;
394 bool if_running;
395
396 OSMO_ASSERT(omnl);
397 OSMO_ASSERT(ifm);
398
399 nsi = omnl->priv;
400
401 if (ifm->ifi_type != ARPHRD_FRAD)
402 return MNL_CB_OK;
403
404 mnl_attr_parse(nlh, sizeof(*ifm), data_attr_cb, tb);
405
406 if (!tb[IFLA_IFNAME])
407 return MNL_CB_OK;
408 ifname = mnl_attr_get_str(tb[IFLA_IFNAME]);
409 if_running = !!(ifm->ifi_flags & IFF_RUNNING);
410
411 bind = bind4netdev(nsi, ifname);
412 if (bind) {
413 struct priv_bind *bpriv = bind->priv;
414 if (bpriv->if_running != if_running) {
415 /* update running state */
Harald Weltef2949742021-01-20 14:54:14 +0100416 LOGBIND(bind, LOGL_NOTICE, "FR net-device '%s': Physical link state changed: %s\n",
417 ifname, if_running ? "UP" : "DOWN");
Harald Welte56f08a32020-12-01 23:07:32 +0100418 bpriv->if_running = if_running;
419 }
420 }
421
422 return MNL_CB_OK;
423}
424
425/* trigger one initial dump of all link information */
426static void linkmon_initial_dump(struct osmo_mnl *omnl)
427{
428 char buf[MNL_SOCKET_BUFFER_SIZE];
429 struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf);
430 struct rtgenmsg *rt;
431
432 nlh->nlmsg_type = RTM_GETLINK;
433 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
434 nlh->nlmsg_seq = time(NULL);
435 rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg));
436 rt->rtgen_family = AF_PACKET;
437
438 if (mnl_socket_sendto(omnl->mnls, nlh, nlh->nlmsg_len) < 0) {
439 LOGP(DLNS, LOGL_ERROR, "linkmon: Cannot send rtnetlink message: %s\n", strerror(errno));
440 }
441
442 /* the response[s] will be handled just like the events */
443}
444#endif /* LIBMNL */
445
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100446static int set_ifupdown(const char *netif, bool up)
447{
448 int sock, rc;
449 struct ifreq req;
450
451 sock = socket(AF_INET, SOCK_DGRAM, 0);
452 if (sock < 0)
453 return sock;
454
455 memset(&req, 0, sizeof req);
Harald Welte7f01b682020-12-21 12:39:38 +0100456 OSMO_STRLCPY_ARRAY(req.ifr_name, netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100457
Alexander Couzensfdea03b2020-12-29 23:13:23 +0100458 rc = ioctl(sock, SIOCGIFFLAGS, &req);
Vadim Yanitskiy222e8442021-01-05 14:33:29 +0100459 if (rc < 0) {
460 close(sock);
Alexander Couzensfdea03b2020-12-29 23:13:23 +0100461 return rc;
Vadim Yanitskiy222e8442021-01-05 14:33:29 +0100462 }
Alexander Couzensfdea03b2020-12-29 23:13:23 +0100463
Vadim Yanitskiy222e8442021-01-05 14:33:29 +0100464 if ((req.ifr_flags & IFF_UP) == up) {
465 close(sock);
Alexander Couzensfdea03b2020-12-29 23:13:23 +0100466 return 0;
Vadim Yanitskiy222e8442021-01-05 14:33:29 +0100467 }
Alexander Couzensfdea03b2020-12-29 23:13:23 +0100468
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100469 if (up)
470 req.ifr_flags |= IFF_UP;
471
472 rc = ioctl(sock, SIOCSIFFLAGS, &req);
473 close(sock);
474 return rc;
475}
476
Harald Weltef2949742021-01-20 14:54:14 +0100477static int setup_device(const char *netif, const struct gprs_ns2_vc_bind *bind)
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100478{
479 int sock, rc;
480 char buffer[128];
481 fr_proto *fr = (void*)buffer;
482 struct ifreq req;
483
484 sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
485 if (sock < 0) {
Harald Weltef2949742021-01-20 14:54:14 +0100486 LOGBIND(bind, LOGL_ERROR, "%s: Unable to create socket: %s\n",
487 netif, strerror(errno));
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100488 return sock;
489 }
490
491 memset(&req, 0, sizeof(struct ifreq));
492 memset(&buffer, 0, sizeof(buffer));
Harald Welteb8de1882020-12-21 12:40:45 +0100493 OSMO_STRLCPY_ARRAY(req.ifr_name, netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100494 req.ifr_settings.ifs_ifsu.sync = (void*)buffer;
495 req.ifr_settings.size = sizeof(buffer);
496 req.ifr_settings.type = IF_GET_PROTO;
497
Alexander Couzens8c33d4a2020-12-22 15:42:01 +0100498 /* EINVAL is returned when no protocol has been set */
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100499 rc = ioctl(sock, SIOCWANDEV, &req);
Alexander Couzens8c33d4a2020-12-22 15:42:01 +0100500 if (rc < 0 && errno != EINVAL) {
Harald Weltef2949742021-01-20 14:54:14 +0100501 LOGBIND(bind, LOGL_ERROR, "%s: Unable to get FR protocol information: %s\n",
502 netif, strerror(errno));
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100503 goto err;
504 }
505
506 /* check if the device is good */
Alexander Couzens8c33d4a2020-12-22 15:42:01 +0100507 if (rc == 0 && req.ifr_settings.type == IF_PROTO_FR && fr->lmi == LMI_NONE) {
Harald Weltef2949742021-01-20 14:54:14 +0100508 LOGBIND(bind, LOGL_NOTICE, "%s: has correct frame relay mode and lmi\n", netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100509 goto ifup;
510 }
511
512 /* modify the device to match */
513 rc = set_ifupdown(netif, false);
514 if (rc) {
Harald Weltef2949742021-01-20 14:54:14 +0100515 LOGBIND(bind, LOGL_ERROR, "Unable to bring down the device %s: %s\n",
516 netif, strerror(errno));
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100517 goto err;
518 }
519
520 memset(&req, 0, sizeof(struct ifreq));
521 memset(fr, 0, sizeof(fr_proto));
Harald Welteb8de1882020-12-21 12:40:45 +0100522 OSMO_STRLCPY_ARRAY(req.ifr_name, netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100523 req.ifr_settings.type = IF_PROTO_FR;
524 req.ifr_settings.size = sizeof(fr_proto);
525 req.ifr_settings.ifs_ifsu.fr = fr;
526 fr->lmi = LMI_NONE;
527 /* even those settings aren't used, they must be in the range */
528 /* polling verification timer*/
529 fr->t391 = 10;
530 /* link integrity verification polling timer */
531 fr->t392 = 15;
532 /* full status polling counter*/
533 fr->n391 = 6;
534 /* error threshold */
535 fr->n392 = 3;
536 /* monitored events count */
537 fr->n393 = 4;
538
Harald Weltef2949742021-01-20 14:54:14 +0100539 LOGBIND(bind, LOGL_INFO, "%s: Setting frame relay related parameters\n", netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100540 rc = ioctl(sock, SIOCWANDEV, &req);
541 if (rc) {
Harald Weltef2949742021-01-20 14:54:14 +0100542 LOGBIND(bind, LOGL_ERROR, "%s: Unable to set FR protocol on information: %s\n",
543 netif, strerror(errno));
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100544 goto err;
545 }
546
547ifup:
548 rc = set_ifupdown(netif, true);
549 if (rc)
Harald Weltef2949742021-01-20 14:54:14 +0100550 LOGBIND(bind, LOGL_ERROR, "Unable to bring up the device %s: %s\n",
551 netif, strerror(errno));
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100552err:
553 close(sock);
554 return rc;
555}
Harald Welte56f08a32020-12-01 23:07:32 +0100556
Alexander Couzens841817e2020-11-19 00:41:29 +0100557/*! Create a new bind for NS over FR.
558 * \param[in] nsi NS instance in which to create the bind
559 * \param[in] netif Network interface to bind to
560 * \param[in] fr_network
561 * \param[in] fr_role
562 * \param[out] result pointer to created bind
563 * \return 0 on success; negative on error */
564int gprs_ns2_fr_bind(struct gprs_ns2_inst *nsi,
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100565 const char *name,
Alexander Couzens841817e2020-11-19 00:41:29 +0100566 const char *netif,
567 struct osmo_fr_network *fr_network,
568 enum osmo_fr_role fr_role,
569 struct gprs_ns2_vc_bind **result)
570{
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100571 struct gprs_ns2_vc_bind *bind;
Alexander Couzens841817e2020-11-19 00:41:29 +0100572 struct priv_bind *priv;
573 struct osmo_fr_link *fr_link;
574 int rc = 0;
575
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100576 if (!name)
577 return -EINVAL;
578
579 if (gprs_ns2_bind_by_name(nsi, name))
580 return -EALREADY;
581
582 bind = talloc_zero(nsi, struct gprs_ns2_vc_bind);
Alexander Couzens841817e2020-11-19 00:41:29 +0100583 if (!bind)
584 return -ENOSPC;
585
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100586 bind->name = talloc_strdup(bind, name);
587 if (!bind->name) {
588 rc = -ENOSPC;
589 goto err_bind;
590 }
591
Alexander Couzens841817e2020-11-19 00:41:29 +0100592 bind->driver = &vc_driver_fr;
Alexander Couzensaac90162020-11-19 02:44:04 +0100593 bind->ll = GPRS_NS2_LL_FR;
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100594 /* 2 mbit */
595 bind->transfer_capability = 2;
Alexander Couzens841817e2020-11-19 00:41:29 +0100596 bind->send_vc = fr_vc_sendmsg;
597 bind->free_vc = free_vc;
598 bind->dump_vty = dump_vty;
599 bind->nsi = nsi;
600 priv = bind->priv = talloc_zero(bind, struct priv_bind);
601 if (!priv) {
602 rc = -ENOSPC;
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100603 goto err_name;
Alexander Couzens841817e2020-11-19 00:41:29 +0100604 }
605
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100606 if (strlen(netif) > IFNAMSIZ) {
Alexander Couzens841817e2020-11-19 00:41:29 +0100607 rc = -EINVAL;
608 goto err_priv;
609 }
Neels Hofmeyr8fef7612020-12-17 18:19:19 +0100610 OSMO_STRLCPY_ARRAY(priv->netif, netif);
Alexander Couzens841817e2020-11-19 00:41:29 +0100611
Alexander Couzens841817e2020-11-19 00:41:29 +0100612 if (result)
613 *result = bind;
614
615 /* FIXME: move fd handling into socket.c */
616 fr_link = osmo_fr_link_alloc(fr_network, fr_role, netif);
617 if (!fr_link) {
618 rc = -EINVAL;
619 goto err_priv;
620 }
621
622 fr_link->tx_cb = fr_tx_cb;
623 fr_link->tx_cb_data = bind;
624 priv->link = fr_link;
Harald Welte41b188b2020-12-10 22:00:23 +0100625
Alexander Couzens6f89c772020-12-17 03:06:39 +0100626 priv->ifindex = rc = devname2ifindex(netif);
627 if (rc < 0) {
Harald Weltef2949742021-01-20 14:54:14 +0100628 LOGBIND(bind, LOGL_ERROR, "Can not get interface index for interface %s\n", netif);
Harald Welte41b188b2020-12-10 22:00:23 +0100629 goto err_fr;
630 }
631
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100632 /* set protocol frame relay and lmi */
Harald Weltef2949742021-01-20 14:54:14 +0100633 rc = setup_device(priv->netif, bind);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100634 if(rc < 0) {
Harald Weltef2949742021-01-20 14:54:14 +0100635 LOGBIND(bind, LOGL_ERROR, "Failed to setup the interface %s for frame relay and lmi\n", netif);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100636 goto err_fr;
637 }
638
Harald Weltef2949742021-01-20 14:54:14 +0100639 rc = open_socket(priv->ifindex, bind);
Alexander Couzens841817e2020-11-19 00:41:29 +0100640 if (rc < 0)
641 goto err_fr;
Alexander Couzens60021a42020-12-17 02:48:22 +0100642 osmo_wqueue_init(&priv->wqueue, 10);
643 priv->wqueue.write_cb = handle_netif_write;
644 priv->wqueue.read_cb = handle_netif_read;
645 osmo_fd_setup(&priv->wqueue.bfd, rc, OSMO_FD_READ, osmo_wqueue_bfd_cb, bind, 0);
646 rc = osmo_fd_register(&priv->wqueue.bfd);
Alexander Couzens841817e2020-11-19 00:41:29 +0100647 if (rc < 0)
648 goto err_fd;
649
650 INIT_LLIST_HEAD(&bind->nsvc);
651 llist_add(&bind->list, &nsi->binding);
652
Harald Welte56f08a32020-12-01 23:07:32 +0100653#ifdef ENABLE_LIBMNL
654 if (!nsi->linkmon_mnl)
655 nsi->linkmon_mnl = osmo_mnl_init(nsi, NETLINK_ROUTE, RTMGRP_LINK, linkmon_mnl_cb, nsi);
656
657 /* we get a new full dump after every bind. which is a bit excessive. But that's just once
658 * at start-up, so we can get away with it */
659 if (nsi->linkmon_mnl)
660 linkmon_initial_dump(nsi->linkmon_mnl);
661#endif
662
Alexander Couzens841817e2020-11-19 00:41:29 +0100663 return rc;
664
665err_fd:
Alexander Couzens60021a42020-12-17 02:48:22 +0100666 close(priv->wqueue.bfd.fd);
Alexander Couzens841817e2020-11-19 00:41:29 +0100667err_fr:
668 osmo_fr_link_free(fr_link);
669err_priv:
670 talloc_free(priv);
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100671err_name:
672 talloc_free((char *)bind->name);
Alexander Couzens841817e2020-11-19 00:41:29 +0100673err_bind:
674 talloc_free(bind);
675
676 return rc;
677}
678
Alexander Couzensc782cec2020-12-10 04:10:25 +0100679/*! Return the frame relay role of a bind
680 * \param[in] bind The bind
681 * \return the frame relay role or -EINVAL if bind is not frame relay
682 */
683enum osmo_fr_role gprs_ns2_fr_bind_role(struct gprs_ns2_vc_bind *bind)
684{
685 struct priv_bind *priv;
686
687 if (bind->driver != &vc_driver_fr)
688 return -EINVAL;
689
690 priv = bind->priv;
691 return priv->link->role;
692}
693
Alexander Couzens841817e2020-11-19 00:41:29 +0100694/*! Return the network interface of the bind
695 * \param[in] bind The bind
696 * \return the network interface
697 */
698const char *gprs_ns2_fr_bind_netif(struct gprs_ns2_vc_bind *bind)
699{
700 struct priv_bind *priv;
701
702 if (bind->driver != &vc_driver_fr)
703 return NULL;
704
705 priv = bind->priv;
706 return priv->netif;
707}
708
709/*! Find NS bind for a given network interface
710 * \param[in] nsi NS instance
711 * \param[in] netif the network interface to search for
712 * \return the bind or NULL if not found
713 */
714struct gprs_ns2_vc_bind *gprs_ns2_fr_bind_by_netif(
715 struct gprs_ns2_inst *nsi,
716 const char *netif)
717{
718 struct gprs_ns2_vc_bind *bind;
719 const char *_netif;
720
721 OSMO_ASSERT(nsi);
722 OSMO_ASSERT(netif);
723
724 llist_for_each_entry(bind, &nsi->binding, list) {
725 if (!gprs_ns2_is_fr_bind(bind))
726 continue;
727
728 _netif = gprs_ns2_fr_bind_netif(bind);
Alexander Couzens5c96f5d2020-12-17 04:45:03 +0100729 if (!strncmp(_netif, netif, IFNAMSIZ))
Alexander Couzens841817e2020-11-19 00:41:29 +0100730 return bind;
731 }
732
733 return NULL;
734}
735
736/*! Create, connect and activate a new FR-based NS-VC
737 * \param[in] bind bind in which the new NS-VC is to be created
738 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
739 * \param[in] dlci Data Link connection identifier
740 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
741struct gprs_ns2_vc *gprs_ns2_fr_connect(struct gprs_ns2_vc_bind *bind,
Alexander Couzensebcbd722020-12-03 06:11:39 +0100742 struct gprs_ns2_nse *nse,
743 uint16_t nsvci,
744 uint16_t dlci)
745{
746 struct gprs_ns2_vc *nsvc = NULL;
747 struct priv_vc *priv = NULL;
Harald Welte603f4042020-11-29 17:39:19 +0100748 struct priv_bind *bpriv = bind->priv;
749 char idbuf[64];
Alexander Couzensebcbd722020-12-03 06:11:39 +0100750
Alexander Couzens55bc8692021-01-18 18:39:57 +0100751 OSMO_ASSERT(gprs_ns2_is_fr_bind(bind));
Alexander Couzensebcbd722020-12-03 06:11:39 +0100752 nsvc = gprs_ns2_fr_nsvc_by_dlci(bind, dlci);
753 if (nsvc) {
754 goto err;
755 }
756
Harald Welte603f4042020-11-29 17:39:19 +0100757 snprintf(idbuf, sizeof(idbuf), "%s-%s-DLCI%u-NSE%05u-NSVC%05u", gprs_ns2_lltype_str(nse->ll),
758 bpriv->netif, dlci, nse->nsei, nsvci);
Alexander Couzens138b96f2021-01-25 16:23:29 +0100759 nsvc = ns2_vc_alloc(bind, nse, true, GPRS_NS2_VC_MODE_BLOCKRESET, idbuf);
Alexander Couzensebcbd722020-12-03 06:11:39 +0100760 if (!nsvc)
761 goto err;
762
763 nsvc->priv = priv = fr_alloc_vc(bind, nsvc, dlci);
764 if (!priv)
765 goto err;
766
767 nsvc->nsvci = nsvci;
768 nsvc->nsvci_is_valid = true;
769
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100770 ns2_vc_fsm_start(nsvc);
Alexander Couzensebcbd722020-12-03 06:11:39 +0100771
772 return nsvc;
773
774err:
775 gprs_ns2_free_nsvc(nsvc);
776 return NULL;
777}
778
779
780/*! Create, connect and activate a new FR-based NS-VC
781 * \param[in] bind bind in which the new NS-VC is to be created
782 * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created
783 * \param[in] dlci Data Link connection identifier
784 * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */
785struct gprs_ns2_vc *gprs_ns2_fr_connect2(struct gprs_ns2_vc_bind *bind,
Alexander Couzens841817e2020-11-19 00:41:29 +0100786 uint16_t nsei,
787 uint16_t nsvci,
788 uint16_t dlci)
789{
790 bool created_nse = false;
791 struct gprs_ns2_vc *nsvc = NULL;
Alexander Couzens55bc8692021-01-18 18:39:57 +0100792 struct gprs_ns2_nse *nse;
793
794 OSMO_ASSERT(gprs_ns2_is_fr_bind(bind));
795 nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei);
Alexander Couzens841817e2020-11-19 00:41:29 +0100796 if (!nse) {
Alexander Couzens138b96f2021-01-25 16:23:29 +0100797 nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_FR, GPRS_NS2_DIALECT_STATIC_RESETBLOCK);
Alexander Couzens841817e2020-11-19 00:41:29 +0100798 if (!nse)
799 return NULL;
800 created_nse = true;
801 }
802
Harald Welte509047b2021-01-17 19:55:51 +0100803 nsvc = gprs_ns2_fr_connect(bind, nse, nsvci, dlci);
Alexander Couzens841817e2020-11-19 00:41:29 +0100804 if (!nsvc)
805 goto err_nse;
806
Alexander Couzens841817e2020-11-19 00:41:29 +0100807 return nsvc;
808
Alexander Couzens841817e2020-11-19 00:41:29 +0100809err_nse:
810 if (created_nse)
811 gprs_ns2_free_nse(nse);
812
813 return NULL;
814}
815
816/*! Return the nsvc by dlci.
817 * \param[in] bind
818 * \param[in] dlci Data Link connection identifier
819 * \return the nsvc or NULL if not found
820 */
821struct gprs_ns2_vc *gprs_ns2_fr_nsvc_by_dlci(struct gprs_ns2_vc_bind *bind,
822 uint16_t dlci)
823{
824 struct gprs_ns2_vc *nsvc;
825 struct priv_vc *vcpriv;
826
Alexander Couzens55bc8692021-01-18 18:39:57 +0100827 OSMO_ASSERT(gprs_ns2_is_fr_bind(bind));
Alexander Couzens841817e2020-11-19 00:41:29 +0100828 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
829 vcpriv = nsvc->priv;
830
831 if (dlci == vcpriv->dlci)
832 return nsvc;
833 }
834
835 return NULL;
836}
837
838/*! Return the dlci of the nsvc
839 * \param[in] nsvc
840 * \return the dlci or 0 on error. 0 is not a valid dlci.
841 */
Alexander Couzens22c26e02020-12-10 04:10:07 +0100842uint16_t gprs_ns2_fr_nsvc_dlci(const struct gprs_ns2_vc *nsvc)
Alexander Couzens841817e2020-11-19 00:41:29 +0100843{
844 struct priv_vc *vcpriv;
845
846 if (!nsvc->bind)
847 return 0;
848
849 if (nsvc->bind->driver != &vc_driver_fr)
850 return 0;
851
852 vcpriv = nsvc->priv;
853 return vcpriv->dlci;
854}