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> |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 39 | #include <linux/if.h> |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 40 | |
| 41 | #include <sys/ioctl.h> |
| 42 | #include <netpacket/packet.h> |
| 43 | #include <linux/if_ether.h> |
| 44 | #include <linux/hdlc.h> |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 45 | #include <linux/hdlc/ioctl.h> |
| 46 | #include <linux/sockios.h> |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 47 | |
| 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 Couzens | 60021a4 | 2020-12-17 02:48:22 +0100 | [diff] [blame] | 55 | #include <osmocom/core/write_queue.h> |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 56 | #include <osmocom/gprs/gprs_ns2.h> |
| 57 | |
Pau Espin Pedrol | d41800c | 2020-12-07 13:35:24 +0100 | [diff] [blame] | 58 | #ifdef ENABLE_LIBMNL |
| 59 | #include <osmocom/core/mnl.h> |
| 60 | #endif |
| 61 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 62 | #include "config.h" |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 63 | #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 | |
| 75 | struct gre_hdr { |
| 76 | uint16_t flags; |
| 77 | uint16_t ptype; |
| 78 | } __attribute__ ((packed)); |
| 79 | |
| 80 | static void free_bind(struct gprs_ns2_vc_bind *bind); |
| 81 | static int fr_dlci_rx_cb(void *cb_data, struct msgb *msg); |
| 82 | |
| 83 | struct gprs_ns2_vc_driver vc_driver_fr = { |
| 84 | .name = "GB frame relay", |
| 85 | .free_bind = free_bind, |
| 86 | }; |
| 87 | |
| 88 | struct priv_bind { |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 89 | char netif[IFNAMSIZ]; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 90 | struct osmo_fr_link *link; |
Alexander Couzens | 60021a4 | 2020-12-17 02:48:22 +0100 | [diff] [blame] | 91 | struct osmo_wqueue wqueue; |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 92 | int ifindex; |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 93 | bool if_running; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | struct priv_vc { |
| 97 | struct osmo_sockaddr remote; |
| 98 | uint16_t dlci; |
| 99 | struct osmo_fr_dlc *dlc; |
| 100 | }; |
| 101 | |
| 102 | static void free_vc(struct gprs_ns2_vc *nsvc) |
| 103 | { |
| 104 | OSMO_ASSERT(nsvc); |
| 105 | |
| 106 | if (!nsvc->priv) |
| 107 | return; |
| 108 | |
| 109 | talloc_free(nsvc->priv); |
| 110 | nsvc->priv = NULL; |
| 111 | } |
| 112 | |
| 113 | static void dump_vty(const struct gprs_ns2_vc_bind *bind, struct vty *vty, bool _stats) |
| 114 | { |
| 115 | struct priv_bind *priv; |
| 116 | struct gprs_ns2_vc *nsvc; |
Harald Welte | 48bd76c | 2020-12-01 16:53:06 +0100 | [diff] [blame] | 117 | struct osmo_fr_link *fr_link; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 118 | |
| 119 | if (!bind) |
| 120 | return; |
| 121 | |
| 122 | priv = bind->priv; |
Harald Welte | 48bd76c | 2020-12-01 16:53:06 +0100 | [diff] [blame] | 123 | fr_link = priv->link; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 124 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 125 | vty_out(vty, "FR bind: %s, role: %s, link: %s%s", priv->netif, |
| 126 | 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] | 127 | |
| 128 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
Harald Welte | 96ec84a | 2020-12-01 17:56:05 +0100 | [diff] [blame] | 129 | 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] | 130 | } |
| 131 | |
| 132 | priv = bind->priv; |
| 133 | } |
| 134 | |
| 135 | /*! clean up all private driver state. Should be only called by gprs_ns2_free_bind() */ |
| 136 | static void free_bind(struct gprs_ns2_vc_bind *bind) |
| 137 | { |
| 138 | struct priv_bind *priv; |
| 139 | |
| 140 | if (!bind) |
| 141 | return; |
| 142 | |
| 143 | priv = bind->priv; |
| 144 | |
| 145 | OSMO_ASSERT(llist_empty(&bind->nsvc)); |
| 146 | |
| 147 | osmo_fr_link_free(priv->link); |
Alexander Couzens | 60021a4 | 2020-12-17 02:48:22 +0100 | [diff] [blame] | 148 | osmo_fd_close(&priv->wqueue.bfd); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 149 | talloc_free(priv); |
| 150 | } |
| 151 | |
| 152 | static struct priv_vc *fr_alloc_vc(struct gprs_ns2_vc_bind *bind, |
| 153 | struct gprs_ns2_vc *nsvc, |
| 154 | uint16_t dlci) |
| 155 | { |
| 156 | struct priv_bind *privb = bind->priv; |
| 157 | struct priv_vc *priv = talloc_zero(bind, struct priv_vc); |
| 158 | if (!priv) |
| 159 | return NULL; |
| 160 | |
| 161 | nsvc->priv = priv; |
| 162 | priv->dlci = dlci; |
| 163 | priv->dlc = osmo_fr_dlc_alloc(privb->link, dlci); |
| 164 | if (!priv->dlc) { |
| 165 | nsvc->priv = NULL; |
| 166 | talloc_free(priv); |
| 167 | return NULL; |
| 168 | } |
| 169 | |
| 170 | priv->dlc->rx_cb_data = nsvc; |
| 171 | priv->dlc->rx_cb = fr_dlci_rx_cb; |
| 172 | |
| 173 | return priv; |
| 174 | } |
| 175 | |
| 176 | int gprs_ns2_find_vc_by_dlci(struct gprs_ns2_vc_bind *bind, |
| 177 | uint16_t dlci, |
| 178 | struct gprs_ns2_vc **result) |
| 179 | { |
| 180 | struct gprs_ns2_vc *nsvc; |
| 181 | struct priv_vc *vcpriv; |
| 182 | |
| 183 | if (!result) |
| 184 | return -EINVAL; |
| 185 | |
| 186 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
| 187 | vcpriv = nsvc->priv; |
| 188 | if (vcpriv->dlci != dlci) { |
| 189 | *result = nsvc; |
| 190 | return 0; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return 1; |
| 195 | } |
| 196 | |
| 197 | /* PDU from the network interface towards the fr layer (upwards) */ |
| 198 | static int handle_netif_read(struct osmo_fd *bfd) |
| 199 | { |
| 200 | struct gprs_ns2_vc_bind *bind = bfd->data; |
| 201 | struct priv_bind *priv = bind->priv; |
| 202 | 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] | 203 | struct sockaddr_ll sll; |
| 204 | socklen_t sll_len = sizeof(sll); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 205 | int rc = 0; |
| 206 | |
| 207 | if (!msg) |
| 208 | return -ENOMEM; |
| 209 | |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 210 | 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] | 211 | if (rc < 0) { |
| 212 | LOGP(DLNS, LOGL_ERROR, "recv error %s during NS-FR-GRE recv\n", |
| 213 | strerror(errno)); |
| 214 | goto out_err; |
| 215 | } else if (rc == 0) { |
| 216 | goto out_err; |
| 217 | } |
| 218 | |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 219 | /* ignore any packets that we might have received for a different interface, between |
| 220 | * the socket() and the bind() call */ |
| 221 | if (sll.sll_ifindex != priv->ifindex) |
| 222 | goto out_err; |
| 223 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 224 | msgb_put(msg, rc); |
| 225 | msg->dst = priv->link; |
| 226 | return osmo_fr_rx(msg); |
| 227 | |
| 228 | out_err: |
| 229 | msgb_free(msg); |
| 230 | return rc; |
| 231 | } |
| 232 | |
| 233 | /* PDU from the frame relay towards the NS-VC (upwards) */ |
| 234 | static int fr_dlci_rx_cb(void *cb_data, struct msgb *msg) |
| 235 | { |
| 236 | int rc; |
| 237 | struct gprs_ns2_vc *nsvc = cb_data; |
| 238 | |
| 239 | rc = ns2_recv_vc(nsvc, msg); |
| 240 | |
| 241 | return rc; |
| 242 | } |
| 243 | |
Alexander Couzens | 60021a4 | 2020-12-17 02:48:22 +0100 | [diff] [blame] | 244 | static int handle_netif_write(struct osmo_fd *ofd, struct msgb *msg) |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 245 | { |
Alexander Couzens | 60021a4 | 2020-12-17 02:48:22 +0100 | [diff] [blame] | 246 | return write(ofd->fd, msgb_data(msg), msgb_length(msg)); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /*! determine if given bind is for FR-GRE encapsulation. */ |
| 250 | int gprs_ns2_is_fr_bind(struct gprs_ns2_vc_bind *bind) |
| 251 | { |
| 252 | return (bind->driver == &vc_driver_fr); |
| 253 | } |
| 254 | |
| 255 | /* PDU from the NS-VC towards the frame relay layer (downwards) */ |
| 256 | static int fr_vc_sendmsg(struct gprs_ns2_vc *nsvc, struct msgb *msg) |
| 257 | { |
| 258 | struct priv_vc *vcpriv = nsvc->priv; |
| 259 | |
| 260 | msg->dst = vcpriv->dlc; |
| 261 | return osmo_fr_tx_dlc(msg); |
| 262 | } |
| 263 | |
| 264 | /* PDU from the frame relay layer towards the network interface (downwards) */ |
| 265 | int fr_tx_cb(void *data, struct msgb *msg) |
| 266 | { |
| 267 | struct gprs_ns2_vc_bind *bind = data; |
| 268 | struct priv_bind *priv = bind->priv; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 269 | |
Alexander Couzens | 60021a4 | 2020-12-17 02:48:22 +0100 | [diff] [blame] | 270 | if (osmo_wqueue_enqueue(&priv->wqueue, msg)) { |
| 271 | LOGP(DLNS, LOGL_ERROR, "frame relay %s: failed to enqueue message\n", |
| 272 | priv->netif); |
| 273 | msgb_free(msg); |
| 274 | return -EINVAL; |
| 275 | } |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 276 | |
Alexander Couzens | 60021a4 | 2020-12-17 02:48:22 +0100 | [diff] [blame] | 277 | return 0; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 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)); |
Neels Hofmeyr | 475a0ac | 2020-12-17 18:10:34 +0100 | [diff] [blame] | 291 | OSMO_STRLCPY_ARRAY(ifr.ifr_name, ifname); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 292 | |
| 293 | rc = ioctl(sk, SIOCGIFINDEX, &ifr); |
| 294 | close(sk); |
| 295 | if (rc < 0) |
| 296 | return rc; |
| 297 | |
| 298 | return ifr.ifr_ifindex; |
| 299 | } |
| 300 | |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 301 | static int open_socket(int ifindex) |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 302 | { |
| 303 | struct sockaddr_ll addr; |
Harald Welte | 4ed0f4e | 2020-12-10 21:50:32 +0100 | [diff] [blame] | 304 | int fd, rc; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 305 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 306 | memset(&addr, 0, sizeof(addr)); |
| 307 | addr.sll_family = AF_PACKET; |
| 308 | addr.sll_protocol = htons(ETH_P_ALL); |
| 309 | addr.sll_ifindex = ifindex; |
| 310 | |
Harald Welte | 5bea72e | 2020-12-10 22:06:21 +0100 | [diff] [blame] | 311 | fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_HDLC)); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 312 | if (fd < 0) { |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 313 | 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] | 314 | return fd; |
| 315 | } |
| 316 | |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 317 | /* there's a race condition between the above syscall and the bind() call below, |
| 318 | * causing other packets to be received in between */ |
| 319 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 320 | rc = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); |
| 321 | if (rc < 0) { |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 322 | 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] | 323 | close(fd); |
| 324 | return rc; |
| 325 | } |
| 326 | |
| 327 | return fd; |
| 328 | } |
| 329 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 330 | #ifdef ENABLE_LIBMNL |
| 331 | |
| 332 | #include <osmocom/core/mnl.h> |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 333 | #include <linux/if_link.h> |
| 334 | #include <linux/rtnetlink.h> |
| 335 | |
| 336 | #ifndef ARPHRD_FRAD |
| 337 | #define ARPHRD_FRAD 770 |
| 338 | #endif |
| 339 | |
| 340 | /* validate the netlink attributes */ |
| 341 | static int data_attr_cb(const struct nlattr *attr, void *data) |
| 342 | { |
| 343 | const struct nlattr **tb = data; |
| 344 | int type = mnl_attr_get_type(attr); |
| 345 | |
| 346 | if (mnl_attr_type_valid(attr, IFLA_MAX) < 0) |
| 347 | return MNL_CB_OK; |
| 348 | |
| 349 | switch (type) { |
| 350 | case IFLA_MTU: |
| 351 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 352 | return MNL_CB_ERROR; |
| 353 | break; |
| 354 | case IFLA_IFNAME: |
| 355 | if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) |
| 356 | return MNL_CB_ERROR; |
| 357 | break; |
| 358 | } |
| 359 | tb[type] = attr; |
| 360 | return MNL_CB_OK; |
| 361 | } |
| 362 | |
| 363 | /* find the bind for the netdev (if any) */ |
| 364 | static struct gprs_ns2_vc_bind *bind4netdev(struct gprs_ns2_inst *nsi, const char *ifname) |
| 365 | { |
| 366 | struct gprs_ns2_vc_bind *bind; |
| 367 | |
| 368 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 369 | struct priv_bind *bpriv = bind->priv; |
| 370 | if (!strcmp(bpriv->netif, ifname)) |
| 371 | return bind; |
| 372 | } |
| 373 | |
| 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | /* handle a single netlink message received via libmnl */ |
| 378 | static int linkmon_mnl_cb(const struct nlmsghdr *nlh, void *data) |
| 379 | { |
| 380 | struct osmo_mnl *omnl = data; |
| 381 | struct gprs_ns2_vc_bind *bind; |
| 382 | struct nlattr *tb[IFLA_MAX+1] = {}; |
| 383 | struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh); |
| 384 | struct gprs_ns2_inst *nsi; |
| 385 | const char *ifname; |
| 386 | bool if_running; |
| 387 | |
| 388 | OSMO_ASSERT(omnl); |
| 389 | OSMO_ASSERT(ifm); |
| 390 | |
| 391 | nsi = omnl->priv; |
| 392 | |
| 393 | if (ifm->ifi_type != ARPHRD_FRAD) |
| 394 | return MNL_CB_OK; |
| 395 | |
| 396 | mnl_attr_parse(nlh, sizeof(*ifm), data_attr_cb, tb); |
| 397 | |
| 398 | if (!tb[IFLA_IFNAME]) |
| 399 | return MNL_CB_OK; |
| 400 | ifname = mnl_attr_get_str(tb[IFLA_IFNAME]); |
| 401 | if_running = !!(ifm->ifi_flags & IFF_RUNNING); |
| 402 | |
| 403 | bind = bind4netdev(nsi, ifname); |
| 404 | if (bind) { |
| 405 | struct priv_bind *bpriv = bind->priv; |
| 406 | if (bpriv->if_running != if_running) { |
| 407 | /* update running state */ |
| 408 | LOGP(DLNS, LOGL_NOTICE, "FR net-device '%s': Physical link state changed: %s\n", |
| 409 | ifname, if_running ? "UP" : "DOWN"); |
| 410 | bpriv->if_running = if_running; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | return MNL_CB_OK; |
| 415 | } |
| 416 | |
| 417 | /* trigger one initial dump of all link information */ |
| 418 | static void linkmon_initial_dump(struct osmo_mnl *omnl) |
| 419 | { |
| 420 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 421 | struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf); |
| 422 | struct rtgenmsg *rt; |
| 423 | |
| 424 | nlh->nlmsg_type = RTM_GETLINK; |
| 425 | nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
| 426 | nlh->nlmsg_seq = time(NULL); |
| 427 | rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg)); |
| 428 | rt->rtgen_family = AF_PACKET; |
| 429 | |
| 430 | if (mnl_socket_sendto(omnl->mnls, nlh, nlh->nlmsg_len) < 0) { |
| 431 | LOGP(DLNS, LOGL_ERROR, "linkmon: Cannot send rtnetlink message: %s\n", strerror(errno)); |
| 432 | } |
| 433 | |
| 434 | /* the response[s] will be handled just like the events */ |
| 435 | } |
| 436 | #endif /* LIBMNL */ |
| 437 | |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 438 | static int set_ifupdown(const char *netif, bool up) |
| 439 | { |
| 440 | int sock, rc; |
| 441 | struct ifreq req; |
| 442 | |
| 443 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 444 | if (sock < 0) |
| 445 | return sock; |
| 446 | |
| 447 | memset(&req, 0, sizeof req); |
Harald Welte | 7f01b68 | 2020-12-21 12:39:38 +0100 | [diff] [blame] | 448 | OSMO_STRLCPY_ARRAY(req.ifr_name, netif); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 449 | |
Alexander Couzens | fdea03b | 2020-12-29 23:13:23 +0100 | [diff] [blame] | 450 | rc = ioctl(sock, SIOCGIFFLAGS, &req); |
Vadim Yanitskiy | 222e844 | 2021-01-05 14:33:29 +0100 | [diff] [blame] | 451 | if (rc < 0) { |
| 452 | close(sock); |
Alexander Couzens | fdea03b | 2020-12-29 23:13:23 +0100 | [diff] [blame] | 453 | return rc; |
Vadim Yanitskiy | 222e844 | 2021-01-05 14:33:29 +0100 | [diff] [blame] | 454 | } |
Alexander Couzens | fdea03b | 2020-12-29 23:13:23 +0100 | [diff] [blame] | 455 | |
Vadim Yanitskiy | 222e844 | 2021-01-05 14:33:29 +0100 | [diff] [blame] | 456 | if ((req.ifr_flags & IFF_UP) == up) { |
| 457 | close(sock); |
Alexander Couzens | fdea03b | 2020-12-29 23:13:23 +0100 | [diff] [blame] | 458 | return 0; |
Vadim Yanitskiy | 222e844 | 2021-01-05 14:33:29 +0100 | [diff] [blame] | 459 | } |
Alexander Couzens | fdea03b | 2020-12-29 23:13:23 +0100 | [diff] [blame] | 460 | |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 461 | if (up) |
| 462 | req.ifr_flags |= IFF_UP; |
| 463 | |
| 464 | rc = ioctl(sock, SIOCSIFFLAGS, &req); |
| 465 | close(sock); |
| 466 | return rc; |
| 467 | } |
| 468 | |
| 469 | static int setup_device(const char *netif) |
| 470 | { |
| 471 | int sock, rc; |
| 472 | char buffer[128]; |
| 473 | fr_proto *fr = (void*)buffer; |
| 474 | struct ifreq req; |
| 475 | |
| 476 | sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 477 | if (sock < 0) { |
| 478 | LOGP(DLNS, LOGL_ERROR, "%s: Unable to create socket: %s\n", |
| 479 | netif, strerror(errno)); |
| 480 | return sock; |
| 481 | } |
| 482 | |
| 483 | memset(&req, 0, sizeof(struct ifreq)); |
| 484 | memset(&buffer, 0, sizeof(buffer)); |
Harald Welte | b8de188 | 2020-12-21 12:40:45 +0100 | [diff] [blame] | 485 | OSMO_STRLCPY_ARRAY(req.ifr_name, netif); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 486 | req.ifr_settings.ifs_ifsu.sync = (void*)buffer; |
| 487 | req.ifr_settings.size = sizeof(buffer); |
| 488 | req.ifr_settings.type = IF_GET_PROTO; |
| 489 | |
Alexander Couzens | 8c33d4a | 2020-12-22 15:42:01 +0100 | [diff] [blame] | 490 | /* EINVAL is returned when no protocol has been set */ |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 491 | rc = ioctl(sock, SIOCWANDEV, &req); |
Alexander Couzens | 8c33d4a | 2020-12-22 15:42:01 +0100 | [diff] [blame] | 492 | if (rc < 0 && errno != EINVAL) { |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 493 | LOGP(DLNS, LOGL_ERROR, "%s: Unable to get FR protocol information: %s\n", |
| 494 | netif, strerror(errno)); |
| 495 | goto err; |
| 496 | } |
| 497 | |
| 498 | /* check if the device is good */ |
Alexander Couzens | 8c33d4a | 2020-12-22 15:42:01 +0100 | [diff] [blame] | 499 | if (rc == 0 && req.ifr_settings.type == IF_PROTO_FR && fr->lmi == LMI_NONE) { |
| 500 | LOGP(DLNS, LOGL_NOTICE, "%s: has correct frame relay mode and lmi\n", netif); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 501 | goto ifup; |
| 502 | } |
| 503 | |
| 504 | /* modify the device to match */ |
| 505 | rc = set_ifupdown(netif, false); |
| 506 | if (rc) { |
Alexander Couzens | 8c33d4a | 2020-12-22 15:42:01 +0100 | [diff] [blame] | 507 | LOGP(DLNS, LOGL_ERROR, "Unable to bring down the device %s: %s\n", |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 508 | netif, strerror(errno)); |
| 509 | goto err; |
| 510 | } |
| 511 | |
| 512 | memset(&req, 0, sizeof(struct ifreq)); |
| 513 | memset(fr, 0, sizeof(fr_proto)); |
Harald Welte | b8de188 | 2020-12-21 12:40:45 +0100 | [diff] [blame] | 514 | OSMO_STRLCPY_ARRAY(req.ifr_name, netif); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 515 | req.ifr_settings.type = IF_PROTO_FR; |
| 516 | req.ifr_settings.size = sizeof(fr_proto); |
| 517 | req.ifr_settings.ifs_ifsu.fr = fr; |
| 518 | fr->lmi = LMI_NONE; |
| 519 | /* even those settings aren't used, they must be in the range */ |
| 520 | /* polling verification timer*/ |
| 521 | fr->t391 = 10; |
| 522 | /* link integrity verification polling timer */ |
| 523 | fr->t392 = 15; |
| 524 | /* full status polling counter*/ |
| 525 | fr->n391 = 6; |
| 526 | /* error threshold */ |
| 527 | fr->n392 = 3; |
| 528 | /* monitored events count */ |
| 529 | fr->n393 = 4; |
| 530 | |
Alexander Couzens | 8c33d4a | 2020-12-22 15:42:01 +0100 | [diff] [blame] | 531 | LOGP(DLNS, LOGL_INFO, "%s: Setting frame relay related parameters\n", netif); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 532 | rc = ioctl(sock, SIOCWANDEV, &req); |
| 533 | if (rc) { |
| 534 | LOGP(DLNS, LOGL_ERROR, "%s: Unable to set FR protocol on information: %s\n", |
| 535 | netif, strerror(errno)); |
| 536 | goto err; |
| 537 | } |
| 538 | |
| 539 | ifup: |
| 540 | rc = set_ifupdown(netif, true); |
| 541 | if (rc) |
| 542 | LOGP(DLNS, LOGL_ERROR, "Unable to bring up the device %s: %s\n", |
| 543 | netif, strerror(errno)); |
| 544 | err: |
| 545 | close(sock); |
| 546 | return rc; |
| 547 | } |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 548 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 549 | /*! Create a new bind for NS over FR. |
| 550 | * \param[in] nsi NS instance in which to create the bind |
| 551 | * \param[in] netif Network interface to bind to |
| 552 | * \param[in] fr_network |
| 553 | * \param[in] fr_role |
| 554 | * \param[out] result pointer to created bind |
| 555 | * \return 0 on success; negative on error */ |
| 556 | int gprs_ns2_fr_bind(struct gprs_ns2_inst *nsi, |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 557 | const char *name, |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 558 | const char *netif, |
| 559 | struct osmo_fr_network *fr_network, |
| 560 | enum osmo_fr_role fr_role, |
| 561 | struct gprs_ns2_vc_bind **result) |
| 562 | { |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 563 | struct gprs_ns2_vc_bind *bind; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 564 | struct priv_bind *priv; |
| 565 | struct osmo_fr_link *fr_link; |
| 566 | int rc = 0; |
| 567 | |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 568 | if (!name) |
| 569 | return -EINVAL; |
| 570 | |
| 571 | if (gprs_ns2_bind_by_name(nsi, name)) |
| 572 | return -EALREADY; |
| 573 | |
| 574 | bind = talloc_zero(nsi, struct gprs_ns2_vc_bind); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 575 | if (!bind) |
| 576 | return -ENOSPC; |
| 577 | |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 578 | bind->name = talloc_strdup(bind, name); |
| 579 | if (!bind->name) { |
| 580 | rc = -ENOSPC; |
| 581 | goto err_bind; |
| 582 | } |
| 583 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 584 | bind->driver = &vc_driver_fr; |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 585 | bind->ll = GPRS_NS2_LL_FR; |
Alexander Couzens | 1c8785d | 2020-12-17 06:58:53 +0100 | [diff] [blame] | 586 | /* 2 mbit */ |
| 587 | bind->transfer_capability = 2; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 588 | bind->send_vc = fr_vc_sendmsg; |
| 589 | bind->free_vc = free_vc; |
| 590 | bind->dump_vty = dump_vty; |
| 591 | bind->nsi = nsi; |
| 592 | priv = bind->priv = talloc_zero(bind, struct priv_bind); |
| 593 | if (!priv) { |
| 594 | rc = -ENOSPC; |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 595 | goto err_name; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 598 | if (strlen(netif) > IFNAMSIZ) { |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 599 | rc = -EINVAL; |
| 600 | goto err_priv; |
| 601 | } |
Neels Hofmeyr | 8fef761 | 2020-12-17 18:19:19 +0100 | [diff] [blame] | 602 | OSMO_STRLCPY_ARRAY(priv->netif, netif); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 603 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 604 | if (result) |
| 605 | *result = bind; |
| 606 | |
| 607 | /* FIXME: move fd handling into socket.c */ |
| 608 | fr_link = osmo_fr_link_alloc(fr_network, fr_role, netif); |
| 609 | if (!fr_link) { |
| 610 | rc = -EINVAL; |
| 611 | goto err_priv; |
| 612 | } |
| 613 | |
| 614 | fr_link->tx_cb = fr_tx_cb; |
| 615 | fr_link->tx_cb_data = bind; |
| 616 | priv->link = fr_link; |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 617 | |
Alexander Couzens | 6f89c77 | 2020-12-17 03:06:39 +0100 | [diff] [blame] | 618 | priv->ifindex = rc = devname2ifindex(netif); |
| 619 | if (rc < 0) { |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 620 | LOGP(DLNS, LOGL_ERROR, "Can not get interface index for interface %s\n", netif); |
| 621 | goto err_fr; |
| 622 | } |
| 623 | |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 624 | /* set protocol frame relay and lmi */ |
| 625 | rc = setup_device(priv->netif); |
| 626 | if(rc < 0) { |
| 627 | LOGP(DLNS, LOGL_ERROR, "Failed to setup the interface %s for frame relay and lmi\n", netif); |
| 628 | goto err_fr; |
| 629 | } |
| 630 | |
Alexander Couzens | 60021a4 | 2020-12-17 02:48:22 +0100 | [diff] [blame] | 631 | rc = open_socket(priv->ifindex); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 632 | if (rc < 0) |
| 633 | goto err_fr; |
Alexander Couzens | 60021a4 | 2020-12-17 02:48:22 +0100 | [diff] [blame] | 634 | osmo_wqueue_init(&priv->wqueue, 10); |
| 635 | priv->wqueue.write_cb = handle_netif_write; |
| 636 | priv->wqueue.read_cb = handle_netif_read; |
| 637 | osmo_fd_setup(&priv->wqueue.bfd, rc, OSMO_FD_READ, osmo_wqueue_bfd_cb, bind, 0); |
| 638 | rc = osmo_fd_register(&priv->wqueue.bfd); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 639 | if (rc < 0) |
| 640 | goto err_fd; |
| 641 | |
| 642 | INIT_LLIST_HEAD(&bind->nsvc); |
| 643 | llist_add(&bind->list, &nsi->binding); |
| 644 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 645 | #ifdef ENABLE_LIBMNL |
| 646 | if (!nsi->linkmon_mnl) |
| 647 | nsi->linkmon_mnl = osmo_mnl_init(nsi, NETLINK_ROUTE, RTMGRP_LINK, linkmon_mnl_cb, nsi); |
| 648 | |
| 649 | /* we get a new full dump after every bind. which is a bit excessive. But that's just once |
| 650 | * at start-up, so we can get away with it */ |
| 651 | if (nsi->linkmon_mnl) |
| 652 | linkmon_initial_dump(nsi->linkmon_mnl); |
| 653 | #endif |
| 654 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 655 | return rc; |
| 656 | |
| 657 | err_fd: |
Alexander Couzens | 60021a4 | 2020-12-17 02:48:22 +0100 | [diff] [blame] | 658 | close(priv->wqueue.bfd.fd); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 659 | err_fr: |
| 660 | osmo_fr_link_free(fr_link); |
| 661 | err_priv: |
| 662 | talloc_free(priv); |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 663 | err_name: |
| 664 | talloc_free((char *)bind->name); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 665 | err_bind: |
| 666 | talloc_free(bind); |
| 667 | |
| 668 | return rc; |
| 669 | } |
| 670 | |
Alexander Couzens | c782cec | 2020-12-10 04:10:25 +0100 | [diff] [blame] | 671 | /*! Return the frame relay role of a bind |
| 672 | * \param[in] bind The bind |
| 673 | * \return the frame relay role or -EINVAL if bind is not frame relay |
| 674 | */ |
| 675 | enum osmo_fr_role gprs_ns2_fr_bind_role(struct gprs_ns2_vc_bind *bind) |
| 676 | { |
| 677 | struct priv_bind *priv; |
| 678 | |
| 679 | if (bind->driver != &vc_driver_fr) |
| 680 | return -EINVAL; |
| 681 | |
| 682 | priv = bind->priv; |
| 683 | return priv->link->role; |
| 684 | } |
| 685 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 686 | /*! Return the network interface of the bind |
| 687 | * \param[in] bind The bind |
| 688 | * \return the network interface |
| 689 | */ |
| 690 | const char *gprs_ns2_fr_bind_netif(struct gprs_ns2_vc_bind *bind) |
| 691 | { |
| 692 | struct priv_bind *priv; |
| 693 | |
| 694 | if (bind->driver != &vc_driver_fr) |
| 695 | return NULL; |
| 696 | |
| 697 | priv = bind->priv; |
| 698 | return priv->netif; |
| 699 | } |
| 700 | |
| 701 | /*! Find NS bind for a given network interface |
| 702 | * \param[in] nsi NS instance |
| 703 | * \param[in] netif the network interface to search for |
| 704 | * \return the bind or NULL if not found |
| 705 | */ |
| 706 | struct gprs_ns2_vc_bind *gprs_ns2_fr_bind_by_netif( |
| 707 | struct gprs_ns2_inst *nsi, |
| 708 | const char *netif) |
| 709 | { |
| 710 | struct gprs_ns2_vc_bind *bind; |
| 711 | const char *_netif; |
| 712 | |
| 713 | OSMO_ASSERT(nsi); |
| 714 | OSMO_ASSERT(netif); |
| 715 | |
| 716 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 717 | if (!gprs_ns2_is_fr_bind(bind)) |
| 718 | continue; |
| 719 | |
| 720 | _netif = gprs_ns2_fr_bind_netif(bind); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 721 | if (!strncmp(_netif, netif, IFNAMSIZ)) |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 722 | return bind; |
| 723 | } |
| 724 | |
| 725 | return NULL; |
| 726 | } |
| 727 | |
| 728 | /*! Create, connect and activate a new FR-based NS-VC |
| 729 | * \param[in] bind bind in which the new NS-VC is to be created |
| 730 | * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created |
| 731 | * \param[in] dlci Data Link connection identifier |
| 732 | * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */ |
| 733 | 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] | 734 | struct gprs_ns2_nse *nse, |
| 735 | uint16_t nsvci, |
| 736 | uint16_t dlci) |
| 737 | { |
| 738 | struct gprs_ns2_vc *nsvc = NULL; |
| 739 | struct priv_vc *priv = NULL; |
| 740 | |
| 741 | nsvc = gprs_ns2_fr_nsvc_by_dlci(bind, dlci); |
| 742 | if (nsvc) { |
| 743 | goto err; |
| 744 | } |
| 745 | |
| 746 | nsvc = ns2_vc_alloc(bind, nse, true, NS2_VC_MODE_BLOCKRESET); |
| 747 | if (!nsvc) |
| 748 | goto err; |
| 749 | |
| 750 | nsvc->priv = priv = fr_alloc_vc(bind, nsvc, dlci); |
| 751 | if (!priv) |
| 752 | goto err; |
| 753 | |
| 754 | nsvc->nsvci = nsvci; |
| 755 | nsvc->nsvci_is_valid = true; |
| 756 | |
| 757 | gprs_ns2_vc_fsm_start(nsvc); |
| 758 | |
| 759 | return nsvc; |
| 760 | |
| 761 | err: |
| 762 | gprs_ns2_free_nsvc(nsvc); |
| 763 | return NULL; |
| 764 | } |
| 765 | |
| 766 | |
| 767 | /*! Create, connect and activate a new FR-based NS-VC |
| 768 | * \param[in] bind bind in which the new NS-VC is to be created |
| 769 | * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created |
| 770 | * \param[in] dlci Data Link connection identifier |
| 771 | * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */ |
| 772 | 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] | 773 | uint16_t nsei, |
| 774 | uint16_t nsvci, |
| 775 | uint16_t dlci) |
| 776 | { |
| 777 | bool created_nse = false; |
| 778 | struct gprs_ns2_vc *nsvc = NULL; |
| 779 | struct priv_vc *priv = NULL; |
| 780 | struct gprs_ns2_nse *nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei); |
| 781 | if (!nse) { |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 782 | 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] | 783 | if (!nse) |
| 784 | return NULL; |
| 785 | created_nse = true; |
| 786 | } |
| 787 | |
| 788 | nsvc = gprs_ns2_fr_nsvc_by_dlci(bind, dlci); |
| 789 | if (nsvc) { |
| 790 | goto err_nse; |
| 791 | } |
| 792 | |
Alexander Couzens | d923cff | 2020-12-01 01:03:52 +0100 | [diff] [blame] | 793 | nsvc = ns2_vc_alloc(bind, nse, true, NS2_VC_MODE_BLOCKRESET); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 794 | if (!nsvc) |
| 795 | goto err_nse; |
| 796 | |
| 797 | nsvc->priv = priv = fr_alloc_vc(bind, nsvc, dlci); |
| 798 | if (!priv) |
| 799 | goto err; |
| 800 | |
| 801 | nsvc->nsvci = nsvci; |
| 802 | nsvc->nsvci_is_valid = true; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 803 | |
| 804 | gprs_ns2_vc_fsm_start(nsvc); |
| 805 | |
| 806 | return nsvc; |
| 807 | |
| 808 | err: |
| 809 | gprs_ns2_free_nsvc(nsvc); |
| 810 | err_nse: |
| 811 | if (created_nse) |
| 812 | gprs_ns2_free_nse(nse); |
| 813 | |
| 814 | return NULL; |
| 815 | } |
| 816 | |
| 817 | /*! Return the nsvc by dlci. |
| 818 | * \param[in] bind |
| 819 | * \param[in] dlci Data Link connection identifier |
| 820 | * \return the nsvc or NULL if not found |
| 821 | */ |
| 822 | struct gprs_ns2_vc *gprs_ns2_fr_nsvc_by_dlci(struct gprs_ns2_vc_bind *bind, |
| 823 | uint16_t dlci) |
| 824 | { |
| 825 | struct gprs_ns2_vc *nsvc; |
| 826 | struct priv_vc *vcpriv; |
| 827 | |
| 828 | 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 Couzens | 22c26e0 | 2020-12-10 04:10:07 +0100 | [diff] [blame] | 842 | uint16_t gprs_ns2_fr_nsvc_dlci(const struct gprs_ns2_vc *nsvc) |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 843 | { |
| 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 | } |