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 | |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 7 | /* (C) 2009-2021 by Harald Welte <laforge@gnumonks.org> |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 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> |
Harald Welte | 7634607 | 2021-01-31 11:54:02 +0100 | [diff] [blame] | 50 | #include <osmocom/core/stat_item.h> |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 51 | #include <osmocom/core/logging.h> |
| 52 | #include <osmocom/core/msgb.h> |
| 53 | #include <osmocom/core/select.h> |
| 54 | #include <osmocom/core/socket.h> |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 55 | #include <osmocom/core/timer.h> |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 56 | #include <osmocom/core/talloc.h> |
| 57 | #include <osmocom/gprs/gprs_ns2.h> |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 58 | #include <osmocom/gprs/protocol/gsm_08_16.h> |
| 59 | #include <osmocom/gprs/protocol/gsm_08_18.h> |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 60 | |
Pau Espin Pedrol | d41800c | 2020-12-07 13:35:24 +0100 | [diff] [blame] | 61 | #ifdef ENABLE_LIBMNL |
| 62 | #include <osmocom/core/mnl.h> |
| 63 | #endif |
| 64 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 65 | #include "config.h" |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 66 | #include "common_vty.h" |
| 67 | #include "gprs_ns2_internal.h" |
| 68 | |
| 69 | #define GRE_PTYPE_FR 0x6559 |
| 70 | #define GRE_PTYPE_IPv4 0x0800 |
| 71 | #define GRE_PTYPE_IPv6 0x86dd |
| 72 | #define GRE_PTYPE_KAR 0x0000 /* keepalive response */ |
| 73 | |
| 74 | #ifndef IPPROTO_GRE |
| 75 | # define IPPROTO_GRE 47 |
| 76 | #endif |
| 77 | |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 78 | #define E1_LINERATE 2048000 |
| 79 | #define E1_SLOTS_TOTAL 32 |
| 80 | #define E1_SLOTS_USED 31 |
| 81 | /* usable bitrate of the E1 superchannel with 31 of 32 timeslots */ |
| 82 | #define SUPERCHANNEL_LINERATE (E1_LINERATE*E1_SLOTS_USED)/E1_SLOTS_TOTAL |
| 83 | /* nanoseconds per bit (504) */ |
| 84 | #define BIT_DURATION_NS (1000000000 / SUPERCHANNEL_LINERATE) |
| 85 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 86 | struct gre_hdr { |
| 87 | uint16_t flags; |
| 88 | uint16_t ptype; |
| 89 | } __attribute__ ((packed)); |
| 90 | |
| 91 | static void free_bind(struct gprs_ns2_vc_bind *bind); |
| 92 | static int fr_dlci_rx_cb(void *cb_data, struct msgb *msg); |
| 93 | |
| 94 | struct gprs_ns2_vc_driver vc_driver_fr = { |
| 95 | .name = "GB frame relay", |
| 96 | .free_bind = free_bind, |
| 97 | }; |
| 98 | |
| 99 | struct priv_bind { |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 100 | char netif[IFNAMSIZ]; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 101 | struct osmo_fr_link *link; |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 102 | int ifindex; |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 103 | bool if_running; |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 104 | /* backlog queue for AF_PACKET / ENOBUFS handling (see OS#4993) */ |
| 105 | struct { |
| 106 | /* file-descriptor for AF_PACKET socket */ |
| 107 | struct osmo_fd ofd; |
Harald Welte | 9374061 | 2021-02-02 19:54:10 +0100 | [diff] [blame] | 108 | /* LMI bucket (we only store the last LMI message, no need to queue */ |
| 109 | struct msgb *lmi_msg; |
| 110 | /* list of NS msgb (backlog) */ |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 111 | struct llist_head list; |
| 112 | /* timer to trigger next attempt of AF_PACKET write */ |
| 113 | struct osmo_timer_list timer; |
| 114 | /* re-try after that many micro-seconds */ |
| 115 | uint32_t retry_us; |
| 116 | } backlog; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | struct priv_vc { |
| 120 | struct osmo_sockaddr remote; |
| 121 | uint16_t dlci; |
| 122 | struct osmo_fr_dlc *dlc; |
| 123 | }; |
| 124 | |
| 125 | static void free_vc(struct gprs_ns2_vc *nsvc) |
| 126 | { |
Alexander Couzens | ea37724 | 2021-01-17 16:51:55 +0100 | [diff] [blame] | 127 | if (!nsvc) |
| 128 | return; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 129 | |
| 130 | if (!nsvc->priv) |
| 131 | return; |
| 132 | |
Alexander Couzens | 55bc869 | 2021-01-18 18:39:57 +0100 | [diff] [blame] | 133 | OSMO_ASSERT(gprs_ns2_is_fr_bind(nsvc->bind)); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 134 | talloc_free(nsvc->priv); |
| 135 | nsvc->priv = NULL; |
| 136 | } |
| 137 | |
| 138 | static void dump_vty(const struct gprs_ns2_vc_bind *bind, struct vty *vty, bool _stats) |
| 139 | { |
| 140 | struct priv_bind *priv; |
| 141 | struct gprs_ns2_vc *nsvc; |
Harald Welte | 48bd76c | 2020-12-01 16:53:06 +0100 | [diff] [blame] | 142 | struct osmo_fr_link *fr_link; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 143 | |
| 144 | if (!bind) |
| 145 | return; |
| 146 | |
| 147 | priv = bind->priv; |
Harald Welte | 48bd76c | 2020-12-01 16:53:06 +0100 | [diff] [blame] | 148 | fr_link = priv->link; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 149 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 150 | vty_out(vty, "FR bind: %s, role: %s, link: %s%s", priv->netif, |
| 151 | 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] | 152 | |
| 153 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
Harald Welte | 96ec84a | 2020-12-01 17:56:05 +0100 | [diff] [blame] | 154 | 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] | 155 | } |
| 156 | |
| 157 | priv = bind->priv; |
| 158 | } |
| 159 | |
| 160 | /*! clean up all private driver state. Should be only called by gprs_ns2_free_bind() */ |
| 161 | static void free_bind(struct gprs_ns2_vc_bind *bind) |
| 162 | { |
| 163 | struct priv_bind *priv; |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 164 | struct msgb *msg, *msg2; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 165 | |
| 166 | if (!bind) |
| 167 | return; |
| 168 | |
Alexander Couzens | 24d9e80 | 2021-02-03 11:30:43 +0100 | [diff] [blame] | 169 | OSMO_ASSERT(gprs_ns2_is_fr_bind(bind)); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 170 | priv = bind->priv; |
| 171 | |
| 172 | OSMO_ASSERT(llist_empty(&bind->nsvc)); |
| 173 | |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 174 | osmo_timer_del(&priv->backlog.timer); |
| 175 | llist_for_each_entry_safe(msg, msg2, &priv->backlog.list, list) { |
| 176 | msgb_free(msg); |
| 177 | } |
Harald Welte | 9374061 | 2021-02-02 19:54:10 +0100 | [diff] [blame] | 178 | msgb_free(priv->backlog.lmi_msg); |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 179 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 180 | osmo_fr_link_free(priv->link); |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 181 | osmo_fd_close(&priv->backlog.ofd); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 182 | talloc_free(priv); |
| 183 | } |
| 184 | |
Harald Welte | 9d28ce5 | 2021-01-31 18:38:20 +0100 | [diff] [blame] | 185 | static void fr_dlci_status_cb(struct osmo_fr_dlc *dlc, void *cb_data, bool active) |
| 186 | { |
| 187 | struct gprs_ns2_vc *nsvc = cb_data; |
| 188 | |
| 189 | if (active) { |
| 190 | ns2_vc_fsm_start(nsvc); |
| 191 | } else { |
| 192 | ns2_vc_force_unconfigured(nsvc); |
| 193 | } |
| 194 | } |
| 195 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 196 | static struct priv_vc *fr_alloc_vc(struct gprs_ns2_vc_bind *bind, |
| 197 | struct gprs_ns2_vc *nsvc, |
| 198 | uint16_t dlci) |
| 199 | { |
| 200 | struct priv_bind *privb = bind->priv; |
| 201 | struct priv_vc *priv = talloc_zero(bind, struct priv_vc); |
| 202 | if (!priv) |
| 203 | return NULL; |
| 204 | |
Alexander Couzens | 55bc869 | 2021-01-18 18:39:57 +0100 | [diff] [blame] | 205 | OSMO_ASSERT(gprs_ns2_is_fr_bind(bind)); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 206 | nsvc->priv = priv; |
| 207 | priv->dlci = dlci; |
| 208 | priv->dlc = osmo_fr_dlc_alloc(privb->link, dlci); |
| 209 | if (!priv->dlc) { |
| 210 | nsvc->priv = NULL; |
| 211 | talloc_free(priv); |
| 212 | return NULL; |
| 213 | } |
| 214 | |
Harald Welte | 2cc1d4d | 2021-01-31 18:33:31 +0100 | [diff] [blame] | 215 | priv->dlc->cb_data = nsvc; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 216 | priv->dlc->rx_cb = fr_dlci_rx_cb; |
Harald Welte | 9d28ce5 | 2021-01-31 18:38:20 +0100 | [diff] [blame] | 217 | priv->dlc->status_cb = fr_dlci_status_cb; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 218 | |
| 219 | return priv; |
| 220 | } |
| 221 | |
| 222 | int gprs_ns2_find_vc_by_dlci(struct gprs_ns2_vc_bind *bind, |
| 223 | uint16_t dlci, |
| 224 | struct gprs_ns2_vc **result) |
| 225 | { |
| 226 | struct gprs_ns2_vc *nsvc; |
| 227 | struct priv_vc *vcpriv; |
| 228 | |
Alexander Couzens | 55bc869 | 2021-01-18 18:39:57 +0100 | [diff] [blame] | 229 | OSMO_ASSERT(gprs_ns2_is_fr_bind(bind)); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 230 | if (!result) |
| 231 | return -EINVAL; |
| 232 | |
| 233 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
| 234 | vcpriv = nsvc->priv; |
| 235 | if (vcpriv->dlci != dlci) { |
| 236 | *result = nsvc; |
| 237 | return 0; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | /* PDU from the network interface towards the fr layer (upwards) */ |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 245 | static int fr_netif_ofd_cb(struct osmo_fd *bfd, uint32_t what) |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 246 | { |
| 247 | struct gprs_ns2_vc_bind *bind = bfd->data; |
| 248 | struct priv_bind *priv = bind->priv; |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 249 | struct msgb *msg; |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 250 | struct sockaddr_ll sll; |
| 251 | socklen_t sll_len = sizeof(sll); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 252 | int rc = 0; |
| 253 | |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 254 | /* we only handle read here. write to AF_PACKET sockets cannot be triggered |
| 255 | * by select or poll, see OS#4995 */ |
| 256 | if (!(what & OSMO_FD_READ)) |
| 257 | return 0; |
| 258 | |
| 259 | msg = msgb_alloc(NS_ALLOC_SIZE, "Gb/NS/FR/GRE Rx"); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 260 | if (!msg) |
| 261 | return -ENOMEM; |
| 262 | |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 263 | 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] | 264 | if (rc < 0) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 265 | LOGBIND(bind, LOGL_ERROR, "recv error %s during NS-FR-GRE recv\n", strerror(errno)); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 266 | goto out_err; |
| 267 | } else if (rc == 0) { |
| 268 | goto out_err; |
| 269 | } |
| 270 | |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 271 | /* ignore any packets that we might have received for a different interface, between |
| 272 | * the socket() and the bind() call */ |
| 273 | if (sll.sll_ifindex != priv->ifindex) |
| 274 | goto out_err; |
| 275 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 276 | msgb_put(msg, rc); |
| 277 | msg->dst = priv->link; |
| 278 | return osmo_fr_rx(msg); |
| 279 | |
| 280 | out_err: |
| 281 | msgb_free(msg); |
| 282 | return rc; |
| 283 | } |
| 284 | |
| 285 | /* PDU from the frame relay towards the NS-VC (upwards) */ |
| 286 | static int fr_dlci_rx_cb(void *cb_data, struct msgb *msg) |
| 287 | { |
| 288 | int rc; |
| 289 | struct gprs_ns2_vc *nsvc = cb_data; |
| 290 | |
| 291 | rc = ns2_recv_vc(nsvc, msg); |
| 292 | |
| 293 | return rc; |
| 294 | } |
| 295 | |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 296 | static int fr_netif_write_one(struct gprs_ns2_vc_bind *bind, struct msgb *msg) |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 297 | { |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 298 | struct priv_bind *priv = bind->priv; |
| 299 | unsigned int len = msgb_length(msg); |
Harald Welte | f0073d7 | 2021-01-30 11:41:13 +0100 | [diff] [blame] | 300 | int rc; |
| 301 | |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 302 | /* estimate the retry time based on the data rate it takes to transmit */ |
| 303 | priv->backlog.retry_us = (BIT_DURATION_NS * 8 * len) / 1000; |
| 304 | |
| 305 | rc = write(priv->backlog.ofd.fd, msgb_data(msg), len); |
| 306 | if (rc == len) { |
| 307 | msgb_free(msg); |
| 308 | return 0; |
| 309 | } else if (rc < 0) { |
| 310 | /* don't free, the caller might want to re-transmit */ |
| 311 | switch (errno) { |
| 312 | case EAGAIN: |
| 313 | case ENOBUFS: |
Harald Welte | 9811f49 | 2021-02-02 22:58:00 +0100 | [diff] [blame] | 314 | /* not a real error, but more a normal event on AF_PACKET */ |
| 315 | /* don't free the message and let the caller re-enqueue */ |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 316 | return -errno; |
| 317 | default: |
Harald Welte | 9811f49 | 2021-02-02 22:58:00 +0100 | [diff] [blame] | 318 | /* an actual error, like -ENETDOWN, -EMSGSIZE */ |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 319 | LOGBIND(bind, LOGL_ERROR, "error during write to AF_PACKET: %s\n", strerror(errno)); |
Harald Welte | 9811f49 | 2021-02-02 22:58:00 +0100 | [diff] [blame] | 320 | msgb_free(msg); |
| 321 | return 0; |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 322 | } |
| 323 | } else { |
| 324 | /* short write */ |
| 325 | LOGBIND(bind, LOGL_ERROR, "short write on AF_PACKET: %d < %d\n", rc, len); |
| 326 | msgb_free(msg); |
| 327 | return 0; |
| 328 | } |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /*! determine if given bind is for FR-GRE encapsulation. */ |
| 332 | int gprs_ns2_is_fr_bind(struct gprs_ns2_vc_bind *bind) |
| 333 | { |
| 334 | return (bind->driver == &vc_driver_fr); |
| 335 | } |
| 336 | |
| 337 | /* PDU from the NS-VC towards the frame relay layer (downwards) */ |
| 338 | static int fr_vc_sendmsg(struct gprs_ns2_vc *nsvc, struct msgb *msg) |
| 339 | { |
| 340 | struct priv_vc *vcpriv = nsvc->priv; |
| 341 | |
| 342 | msg->dst = vcpriv->dlc; |
| 343 | return osmo_fr_tx_dlc(msg); |
| 344 | } |
| 345 | |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 346 | static void enqueue_at_head(struct gprs_ns2_vc_bind *bind, struct msgb *msg) |
| 347 | { |
| 348 | struct priv_bind *priv = bind->priv; |
| 349 | llist_add(&msg->list, &priv->backlog.list); |
Harald Welte | 7634607 | 2021-01-31 11:54:02 +0100 | [diff] [blame] | 350 | osmo_stat_item_inc(bind->statg->items[NS2_BIND_STAT_BACKLOG_LEN], 1); |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 351 | osmo_timer_schedule(&priv->backlog.timer, 0, priv->backlog.retry_us); |
| 352 | } |
| 353 | |
| 354 | static void enqueue_at_tail(struct gprs_ns2_vc_bind *bind, struct msgb *msg) |
| 355 | { |
| 356 | struct priv_bind *priv = bind->priv; |
| 357 | llist_add_tail(&msg->list, &priv->backlog.list); |
Harald Welte | 7634607 | 2021-01-31 11:54:02 +0100 | [diff] [blame] | 358 | osmo_stat_item_inc(bind->statg->items[NS2_BIND_STAT_BACKLOG_LEN], 1); |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 359 | osmo_timer_schedule(&priv->backlog.timer, 0, priv->backlog.retry_us); |
| 360 | } |
| 361 | |
| 362 | #define LMI_Q933A_DLCI 0 |
| 363 | |
| 364 | /* enqueue to backlog (LMI, signaling) or drop (userdata msg) */ |
Harald Welte | 5e0ef6f | 2021-01-30 22:03:26 +0100 | [diff] [blame] | 365 | static int backlog_enqueue_or_free(struct gprs_ns2_vc_bind *bind, struct msgb *msg) |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 366 | { |
Harald Welte | 9374061 | 2021-02-02 19:54:10 +0100 | [diff] [blame] | 367 | struct priv_bind *priv = bind->priv; |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 368 | uint8_t dlci = msg->data[0]; |
| 369 | uint8_t ns_pdu_type; |
| 370 | uint16_t bvci; |
| 371 | |
| 372 | if (msgb_length(msg) < 1) |
| 373 | goto out_free; |
| 374 | |
| 375 | /* we want to enqueue only Q.933 LMI traffic or NS signaling; NOT user traffic */ |
| 376 | switch (dlci) { |
| 377 | case LMI_Q933A_DLCI: |
Harald Welte | 9374061 | 2021-02-02 19:54:10 +0100 | [diff] [blame] | 378 | /* always store only the last LMI message in the lmi_msg bucket */ |
| 379 | msgb_free(priv->backlog.lmi_msg); |
| 380 | priv->backlog.lmi_msg = msg; |
Harald Welte | 5e0ef6f | 2021-01-30 22:03:26 +0100 | [diff] [blame] | 381 | return 0; |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 382 | default: |
Harald Welte | 6972aed | 2021-02-02 20:29:32 +0100 | [diff] [blame] | 383 | /* there's no point in trying to enqueue messages if the interface is down */ |
| 384 | if (!priv->if_running) |
| 385 | break; |
| 386 | |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 387 | if (msgb_length(msg) < 3) |
| 388 | break; |
| 389 | ns_pdu_type = msg->data[2]; |
| 390 | switch (ns_pdu_type) { |
| 391 | case NS_PDUT_UNITDATA: |
| 392 | if (msgb_length(msg) < 6) |
| 393 | break; |
| 394 | bvci = osmo_load16be(msg->data + 4); |
| 395 | /* enqueue BVCI=0 traffic at tail of queue */ |
| 396 | if (bvci == BVCI_SIGNALLING) { |
| 397 | enqueue_at_tail(bind, msg); |
Harald Welte | 5e0ef6f | 2021-01-30 22:03:26 +0100 | [diff] [blame] | 398 | return 0; |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 399 | } |
| 400 | break; |
| 401 | default: |
| 402 | /* enqueue NS signaling traffic at head of queue */ |
| 403 | enqueue_at_head(bind, msg); |
Harald Welte | 5e0ef6f | 2021-01-30 22:03:26 +0100 | [diff] [blame] | 404 | return 0; |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 405 | } |
| 406 | break; |
| 407 | } |
| 408 | |
| 409 | out_free: |
| 410 | /* drop everything that is not LMI, NS-signaling or BVCI-0 */ |
| 411 | msgb_free(msg); |
Harald Welte | 5e0ef6f | 2021-01-30 22:03:26 +0100 | [diff] [blame] | 412 | return -1; |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | static void fr_backlog_timer_cb(void *data) |
| 416 | { |
| 417 | struct gprs_ns2_vc_bind *bind = data; |
| 418 | struct priv_bind *priv = bind->priv; |
| 419 | int i, rc; |
| 420 | |
Harald Welte | 9374061 | 2021-02-02 19:54:10 +0100 | [diff] [blame] | 421 | /* first try to get rid of the LMI message, if any */ |
| 422 | if (priv->backlog.lmi_msg) { |
| 423 | rc = fr_netif_write_one(bind, priv->backlog.lmi_msg); |
| 424 | if (rc < 0) |
| 425 | goto restart_timer; |
| 426 | /* fr_netif_write_one() has just free'd it */ |
| 427 | priv->backlog.lmi_msg = NULL; |
| 428 | } |
| 429 | |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 430 | /* attempt to send up to 10 messages in every timer */ |
| 431 | for (i = 0; i < 10; i++) { |
| 432 | struct msgb *msg = msgb_dequeue(&priv->backlog.list); |
| 433 | if (!msg) |
| 434 | break; |
| 435 | |
| 436 | rc = fr_netif_write_one(bind, msg); |
| 437 | if (rc < 0) { |
| 438 | /* re-add at head of list */ |
| 439 | llist_add(&msg->list, &priv->backlog.list); |
| 440 | break; |
| 441 | } |
Harald Welte | 7634607 | 2021-01-31 11:54:02 +0100 | [diff] [blame] | 442 | osmo_stat_item_dec(bind->statg->items[NS2_BIND_STAT_BACKLOG_LEN], 1); |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 443 | } |
| 444 | |
Harald Welte | 9374061 | 2021-02-02 19:54:10 +0100 | [diff] [blame] | 445 | restart_timer: |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 446 | /* re-start timer if we still have data in the queue */ |
| 447 | if (!llist_empty(&priv->backlog.list)) |
| 448 | osmo_timer_schedule(&priv->backlog.timer, 0, priv->backlog.retry_us); |
| 449 | } |
| 450 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 451 | /* PDU from the frame relay layer towards the network interface (downwards) */ |
| 452 | int fr_tx_cb(void *data, struct msgb *msg) |
| 453 | { |
| 454 | struct gprs_ns2_vc_bind *bind = data; |
| 455 | struct priv_bind *priv = bind->priv; |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 456 | int rc; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 457 | |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 458 | if (llist_empty(&priv->backlog.list)) { |
| 459 | /* attempt to transmit right now */ |
| 460 | rc = fr_netif_write_one(bind, msg); |
| 461 | if (rc < 0) { |
| 462 | /* enqueue to backlog in case it fails */ |
Harald Welte | 5e0ef6f | 2021-01-30 22:03:26 +0100 | [diff] [blame] | 463 | return backlog_enqueue_or_free(bind, msg); |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 464 | } |
| 465 | } else { |
| 466 | /* enqueue to backlog */ |
Harald Welte | 5e0ef6f | 2021-01-30 22:03:26 +0100 | [diff] [blame] | 467 | return backlog_enqueue_or_free(bind, msg); |
Alexander Couzens | 60021a4 | 2020-12-17 02:48:22 +0100 | [diff] [blame] | 468 | } |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 469 | |
Alexander Couzens | 60021a4 | 2020-12-17 02:48:22 +0100 | [diff] [blame] | 470 | return 0; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static int devname2ifindex(const char *ifname) |
| 474 | { |
| 475 | struct ifreq ifr; |
| 476 | int sk, rc; |
| 477 | |
| 478 | sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 479 | if (sk < 0) |
| 480 | return sk; |
| 481 | |
| 482 | |
| 483 | memset(&ifr, 0, sizeof(ifr)); |
Neels Hofmeyr | 475a0ac | 2020-12-17 18:10:34 +0100 | [diff] [blame] | 484 | OSMO_STRLCPY_ARRAY(ifr.ifr_name, ifname); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 485 | |
| 486 | rc = ioctl(sk, SIOCGIFINDEX, &ifr); |
| 487 | close(sk); |
| 488 | if (rc < 0) |
| 489 | return rc; |
| 490 | |
| 491 | return ifr.ifr_ifindex; |
| 492 | } |
| 493 | |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 494 | static int open_socket(int ifindex, const struct gprs_ns2_vc_bind *nsbind) |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 495 | { |
| 496 | struct sockaddr_ll addr; |
Harald Welte | 4ed0f4e | 2020-12-10 21:50:32 +0100 | [diff] [blame] | 497 | int fd, rc; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 498 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 499 | memset(&addr, 0, sizeof(addr)); |
| 500 | addr.sll_family = AF_PACKET; |
| 501 | addr.sll_protocol = htons(ETH_P_ALL); |
| 502 | addr.sll_ifindex = ifindex; |
| 503 | |
Harald Welte | 5bea72e | 2020-12-10 22:06:21 +0100 | [diff] [blame] | 504 | fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_HDLC)); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 505 | if (fd < 0) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 506 | LOGBIND(nsbind, LOGL_ERROR, "Can not create AF_PACKET socket. Are you root or have CAP_NET_RAW?\n"); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 507 | return fd; |
| 508 | } |
| 509 | |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 510 | /* there's a race condition between the above syscall and the bind() call below, |
| 511 | * causing other packets to be received in between */ |
| 512 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 513 | rc = bind(fd, (struct sockaddr *)&addr, sizeof(addr)); |
| 514 | if (rc < 0) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 515 | LOGBIND(nsbind, 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] | 516 | close(fd); |
| 517 | return rc; |
| 518 | } |
| 519 | |
| 520 | return fd; |
| 521 | } |
| 522 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 523 | #ifdef ENABLE_LIBMNL |
| 524 | |
| 525 | #include <osmocom/core/mnl.h> |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 526 | #include <linux/if_link.h> |
| 527 | #include <linux/rtnetlink.h> |
| 528 | |
| 529 | #ifndef ARPHRD_FRAD |
| 530 | #define ARPHRD_FRAD 770 |
| 531 | #endif |
| 532 | |
| 533 | /* validate the netlink attributes */ |
| 534 | static int data_attr_cb(const struct nlattr *attr, void *data) |
| 535 | { |
| 536 | const struct nlattr **tb = data; |
| 537 | int type = mnl_attr_get_type(attr); |
| 538 | |
| 539 | if (mnl_attr_type_valid(attr, IFLA_MAX) < 0) |
| 540 | return MNL_CB_OK; |
| 541 | |
| 542 | switch (type) { |
| 543 | case IFLA_MTU: |
| 544 | if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) |
| 545 | return MNL_CB_ERROR; |
| 546 | break; |
| 547 | case IFLA_IFNAME: |
| 548 | if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) |
| 549 | return MNL_CB_ERROR; |
| 550 | break; |
| 551 | } |
| 552 | tb[type] = attr; |
| 553 | return MNL_CB_OK; |
| 554 | } |
| 555 | |
| 556 | /* find the bind for the netdev (if any) */ |
| 557 | static struct gprs_ns2_vc_bind *bind4netdev(struct gprs_ns2_inst *nsi, const char *ifname) |
| 558 | { |
| 559 | struct gprs_ns2_vc_bind *bind; |
| 560 | |
| 561 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 562 | struct priv_bind *bpriv = bind->priv; |
| 563 | if (!strcmp(bpriv->netif, ifname)) |
| 564 | return bind; |
| 565 | } |
| 566 | |
| 567 | return NULL; |
| 568 | } |
| 569 | |
Harald Welte | 6972aed | 2021-02-02 20:29:32 +0100 | [diff] [blame] | 570 | static void link_state_change(struct gprs_ns2_vc_bind *bind, bool if_running) |
| 571 | { |
| 572 | struct priv_bind *bpriv = bind->priv; |
| 573 | struct msgb *msg, *msg2; |
| 574 | |
| 575 | if (bpriv->if_running == if_running) |
| 576 | return; |
| 577 | |
| 578 | LOGBIND(bind, LOGL_NOTICE, "FR net-device '%s': Physical link state changed: %s\n", |
| 579 | bpriv->netif, if_running ? "UP" : "DOWN"); |
| 580 | |
| 581 | /* free any backlog, both on IFUP and IFDOWN. Keep the LMI, as it makes |
| 582 | * sense to get one out of the door ASAP. */ |
| 583 | llist_for_each_entry_safe(msg, msg2, &bpriv->backlog.list, list) { |
| 584 | msgb_free(msg); |
| 585 | } |
| 586 | |
| 587 | if (if_running) { |
| 588 | /* interface just came up */ |
| 589 | if (bpriv->backlog.lmi_msg) |
| 590 | osmo_timer_schedule(&bpriv->backlog.timer, 0, bpriv->backlog.retry_us); |
| 591 | } else { |
| 592 | /* interface just went down; no need to retransmit */ |
| 593 | osmo_timer_del(&bpriv->backlog.timer); |
| 594 | } |
| 595 | |
| 596 | bpriv->if_running = if_running; |
| 597 | } |
| 598 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 599 | /* handle a single netlink message received via libmnl */ |
| 600 | static int linkmon_mnl_cb(const struct nlmsghdr *nlh, void *data) |
| 601 | { |
| 602 | struct osmo_mnl *omnl = data; |
| 603 | struct gprs_ns2_vc_bind *bind; |
| 604 | struct nlattr *tb[IFLA_MAX+1] = {}; |
| 605 | struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh); |
| 606 | struct gprs_ns2_inst *nsi; |
| 607 | const char *ifname; |
| 608 | bool if_running; |
| 609 | |
| 610 | OSMO_ASSERT(omnl); |
| 611 | OSMO_ASSERT(ifm); |
| 612 | |
| 613 | nsi = omnl->priv; |
| 614 | |
| 615 | if (ifm->ifi_type != ARPHRD_FRAD) |
| 616 | return MNL_CB_OK; |
| 617 | |
| 618 | mnl_attr_parse(nlh, sizeof(*ifm), data_attr_cb, tb); |
| 619 | |
| 620 | if (!tb[IFLA_IFNAME]) |
| 621 | return MNL_CB_OK; |
| 622 | ifname = mnl_attr_get_str(tb[IFLA_IFNAME]); |
| 623 | if_running = !!(ifm->ifi_flags & IFF_RUNNING); |
| 624 | |
| 625 | bind = bind4netdev(nsi, ifname); |
Harald Welte | 6972aed | 2021-02-02 20:29:32 +0100 | [diff] [blame] | 626 | if (bind) |
| 627 | link_state_change(bind, if_running); |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 628 | |
| 629 | return MNL_CB_OK; |
| 630 | } |
| 631 | |
| 632 | /* trigger one initial dump of all link information */ |
| 633 | static void linkmon_initial_dump(struct osmo_mnl *omnl) |
| 634 | { |
| 635 | char buf[MNL_SOCKET_BUFFER_SIZE]; |
| 636 | struct nlmsghdr *nlh = mnl_nlmsg_put_header(buf); |
| 637 | struct rtgenmsg *rt; |
| 638 | |
| 639 | nlh->nlmsg_type = RTM_GETLINK; |
| 640 | nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
| 641 | nlh->nlmsg_seq = time(NULL); |
| 642 | rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg)); |
| 643 | rt->rtgen_family = AF_PACKET; |
| 644 | |
| 645 | if (mnl_socket_sendto(omnl->mnls, nlh, nlh->nlmsg_len) < 0) { |
| 646 | LOGP(DLNS, LOGL_ERROR, "linkmon: Cannot send rtnetlink message: %s\n", strerror(errno)); |
| 647 | } |
| 648 | |
| 649 | /* the response[s] will be handled just like the events */ |
| 650 | } |
| 651 | #endif /* LIBMNL */ |
| 652 | |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 653 | static int set_ifupdown(const char *netif, bool up) |
| 654 | { |
| 655 | int sock, rc; |
| 656 | struct ifreq req; |
| 657 | |
| 658 | sock = socket(AF_INET, SOCK_DGRAM, 0); |
| 659 | if (sock < 0) |
| 660 | return sock; |
| 661 | |
| 662 | memset(&req, 0, sizeof req); |
Harald Welte | 7f01b68 | 2020-12-21 12:39:38 +0100 | [diff] [blame] | 663 | OSMO_STRLCPY_ARRAY(req.ifr_name, netif); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 664 | |
Alexander Couzens | fdea03b | 2020-12-29 23:13:23 +0100 | [diff] [blame] | 665 | rc = ioctl(sock, SIOCGIFFLAGS, &req); |
Vadim Yanitskiy | 222e844 | 2021-01-05 14:33:29 +0100 | [diff] [blame] | 666 | if (rc < 0) { |
| 667 | close(sock); |
Alexander Couzens | fdea03b | 2020-12-29 23:13:23 +0100 | [diff] [blame] | 668 | return rc; |
Vadim Yanitskiy | 222e844 | 2021-01-05 14:33:29 +0100 | [diff] [blame] | 669 | } |
Alexander Couzens | fdea03b | 2020-12-29 23:13:23 +0100 | [diff] [blame] | 670 | |
Vadim Yanitskiy | 222e844 | 2021-01-05 14:33:29 +0100 | [diff] [blame] | 671 | if ((req.ifr_flags & IFF_UP) == up) { |
| 672 | close(sock); |
Alexander Couzens | fdea03b | 2020-12-29 23:13:23 +0100 | [diff] [blame] | 673 | return 0; |
Vadim Yanitskiy | 222e844 | 2021-01-05 14:33:29 +0100 | [diff] [blame] | 674 | } |
Alexander Couzens | fdea03b | 2020-12-29 23:13:23 +0100 | [diff] [blame] | 675 | |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 676 | if (up) |
| 677 | req.ifr_flags |= IFF_UP; |
| 678 | |
| 679 | rc = ioctl(sock, SIOCSIFFLAGS, &req); |
| 680 | close(sock); |
| 681 | return rc; |
| 682 | } |
| 683 | |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 684 | static int setup_device(const char *netif, const struct gprs_ns2_vc_bind *bind) |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 685 | { |
| 686 | int sock, rc; |
| 687 | char buffer[128]; |
| 688 | fr_proto *fr = (void*)buffer; |
| 689 | struct ifreq req; |
| 690 | |
| 691 | sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
| 692 | if (sock < 0) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 693 | LOGBIND(bind, LOGL_ERROR, "%s: Unable to create socket: %s\n", |
| 694 | netif, strerror(errno)); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 695 | return sock; |
| 696 | } |
| 697 | |
| 698 | memset(&req, 0, sizeof(struct ifreq)); |
| 699 | memset(&buffer, 0, sizeof(buffer)); |
Harald Welte | b8de188 | 2020-12-21 12:40:45 +0100 | [diff] [blame] | 700 | OSMO_STRLCPY_ARRAY(req.ifr_name, netif); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 701 | req.ifr_settings.ifs_ifsu.sync = (void*)buffer; |
| 702 | req.ifr_settings.size = sizeof(buffer); |
| 703 | req.ifr_settings.type = IF_GET_PROTO; |
| 704 | |
Alexander Couzens | 8c33d4a | 2020-12-22 15:42:01 +0100 | [diff] [blame] | 705 | /* EINVAL is returned when no protocol has been set */ |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 706 | rc = ioctl(sock, SIOCWANDEV, &req); |
Alexander Couzens | 8c33d4a | 2020-12-22 15:42:01 +0100 | [diff] [blame] | 707 | if (rc < 0 && errno != EINVAL) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 708 | LOGBIND(bind, LOGL_ERROR, "%s: Unable to get FR protocol information: %s\n", |
| 709 | netif, strerror(errno)); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 710 | goto err; |
| 711 | } |
| 712 | |
| 713 | /* check if the device is good */ |
Alexander Couzens | 8c33d4a | 2020-12-22 15:42:01 +0100 | [diff] [blame] | 714 | if (rc == 0 && req.ifr_settings.type == IF_PROTO_FR && fr->lmi == LMI_NONE) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 715 | LOGBIND(bind, LOGL_NOTICE, "%s: has correct frame relay mode and lmi\n", netif); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 716 | goto ifup; |
| 717 | } |
| 718 | |
| 719 | /* modify the device to match */ |
| 720 | rc = set_ifupdown(netif, false); |
| 721 | if (rc) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 722 | LOGBIND(bind, LOGL_ERROR, "Unable to bring down the device %s: %s\n", |
| 723 | netif, strerror(errno)); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 724 | goto err; |
| 725 | } |
| 726 | |
| 727 | memset(&req, 0, sizeof(struct ifreq)); |
| 728 | memset(fr, 0, sizeof(fr_proto)); |
Harald Welte | b8de188 | 2020-12-21 12:40:45 +0100 | [diff] [blame] | 729 | OSMO_STRLCPY_ARRAY(req.ifr_name, netif); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 730 | req.ifr_settings.type = IF_PROTO_FR; |
| 731 | req.ifr_settings.size = sizeof(fr_proto); |
| 732 | req.ifr_settings.ifs_ifsu.fr = fr; |
| 733 | fr->lmi = LMI_NONE; |
| 734 | /* even those settings aren't used, they must be in the range */ |
| 735 | /* polling verification timer*/ |
| 736 | fr->t391 = 10; |
| 737 | /* link integrity verification polling timer */ |
| 738 | fr->t392 = 15; |
| 739 | /* full status polling counter*/ |
| 740 | fr->n391 = 6; |
| 741 | /* error threshold */ |
| 742 | fr->n392 = 3; |
| 743 | /* monitored events count */ |
| 744 | fr->n393 = 4; |
| 745 | |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 746 | LOGBIND(bind, LOGL_INFO, "%s: Setting frame relay related parameters\n", netif); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 747 | rc = ioctl(sock, SIOCWANDEV, &req); |
| 748 | if (rc) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 749 | LOGBIND(bind, LOGL_ERROR, "%s: Unable to set FR protocol on information: %s\n", |
| 750 | netif, strerror(errno)); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 751 | goto err; |
| 752 | } |
| 753 | |
| 754 | ifup: |
| 755 | rc = set_ifupdown(netif, true); |
| 756 | if (rc) |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 757 | LOGBIND(bind, LOGL_ERROR, "Unable to bring up the device %s: %s\n", |
| 758 | netif, strerror(errno)); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 759 | err: |
| 760 | close(sock); |
| 761 | return rc; |
| 762 | } |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 763 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 764 | /*! Create a new bind for NS over FR. |
| 765 | * \param[in] nsi NS instance in which to create the bind |
| 766 | * \param[in] netif Network interface to bind to |
| 767 | * \param[in] fr_network |
| 768 | * \param[in] fr_role |
Alexander Couzens | c80a874 | 2021-02-03 11:27:52 +0100 | [diff] [blame] | 769 | * \param[out] result pointer to the created bind or if a bind with the name exists return the bind. |
| 770 | * \return 0 on success; negative on error. -EALREADY returned in case a bind with the name exists */ |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 771 | int gprs_ns2_fr_bind(struct gprs_ns2_inst *nsi, |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 772 | const char *name, |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 773 | const char *netif, |
| 774 | struct osmo_fr_network *fr_network, |
| 775 | enum osmo_fr_role fr_role, |
| 776 | struct gprs_ns2_vc_bind **result) |
| 777 | { |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 778 | struct gprs_ns2_vc_bind *bind; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 779 | struct priv_bind *priv; |
| 780 | struct osmo_fr_link *fr_link; |
| 781 | int rc = 0; |
| 782 | |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 783 | if (strlen(netif) > IFNAMSIZ) |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 784 | return -EINVAL; |
| 785 | |
Alexander Couzens | c80a874 | 2021-02-03 11:27:52 +0100 | [diff] [blame] | 786 | bind = gprs_ns2_bind_by_name(nsi, name); |
| 787 | if (bind) { |
| 788 | if (result) |
| 789 | *result = bind; |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 790 | return -EALREADY; |
Alexander Couzens | c80a874 | 2021-02-03 11:27:52 +0100 | [diff] [blame] | 791 | } |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 792 | |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 793 | rc = ns2_bind_alloc(nsi, name, &bind); |
| 794 | if (rc < 0) |
| 795 | return rc; |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 796 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 797 | bind->driver = &vc_driver_fr; |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 798 | bind->ll = GPRS_NS2_LL_FR; |
Alexander Couzens | 1c8785d | 2020-12-17 06:58:53 +0100 | [diff] [blame] | 799 | /* 2 mbit */ |
| 800 | bind->transfer_capability = 2; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 801 | bind->send_vc = fr_vc_sendmsg; |
| 802 | bind->free_vc = free_vc; |
| 803 | bind->dump_vty = dump_vty; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 804 | priv = bind->priv = talloc_zero(bind, struct priv_bind); |
| 805 | if (!priv) { |
Harald Welte | bdfb8b9 | 2021-01-31 11:44:57 +0100 | [diff] [blame] | 806 | rc = -ENOMEM; |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 807 | goto err_bind; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 808 | } |
| 809 | |
Harald Welte | ce6e4b7 | 2021-02-11 15:58:40 +0100 | [diff] [blame] | 810 | INIT_LLIST_HEAD(&priv->backlog.list); |
Neels Hofmeyr | 8fef761 | 2020-12-17 18:19:19 +0100 | [diff] [blame] | 811 | OSMO_STRLCPY_ARRAY(priv->netif, netif); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 812 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 813 | /* FIXME: move fd handling into socket.c */ |
| 814 | fr_link = osmo_fr_link_alloc(fr_network, fr_role, netif); |
| 815 | if (!fr_link) { |
| 816 | rc = -EINVAL; |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 817 | goto err_bind; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | fr_link->tx_cb = fr_tx_cb; |
Harald Welte | 2cc1d4d | 2021-01-31 18:33:31 +0100 | [diff] [blame] | 821 | fr_link->cb_data = bind; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 822 | priv->link = fr_link; |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 823 | |
Alexander Couzens | 6f89c77 | 2020-12-17 03:06:39 +0100 | [diff] [blame] | 824 | priv->ifindex = rc = devname2ifindex(netif); |
| 825 | if (rc < 0) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 826 | LOGBIND(bind, LOGL_ERROR, "Can not get interface index for interface %s\n", netif); |
Harald Welte | 41b188b | 2020-12-10 22:00:23 +0100 | [diff] [blame] | 827 | goto err_fr; |
| 828 | } |
| 829 | |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 830 | /* set protocol frame relay and lmi */ |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 831 | rc = setup_device(priv->netif, bind); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 832 | if(rc < 0) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 833 | LOGBIND(bind, LOGL_ERROR, "Failed to setup the interface %s for frame relay and lmi\n", netif); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 834 | goto err_fr; |
| 835 | } |
| 836 | |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 837 | rc = open_socket(priv->ifindex, bind); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 838 | if (rc < 0) |
| 839 | goto err_fr; |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 840 | priv->backlog.retry_us = 2500; /* start with some non-zero value; this corrsponds to 496 bytes */ |
| 841 | osmo_timer_setup(&priv->backlog.timer, fr_backlog_timer_cb, bind); |
| 842 | osmo_fd_setup(&priv->backlog.ofd, rc, OSMO_FD_READ, fr_netif_ofd_cb, bind, 0); |
| 843 | rc = osmo_fd_register(&priv->backlog.ofd); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 844 | if (rc < 0) |
| 845 | goto err_fd; |
| 846 | |
Harald Welte | 56f08a3 | 2020-12-01 23:07:32 +0100 | [diff] [blame] | 847 | #ifdef ENABLE_LIBMNL |
| 848 | if (!nsi->linkmon_mnl) |
| 849 | nsi->linkmon_mnl = osmo_mnl_init(nsi, NETLINK_ROUTE, RTMGRP_LINK, linkmon_mnl_cb, nsi); |
| 850 | |
| 851 | /* we get a new full dump after every bind. which is a bit excessive. But that's just once |
| 852 | * at start-up, so we can get away with it */ |
| 853 | if (nsi->linkmon_mnl) |
| 854 | linkmon_initial_dump(nsi->linkmon_mnl); |
| 855 | #endif |
| 856 | |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 857 | if (result) |
| 858 | *result = bind; |
| 859 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 860 | return rc; |
| 861 | |
| 862 | err_fd: |
Harald Welte | d06128d | 2021-01-30 21:17:21 +0100 | [diff] [blame] | 863 | close(priv->backlog.ofd.fd); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 864 | err_fr: |
| 865 | osmo_fr_link_free(fr_link); |
Harald Welte | 855155c | 2021-02-11 16:07:18 +0100 | [diff] [blame] | 866 | priv->link = NULL; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 867 | err_bind: |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 868 | gprs_ns2_free_bind(bind); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 869 | |
| 870 | return rc; |
| 871 | } |
| 872 | |
Alexander Couzens | c782cec | 2020-12-10 04:10:25 +0100 | [diff] [blame] | 873 | /*! Return the frame relay role of a bind |
| 874 | * \param[in] bind The bind |
| 875 | * \return the frame relay role or -EINVAL if bind is not frame relay |
| 876 | */ |
| 877 | enum osmo_fr_role gprs_ns2_fr_bind_role(struct gprs_ns2_vc_bind *bind) |
| 878 | { |
| 879 | struct priv_bind *priv; |
| 880 | |
| 881 | if (bind->driver != &vc_driver_fr) |
| 882 | return -EINVAL; |
| 883 | |
| 884 | priv = bind->priv; |
| 885 | return priv->link->role; |
| 886 | } |
| 887 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 888 | /*! Return the network interface of the bind |
| 889 | * \param[in] bind The bind |
| 890 | * \return the network interface |
| 891 | */ |
| 892 | const char *gprs_ns2_fr_bind_netif(struct gprs_ns2_vc_bind *bind) |
| 893 | { |
| 894 | struct priv_bind *priv; |
| 895 | |
| 896 | if (bind->driver != &vc_driver_fr) |
| 897 | return NULL; |
| 898 | |
| 899 | priv = bind->priv; |
| 900 | return priv->netif; |
| 901 | } |
| 902 | |
| 903 | /*! Find NS bind for a given network interface |
| 904 | * \param[in] nsi NS instance |
| 905 | * \param[in] netif the network interface to search for |
| 906 | * \return the bind or NULL if not found |
| 907 | */ |
| 908 | struct gprs_ns2_vc_bind *gprs_ns2_fr_bind_by_netif( |
| 909 | struct gprs_ns2_inst *nsi, |
| 910 | const char *netif) |
| 911 | { |
| 912 | struct gprs_ns2_vc_bind *bind; |
| 913 | const char *_netif; |
| 914 | |
| 915 | OSMO_ASSERT(nsi); |
| 916 | OSMO_ASSERT(netif); |
| 917 | |
| 918 | llist_for_each_entry(bind, &nsi->binding, list) { |
| 919 | if (!gprs_ns2_is_fr_bind(bind)) |
| 920 | continue; |
| 921 | |
| 922 | _netif = gprs_ns2_fr_bind_netif(bind); |
Alexander Couzens | 5c96f5d | 2020-12-17 04:45:03 +0100 | [diff] [blame] | 923 | if (!strncmp(_netif, netif, IFNAMSIZ)) |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 924 | return bind; |
| 925 | } |
| 926 | |
| 927 | return NULL; |
| 928 | } |
| 929 | |
| 930 | /*! Create, connect and activate a new FR-based NS-VC |
| 931 | * \param[in] bind bind in which the new NS-VC is to be created |
| 932 | * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created |
| 933 | * \param[in] dlci Data Link connection identifier |
| 934 | * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */ |
| 935 | 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] | 936 | struct gprs_ns2_nse *nse, |
| 937 | uint16_t nsvci, |
| 938 | uint16_t dlci) |
| 939 | { |
| 940 | struct gprs_ns2_vc *nsvc = NULL; |
| 941 | struct priv_vc *priv = NULL; |
Harald Welte | 603f404 | 2020-11-29 17:39:19 +0100 | [diff] [blame] | 942 | struct priv_bind *bpriv = bind->priv; |
| 943 | char idbuf[64]; |
Alexander Couzens | ebcbd72 | 2020-12-03 06:11:39 +0100 | [diff] [blame] | 944 | |
Alexander Couzens | 55bc869 | 2021-01-18 18:39:57 +0100 | [diff] [blame] | 945 | OSMO_ASSERT(gprs_ns2_is_fr_bind(bind)); |
Alexander Couzens | ebcbd72 | 2020-12-03 06:11:39 +0100 | [diff] [blame] | 946 | nsvc = gprs_ns2_fr_nsvc_by_dlci(bind, dlci); |
| 947 | if (nsvc) { |
| 948 | goto err; |
| 949 | } |
| 950 | |
Harald Welte | 603f404 | 2020-11-29 17:39:19 +0100 | [diff] [blame] | 951 | snprintf(idbuf, sizeof(idbuf), "%s-%s-DLCI%u-NSE%05u-NSVC%05u", gprs_ns2_lltype_str(nse->ll), |
| 952 | bpriv->netif, dlci, nse->nsei, nsvci); |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 953 | nsvc = ns2_vc_alloc(bind, nse, true, GPRS_NS2_VC_MODE_BLOCKRESET, idbuf); |
Alexander Couzens | ebcbd72 | 2020-12-03 06:11:39 +0100 | [diff] [blame] | 954 | if (!nsvc) |
| 955 | goto err; |
| 956 | |
| 957 | nsvc->priv = priv = fr_alloc_vc(bind, nsvc, dlci); |
| 958 | if (!priv) |
| 959 | goto err; |
| 960 | |
| 961 | nsvc->nsvci = nsvci; |
| 962 | nsvc->nsvci_is_valid = true; |
| 963 | |
Alexander Couzens | ebcbd72 | 2020-12-03 06:11:39 +0100 | [diff] [blame] | 964 | return nsvc; |
| 965 | |
| 966 | err: |
| 967 | gprs_ns2_free_nsvc(nsvc); |
| 968 | return NULL; |
| 969 | } |
| 970 | |
| 971 | |
| 972 | /*! Create, connect and activate a new FR-based NS-VC |
| 973 | * \param[in] bind bind in which the new NS-VC is to be created |
| 974 | * \param[in] nsei NSEI of the NS Entity in which the NS-VC is to be created |
| 975 | * \param[in] dlci Data Link connection identifier |
| 976 | * \return pointer to newly-allocated, connected and activated NS-VC; NULL on error */ |
| 977 | 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] | 978 | uint16_t nsei, |
| 979 | uint16_t nsvci, |
| 980 | uint16_t dlci) |
| 981 | { |
| 982 | bool created_nse = false; |
| 983 | struct gprs_ns2_vc *nsvc = NULL; |
Alexander Couzens | 55bc869 | 2021-01-18 18:39:57 +0100 | [diff] [blame] | 984 | struct gprs_ns2_nse *nse; |
| 985 | |
| 986 | OSMO_ASSERT(gprs_ns2_is_fr_bind(bind)); |
| 987 | nse = gprs_ns2_nse_by_nsei(bind->nsi, nsei); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 988 | if (!nse) { |
Alexander Couzens | 138b96f | 2021-01-25 16:23:29 +0100 | [diff] [blame] | 989 | nse = gprs_ns2_create_nse(bind->nsi, nsei, GPRS_NS2_LL_FR, GPRS_NS2_DIALECT_STATIC_RESETBLOCK); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 990 | if (!nse) |
| 991 | return NULL; |
| 992 | created_nse = true; |
| 993 | } |
| 994 | |
Harald Welte | 509047b | 2021-01-17 19:55:51 +0100 | [diff] [blame] | 995 | nsvc = gprs_ns2_fr_connect(bind, nse, nsvci, dlci); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 996 | if (!nsvc) |
| 997 | goto err_nse; |
| 998 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 999 | return nsvc; |
| 1000 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 1001 | err_nse: |
| 1002 | if (created_nse) |
| 1003 | gprs_ns2_free_nse(nse); |
| 1004 | |
| 1005 | return NULL; |
| 1006 | } |
| 1007 | |
| 1008 | /*! Return the nsvc by dlci. |
| 1009 | * \param[in] bind |
| 1010 | * \param[in] dlci Data Link connection identifier |
| 1011 | * \return the nsvc or NULL if not found |
| 1012 | */ |
| 1013 | struct gprs_ns2_vc *gprs_ns2_fr_nsvc_by_dlci(struct gprs_ns2_vc_bind *bind, |
| 1014 | uint16_t dlci) |
| 1015 | { |
| 1016 | struct gprs_ns2_vc *nsvc; |
| 1017 | struct priv_vc *vcpriv; |
| 1018 | |
Alexander Couzens | 55bc869 | 2021-01-18 18:39:57 +0100 | [diff] [blame] | 1019 | OSMO_ASSERT(gprs_ns2_is_fr_bind(bind)); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 1020 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
| 1021 | vcpriv = nsvc->priv; |
| 1022 | |
| 1023 | if (dlci == vcpriv->dlci) |
| 1024 | return nsvc; |
| 1025 | } |
| 1026 | |
| 1027 | return NULL; |
| 1028 | } |
| 1029 | |
| 1030 | /*! Return the dlci of the nsvc |
| 1031 | * \param[in] nsvc |
| 1032 | * \return the dlci or 0 on error. 0 is not a valid dlci. |
| 1033 | */ |
Alexander Couzens | 22c26e0 | 2020-12-10 04:10:07 +0100 | [diff] [blame] | 1034 | 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] | 1035 | { |
| 1036 | struct priv_vc *vcpriv; |
| 1037 | |
| 1038 | if (!nsvc->bind) |
| 1039 | return 0; |
| 1040 | |
| 1041 | if (nsvc->bind->driver != &vc_driver_fr) |
| 1042 | return 0; |
| 1043 | |
| 1044 | vcpriv = nsvc->priv; |
| 1045 | return vcpriv->dlci; |
| 1046 | } |