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 | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 89 | int ifindex; |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 90 | bool if_running; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | struct priv_vc { |
| 94 | struct osmo_sockaddr remote; |
| 95 | uint16_t dlci; |
| 96 | struct osmo_fr_dlc *dlc; |
| 97 | }; |
| 98 | |
| 99 | static void free_vc(struct gprs_ns2_vc *nsvc) |
| 100 | { |
| 101 | OSMO_ASSERT(nsvc); |
| 102 | |
| 103 | if (!nsvc->priv) |
| 104 | return; |
| 105 | |
| 106 | talloc_free(nsvc->priv); |
| 107 | nsvc->priv = NULL; |
| 108 | } |
| 109 | |
| 110 | static void dump_vty(const struct gprs_ns2_vc_bind *bind, struct vty *vty, bool _stats) |
| 111 | { |
| 112 | struct priv_bind *priv; |
| 113 | struct gprs_ns2_vc *nsvc; |
Harald Welte | 48bd76c | 2020-12-01 16:53:06 +0100 | [diff] [blame] | 114 | struct osmo_fr_link *fr_link; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 115 | |
| 116 | if (!bind) |
| 117 | return; |
| 118 | |
| 119 | priv = bind->priv; |
Harald Welte | 48bd76c | 2020-12-01 16:53:06 +0100 | [diff] [blame] | 120 | fr_link = priv->link; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 121 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 122 | vty_out(vty, "FR bind: %s, role: %s, link: %s%s", priv->netif, |
| 123 | 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] | 124 | |
| 125 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
Harald Welte | 96ec84a | 2020-12-01 17:56:05 +0100 | [diff] [blame] | 126 | 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] | 127 | } |
| 128 | |
| 129 | priv = bind->priv; |
| 130 | } |
| 131 | |
| 132 | /*! clean up all private driver state. Should be only called by gprs_ns2_free_bind() */ |
| 133 | static void free_bind(struct gprs_ns2_vc_bind *bind) |
| 134 | { |
| 135 | struct priv_bind *priv; |
| 136 | |
| 137 | if (!bind) |
| 138 | return; |
| 139 | |
| 140 | priv = bind->priv; |
| 141 | |
| 142 | OSMO_ASSERT(llist_empty(&bind->nsvc)); |
| 143 | |
| 144 | osmo_fr_link_free(priv->link); |
| 145 | osmo_fd_close(&priv->fd); |
| 146 | talloc_free(priv); |
| 147 | } |
| 148 | |
| 149 | static struct priv_vc *fr_alloc_vc(struct gprs_ns2_vc_bind *bind, |
| 150 | struct gprs_ns2_vc *nsvc, |
| 151 | uint16_t dlci) |
| 152 | { |
| 153 | struct priv_bind *privb = bind->priv; |
| 154 | struct priv_vc *priv = talloc_zero(bind, struct priv_vc); |
| 155 | if (!priv) |
| 156 | return NULL; |
| 157 | |
| 158 | nsvc->priv = priv; |
| 159 | priv->dlci = dlci; |
| 160 | priv->dlc = osmo_fr_dlc_alloc(privb->link, dlci); |
| 161 | if (!priv->dlc) { |
| 162 | nsvc->priv = NULL; |
| 163 | talloc_free(priv); |
| 164 | return NULL; |
| 165 | } |
| 166 | |
| 167 | priv->dlc->rx_cb_data = nsvc; |
| 168 | priv->dlc->rx_cb = fr_dlci_rx_cb; |
| 169 | |
| 170 | return priv; |
| 171 | } |
| 172 | |
| 173 | int gprs_ns2_find_vc_by_dlci(struct gprs_ns2_vc_bind *bind, |
| 174 | uint16_t dlci, |
| 175 | struct gprs_ns2_vc **result) |
| 176 | { |
| 177 | struct gprs_ns2_vc *nsvc; |
| 178 | struct priv_vc *vcpriv; |
| 179 | |
| 180 | if (!result) |
| 181 | return -EINVAL; |
| 182 | |
| 183 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
| 184 | vcpriv = nsvc->priv; |
| 185 | if (vcpriv->dlci != dlci) { |
| 186 | *result = nsvc; |
| 187 | return 0; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | /* PDU from the network interface towards the fr layer (upwards) */ |
| 195 | static int handle_netif_read(struct osmo_fd *bfd) |
| 196 | { |
| 197 | struct gprs_ns2_vc_bind *bind = bfd->data; |
| 198 | struct priv_bind *priv = bind->priv; |
| 199 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Gb/NS/FR/GRE Rx"); |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 200 | struct sockaddr_ll sll; |
| 201 | socklen_t sll_len = sizeof(sll); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 202 | int rc = 0; |
| 203 | |
| 204 | if (!msg) |
| 205 | return -ENOMEM; |
| 206 | |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 207 | rc = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0, (struct sockaddr *)&sll, &sll_len); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 208 | if (rc < 0) { |
| 209 | LOGP(DLNS, LOGL_ERROR, "recv error %s during NS-FR-GRE recv\n", |
| 210 | strerror(errno)); |
| 211 | goto out_err; |
| 212 | } else if (rc == 0) { |
| 213 | goto out_err; |
| 214 | } |
| 215 | |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 216 | /* ignore any packets that we might have received for a different interface, between |
| 217 | * the socket() and the bind() call */ |
| 218 | if (sll.sll_ifindex != priv->ifindex) |
| 219 | goto out_err; |
| 220 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 221 | msgb_put(msg, rc); |
| 222 | msg->dst = priv->link; |
| 223 | return osmo_fr_rx(msg); |
| 224 | |
| 225 | out_err: |
| 226 | msgb_free(msg); |
| 227 | return rc; |
| 228 | } |
| 229 | |
| 230 | /* PDU from the frame relay towards the NS-VC (upwards) */ |
| 231 | static int fr_dlci_rx_cb(void *cb_data, struct msgb *msg) |
| 232 | { |
| 233 | int rc; |
| 234 | struct gprs_ns2_vc *nsvc = cb_data; |
| 235 | |
| 236 | rc = ns2_recv_vc(nsvc, msg); |
| 237 | |
| 238 | return rc; |
| 239 | } |
| 240 | |
| 241 | static int handle_netif_write(struct osmo_fd *bfd) |
| 242 | { |
| 243 | /* FIXME */ |
| 244 | return -EIO; |
| 245 | } |
| 246 | |
| 247 | static int fr_fd_cb(struct osmo_fd *bfd, unsigned int what) |
| 248 | { |
| 249 | int rc = 0; |
| 250 | |
| 251 | if (what & OSMO_FD_READ) |
| 252 | rc = handle_netif_read(bfd); |
| 253 | if (what & OSMO_FD_WRITE) |
| 254 | rc = handle_netif_write(bfd); |
| 255 | |
| 256 | return rc; |
| 257 | } |
| 258 | |
| 259 | /*! determine if given bind is for FR-GRE encapsulation. */ |
| 260 | int gprs_ns2_is_fr_bind(struct gprs_ns2_vc_bind *bind) |
| 261 | { |
| 262 | return (bind->driver == &vc_driver_fr); |
| 263 | } |
| 264 | |
| 265 | /* PDU from the NS-VC towards the frame relay layer (downwards) */ |
| 266 | static int fr_vc_sendmsg(struct gprs_ns2_vc *nsvc, struct msgb *msg) |
| 267 | { |
| 268 | struct priv_vc *vcpriv = nsvc->priv; |
| 269 | |
| 270 | msg->dst = vcpriv->dlc; |
| 271 | return osmo_fr_tx_dlc(msg); |
| 272 | } |
| 273 | |
| 274 | /* PDU from the frame relay layer towards the network interface (downwards) */ |
| 275 | int fr_tx_cb(void *data, struct msgb *msg) |
| 276 | { |
| 277 | struct gprs_ns2_vc_bind *bind = data; |
| 278 | struct priv_bind *priv = bind->priv; |
| 279 | int rc; |
| 280 | |
| 281 | /* FIXME half writes */ |
| 282 | rc = write(priv->fd.fd, msg->data, msg->len); |
| 283 | msgb_free(msg); |
| 284 | |
| 285 | return rc; |
| 286 | } |
| 287 | |
| 288 | static 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)); |
| 299 | strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)); |
| 300 | ifr.ifr_name[sizeof(ifr.ifr_name)-1] = 0; |
| 301 | |
| 302 | rc = ioctl(sk, SIOCGIFINDEX, &ifr); |
| 303 | close(sk); |
| 304 | if (rc < 0) |
| 305 | return rc; |
| 306 | |
| 307 | return ifr.ifr_ifindex; |
| 308 | } |
| 309 | |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 310 | static int open_socket(int ifindex) |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 311 | { |
| 312 | struct sockaddr_ll addr; |
Harald Welte | 4ed0f4e | 2020-12-10 21:50:32 +0100 | [diff] [blame] | 313 | int fd, rc; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 314 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 315 | memset(&addr, 0, sizeof(addr)); |
| 316 | addr.sll_family = AF_PACKET; |
| 317 | addr.sll_protocol = htons(ETH_P_ALL); |
| 318 | addr.sll_ifindex = ifindex; |
| 319 | |
Harald Welte | 5bea72e | 2020-12-10 22:06:21 +0100 | [diff] [blame] | 320 | fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_HDLC)); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 321 | if (fd < 0) { |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 322 | LOGP(DLNS, LOGL_ERROR, "Can not create AF_PACKET socket. Are you root or have CAP_RAW_SOCKET?\n"); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 323 | return fd; |
| 324 | } |
| 325 | |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 326 | /* there's a race condition between the above syscall and the bind() call below, |
| 327 | * causing other packets to be received in between */ |
| 328 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 329 | rc = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); |
| 330 | if (rc < 0) { |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 331 | LOGP(DLNS, LOGL_ERROR, "Can not bind AF_PACKET socket to ifindex %d\n", ifindex); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 332 | close(fd); |
| 333 | return rc; |
| 334 | } |
| 335 | |
| 336 | return fd; |
| 337 | } |
| 338 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 339 | #ifdef ENABLE_LIBMNL |
| 340 | |
| 341 | #include <osmocom/core/mnl.h> |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 342 | #include <linux/if_link.h> |
| 343 | #include <linux/rtnetlink.h> |
| 344 | |
| 345 | #ifndef ARPHRD_FRAD |
| 346 | #define ARPHRD_FRAD 770 |
| 347 | #endif |
| 348 | |
| 349 | /* validate the netlink attributes */ |
| 350 | static int data_attr_cb(const struct nlattr *attr, void *data) |
| 351 | { |
| 352 | const struct nlattr **tb = data; |
| 353 | int type = mnl_attr_get_type(attr); |
| 354 | |
| 355 | if (mnl_attr_type_valid(attr, IFLA_MAX) < 0) |
| 356 | return MNL_CB_OK; |
| 357 | |
| 358 | switch (type) { |
| 359 | case IFLA_MTU: |
| 360 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 361 | return MNL_CB_ERROR; |
| 362 | break; |
| 363 | case IFLA_IFNAME: |
| 364 | if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) |
| 365 | return MNL_CB_ERROR; |
| 366 | break; |
| 367 | } |
| 368 | tb[type] = attr; |
| 369 | return MNL_CB_OK; |
| 370 | } |
| 371 | |
| 372 | /* find the bind for the netdev (if any) */ |
| 373 | static struct gprs_ns2_vc_bind *bind4netdev(struct gprs_ns2_inst *nsi, const char *ifname) |
| 374 | { |
| 375 | struct gprs_ns2_vc_bind *bind; |
| 376 | |
| 377 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 378 | struct priv_bind *bpriv = bind->priv; |
| 379 | if (!strcmp(bpriv->netif, ifname)) |
| 380 | return bind; |
| 381 | } |
| 382 | |
| 383 | return NULL; |
| 384 | } |
| 385 | |
| 386 | /* handle a single netlink message received via libmnl */ |
| 387 | static int linkmon_mnl_cb(const struct nlmsghdr *nlh, void *data) |
| 388 | { |
| 389 | struct osmo_mnl *omnl = data; |
| 390 | struct gprs_ns2_vc_bind *bind; |
| 391 | struct nlattr *tb[IFLA_MAX+1] = {}; |
| 392 | struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh); |
| 393 | struct gprs_ns2_inst *nsi; |
| 394 | const char *ifname; |
| 395 | bool if_running; |
| 396 | |
| 397 | OSMO_ASSERT(omnl); |
| 398 | OSMO_ASSERT(ifm); |
| 399 | |
| 400 | nsi = omnl->priv; |
| 401 | |
| 402 | if (ifm->ifi_type != ARPHRD_FRAD) |
| 403 | return MNL_CB_OK; |
| 404 | |
| 405 | mnl_attr_parse(nlh, sizeof(*ifm), data_attr_cb, tb); |
| 406 | |
| 407 | if (!tb[IFLA_IFNAME]) |
| 408 | return MNL_CB_OK; |
| 409 | ifname = mnl_attr_get_str(tb[IFLA_IFNAME]); |
| 410 | if_running = !!(ifm->ifi_flags & IFF_RUNNING); |
| 411 | |
| 412 | bind = bind4netdev(nsi, ifname); |
| 413 | if (bind) { |
| 414 | struct priv_bind *bpriv = bind->priv; |
| 415 | if (bpriv->if_running != if_running) { |
| 416 | /* update running state */ |
| 417 | LOGP(DLNS, LOGL_NOTICE, "FR net-device '%s': Physical link state changed: %s\n", |
| 418 | ifname, if_running ? "UP" : "DOWN"); |
| 419 | bpriv->if_running = if_running; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | return MNL_CB_OK; |
| 424 | } |
| 425 | |
| 426 | /* trigger one initial dump of all link information */ |
| 427 | static void linkmon_initial_dump(struct osmo_mnl *omnl) |
| 428 | { |
| 429 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 430 | struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf); |
| 431 | struct rtgenmsg *rt; |
| 432 | |
| 433 | nlh->nlmsg_type = RTM_GETLINK; |
| 434 | nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
| 435 | nlh->nlmsg_seq = time(NULL); |
| 436 | rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg)); |
| 437 | rt->rtgen_family = AF_PACKET; |
| 438 | |
| 439 | if (mnl_socket_sendto(omnl->mnls, nlh, nlh->nlmsg_len) < 0) { |
| 440 | LOGP(DLNS, LOGL_ERROR, "linkmon: Cannot send rtnetlink message: %s\n", strerror(errno)); |
| 441 | } |
| 442 | |
| 443 | /* the response[s] will be handled just like the events */ |
| 444 | } |
| 445 | #endif /* LIBMNL */ |
| 446 | |
| 447 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 448 | /*! Create a new bind for NS over FR. |
| 449 | * \param[in] nsi NS instance in which to create the bind |
| 450 | * \param[in] netif Network interface to bind to |
| 451 | * \param[in] fr_network |
| 452 | * \param[in] fr_role |
| 453 | * \param[out] result pointer to created bind |
| 454 | * \return 0 on success; negative on error */ |
| 455 | int gprs_ns2_fr_bind(struct gprs_ns2_inst *nsi, |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 456 | const char *name, |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 457 | const char *netif, |
| 458 | struct osmo_fr_network *fr_network, |
| 459 | enum osmo_fr_role fr_role, |
| 460 | struct gprs_ns2_vc_bind **result) |
| 461 | { |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 462 | struct gprs_ns2_vc_bind *bind; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 463 | struct priv_bind *priv; |
| 464 | struct osmo_fr_link *fr_link; |
| 465 | int rc = 0; |
| 466 | |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 467 | if (!name) |
| 468 | return -EINVAL; |
| 469 | |
| 470 | if (gprs_ns2_bind_by_name(nsi, name)) |
| 471 | return -EALREADY; |
| 472 | |
| 473 | bind = talloc_zero(nsi, struct gprs_ns2_vc_bind); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 474 | if (!bind) |
| 475 | return -ENOSPC; |
| 476 | |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 477 | bind->name = talloc_strdup(bind, name); |
| 478 | if (!bind->name) { |
| 479 | rc = -ENOSPC; |
| 480 | goto err_bind; |
| 481 | } |
| 482 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 483 | bind->driver = &vc_driver_fr; |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 484 | bind->ll = GPRS_NS2_LL_FR; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 485 | bind->send_vc = fr_vc_sendmsg; |
| 486 | bind->free_vc = free_vc; |
| 487 | bind->dump_vty = dump_vty; |
| 488 | bind->nsi = nsi; |
| 489 | priv = bind->priv = talloc_zero(bind, struct priv_bind); |
| 490 | if (!priv) { |
| 491 | rc = -ENOSPC; |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 492 | goto err_name; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | priv->fd.cb = fr_fd_cb; |
| 496 | priv->fd.data = bind; |
| 497 | if (strlen(netif) > IF_NAMESIZE) { |
| 498 | rc = -EINVAL; |
| 499 | goto err_priv; |
| 500 | } |
| 501 | strncpy(priv->netif, netif, sizeof(priv->netif)); |
| 502 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 503 | if (result) |
| 504 | *result = bind; |
| 505 | |
| 506 | /* FIXME: move fd handling into socket.c */ |
| 507 | fr_link = osmo_fr_link_alloc(fr_network, fr_role, netif); |
| 508 | if (!fr_link) { |
| 509 | rc = -EINVAL; |
| 510 | goto err_priv; |
| 511 | } |
| 512 | |
| 513 | fr_link->tx_cb = fr_tx_cb; |
| 514 | fr_link->tx_cb_data = bind; |
| 515 | priv->link = fr_link; |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 516 | |
| 517 | priv->ifindex = devname2ifindex(netif); |
| 518 | if (priv->ifindex < 0) { |
| 519 | LOGP(DLNS, LOGL_ERROR, "Can not get interface index for interface %s\n", netif); |
| 520 | goto err_fr; |
| 521 | } |
| 522 | |
| 523 | priv->fd.fd = rc = open_socket(priv->ifindex); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 524 | if (rc < 0) |
| 525 | goto err_fr; |
| 526 | |
| 527 | priv->fd.when = OSMO_FD_READ; |
| 528 | rc = osmo_fd_register(&priv->fd); |
| 529 | if (rc < 0) |
| 530 | goto err_fd; |
| 531 | |
| 532 | INIT_LLIST_HEAD(&bind->nsvc); |
| 533 | llist_add(&bind->list, &nsi->binding); |
| 534 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 535 | #ifdef ENABLE_LIBMNL |
| 536 | if (!nsi->linkmon_mnl) |
| 537 | nsi->linkmon_mnl = osmo_mnl_init(nsi, NETLINK_ROUTE, RTMGRP_LINK, linkmon_mnl_cb, nsi); |
| 538 | |
| 539 | /* we get a new full dump after every bind. which is a bit excessive. But that's just once |
| 540 | * at start-up, so we can get away with it */ |
| 541 | if (nsi->linkmon_mnl) |
| 542 | linkmon_initial_dump(nsi->linkmon_mnl); |
| 543 | #endif |
| 544 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 545 | return rc; |
| 546 | |
| 547 | err_fd: |
| 548 | close(priv->fd.fd); |
| 549 | err_fr: |
| 550 | osmo_fr_link_free(fr_link); |
| 551 | err_priv: |
| 552 | talloc_free(priv); |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 553 | err_name: |
| 554 | talloc_free((char *)bind->name); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 555 | err_bind: |
| 556 | talloc_free(bind); |
| 557 | |
| 558 | return rc; |
| 559 | } |
| 560 | |
| 561 | /*! Return the network interface of the bind |
| 562 | * \param[in] bind The bind |
| 563 | * \return the network interface |
| 564 | */ |
| 565 | const char *gprs_ns2_fr_bind_netif(struct gprs_ns2_vc_bind *bind) |
| 566 | { |
| 567 | struct priv_bind *priv; |
| 568 | |
| 569 | if (bind->driver != &vc_driver_fr) |
| 570 | return NULL; |
| 571 | |
| 572 | priv = bind->priv; |
| 573 | return priv->netif; |
| 574 | } |
| 575 | |
| 576 | /*! Find NS bind for a given network interface |
| 577 | * \param[in] nsi NS instance |
| 578 | * \param[in] netif the network interface to search for |
| 579 | * \return the bind or NULL if not found |
| 580 | */ |
| 581 | struct gprs_ns2_vc_bind *gprs_ns2_fr_bind_by_netif( |
| 582 | struct gprs_ns2_inst *nsi, |
| 583 | const char *netif) |
| 584 | { |
| 585 | struct gprs_ns2_vc_bind *bind; |
| 586 | const char *_netif; |
| 587 | |
| 588 | OSMO_ASSERT(nsi); |
| 589 | OSMO_ASSERT(netif); |
| 590 | |
| 591 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 592 | if (!gprs_ns2_is_fr_bind(bind)) |
| 593 | continue; |
| 594 | |
| 595 | _netif = gprs_ns2_fr_bind_netif(bind); |
| 596 | if (!strncmp(_netif, netif, IF_NAMESIZE)) |
| 597 | return bind; |
| 598 | } |
| 599 | |
| 600 | return NULL; |
| 601 | } |
| 602 | |
| 603 | /*! Create, connect and activate a new FR-based NS-VC |
| 604 | * \param[in] bind bind in which the new NS-VC is to be created |
| 605 | * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created |
| 606 | * \param[in] dlci Data Link connection identifier |
| 607 | * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */ |
| 608 | struct gprs_ns2_vc *gprs_ns2_fr_connect(struct gprs_ns2_vc_bind *bind, |
Alexander Couzens | ebcbd72 | 2020-12-03 06:11:39 +0100 | [diff] [blame] | 609 | struct gprs_ns2_nse *nse, |
| 610 | uint16_t nsvci, |
| 611 | uint16_t dlci) |
| 612 | { |
| 613 | struct gprs_ns2_vc *nsvc = NULL; |
| 614 | struct priv_vc *priv = NULL; |
| 615 | |
| 616 | nsvc = gprs_ns2_fr_nsvc_by_dlci(bind, dlci); |
| 617 | if (nsvc) { |
| 618 | goto err; |
| 619 | } |
| 620 | |
| 621 | nsvc = ns2_vc_alloc(bind, nse, true, NS2_VC_MODE_BLOCKRESET); |
| 622 | if (!nsvc) |
| 623 | goto err; |
| 624 | |
| 625 | nsvc->priv = priv = fr_alloc_vc(bind, nsvc, dlci); |
| 626 | if (!priv) |
| 627 | goto err; |
| 628 | |
| 629 | nsvc->nsvci = nsvci; |
| 630 | nsvc->nsvci_is_valid = true; |
| 631 | |
| 632 | gprs_ns2_vc_fsm_start(nsvc); |
| 633 | |
| 634 | return nsvc; |
| 635 | |
| 636 | err: |
| 637 | gprs_ns2_free_nsvc(nsvc); |
| 638 | return NULL; |
| 639 | } |
| 640 | |
| 641 | |
| 642 | /*! Create, connect and activate a new FR-based NS-VC |
| 643 | * \param[in] bind bind in which the new NS-VC is to be created |
| 644 | * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created |
| 645 | * \param[in] dlci Data Link connection identifier |
| 646 | * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */ |
| 647 | struct gprs_ns2_vc *gprs_ns2_fr_connect2(struct gprs_ns2_vc_bind *bind, |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 648 | uint16_t nsei, |
| 649 | uint16_t nsvci, |
| 650 | uint16_t dlci) |
| 651 | { |
| 652 | bool created_nse = false; |
| 653 | struct gprs_ns2_vc *nsvc = NULL; |
| 654 | struct priv_vc *priv = NULL; |
| 655 | struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei); |
| 656 | if (!nse) { |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 657 | nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_FR, NS2_DIALECT_STATIC_RESETBLOCK); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 658 | if (!nse) |
| 659 | return NULL; |
| 660 | created_nse = true; |
| 661 | } |
| 662 | |
| 663 | nsvc = gprs_ns2_fr_nsvc_by_dlci(bind, dlci); |
| 664 | if (nsvc) { |
| 665 | goto err_nse; |
| 666 | } |
| 667 | |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 668 | nsvc = ns2_vc_alloc(bind, nse, true, NS2_VC_MODE_BLOCKRESET); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 669 | if (!nsvc) |
| 670 | goto err_nse; |
| 671 | |
| 672 | nsvc->priv = priv = fr_alloc_vc(bind, nsvc, dlci); |
| 673 | if (!priv) |
| 674 | goto err; |
| 675 | |
| 676 | nsvc->nsvci = nsvci; |
| 677 | nsvc->nsvci_is_valid = true; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 678 | |
| 679 | gprs_ns2_vc_fsm_start(nsvc); |
| 680 | |
| 681 | return nsvc; |
| 682 | |
| 683 | err: |
| 684 | gprs_ns2_free_nsvc(nsvc); |
| 685 | err_nse: |
| 686 | if (created_nse) |
| 687 | gprs_ns2_free_nse(nse); |
| 688 | |
| 689 | return NULL; |
| 690 | } |
| 691 | |
| 692 | /*! Return the nsvc by dlci. |
| 693 | * \param[in] bind |
| 694 | * \param[in] dlci Data Link connection identifier |
| 695 | * \return the nsvc or NULL if not found |
| 696 | */ |
| 697 | struct gprs_ns2_vc *gprs_ns2_fr_nsvc_by_dlci(struct gprs_ns2_vc_bind *bind, |
| 698 | uint16_t dlci) |
| 699 | { |
| 700 | struct gprs_ns2_vc *nsvc; |
| 701 | struct priv_vc *vcpriv; |
| 702 | |
| 703 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
| 704 | vcpriv = nsvc->priv; |
| 705 | |
| 706 | if (dlci == vcpriv->dlci) |
| 707 | return nsvc; |
| 708 | } |
| 709 | |
| 710 | return NULL; |
| 711 | } |
| 712 | |
| 713 | /*! Return the dlci of the nsvc |
| 714 | * \param[in] nsvc |
| 715 | * \return the dlci or 0 on error. 0 is not a valid dlci. |
| 716 | */ |
| 717 | uint16_t gprs_ns2_fr_nsvc_dlci(struct gprs_ns2_vc *nsvc) |
| 718 | { |
| 719 | struct priv_vc *vcpriv; |
| 720 | |
| 721 | if (!nsvc->bind) |
| 722 | return 0; |
| 723 | |
| 724 | if (nsvc->bind->driver != &vc_driver_fr) |
| 725 | return 0; |
| 726 | |
| 727 | vcpriv = nsvc->priv; |
| 728 | return vcpriv->dlci; |
| 729 | } |