Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 1 | /*! \file gprs_ns2_frgre.c |
| 2 | * NS-over-FR-over-GRE implementation. |
| 3 | * GPRS Networks Service (NS) messages on the Gb interface. |
| 4 | * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) |
| 5 | * as well as its successor 3GPP TS 48.016 */ |
| 6 | |
| 7 | /* (C) 2009-2010,2014,2017 by Harald Welte <laforge@gnumonks.org> |
| 8 | * (C) 2020 sysmocom - s.f.m.c. GmbH |
| 9 | * Author: Alexander Couzens <lynxis@fe80.eu> |
| 10 | * |
| 11 | * All Rights Reserved |
| 12 | * |
| 13 | * SPDX-License-Identifier: GPL-2.0+ |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | * |
| 28 | */ |
| 29 | |
| 30 | #include <errno.h> |
| 31 | #include <string.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | #include <sys/socket.h> |
| 35 | #include <netinet/in.h> |
| 36 | #include <netinet/ip.h> |
| 37 | #include <netinet/ip6.h> |
| 38 | #include <arpa/inet.h> |
| 39 | |
| 40 | #include <osmocom/core/byteswap.h> |
| 41 | #include <osmocom/core/logging.h> |
| 42 | #include <osmocom/core/msgb.h> |
| 43 | #include <osmocom/core/select.h> |
| 44 | #include <osmocom/core/socket.h> |
| 45 | #include <osmocom/core/talloc.h> |
| 46 | #include <osmocom/gprs/gprs_ns2.h> |
| 47 | |
| 48 | #include "gprs_ns2_internal.h" |
| 49 | |
| 50 | #define GRE_PTYPE_FR 0x6559 |
| 51 | #define GRE_PTYPE_IPv4 0x0800 |
| 52 | #define GRE_PTYPE_IPv6 0x86dd |
| 53 | #define GRE_PTYPE_KAR 0x0000 /* keepalive response */ |
| 54 | |
| 55 | #ifndef IPPROTO_GRE |
| 56 | # define IPPROTO_GRE 47 |
| 57 | #endif |
| 58 | |
| 59 | struct gre_hdr { |
| 60 | uint16_t flags; |
| 61 | uint16_t ptype; |
| 62 | } __attribute__ ((packed)); |
| 63 | |
| 64 | #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__CYGWIN__) |
| 65 | /** |
| 66 | * On BSD the IPv4 struct is called struct ip and instead of iXX |
| 67 | * the members are called ip_XX. One could change this code to use |
| 68 | * struct ip but that would require to define _BSD_SOURCE and that |
| 69 | * might have other complications. Instead make sure struct iphdr |
| 70 | * is present on FreeBSD. The below is taken from GLIBC. |
| 71 | * |
| 72 | * The GNU C Library is free software; you can redistribute it and/or |
| 73 | * modify it under the terms of the GNU Lesser General Public |
| 74 | * License as published by the Free Software Foundation; either |
| 75 | * version 2.1 of the License, or (at your option) any later version. |
| 76 | */ |
| 77 | struct iphdr |
| 78 | { |
| 79 | #if BYTE_ORDER == LITTLE_ENDIAN |
| 80 | unsigned int ihl:4; |
| 81 | unsigned int version:4; |
| 82 | #elif BYTE_ORDER == BIG_ENDIAN |
| 83 | unsigned int version:4; |
| 84 | unsigned int ihl:4; |
| 85 | #endif |
| 86 | u_int8_t tos; |
| 87 | u_int16_t tot_len; |
| 88 | u_int16_t id; |
| 89 | u_int16_t frag_off; |
| 90 | u_int8_t ttl; |
| 91 | u_int8_t protocol; |
| 92 | u_int16_t check; |
| 93 | u_int32_t saddr; |
| 94 | u_int32_t daddr; |
| 95 | /*The options start here. */ |
| 96 | }; |
| 97 | #endif |
| 98 | |
| 99 | |
| 100 | static void free_bind(struct gprs_ns2_vc_bind *bind); |
| 101 | static inline int frgre_sendmsg(struct gprs_ns2_vc_bind *bind, |
| 102 | struct msgb *msg, |
| 103 | struct osmo_sockaddr *dest); |
| 104 | |
| 105 | struct gprs_ns2_vc_driver vc_driver_frgre = { |
| 106 | .name = "GB frame relay over GRE", |
| 107 | .free_bind = free_bind, |
| 108 | }; |
| 109 | |
| 110 | struct priv_bind { |
| 111 | struct osmo_fd fd; |
| 112 | struct osmo_sockaddr addr; |
| 113 | uint16_t dlci; |
| 114 | int dscp; |
| 115 | }; |
| 116 | |
| 117 | struct priv_vc { |
| 118 | struct osmo_sockaddr remote; |
| 119 | uint16_t dlci; |
| 120 | }; |
| 121 | |
| 122 | static void free_vc(struct gprs_ns2_vc *nsvc) |
| 123 | { |
| 124 | OSMO_ASSERT(nsvc); |
| 125 | |
| 126 | if (!nsvc->priv) |
| 127 | return; |
| 128 | |
| 129 | talloc_free(nsvc->priv); |
| 130 | nsvc->priv = NULL; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | /*! clean up all private driver state. Should be only called by gprs_ns2_free_bind() */ |
| 135 | static void free_bind(struct gprs_ns2_vc_bind *bind) |
| 136 | { |
| 137 | struct priv_bind *priv; |
| 138 | |
| 139 | if (!bind) |
| 140 | return; |
| 141 | |
| 142 | priv = bind->priv; |
| 143 | |
| 144 | OSMO_ASSERT(llist_empty(&bind->nsvc)); |
| 145 | |
| 146 | osmo_fd_close(&priv->fd); |
| 147 | talloc_free(priv); |
| 148 | } |
| 149 | |
| 150 | static struct priv_vc *frgre_alloc_vc(struct gprs_ns2_vc_bind *bind, |
| 151 | struct gprs_ns2_vc *nsvc, |
| 152 | struct osmo_sockaddr *remote, |
| 153 | uint16_t dlci) |
| 154 | { |
| 155 | struct priv_vc *priv = talloc_zero(bind, struct priv_vc); |
| 156 | if (!priv) |
| 157 | return NULL; |
| 158 | |
| 159 | nsvc->priv = priv; |
| 160 | priv->remote = *remote; |
| 161 | priv->dlci = dlci; |
| 162 | |
| 163 | return priv; |
| 164 | } |
| 165 | |
| 166 | static int handle_rx_gre_ipv6(struct osmo_fd *bfd, struct msgb *msg, |
| 167 | struct ip6_hdr *ip6hdr, struct gre_hdr *greh) |
| 168 | { |
| 169 | /* RFC 7676 IPv6 Support for Generic Routing Encapsulation (GRE) */ |
| 170 | struct gprs_ns2_vc_bind *bind = bfd->data; |
| 171 | struct priv_bind *priv = bind->priv; |
| 172 | int gre_payload_len; |
| 173 | struct ip6_hdr *inner_ip6h; |
| 174 | struct gre_hdr *inner_greh; |
| 175 | struct sockaddr_in6 daddr; |
| 176 | struct in6_addr ia6; |
| 177 | |
| 178 | gre_payload_len = msg->len - (sizeof(*ip6hdr) + sizeof(*greh)); |
| 179 | |
| 180 | inner_ip6h = (struct ip6_hdr *) ((uint8_t *)greh + sizeof(*greh)); |
| 181 | |
| 182 | if (gre_payload_len < sizeof(*ip6hdr) + sizeof(*inner_greh)) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 183 | LOGBIND(bind, LOGL_ERROR, "GRE keepalive too short\n"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 184 | return -EIO; |
| 185 | } |
| 186 | |
| 187 | if (!memcmp(&inner_ip6h->ip6_src, &ip6hdr->ip6_src, sizeof(struct in6_addr)) || |
| 188 | !memcmp(&inner_ip6h->ip6_dst, &ip6hdr->ip6_dst, sizeof(struct in6_addr))) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 189 | LOGBIND(bind, LOGL_ERROR, "GRE keepalive with wrong tunnel addresses\n"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 190 | return -EIO; |
| 191 | } |
| 192 | |
| 193 | /* Are IPv6 extensions header are allowed in the *inner*? In the outer they are */ |
| 194 | if (inner_ip6h->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_GRE) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 195 | LOGBIND(bind, LOGL_ERROR, "GRE keepalive with wrong protocol\n"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 196 | return -EIO; |
| 197 | } |
| 198 | |
| 199 | inner_greh = (struct gre_hdr *) ((uint8_t *)inner_ip6h + sizeof(struct ip6_hdr)); |
| 200 | if (inner_greh->ptype != osmo_htons(GRE_PTYPE_KAR)) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 201 | LOGBIND(bind, LOGL_ERROR, "GRE keepalive inner GRE type != 0\n"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 202 | return -EIO; |
| 203 | } |
| 204 | |
| 205 | /* Actually send the response back */ |
| 206 | |
| 207 | daddr.sin6_family = AF_INET6; |
| 208 | daddr.sin6_addr = inner_ip6h->ip6_dst; |
| 209 | daddr.sin6_port = IPPROTO_GRE; |
| 210 | |
| 211 | ia6 = ip6hdr->ip6_src; |
| 212 | char ip6str[INET6_ADDRSTRLEN] = {}; |
| 213 | inet_ntop(AF_INET6, &ia6, ip6str, INET6_ADDRSTRLEN); |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 214 | LOGBIND(bind, LOGL_DEBUG, "GRE keepalive from %s, responding\n", ip6str); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 215 | |
| 216 | /* why does it reduce the gre_payload_len by the ipv6 header? |
| 217 | * make it similiar to ipv4 even this seems to be wrong */ |
| 218 | return sendto(priv->fd.fd, inner_greh, |
| 219 | gre_payload_len - sizeof(*inner_ip6h), 0, |
| 220 | (struct sockaddr *)&daddr, sizeof(daddr)); |
| 221 | } |
| 222 | |
| 223 | /* IPv4 messages inside the GRE tunnel might be GRE keepalives */ |
| 224 | static int handle_rx_gre_ipv4(struct osmo_fd *bfd, struct msgb *msg, |
| 225 | struct iphdr *iph, struct gre_hdr *greh) |
| 226 | { |
| 227 | struct gprs_ns2_vc_bind *bind = bfd->data; |
| 228 | struct priv_bind *priv = bind->priv; |
| 229 | int gre_payload_len; |
| 230 | struct iphdr *inner_iph; |
| 231 | struct gre_hdr *inner_greh; |
| 232 | struct sockaddr_in daddr; |
| 233 | struct in_addr ia; |
| 234 | |
| 235 | gre_payload_len = msg->len - (iph->ihl*4 + sizeof(*greh)); |
| 236 | |
| 237 | inner_iph = (struct iphdr *) ((uint8_t *)greh + sizeof(*greh)); |
| 238 | |
| 239 | if (gre_payload_len < inner_iph->ihl*4 + sizeof(*inner_greh)) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 240 | LOGBIND(bind, LOGL_ERROR, "GRE keepalive too short\n"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 241 | return -EIO; |
| 242 | } |
| 243 | |
| 244 | if (inner_iph->saddr != iph->daddr || |
| 245 | inner_iph->daddr != iph->saddr) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 246 | LOGBIND(bind, LOGL_ERROR, "GRE keepalive with wrong tunnel addresses\n"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 247 | return -EIO; |
| 248 | } |
| 249 | |
| 250 | if (inner_iph->protocol != IPPROTO_GRE) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 251 | LOGBIND(bind, LOGL_ERROR, "GRE keepalive with wrong protocol\n"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 252 | return -EIO; |
| 253 | } |
| 254 | |
| 255 | inner_greh = (struct gre_hdr *) ((uint8_t *)inner_iph + iph->ihl*4); |
| 256 | if (inner_greh->ptype != osmo_htons(GRE_PTYPE_KAR)) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 257 | LOGBIND(bind, LOGL_ERROR, "GRE keepalive inner GRE type != 0\n"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 258 | return -EIO; |
| 259 | } |
| 260 | |
| 261 | /* Actually send the response back */ |
| 262 | |
| 263 | daddr.sin_family = AF_INET; |
| 264 | daddr.sin_addr.s_addr = inner_iph->daddr; |
| 265 | daddr.sin_port = IPPROTO_GRE; |
| 266 | |
| 267 | ia.s_addr = iph->saddr; |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 268 | LOGBIND(bind, LOGL_DEBUG, "GRE keepalive from %s, responding\n", inet_ntoa(ia)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 269 | |
| 270 | /* why does it reduce the gre_payload_len by the ipv4 header? */ |
| 271 | return sendto(priv->fd.fd, inner_greh, |
| 272 | gre_payload_len - inner_iph->ihl*4, 0, |
| 273 | (struct sockaddr *)&daddr, sizeof(daddr)); |
| 274 | } |
| 275 | |
| 276 | static struct msgb *read_nsfrgre_msg(struct osmo_fd *bfd, int *error, |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 277 | struct osmo_sockaddr *saddr, uint16_t *dlci, |
| 278 | const struct gprs_ns2_vc_bind *bind) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 279 | { |
| 280 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Gb/NS/FR/GRE Rx"); |
| 281 | int ret = 0; |
| 282 | socklen_t saddr_len = sizeof(*saddr); |
| 283 | struct iphdr *iph = NULL; |
| 284 | struct ip6_hdr *ip6h = NULL; |
| 285 | size_t ip46hdr; |
| 286 | struct gre_hdr *greh; |
| 287 | uint8_t *frh; |
| 288 | |
| 289 | if (!msg) { |
| 290 | *error = -ENOMEM; |
| 291 | return NULL; |
| 292 | } |
| 293 | |
| 294 | ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0, |
| 295 | &saddr->u.sa, &saddr_len); |
| 296 | if (ret < 0) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 297 | LOGBIND(bind, LOGL_ERROR, "recv error %s during NS-FR-GRE recv\n", strerror(errno)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 298 | *error = ret; |
| 299 | goto out_err; |
| 300 | } else if (ret == 0) { |
| 301 | *error = ret; |
| 302 | goto out_err; |
| 303 | } |
| 304 | |
| 305 | msgb_put(msg, ret); |
| 306 | |
| 307 | /* we've received a raw packet including the IPv4 or IPv6 header */ |
| 308 | switch (saddr->u.sa.sa_family) { |
| 309 | case AF_INET: |
| 310 | ip46hdr = sizeof(struct iphdr); |
| 311 | break; |
| 312 | case AF_INET6: |
| 313 | ip46hdr = sizeof(struct ip6_hdr); |
Daniel Willmann | 210774f | 2021-01-07 13:31:56 +0100 | [diff] [blame] | 314 | break; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 315 | default: |
| 316 | *error = -EIO; |
| 317 | goto out_err; |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | /* TODO: add support for the extension headers */ |
| 322 | if (msg->len < ip46hdr + sizeof(*greh) + 2) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 323 | LOGBIND(bind, LOGL_ERROR, "Short IP packet: %u bytes\n", msg->len); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 324 | *error = -EIO; |
| 325 | goto out_err; |
| 326 | } |
| 327 | |
| 328 | switch (saddr->u.sa.sa_family) { |
| 329 | case AF_INET: |
| 330 | iph = (struct iphdr *) msg->data; |
| 331 | if (msg->len < (iph->ihl*4 + sizeof(*greh) + 2)) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 332 | LOGBIND(bind, LOGL_ERROR, "Short IP packet: %u bytes\n", msg->len); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 333 | *error = -EIO; |
| 334 | goto out_err; |
| 335 | } |
| 336 | break; |
| 337 | case AF_INET6: |
| 338 | ip6h = (struct ip6_hdr *) msg->data; |
| 339 | break; |
| 340 | } |
| 341 | |
Alexander Couzens | 47ed2a6 | 2021-02-03 11:46:59 +0100 | [diff] [blame] | 342 | if (iph) |
| 343 | greh = (struct gre_hdr *) (msg->data + iph->ihl*4); |
| 344 | else |
| 345 | greh = (struct gre_hdr *) (msg->data + sizeof(struct ip6_hdr)); |
| 346 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 347 | if (greh->flags) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 348 | LOGBIND(bind, LOGL_NOTICE, "Unknown GRE flags 0x%04x\n", osmo_ntohs(greh->flags)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | switch (osmo_ntohs(greh->ptype)) { |
| 352 | case GRE_PTYPE_IPv4: |
| 353 | /* IPv4 messages might be GRE keepalives */ |
Alexander Couzens | dc43f7d | 2021-02-03 11:53:11 +0100 | [diff] [blame] | 354 | if (iph) |
| 355 | *error = handle_rx_gre_ipv4(bfd, msg, iph, greh); |
| 356 | else |
| 357 | *error = -EIO; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 358 | goto out_err; |
| 359 | break; |
| 360 | case GRE_PTYPE_IPv6: |
Alexander Couzens | dc43f7d | 2021-02-03 11:53:11 +0100 | [diff] [blame] | 361 | if (ip6h) |
| 362 | *error = handle_rx_gre_ipv6(bfd, msg, ip6h, greh); |
| 363 | else |
| 364 | *error = -EIO; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 365 | goto out_err; |
| 366 | break; |
| 367 | case GRE_PTYPE_FR: |
| 368 | /* continue as usual */ |
| 369 | break; |
| 370 | default: |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 371 | LOGBIND(bind, LOGL_NOTICE, "Unknown GRE protocol 0x%04x != FR\n", osmo_ntohs(greh->ptype)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 372 | *error = -EIO; |
| 373 | goto out_err; |
| 374 | break; |
| 375 | } |
| 376 | |
| 377 | if (msg->len < sizeof(*greh) + 2) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 378 | LOGBIND(bind, LOGL_ERROR, "Short FR header: %u bytes\n", msg->len); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 379 | *error = -EIO; |
| 380 | goto out_err; |
| 381 | } |
| 382 | |
| 383 | frh = (uint8_t *)greh + sizeof(*greh); |
| 384 | if (frh[0] & 0x01) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 385 | LOGBIND(bind, LOGL_NOTICE, "Unsupported single-byte FR address\n"); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 386 | *error = -EIO; |
| 387 | goto out_err; |
| 388 | } |
| 389 | *dlci = ((frh[0] & 0xfc) << 2); |
| 390 | if ((frh[1] & 0x0f) != 0x01) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 391 | LOGBIND(bind, LOGL_NOTICE, "Unknown second FR octet 0x%02x\n", frh[1]); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 392 | *error = -EIO; |
| 393 | goto out_err; |
| 394 | } |
| 395 | *dlci |= (frh[1] >> 4); |
| 396 | |
| 397 | msg->l2h = frh+2; |
| 398 | |
| 399 | return msg; |
| 400 | |
| 401 | out_err: |
| 402 | msgb_free(msg); |
| 403 | return NULL; |
| 404 | } |
| 405 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 406 | static int ns2_find_vc_by_dlci(struct gprs_ns2_vc_bind *bind, |
| 407 | uint16_t dlci, |
| 408 | struct gprs_ns2_vc **result) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 409 | { |
| 410 | struct gprs_ns2_vc *nsvc; |
| 411 | struct priv_vc *vcpriv; |
| 412 | |
| 413 | if (!result) |
| 414 | return -EINVAL; |
| 415 | |
| 416 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
| 417 | vcpriv = nsvc->priv; |
| 418 | if (vcpriv->dlci != dlci) { |
| 419 | *result = nsvc; |
| 420 | return 0; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | return 1; |
| 425 | } |
| 426 | |
| 427 | static int handle_nsfrgre_read(struct osmo_fd *bfd) |
| 428 | { |
| 429 | int rc; |
| 430 | struct osmo_sockaddr saddr; |
| 431 | struct gprs_ns2_vc *nsvc; |
| 432 | struct gprs_ns2_vc_bind *bind = bfd->data; |
| 433 | struct msgb *msg; |
| 434 | struct msgb *reject; |
| 435 | uint16_t dlci; |
| 436 | |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 437 | msg = read_nsfrgre_msg(bfd, &rc, &saddr, &dlci, bind); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 438 | if (!msg) |
| 439 | return rc; |
| 440 | |
| 441 | if (dlci == 0 || dlci == 1023) { |
Harald Welte | f294974 | 2021-01-20 14:54:14 +0100 | [diff] [blame] | 442 | LOGBIND(bind, LOGL_INFO, "Received FR on LMI DLCI %u - ignoring\n", dlci); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 443 | rc = 0; |
| 444 | goto out; |
| 445 | } |
| 446 | |
Alexander Couzens | f7e2cac | 2021-01-25 16:18:21 +0100 | [diff] [blame] | 447 | rc = ns2_find_vc_by_dlci(bind, dlci, &nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 448 | if (rc) { |
| 449 | /* VC not found */ |
Harald Welte | 7d0daac | 2021-03-02 23:08:43 +0100 | [diff] [blame] | 450 | rc = ns2_create_vc(bind, msg, &saddr, "newconnection", &reject, &nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 451 | switch (rc) { |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 452 | case NS2_CS_FOUND: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 453 | break; |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 454 | case NS2_CS_ERROR: |
| 455 | case NS2_CS_SKIPPED: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 456 | rc = 0; |
Alexander Couzens | 1301012 | 2020-09-24 00:54:51 +0200 | [diff] [blame] | 457 | goto out; |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 458 | case NS2_CS_REJECTED: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 459 | /* nsip_sendmsg will free reject */ |
Alexander Couzens | 772ca61 | 2020-09-24 16:19:02 +0200 | [diff] [blame] | 460 | rc = frgre_sendmsg(bind, reject, &saddr); |
| 461 | goto out; |
Alexander Couzens | ba5a992 | 2021-01-25 16:03:23 +0100 | [diff] [blame] | 462 | case NS2_CS_CREATED: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 463 | frgre_alloc_vc(bind, nsvc, &saddr, dlci); |
Alexander Couzens | 8dfc24c | 2021-01-25 16:09:23 +0100 | [diff] [blame] | 464 | ns2_vc_fsm_start(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 465 | break; |
| 466 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 467 | } |
Alexander Couzens | 1301012 | 2020-09-24 00:54:51 +0200 | [diff] [blame] | 468 | |
| 469 | rc = ns2_recv_vc(nsvc, msg); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 470 | out: |
| 471 | msgb_free(msg); |
| 472 | |
| 473 | return rc; |
| 474 | } |
| 475 | |
| 476 | static int handle_nsfrgre_write(struct osmo_fd *bfd) |
| 477 | { |
| 478 | /* FIXME: actually send the data here instead of nsip_sendmsg() */ |
| 479 | return -EIO; |
| 480 | } |
| 481 | |
| 482 | static inline int frgre_sendmsg(struct gprs_ns2_vc_bind *bind, |
| 483 | struct msgb *msg, |
| 484 | struct osmo_sockaddr *dest) |
| 485 | { |
| 486 | int rc; |
| 487 | struct priv_bind *priv = bind->priv; |
| 488 | |
| 489 | rc = sendto(priv->fd.fd, msg->data, msg->len, 0, |
| 490 | &dest->u.sa, sizeof(*dest)); |
| 491 | |
| 492 | msgb_free(msg); |
| 493 | |
| 494 | return rc; |
| 495 | } |
| 496 | |
| 497 | static int frgre_vc_sendmsg(struct gprs_ns2_vc *nsvc, struct msgb *msg) |
| 498 | { |
| 499 | struct gprs_ns2_vc_bind *bind = nsvc->bind; |
| 500 | struct priv_vc *vcpriv = nsvc->priv; |
| 501 | struct priv_bind *bindpriv = bind->priv; |
| 502 | |
| 503 | uint16_t dlci = osmo_htons(bindpriv->dlci); |
| 504 | uint8_t *frh; |
| 505 | struct gre_hdr *greh; |
| 506 | |
| 507 | /* Prepend the FR header */ |
| 508 | frh = msgb_push(msg, 2); |
| 509 | frh[0] = (dlci >> 2) & 0xfc; |
| 510 | frh[1] = ((dlci & 0xf)<<4) | 0x01; |
| 511 | |
| 512 | /* Prepend the GRE header */ |
| 513 | greh = (struct gre_hdr *) msgb_push(msg, sizeof(*greh)); |
| 514 | greh->flags = 0; |
| 515 | greh->ptype = osmo_htons(GRE_PTYPE_FR); |
| 516 | |
| 517 | return frgre_sendmsg(bind, msg, &vcpriv->remote); |
| 518 | } |
| 519 | |
| 520 | static int frgre_fd_cb(struct osmo_fd *bfd, unsigned int what) |
| 521 | { |
| 522 | int rc = 0; |
| 523 | |
| 524 | if (what & OSMO_FD_READ) |
| 525 | rc = handle_nsfrgre_read(bfd); |
| 526 | if (what & OSMO_FD_WRITE) |
| 527 | rc = handle_nsfrgre_write(bfd); |
| 528 | |
| 529 | return rc; |
| 530 | } |
| 531 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 532 | /*! determine if given bind is for FR-GRE encapsulation. */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 533 | int gprs_ns2_is_frgre_bind(struct gprs_ns2_vc_bind *bind) |
| 534 | { |
| 535 | return (bind->driver == &vc_driver_frgre); |
| 536 | } |
| 537 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 538 | /*! Create a new bind for NS over FR-GRE. |
| 539 | * \param[in] nsi NS instance in which to create the bind |
| 540 | * \param[in] local local address on which to bind |
| 541 | * \param[in] dscp DSCP/TOS bits to use for transmitted data on this bind |
Alexander Couzens | c80a874 | 2021-02-03 11:27:52 +0100 | [diff] [blame] | 542 | * \param[out] result pointer to the created bind or if a bind with the name exists return the bind. |
| 543 | * \return 0 on success; negative on error. -EALREADY returned in case a bind with the name exists */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 544 | int gprs_ns2_frgre_bind(struct gprs_ns2_inst *nsi, |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 545 | const char *name, |
Vadim Yanitskiy | a07f25e | 2020-10-09 21:47:01 +0700 | [diff] [blame] | 546 | const struct osmo_sockaddr *local, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 547 | int dscp, |
| 548 | struct gprs_ns2_vc_bind **result) |
| 549 | { |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 550 | struct gprs_ns2_vc_bind *bind; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 551 | struct priv_bind *priv; |
| 552 | int rc; |
| 553 | |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 554 | if (local->u.sa.sa_family != AF_INET && local->u.sa.sa_family != AF_INET6) |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 555 | return -EINVAL; |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 556 | |
Harald Welte | f8a4e13 | 2021-04-28 19:44:48 +0200 | [diff] [blame] | 557 | if (dscp < 0 || dscp > 63) |
| 558 | return -EINVAL; |
| 559 | |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 560 | bind = gprs_ns2_bind_by_name(nsi, name); |
| 561 | if (bind) { |
Alexander Couzens | c80a874 | 2021-02-03 11:27:52 +0100 | [diff] [blame] | 562 | if (result) |
| 563 | *result = bind; |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 564 | return -EALREADY; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 565 | } |
| 566 | |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 567 | rc = ns2_bind_alloc(nsi, name, &bind); |
| 568 | if (rc < 0) |
| 569 | return rc; |
Alexander Couzens | aaa55a6 | 2020-12-03 06:02:03 +0100 | [diff] [blame] | 570 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 571 | bind->driver = &vc_driver_frgre; |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 572 | bind->ll = GPRS_NS2_LL_FR_GRE; |
Alexander Couzens | 1c8785d | 2020-12-17 06:58:53 +0100 | [diff] [blame] | 573 | /* 2 mbit transfer capability. Counting should be done different for this. */ |
| 574 | bind->transfer_capability = 2; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 575 | bind->send_vc = frgre_vc_sendmsg; |
| 576 | bind->free_vc = free_vc; |
| 577 | bind->nsi = nsi; |
Alexander Couzens | 4f1128f | 2021-01-20 17:42:48 +0100 | [diff] [blame] | 578 | /* TODO: allow to set the MTU via vty. It can not be automatic detected because it goes over an |
| 579 | * ethernet device and the MTU here must match the FR side of the FR-to-GRE gateway. |
| 580 | */ |
| 581 | bind->mtu = FRAME_RELAY_SDU; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 582 | |
| 583 | priv = bind->priv = talloc_zero(bind, struct priv_bind); |
| 584 | if (!priv) { |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 585 | gprs_ns2_free_bind(bind); |
Harald Welte | bdfb8b9 | 2021-01-31 11:44:57 +0100 | [diff] [blame] | 586 | return -ENOMEM; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 587 | } |
| 588 | priv->fd.cb = frgre_fd_cb; |
| 589 | priv->fd.data = bind; |
| 590 | priv->addr = *local; |
| 591 | INIT_LLIST_HEAD(&bind->nsvc); |
Harald Welte | 485b3f7 | 2021-04-28 19:45:34 +0200 | [diff] [blame] | 592 | priv->dscp = dscp; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 593 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 594 | rc = osmo_sock_init_osa_ofd(&priv->fd, SOCK_RAW, IPPROTO_GRE, |
| 595 | local, NULL, |
Harald Welte | 485b3f7 | 2021-04-28 19:45:34 +0200 | [diff] [blame] | 596 | OSMO_SOCK_F_BIND | OSMO_SOCK_F_DSCP(priv->dscp)); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 597 | if (rc < 0) { |
Harald Welte | c3aa8f9 | 2021-01-31 11:41:34 +0100 | [diff] [blame] | 598 | gprs_ns2_free_bind(bind); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 599 | return rc; |
| 600 | } |
| 601 | |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 602 | if (result) |
| 603 | *result = bind; |
| 604 | |
| 605 | return rc; |
| 606 | } |