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