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)) { |
| 183 | LOGP(DLNS, LOGL_ERROR, "GRE keepalive too short\n"); |
| 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))) { |
| 189 | LOGP(DLNS, LOGL_ERROR, |
| 190 | "GRE keepalive with wrong tunnel addresses\n"); |
| 191 | return -EIO; |
| 192 | } |
| 193 | |
| 194 | /* Are IPv6 extensions header are allowed in the *inner*? In the outer they are */ |
| 195 | if (inner_ip6h->ip6_ctlun.ip6_un1.ip6_un1_nxt != IPPROTO_GRE) { |
| 196 | LOGP(DLNS, LOGL_ERROR, "GRE keepalive with wrong protocol\n"); |
| 197 | return -EIO; |
| 198 | } |
| 199 | |
| 200 | inner_greh = (struct gre_hdr *) ((uint8_t *)inner_ip6h + sizeof(struct ip6_hdr)); |
| 201 | if (inner_greh->ptype != osmo_htons(GRE_PTYPE_KAR)) { |
| 202 | LOGP(DLNS, LOGL_ERROR, "GRE keepalive inner GRE type != 0\n"); |
| 203 | return -EIO; |
| 204 | } |
| 205 | |
| 206 | /* Actually send the response back */ |
| 207 | |
| 208 | daddr.sin6_family = AF_INET6; |
| 209 | daddr.sin6_addr = inner_ip6h->ip6_dst; |
| 210 | daddr.sin6_port = IPPROTO_GRE; |
| 211 | |
| 212 | ia6 = ip6hdr->ip6_src; |
| 213 | char ip6str[INET6_ADDRSTRLEN] = {}; |
| 214 | inet_ntop(AF_INET6, &ia6, ip6str, INET6_ADDRSTRLEN); |
| 215 | LOGP(DLNS, LOGL_DEBUG, "GRE keepalive from %s, responding\n", ip6str); |
| 216 | |
| 217 | /* why does it reduce the gre_payload_len by the ipv6 header? |
| 218 | * make it similiar to ipv4 even this seems to be wrong */ |
| 219 | return sendto(priv->fd.fd, inner_greh, |
| 220 | gre_payload_len - sizeof(*inner_ip6h), 0, |
| 221 | (struct sockaddr *)&daddr, sizeof(daddr)); |
| 222 | } |
| 223 | |
| 224 | /* IPv4 messages inside the GRE tunnel might be GRE keepalives */ |
| 225 | static int handle_rx_gre_ipv4(struct osmo_fd *bfd, struct msgb *msg, |
| 226 | struct iphdr *iph, struct gre_hdr *greh) |
| 227 | { |
| 228 | struct gprs_ns2_vc_bind *bind = bfd->data; |
| 229 | struct priv_bind *priv = bind->priv; |
| 230 | int gre_payload_len; |
| 231 | struct iphdr *inner_iph; |
| 232 | struct gre_hdr *inner_greh; |
| 233 | struct sockaddr_in daddr; |
| 234 | struct in_addr ia; |
| 235 | |
| 236 | gre_payload_len = msg->len - (iph->ihl*4 + sizeof(*greh)); |
| 237 | |
| 238 | inner_iph = (struct iphdr *) ((uint8_t *)greh + sizeof(*greh)); |
| 239 | |
| 240 | if (gre_payload_len < inner_iph->ihl*4 + sizeof(*inner_greh)) { |
| 241 | LOGP(DLNS, LOGL_ERROR, "GRE keepalive too short\n"); |
| 242 | return -EIO; |
| 243 | } |
| 244 | |
| 245 | if (inner_iph->saddr != iph->daddr || |
| 246 | inner_iph->daddr != iph->saddr) { |
| 247 | LOGP(DLNS, LOGL_ERROR, |
| 248 | "GRE keepalive with wrong tunnel addresses\n"); |
| 249 | return -EIO; |
| 250 | } |
| 251 | |
| 252 | if (inner_iph->protocol != IPPROTO_GRE) { |
| 253 | LOGP(DLNS, LOGL_ERROR, "GRE keepalive with wrong protocol\n"); |
| 254 | return -EIO; |
| 255 | } |
| 256 | |
| 257 | inner_greh = (struct gre_hdr *) ((uint8_t *)inner_iph + iph->ihl*4); |
| 258 | if (inner_greh->ptype != osmo_htons(GRE_PTYPE_KAR)) { |
| 259 | LOGP(DLNS, LOGL_ERROR, "GRE keepalive inner GRE type != 0\n"); |
| 260 | return -EIO; |
| 261 | } |
| 262 | |
| 263 | /* Actually send the response back */ |
| 264 | |
| 265 | daddr.sin_family = AF_INET; |
| 266 | daddr.sin_addr.s_addr = inner_iph->daddr; |
| 267 | daddr.sin_port = IPPROTO_GRE; |
| 268 | |
| 269 | ia.s_addr = iph->saddr; |
| 270 | LOGP(DLNS, LOGL_DEBUG, "GRE keepalive from %s, responding\n", |
| 271 | inet_ntoa(ia)); |
| 272 | |
| 273 | /* why does it reduce the gre_payload_len by the ipv4 header? */ |
| 274 | return sendto(priv->fd.fd, inner_greh, |
| 275 | gre_payload_len - inner_iph->ihl*4, 0, |
| 276 | (struct sockaddr *)&daddr, sizeof(daddr)); |
| 277 | } |
| 278 | |
| 279 | static struct msgb *read_nsfrgre_msg(struct osmo_fd *bfd, int *error, |
| 280 | struct osmo_sockaddr *saddr, uint16_t *dlci) |
| 281 | { |
| 282 | struct msgb *msg = msgb_alloc(NS_ALLOC_SIZE, "Gb/NS/FR/GRE Rx"); |
| 283 | int ret = 0; |
| 284 | socklen_t saddr_len = sizeof(*saddr); |
| 285 | struct iphdr *iph = NULL; |
| 286 | struct ip6_hdr *ip6h = NULL; |
| 287 | size_t ip46hdr; |
| 288 | struct gre_hdr *greh; |
| 289 | uint8_t *frh; |
| 290 | |
| 291 | if (!msg) { |
| 292 | *error = -ENOMEM; |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | ret = recvfrom(bfd->fd, msg->data, NS_ALLOC_SIZE, 0, |
| 297 | &saddr->u.sa, &saddr_len); |
| 298 | if (ret < 0) { |
| 299 | LOGP(DLNS, LOGL_ERROR, "recv error %s during NS-FR-GRE recv\n", |
| 300 | strerror(errno)); |
| 301 | *error = ret; |
| 302 | goto out_err; |
| 303 | } else if (ret == 0) { |
| 304 | *error = ret; |
| 305 | goto out_err; |
| 306 | } |
| 307 | |
| 308 | msgb_put(msg, ret); |
| 309 | |
| 310 | /* we've received a raw packet including the IPv4 or IPv6 header */ |
| 311 | switch (saddr->u.sa.sa_family) { |
| 312 | case AF_INET: |
| 313 | ip46hdr = sizeof(struct iphdr); |
| 314 | break; |
| 315 | case AF_INET6: |
| 316 | ip46hdr = sizeof(struct ip6_hdr); |
| 317 | default: |
| 318 | *error = -EIO; |
| 319 | goto out_err; |
| 320 | break; |
| 321 | } |
| 322 | |
| 323 | /* TODO: add support for the extension headers */ |
| 324 | if (msg->len < ip46hdr + sizeof(*greh) + 2) { |
| 325 | LOGP(DLNS, LOGL_ERROR, "Short IP packet: %u bytes\n", msg->len); |
| 326 | *error = -EIO; |
| 327 | goto out_err; |
| 328 | } |
| 329 | |
| 330 | switch (saddr->u.sa.sa_family) { |
| 331 | case AF_INET: |
| 332 | iph = (struct iphdr *) msg->data; |
| 333 | if (msg->len < (iph->ihl*4 + sizeof(*greh) + 2)) { |
| 334 | LOGP(DLNS, LOGL_ERROR, "Short IP packet: %u bytes\n", msg->len); |
| 335 | *error = -EIO; |
| 336 | goto out_err; |
| 337 | } |
| 338 | break; |
| 339 | case AF_INET6: |
| 340 | ip6h = (struct ip6_hdr *) msg->data; |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | greh = (struct gre_hdr *) (msg->data + iph->ihl*4); |
| 345 | if (greh->flags) { |
| 346 | LOGP(DLNS, LOGL_NOTICE, "Unknown GRE flags 0x%04x\n", |
| 347 | osmo_ntohs(greh->flags)); |
| 348 | } |
| 349 | |
| 350 | switch (osmo_ntohs(greh->ptype)) { |
| 351 | case GRE_PTYPE_IPv4: |
| 352 | /* IPv4 messages might be GRE keepalives */ |
| 353 | *error = handle_rx_gre_ipv4(bfd, msg, iph, greh); |
| 354 | goto out_err; |
| 355 | break; |
| 356 | case GRE_PTYPE_IPv6: |
| 357 | *error = handle_rx_gre_ipv6(bfd, msg, ip6h, greh); |
| 358 | goto out_err; |
| 359 | break; |
| 360 | case GRE_PTYPE_FR: |
| 361 | /* continue as usual */ |
| 362 | break; |
| 363 | default: |
| 364 | LOGP(DLNS, LOGL_NOTICE, "Unknown GRE protocol 0x%04x != FR\n", |
| 365 | osmo_ntohs(greh->ptype)); |
| 366 | *error = -EIO; |
| 367 | goto out_err; |
| 368 | break; |
| 369 | } |
| 370 | |
| 371 | if (msg->len < sizeof(*greh) + 2) { |
| 372 | LOGP(DLNS, LOGL_ERROR, "Short FR header: %u bytes\n", msg->len); |
| 373 | *error = -EIO; |
| 374 | goto out_err; |
| 375 | } |
| 376 | |
| 377 | frh = (uint8_t *)greh + sizeof(*greh); |
| 378 | if (frh[0] & 0x01) { |
| 379 | LOGP(DLNS, LOGL_NOTICE, "Unsupported single-byte FR address\n"); |
| 380 | *error = -EIO; |
| 381 | goto out_err; |
| 382 | } |
| 383 | *dlci = ((frh[0] & 0xfc) << 2); |
| 384 | if ((frh[1] & 0x0f) != 0x01) { |
| 385 | LOGP(DLNS, LOGL_NOTICE, "Unknown second FR octet 0x%02x\n", |
| 386 | frh[1]); |
| 387 | *error = -EIO; |
| 388 | goto out_err; |
| 389 | } |
| 390 | *dlci |= (frh[1] >> 4); |
| 391 | |
| 392 | msg->l2h = frh+2; |
| 393 | |
| 394 | return msg; |
| 395 | |
| 396 | out_err: |
| 397 | msgb_free(msg); |
| 398 | return NULL; |
| 399 | } |
| 400 | |
| 401 | static int gprs_ns2_find_vc_by_dlci(struct gprs_ns2_vc_bind *bind, |
| 402 | uint16_t dlci, |
| 403 | struct gprs_ns2_vc **result) |
| 404 | { |
| 405 | struct gprs_ns2_vc *nsvc; |
| 406 | struct priv_vc *vcpriv; |
| 407 | |
| 408 | if (!result) |
| 409 | return -EINVAL; |
| 410 | |
| 411 | llist_for_each_entry(nsvc, &bind->nsvc, blist) { |
| 412 | vcpriv = nsvc->priv; |
| 413 | if (vcpriv->dlci != dlci) { |
| 414 | *result = nsvc; |
| 415 | return 0; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return 1; |
| 420 | } |
| 421 | |
| 422 | static int handle_nsfrgre_read(struct osmo_fd *bfd) |
| 423 | { |
| 424 | int rc; |
| 425 | struct osmo_sockaddr saddr; |
| 426 | struct gprs_ns2_vc *nsvc; |
| 427 | struct gprs_ns2_vc_bind *bind = bfd->data; |
| 428 | struct msgb *msg; |
| 429 | struct msgb *reject; |
| 430 | uint16_t dlci; |
| 431 | |
| 432 | msg = read_nsfrgre_msg(bfd, &rc, &saddr, &dlci); |
| 433 | if (!msg) |
| 434 | return rc; |
| 435 | |
| 436 | if (dlci == 0 || dlci == 1023) { |
| 437 | LOGP(DLNS, LOGL_INFO, "Received FR on LMI DLCI %u - ignoring\n", |
| 438 | dlci); |
| 439 | rc = 0; |
| 440 | goto out; |
| 441 | } |
| 442 | |
| 443 | rc = gprs_ns2_find_vc_by_dlci(bind, dlci, &nsvc); |
| 444 | if (rc) { |
| 445 | /* VC not found */ |
| 446 | rc = ns2_create_vc(bind, msg, "newconnection", &reject, &nsvc); |
| 447 | switch (rc) { |
| 448 | case GPRS_NS2_CS_FOUND: |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 449 | break; |
| 450 | case GPRS_NS2_CS_ERROR: |
| 451 | case GPRS_NS2_CS_SKIPPED: |
| 452 | rc = 0; |
Alexander Couzens | 1301012 | 2020-09-24 00:54:51 +0200 | [diff] [blame] | 453 | goto out; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 454 | case GPRS_NS2_CS_REJECTED: |
| 455 | /* nsip_sendmsg will free reject */ |
Alexander Couzens | 772ca61 | 2020-09-24 16:19:02 +0200 | [diff] [blame] | 456 | rc = frgre_sendmsg(bind, reject, &saddr); |
| 457 | goto out; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 458 | case GPRS_NS2_CS_CREATED: |
| 459 | frgre_alloc_vc(bind, nsvc, &saddr, dlci); |
| 460 | gprs_ns2_vc_fsm_start(nsvc); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 461 | break; |
| 462 | } |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 463 | } |
Alexander Couzens | 1301012 | 2020-09-24 00:54:51 +0200 | [diff] [blame] | 464 | |
| 465 | rc = ns2_recv_vc(nsvc, msg); |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 466 | out: |
| 467 | msgb_free(msg); |
| 468 | |
| 469 | return rc; |
| 470 | } |
| 471 | |
| 472 | static int handle_nsfrgre_write(struct osmo_fd *bfd) |
| 473 | { |
| 474 | /* FIXME: actually send the data here instead of nsip_sendmsg() */ |
| 475 | return -EIO; |
| 476 | } |
| 477 | |
| 478 | static inline int frgre_sendmsg(struct gprs_ns2_vc_bind *bind, |
| 479 | struct msgb *msg, |
| 480 | struct osmo_sockaddr *dest) |
| 481 | { |
| 482 | int rc; |
| 483 | struct priv_bind *priv = bind->priv; |
| 484 | |
| 485 | rc = sendto(priv->fd.fd, msg->data, msg->len, 0, |
| 486 | &dest->u.sa, sizeof(*dest)); |
| 487 | |
| 488 | msgb_free(msg); |
| 489 | |
| 490 | return rc; |
| 491 | } |
| 492 | |
| 493 | static int frgre_vc_sendmsg(struct gprs_ns2_vc *nsvc, struct msgb *msg) |
| 494 | { |
| 495 | struct gprs_ns2_vc_bind *bind = nsvc->bind; |
| 496 | struct priv_vc *vcpriv = nsvc->priv; |
| 497 | struct priv_bind *bindpriv = bind->priv; |
| 498 | |
| 499 | uint16_t dlci = osmo_htons(bindpriv->dlci); |
| 500 | uint8_t *frh; |
| 501 | struct gre_hdr *greh; |
| 502 | |
| 503 | /* Prepend the FR header */ |
| 504 | frh = msgb_push(msg, 2); |
| 505 | frh[0] = (dlci >> 2) & 0xfc; |
| 506 | frh[1] = ((dlci & 0xf)<<4) | 0x01; |
| 507 | |
| 508 | /* Prepend the GRE header */ |
| 509 | greh = (struct gre_hdr *) msgb_push(msg, sizeof(*greh)); |
| 510 | greh->flags = 0; |
| 511 | greh->ptype = osmo_htons(GRE_PTYPE_FR); |
| 512 | |
| 513 | return frgre_sendmsg(bind, msg, &vcpriv->remote); |
| 514 | } |
| 515 | |
| 516 | static int frgre_fd_cb(struct osmo_fd *bfd, unsigned int what) |
| 517 | { |
| 518 | int rc = 0; |
| 519 | |
| 520 | if (what & OSMO_FD_READ) |
| 521 | rc = handle_nsfrgre_read(bfd); |
| 522 | if (what & OSMO_FD_WRITE) |
| 523 | rc = handle_nsfrgre_write(bfd); |
| 524 | |
| 525 | return rc; |
| 526 | } |
| 527 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 528 | /*! determine if given bind is for FR-GRE encapsulation. */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 529 | int gprs_ns2_is_frgre_bind(struct gprs_ns2_vc_bind *bind) |
| 530 | { |
| 531 | return (bind->driver == &vc_driver_frgre); |
| 532 | } |
| 533 | |
Harald Welte | 5bef2cc | 2020-09-18 22:33:24 +0200 | [diff] [blame] | 534 | /*! Create a new bind for NS over FR-GRE. |
| 535 | * \param[in] nsi NS instance in which to create the bind |
| 536 | * \param[in] local local address on which to bind |
| 537 | * \param[in] dscp DSCP/TOS bits to use for transmitted data on this bind |
| 538 | * \param[out] result pointer to created bind |
| 539 | * \return 0 on success; negative on error */ |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 540 | int gprs_ns2_frgre_bind(struct gprs_ns2_inst *nsi, |
Vadim Yanitskiy | a07f25e | 2020-10-09 21:47:01 +0700 | [diff] [blame] | 541 | const struct osmo_sockaddr *local, |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 542 | int dscp, |
| 543 | struct gprs_ns2_vc_bind **result) |
| 544 | { |
| 545 | struct gprs_ns2_vc_bind *bind = talloc_zero(nsi, struct gprs_ns2_vc_bind); |
| 546 | struct priv_bind *priv; |
| 547 | int rc; |
| 548 | |
| 549 | if (!bind) |
| 550 | return -ENOSPC; |
| 551 | |
| 552 | if (local->u.sa.sa_family != AF_INET && local->u.sa.sa_family != AF_INET6) { |
| 553 | talloc_free(bind); |
| 554 | return -EINVAL; |
| 555 | } |
| 556 | |
| 557 | bind->driver = &vc_driver_frgre; |
Alexander Couzens | aac9016 | 2020-11-19 02:44:04 +0100 | [diff] [blame] | 558 | bind->ll = GPRS_NS2_LL_FR_GRE; |
Alexander Couzens | 6a16149 | 2020-07-12 13:45:50 +0200 | [diff] [blame] | 559 | bind->send_vc = frgre_vc_sendmsg; |
| 560 | bind->free_vc = free_vc; |
| 561 | bind->nsi = nsi; |
| 562 | |
| 563 | priv = bind->priv = talloc_zero(bind, struct priv_bind); |
| 564 | if (!priv) { |
| 565 | talloc_free(bind); |
| 566 | return -ENOSPC; |
| 567 | } |
| 568 | priv->fd.cb = frgre_fd_cb; |
| 569 | priv->fd.data = bind; |
| 570 | priv->addr = *local; |
| 571 | INIT_LLIST_HEAD(&bind->nsvc); |
| 572 | |
| 573 | llist_add(&bind->list, &nsi->binding); |
| 574 | |
| 575 | rc = osmo_sock_init_osa_ofd(&priv->fd, SOCK_RAW, IPPROTO_GRE, |
| 576 | local, NULL, |
| 577 | OSMO_SOCK_F_BIND); |
| 578 | if (rc < 0) { |
| 579 | talloc_free(priv); |
| 580 | talloc_free(bind); |
| 581 | return rc; |
| 582 | } |
| 583 | |
| 584 | if (dscp > 0) { |
| 585 | priv->dscp = dscp; |
| 586 | |
| 587 | rc = setsockopt(priv->fd.fd, IPPROTO_IP, IP_TOS, |
| 588 | &dscp, sizeof(dscp)); |
| 589 | if (rc < 0) |
| 590 | LOGP(DLNS, LOGL_ERROR, |
| 591 | "Failed to set the DSCP to %d with ret(%d) errno(%d)\n", |
| 592 | dscp, rc, errno); |
| 593 | } |
| 594 | |
| 595 | ns2_vty_bind_apply(bind); |
| 596 | |
| 597 | if (result) |
| 598 | *result = bind; |
| 599 | |
| 600 | return rc; |
| 601 | } |