Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 1 | /*! \file gprs_ns2_fr.c |
| 2 | * NS-over-FR-over-GRE implementation. |
| 3 | * GPRS Networks Service (NS) messages on the Gb interface. |
| 4 | * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) |
| 5 | * as well as its successor 3GPP TS 48.016 */ |
| 6 | |
| 7 | /* (C) 2009-2010,2014,2017 by Harald Welte <laforge@gnumonks.org> |
| 8 | * (C) 2020 sysmocom - s.f.m.c. GmbH |
| 9 | * Author: Alexander Couzens <lynxis@fe80.eu> |
| 10 | * |
| 11 | * All Rights Reserved |
| 12 | * |
| 13 | * SPDX-License-Identifier: GPL-2.0+ |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <errno.h> |
| 31 | #include <string.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | #include <sys/socket.h> |
| 35 | #include <netinet/in.h> |
| 36 | #include <netinet/ip.h> |
| 37 | #include <netinet/ip6.h> |
| 38 | #include <arpa/inet.h> |
| 39 | #include <net/if.h> |
| 40 | |
| 41 | #include <sys/ioctl.h> |
| 42 | #include <netpacket/packet.h> |
| 43 | #include <linux/if_ether.h> |
| 44 | #include <linux/hdlc.h> |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 45 | |
| 46 | #include <osmocom/gprs/frame_relay.h> |
| 47 | #include <osmocom/core/byteswap.h> |
| 48 | #include <osmocom/core/logging.h> |
| 49 | #include <osmocom/core/msgb.h> |
| 50 | #include <osmocom/core/select.h> |
| 51 | #include <osmocom/core/socket.h> |
| 52 | #include <osmocom/core/talloc.h> |
| 53 | #include <osmocom/gprs/gprs_ns2.h> |
| 54 | |
Pau Espin Pedrol | d41800c | 2020-12-07 13:35:24 +0100 | [diff] [blame] | 55 | #ifdef ENABLE_LIBMNL |
| 56 | #include <osmocom/core/mnl.h> |
| 57 | #endif |
| 58 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 59 | #include "config.h" |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 60 | #include "common_vty.h" |
| 61 | #include "gprs_ns2_internal.h" |
| 62 | |
| 63 | #define GRE_PTYPE_FR 0x6559 |
| 64 | #define GRE_PTYPE_IPv4 0x0800 |
| 65 | #define GRE_PTYPE_IPv6 0x86dd |
| 66 | #define GRE_PTYPE_KAR 0x0000 /* keepalive response */ |
| 67 | |
| 68 | #ifndef IPPROTO_GRE |
| 69 | # define IPPROTO_GRE 47 |
| 70 | #endif |
| 71 | |
| 72 | struct gre_hdr { |
| 73 | uint16_t flags; |
| 74 | uint16_t ptype; |
| 75 | } __attribute__ ((packed)); |
| 76 | |
| 77 | static void free_bind(struct gprs_ns2_vc_bind *bind); |
| 78 | static int fr_dlci_rx_cb(void *cb_data, struct msgb *msg); |
| 79 | |
| 80 | struct gprs_ns2_vc_driver vc_driver_fr = { |
| 81 | .name = "GB frame relay", |
| 82 | .free_bind = free_bind, |
| 83 | }; |
| 84 | |
| 85 | struct priv_bind { |
| 86 | struct osmo_fd fd; |
| 87 | char netif[IF_NAMESIZE]; |
| 88 | struct osmo_fr_link *link; |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 89 | bool if_running; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | struct priv_vc { |
| 93 | struct osmo_sockaddr remote; |
| 94 | uint16_t dlci; |
| 95 | struct osmo_fr_dlc *dlc; |
| 96 | }; |
| 97 | |
| 98 | static void free_vc(struct gprs_ns2_vc *nsvc) |
| 99 | { |
| 100 | OSMO_ASSERT(nsvc); |
| 101 | |
| 102 | if (!nsvc->priv) |
| 103 | return; |
| 104 | |
| 105 | talloc_free(nsvc->priv); |
| 106 | nsvc->priv = NULL; |
| 107 | } |
| 108 | |
| 109 | static void dump_vty(const struct gprs_ns2_vc_bind *bind, struct vty *vty, bool _stats) |
| 110 | { |
| 111 | struct priv_bind *priv; |
| 112 | struct gprs_ns2_vc *nsvc; |
Harald Welte | 48bd76c | 2020-12-01 16:53:06 +0100 | [diff] [blame] | 113 | struct osmo_fr_link *fr_link; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 114 | |
| 115 | if (!bind) |
| 116 | return; |
| 117 | |
| 118 | priv = bind->priv; |
Harald Welte | 48bd76c | 2020-12-01 16:53:06 +0100 | [diff] [blame] | 119 | fr_link = priv->link; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 120 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 121 | vty_out(vty, "FR bind: %s, role: %s, link: %s%s", priv->netif, |
| 122 | osmo_fr_role_str(fr_link->role), priv->if_running ? "UP" : "DOWN", VTY_NEWLINE); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 123 | |
| 124 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
Harald Welte | 96ec84a | 2020-12-01 17:56:05 +0100 | [diff] [blame] | 125 | vty_out(vty, " NSVCI %05u: %s%s", nsvc->nsvci, gprs_ns2_ll_str(nsvc), VTY_NEWLINE); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | priv = bind->priv; |
| 129 | } |
| 130 | |
| 131 | /*! clean up all private driver state. Should be only called by gprs_ns2_free_bind() */ |
| 132 | static void free_bind(struct gprs_ns2_vc_bind *bind) |
| 133 | { |
| 134 | struct priv_bind *priv; |
| 135 | |
| 136 | if (!bind) |
| 137 | return; |
| 138 | |
| 139 | priv = bind->priv; |
| 140 | |
| 141 | OSMO_ASSERT(llist_empty(&bind->nsvc)); |
| 142 | |
| 143 | osmo_fr_link_free(priv->link); |
| 144 | osmo_fd_close(&priv->fd); |
| 145 | talloc_free(priv); |
| 146 | } |
| 147 | |
| 148 | static struct priv_vc *fr_alloc_vc(struct gprs_ns2_vc_bind *bind, |
| 149 | struct gprs_ns2_vc *nsvc, |
| 150 | uint16_t dlci) |
| 151 | { |
| 152 | struct priv_bind *privb = bind->priv; |
| 153 | struct priv_vc *priv = talloc_zero(bind, struct priv_vc); |
| 154 | if (!priv) |
| 155 | return NULL; |
| 156 | |
| 157 | nsvc->priv = priv; |
| 158 | priv->dlci = dlci; |
| 159 | priv->dlc = osmo_fr_dlc_alloc(privb->link, dlci); |
| 160 | if (!priv->dlc) { |
| 161 | nsvc->priv = NULL; |
| 162 | talloc_free(priv); |
| 163 | return NULL; |
| 164 | } |
| 165 | |
| 166 | priv->dlc->rx_cb_data = nsvc; |
| 167 | priv->dlc->rx_cb = fr_dlci_rx_cb; |
| 168 | |
| 169 | return priv; |
| 170 | } |
| 171 | |
| 172 | int gprs_ns2_find_vc_by_dlci(struct gprs_ns2_vc_bind *bind, |
| 173 | uint16_t dlci, |
| 174 | struct gprs_ns2_vc **result) |
| 175 | { |
| 176 | struct gprs_ns2_vc *nsvc; |
| 177 | struct priv_vc *vcpriv; |
| 178 | |
| 179 | if (!result) |
| 180 | return -EINVAL; |
| 181 | |
| 182 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
| 183 | vcpriv = nsvc->priv; |
| 184 | if (vcpriv->dlci != dlci) { |
| 185 | *result = nsvc; |
| 186 | return 0; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | return 1; |
| 191 | } |
| 192 | |
| 193 | /* PDU from the network interface towards the fr layer (upwards) */ |
| 194 | static int handle_netif_read(struct osmo_fd *bfd) |
| 195 | { |
| 196 | struct gprs_ns2_vc_bind *bind = bfd->data; |
| 197 | struct priv_bind *priv = bind->priv; |
| 198 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Gb/NS/FR/GRE Rx"); |
| 199 | int rc = 0; |
| 200 | |
| 201 | if (!msg) |
| 202 | return -ENOMEM; |
| 203 | |
| 204 | rc = read(bfd->fd, msg->data, NS_ALLOC_SIZE); |
| 205 | if (rc < 0) { |
| 206 | LOGP(DLNS, LOGL_ERROR, "recv error %s during NS-FR-GRE recv\n", |
| 207 | strerror(errno)); |
| 208 | goto out_err; |
| 209 | } else if (rc == 0) { |
| 210 | goto out_err; |
| 211 | } |
| 212 | |
| 213 | msgb_put(msg, rc); |
| 214 | msg->dst = priv->link; |
| 215 | return osmo_fr_rx(msg); |
| 216 | |
| 217 | out_err: |
| 218 | msgb_free(msg); |
| 219 | return rc; |
| 220 | } |
| 221 | |
| 222 | /* PDU from the frame relay towards the NS-VC (upwards) */ |
| 223 | static int fr_dlci_rx_cb(void *cb_data, struct msgb *msg) |
| 224 | { |
| 225 | int rc; |
| 226 | struct gprs_ns2_vc *nsvc = cb_data; |
| 227 | |
| 228 | rc = ns2_recv_vc(nsvc, msg); |
| 229 | |
| 230 | return rc; |
| 231 | } |
| 232 | |
| 233 | static int handle_netif_write(struct osmo_fd *bfd) |
| 234 | { |
| 235 | /* FIXME */ |
| 236 | return -EIO; |
| 237 | } |
| 238 | |
| 239 | static int fr_fd_cb(struct osmo_fd *bfd, unsigned int what) |
| 240 | { |
| 241 | int rc = 0; |
| 242 | |
| 243 | if (what & OSMO_FD_READ) |
| 244 | rc = handle_netif_read(bfd); |
| 245 | if (what & OSMO_FD_WRITE) |
| 246 | rc = handle_netif_write(bfd); |
| 247 | |
| 248 | return rc; |
| 249 | } |
| 250 | |
| 251 | /*! determine if given bind is for FR-GRE encapsulation. */ |
| 252 | int gprs_ns2_is_fr_bind(struct gprs_ns2_vc_bind *bind) |
| 253 | { |
| 254 | return (bind->driver == &vc_driver_fr); |
| 255 | } |
| 256 | |
| 257 | /* PDU from the NS-VC towards the frame relay layer (downwards) */ |
| 258 | static int fr_vc_sendmsg(struct gprs_ns2_vc *nsvc, struct msgb *msg) |
| 259 | { |
| 260 | struct priv_vc *vcpriv = nsvc->priv; |
| 261 | |
| 262 | msg->dst = vcpriv->dlc; |
| 263 | return osmo_fr_tx_dlc(msg); |
| 264 | } |
| 265 | |
| 266 | /* PDU from the frame relay layer towards the network interface (downwards) */ |
| 267 | int fr_tx_cb(void *data, struct msgb *msg) |
| 268 | { |
| 269 | struct gprs_ns2_vc_bind *bind = data; |
| 270 | struct priv_bind *priv = bind->priv; |
| 271 | int rc; |
| 272 | |
| 273 | /* FIXME half writes */ |
| 274 | rc = write(priv->fd.fd, msg->data, msg->len); |
| 275 | msgb_free(msg); |
| 276 | |
| 277 | return rc; |
| 278 | } |
| 279 | |
| 280 | static int devname2ifindex(const char *ifname) |
| 281 | { |
| 282 | struct ifreq ifr; |
| 283 | int sk, rc; |
| 284 | |
| 285 | sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 286 | if (sk < 0) |
| 287 | return sk; |
| 288 | |
| 289 | |
| 290 | memset(&ifr, 0, sizeof(ifr)); |
| 291 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 292 | ifr.ifr_name[sizeof(ifr.ifr_name)-1] = 0; |
| 293 | |
| 294 | rc = ioctl(sk, SIOCGIFINDEX, &ifr); |
| 295 | close(sk); |
| 296 | if (rc < 0) |
| 297 | return rc; |
| 298 | |
| 299 | return ifr.ifr_ifindex; |
| 300 | } |
| 301 | |
| 302 | static int open_socket(const char *ifname) |
| 303 | { |
| 304 | struct sockaddr_ll addr; |
| 305 | int ifindex; |
| 306 | int fd, rc, on = 1; |
| 307 | |
| 308 | ifindex = devname2ifindex(ifname); |
| 309 | if (ifindex < 0) { |
| 310 | LOGP(DLNS, LOGL_ERROR, "Can not get interface index for interface %s\n", ifname); |
| 311 | return ifindex; |
| 312 | } |
| 313 | |
| 314 | 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 | |
| 319 | fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); |
| 320 | if (fd < 0) { |
| 321 | LOGP(DLNS, LOGL_ERROR, "Can not get socket for interface %s. Are you root or have CAP_RAW_SOCKET?\n", ifname); |
| 322 | return fd; |
| 323 | } |
| 324 | |
| 325 | if (ioctl(fd, FIONBIO, (unsigned char *)&on) < 0) { |
| 326 | LOGP(DLGLOBAL, LOGL_ERROR, |
| 327 | "cannot set this socket unblocking: %s\n", |
| 328 | strerror(errno)); |
| 329 | close(fd); |
Harald Welte | b6b82da | 2020-11-26 09:30:37 +0100 | [diff] [blame] | 330 | return -EINVAL; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | rc = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); |
| 334 | if (rc < 0) { |
| 335 | LOGP(DLNS, LOGL_ERROR, "Can not bind for interface %s\n", ifname); |
| 336 | close(fd); |
| 337 | return rc; |
| 338 | } |
| 339 | |
| 340 | return fd; |
| 341 | } |
| 342 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 343 | #ifdef ENABLE_LIBMNL |
| 344 | |
| 345 | #include <osmocom/core/mnl.h> |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 346 | #include <linux/if_link.h> |
| 347 | #include <linux/rtnetlink.h> |
| 348 | |
| 349 | #ifndef ARPHRD_FRAD |
| 350 | #define ARPHRD_FRAD 770 |
| 351 | #endif |
| 352 | |
| 353 | /* validate the netlink attributes */ |
| 354 | static int data_attr_cb(const struct nlattr *attr, void *data) |
| 355 | { |
| 356 | const struct nlattr **tb = data; |
| 357 | int type = mnl_attr_get_type(attr); |
| 358 | |
| 359 | if (mnl_attr_type_valid(attr, IFLA_MAX) < 0) |
| 360 | return MNL_CB_OK; |
| 361 | |
| 362 | switch (type) { |
| 363 | case IFLA_MTU: |
| 364 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 365 | return MNL_CB_ERROR; |
| 366 | break; |
| 367 | case IFLA_IFNAME: |
| 368 | if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) |
| 369 | return MNL_CB_ERROR; |
| 370 | break; |
| 371 | } |
| 372 | tb[type] = attr; |
| 373 | return MNL_CB_OK; |
| 374 | } |
| 375 | |
| 376 | /* find the bind for the netdev (if any) */ |
| 377 | static struct gprs_ns2_vc_bind *bind4netdev(struct gprs_ns2_inst *nsi, const char *ifname) |
| 378 | { |
| 379 | struct gprs_ns2_vc_bind *bind; |
| 380 | |
| 381 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 382 | struct priv_bind *bpriv = bind->priv; |
| 383 | if (!strcmp(bpriv->netif, ifname)) |
| 384 | return bind; |
| 385 | } |
| 386 | |
| 387 | return NULL; |
| 388 | } |
| 389 | |
| 390 | /* handle a single netlink message received via libmnl */ |
| 391 | static int linkmon_mnl_cb(const struct nlmsghdr *nlh, void *data) |
| 392 | { |
| 393 | struct osmo_mnl *omnl = data; |
| 394 | struct gprs_ns2_vc_bind *bind; |
| 395 | struct nlattr *tb[IFLA_MAX+1] = {}; |
| 396 | struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh); |
| 397 | struct gprs_ns2_inst *nsi; |
| 398 | const char *ifname; |
| 399 | bool if_running; |
| 400 | |
| 401 | OSMO_ASSERT(omnl); |
| 402 | OSMO_ASSERT(ifm); |
| 403 | |
| 404 | nsi = omnl->priv; |
| 405 | |
| 406 | if (ifm->ifi_type != ARPHRD_FRAD) |
| 407 | return MNL_CB_OK; |
| 408 | |
| 409 | mnl_attr_parse(nlh, sizeof(*ifm), data_attr_cb, tb); |
| 410 | |
| 411 | if (!tb[IFLA_IFNAME]) |
| 412 | return MNL_CB_OK; |
| 413 | ifname = mnl_attr_get_str(tb[IFLA_IFNAME]); |
| 414 | if_running = !!(ifm->ifi_flags & IFF_RUNNING); |
| 415 | |
| 416 | bind = bind4netdev(nsi, ifname); |
| 417 | if (bind) { |
| 418 | struct priv_bind *bpriv = bind->priv; |
| 419 | if (bpriv->if_running != if_running) { |
| 420 | /* update running state */ |
| 421 | LOGP(DLNS, LOGL_NOTICE, "FR net-device '%s': Physical link state changed: %s\n", |
| 422 | ifname, if_running ? "UP" : "DOWN"); |
| 423 | bpriv->if_running = if_running; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | return MNL_CB_OK; |
| 428 | } |
| 429 | |
| 430 | /* trigger one initial dump of all link information */ |
| 431 | static void linkmon_initial_dump(struct osmo_mnl *omnl) |
| 432 | { |
| 433 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 434 | struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf); |
| 435 | struct rtgenmsg *rt; |
| 436 | |
| 437 | nlh->nlmsg_type = RTM_GETLINK; |
| 438 | nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
| 439 | nlh->nlmsg_seq = time(NULL); |
| 440 | rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg)); |
| 441 | rt->rtgen_family = AF_PACKET; |
| 442 | |
| 443 | if (mnl_socket_sendto(omnl->mnls, nlh, nlh->nlmsg_len) < 0) { |
| 444 | LOGP(DLNS, LOGL_ERROR, "linkmon: Cannot send rtnetlink message: %s\n", strerror(errno)); |
| 445 | } |
| 446 | |
| 447 | /* the response[s] will be handled just like the events */ |
| 448 | } |
| 449 | #endif /* LIBMNL */ |
| 450 | |
| 451 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 452 | /*! Create a new bind for NS over FR. |
| 453 | * \param[in] nsi NS instance in which to create the bind |
| 454 | * \param[in] netif Network interface to bind to |
| 455 | * \param[in] fr_network |
| 456 | * \param[in] fr_role |
| 457 | * \param[out] result pointer to created bind |
| 458 | * \return 0 on success; negative on error */ |
| 459 | int gprs_ns2_fr_bind(struct gprs_ns2_inst *nsi, |
| 460 | const char *netif, |
| 461 | struct osmo_fr_network *fr_network, |
| 462 | enum osmo_fr_role fr_role, |
| 463 | struct gprs_ns2_vc_bind **result) |
| 464 | { |
| 465 | struct gprs_ns2_vc_bind *bind = talloc_zero(nsi, struct gprs_ns2_vc_bind); |
| 466 | struct priv_bind *priv; |
| 467 | struct osmo_fr_link *fr_link; |
| 468 | int rc = 0; |
| 469 | |
| 470 | if (!bind) |
| 471 | return -ENOSPC; |
| 472 | |
| 473 | bind->driver = &vc_driver_fr; |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 474 | bind->ll = GPRS_NS2_LL_FR; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 475 | bind->send_vc = fr_vc_sendmsg; |
| 476 | bind->free_vc = free_vc; |
| 477 | bind->dump_vty = dump_vty; |
| 478 | bind->nsi = nsi; |
| 479 | priv = bind->priv = talloc_zero(bind, struct priv_bind); |
| 480 | if (!priv) { |
| 481 | rc = -ENOSPC; |
| 482 | goto err_bind; |
| 483 | } |
| 484 | |
| 485 | priv->fd.cb = fr_fd_cb; |
| 486 | priv->fd.data = bind; |
| 487 | if (strlen(netif) > IF_NAMESIZE) { |
| 488 | rc = -EINVAL; |
| 489 | goto err_priv; |
| 490 | } |
| 491 | strncpy(priv->netif, netif, sizeof(priv->netif)); |
| 492 | |
| 493 | ns2_vty_bind_apply(bind); |
| 494 | if (result) |
| 495 | *result = bind; |
| 496 | |
| 497 | /* FIXME: move fd handling into socket.c */ |
| 498 | fr_link = osmo_fr_link_alloc(fr_network, fr_role, netif); |
| 499 | if (!fr_link) { |
| 500 | rc = -EINVAL; |
| 501 | goto err_priv; |
| 502 | } |
| 503 | |
| 504 | fr_link->tx_cb = fr_tx_cb; |
| 505 | fr_link->tx_cb_data = bind; |
| 506 | priv->link = fr_link; |
| 507 | priv->fd.fd = rc = open_socket(netif); |
| 508 | if (rc < 0) |
| 509 | goto err_fr; |
| 510 | |
| 511 | priv->fd.when = OSMO_FD_READ; |
| 512 | rc = osmo_fd_register(&priv->fd); |
| 513 | if (rc < 0) |
| 514 | goto err_fd; |
| 515 | |
| 516 | INIT_LLIST_HEAD(&bind->nsvc); |
| 517 | llist_add(&bind->list, &nsi->binding); |
| 518 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 519 | #ifdef ENABLE_LIBMNL |
| 520 | if (!nsi->linkmon_mnl) |
| 521 | nsi->linkmon_mnl = osmo_mnl_init(nsi, NETLINK_ROUTE, RTMGRP_LINK, linkmon_mnl_cb, nsi); |
| 522 | |
| 523 | /* we get a new full dump after every bind. which is a bit excessive. But that's just once |
| 524 | * at start-up, so we can get away with it */ |
| 525 | if (nsi->linkmon_mnl) |
| 526 | linkmon_initial_dump(nsi->linkmon_mnl); |
| 527 | #endif |
| 528 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 529 | return rc; |
| 530 | |
| 531 | err_fd: |
| 532 | close(priv->fd.fd); |
| 533 | err_fr: |
| 534 | osmo_fr_link_free(fr_link); |
| 535 | err_priv: |
| 536 | talloc_free(priv); |
| 537 | err_bind: |
| 538 | talloc_free(bind); |
| 539 | |
| 540 | return rc; |
| 541 | } |
| 542 | |
| 543 | /*! Return the network interface of the bind |
| 544 | * \param[in] bind The bind |
| 545 | * \return the network interface |
| 546 | */ |
| 547 | const char *gprs_ns2_fr_bind_netif(struct gprs_ns2_vc_bind *bind) |
| 548 | { |
| 549 | struct priv_bind *priv; |
| 550 | |
| 551 | if (bind->driver != &vc_driver_fr) |
| 552 | return NULL; |
| 553 | |
| 554 | priv = bind->priv; |
| 555 | return priv->netif; |
| 556 | } |
| 557 | |
| 558 | /*! Find NS bind for a given network interface |
| 559 | * \param[in] nsi NS instance |
| 560 | * \param[in] netif the network interface to search for |
| 561 | * \return the bind or NULL if not found |
| 562 | */ |
| 563 | struct gprs_ns2_vc_bind *gprs_ns2_fr_bind_by_netif( |
| 564 | struct gprs_ns2_inst *nsi, |
| 565 | const char *netif) |
| 566 | { |
| 567 | struct gprs_ns2_vc_bind *bind; |
| 568 | const char *_netif; |
| 569 | |
| 570 | OSMO_ASSERT(nsi); |
| 571 | OSMO_ASSERT(netif); |
| 572 | |
| 573 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 574 | if (!gprs_ns2_is_fr_bind(bind)) |
| 575 | continue; |
| 576 | |
| 577 | _netif = gprs_ns2_fr_bind_netif(bind); |
| 578 | if (!strncmp(_netif, netif, IF_NAMESIZE)) |
| 579 | return bind; |
| 580 | } |
| 581 | |
| 582 | return NULL; |
| 583 | } |
| 584 | |
| 585 | /*! Create, connect and activate a new FR-based NS-VC |
| 586 | * \param[in] bind bind in which the new NS-VC is to be created |
| 587 | * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created |
| 588 | * \param[in] dlci Data Link connection identifier |
| 589 | * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */ |
| 590 | struct gprs_ns2_vc *gprs_ns2_fr_connect(struct gprs_ns2_vc_bind *bind, |
| 591 | uint16_t nsei, |
| 592 | uint16_t nsvci, |
| 593 | uint16_t dlci) |
| 594 | { |
| 595 | bool created_nse = false; |
| 596 | struct gprs_ns2_vc *nsvc = NULL; |
| 597 | struct priv_vc *priv = NULL; |
| 598 | struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei); |
| 599 | if (!nse) { |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 600 | nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_FR); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 601 | if (!nse) |
| 602 | return NULL; |
| 603 | created_nse = true; |
| 604 | } |
| 605 | |
| 606 | nsvc = gprs_ns2_fr_nsvc_by_dlci(bind, dlci); |
| 607 | if (nsvc) { |
| 608 | goto err_nse; |
| 609 | } |
| 610 | |
| 611 | nsvc = ns2_vc_alloc(bind, nse, true); |
| 612 | if (!nsvc) |
| 613 | goto err_nse; |
| 614 | |
| 615 | nsvc->priv = priv = fr_alloc_vc(bind, nsvc, dlci); |
| 616 | if (!priv) |
| 617 | goto err; |
| 618 | |
| 619 | nsvc->nsvci = nsvci; |
| 620 | nsvc->nsvci_is_valid = true; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 621 | |
| 622 | gprs_ns2_vc_fsm_start(nsvc); |
| 623 | |
| 624 | return nsvc; |
| 625 | |
| 626 | err: |
| 627 | gprs_ns2_free_nsvc(nsvc); |
| 628 | err_nse: |
| 629 | if (created_nse) |
| 630 | gprs_ns2_free_nse(nse); |
| 631 | |
| 632 | return NULL; |
| 633 | } |
| 634 | |
| 635 | /*! Return the nsvc by dlci. |
| 636 | * \param[in] bind |
| 637 | * \param[in] dlci Data Link connection identifier |
| 638 | * \return the nsvc or NULL if not found |
| 639 | */ |
| 640 | struct gprs_ns2_vc *gprs_ns2_fr_nsvc_by_dlci(struct gprs_ns2_vc_bind *bind, |
| 641 | uint16_t dlci) |
| 642 | { |
| 643 | struct gprs_ns2_vc *nsvc; |
| 644 | struct priv_vc *vcpriv; |
| 645 | |
| 646 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
| 647 | vcpriv = nsvc->priv; |
| 648 | |
| 649 | if (dlci == vcpriv->dlci) |
| 650 | return nsvc; |
| 651 | } |
| 652 | |
| 653 | return NULL; |
| 654 | } |
| 655 | |
| 656 | /*! Return the dlci of the nsvc |
| 657 | * \param[in] nsvc |
| 658 | * \return the dlci or 0 on error. 0 is not a valid dlci. |
| 659 | */ |
| 660 | uint16_t gprs_ns2_fr_nsvc_dlci(struct gprs_ns2_vc *nsvc) |
| 661 | { |
| 662 | struct priv_vc *vcpriv; |
| 663 | |
| 664 | if (!nsvc->bind) |
| 665 | return 0; |
| 666 | |
| 667 | if (nsvc->bind->driver != &vc_driver_fr) |
| 668 | return 0; |
| 669 | |
| 670 | vcpriv = nsvc->priv; |
| 671 | return vcpriv->dlci; |
| 672 | } |