blob: b99761ebd8dfbfef1c5e35ee4d4061217dd287a4 [file] [log] [blame]
Alexander Couzens6a161492020-07-12 13:45:50 +02001/*! \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
59struct 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 */
77struct 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
100static void free_bind(struct gprs_ns2_vc_bind *bind);
101static inline int frgre_sendmsg(struct gprs_ns2_vc_bind *bind,
102 struct msgb *msg,
103 struct osmo_sockaddr *dest);
104
105struct gprs_ns2_vc_driver vc_driver_frgre = {
106 .name = "GB frame relay over GRE",
107 .free_bind = free_bind,
108};
109
110struct priv_bind {
111 struct osmo_fd fd;
112 struct osmo_sockaddr addr;
113 uint16_t dlci;
114 int dscp;
115};
116
117struct priv_vc {
118 struct osmo_sockaddr remote;
119 uint16_t dlci;
120};
121
122static 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() */
135static 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
150static 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
166static 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 Weltef2949742021-01-20 14:54:14 +0100183 LOGBIND(bind, LOGL_ERROR, "GRE keepalive too short\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200184 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 Weltef2949742021-01-20 14:54:14 +0100189 LOGBIND(bind, LOGL_ERROR, "GRE keepalive with wrong tunnel addresses\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200190 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 Weltef2949742021-01-20 14:54:14 +0100195 LOGBIND(bind, LOGL_ERROR, "GRE keepalive with wrong protocol\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200196 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 Weltef2949742021-01-20 14:54:14 +0100201 LOGBIND(bind, LOGL_ERROR, "GRE keepalive inner GRE type != 0\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200202 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 Weltef2949742021-01-20 14:54:14 +0100214 LOGBIND(bind, LOGL_DEBUG, "GRE keepalive from %s, responding\n", ip6str);
Alexander Couzens6a161492020-07-12 13:45:50 +0200215
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 */
224static 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 Weltef2949742021-01-20 14:54:14 +0100240 LOGBIND(bind, LOGL_ERROR, "GRE keepalive too short\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200241 return -EIO;
242 }
243
244 if (inner_iph->saddr != iph->daddr ||
245 inner_iph->daddr != iph->saddr) {
Harald Weltef2949742021-01-20 14:54:14 +0100246 LOGBIND(bind, LOGL_ERROR, "GRE keepalive with wrong tunnel addresses\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200247 return -EIO;
248 }
249
250 if (inner_iph->protocol != IPPROTO_GRE) {
Harald Weltef2949742021-01-20 14:54:14 +0100251 LOGBIND(bind, LOGL_ERROR, "GRE keepalive with wrong protocol\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200252 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 Weltef2949742021-01-20 14:54:14 +0100257 LOGBIND(bind, LOGL_ERROR, "GRE keepalive inner GRE type != 0\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200258 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 Weltef2949742021-01-20 14:54:14 +0100268 LOGBIND(bind, LOGL_DEBUG, "GRE keepalive from %s, responding\n", inet_ntoa(ia));
Alexander Couzens6a161492020-07-12 13:45:50 +0200269
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
276static struct msgb *read_nsfrgre_msg(struct osmo_fd *bfd, int *error,
Harald Weltef2949742021-01-20 14:54:14 +0100277 struct osmo_sockaddr *saddr, uint16_t *dlci,
278 const struct gprs_ns2_vc_bind *bind)
Alexander Couzens6a161492020-07-12 13:45:50 +0200279{
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 Weltef2949742021-01-20 14:54:14 +0100297 LOGBIND(bind, LOGL_ERROR, "recv error %s during NS-FR-GRE recv\n", strerror(errno));
Alexander Couzens6a161492020-07-12 13:45:50 +0200298 *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 Willmann210774f2021-01-07 13:31:56 +0100314 break;
Alexander Couzens6a161492020-07-12 13:45:50 +0200315 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 Weltef2949742021-01-20 14:54:14 +0100323 LOGBIND(bind, LOGL_ERROR, "Short IP packet: %u bytes\n", msg->len);
Alexander Couzens6a161492020-07-12 13:45:50 +0200324 *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 Weltef2949742021-01-20 14:54:14 +0100332 LOGBIND(bind, LOGL_ERROR, "Short IP packet: %u bytes\n", msg->len);
Alexander Couzens6a161492020-07-12 13:45:50 +0200333 *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 Couzens47ed2a62021-02-03 11:46:59 +0100342 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 Couzens6a161492020-07-12 13:45:50 +0200347 if (greh->flags) {
Harald Weltef2949742021-01-20 14:54:14 +0100348 LOGBIND(bind, LOGL_NOTICE, "Unknown GRE flags 0x%04x\n", osmo_ntohs(greh->flags));
Alexander Couzens6a161492020-07-12 13:45:50 +0200349 }
350
351 switch (osmo_ntohs(greh->ptype)) {
352 case GRE_PTYPE_IPv4:
353 /* IPv4 messages might be GRE keepalives */
Alexander Couzensdc43f7d2021-02-03 11:53:11 +0100354 if (iph)
355 *error = handle_rx_gre_ipv4(bfd, msg, iph, greh);
356 else
357 *error = -EIO;
Alexander Couzens6a161492020-07-12 13:45:50 +0200358 goto out_err;
359 break;
360 case GRE_PTYPE_IPv6:
Alexander Couzensdc43f7d2021-02-03 11:53:11 +0100361 if (ip6h)
362 *error = handle_rx_gre_ipv6(bfd, msg, ip6h, greh);
363 else
364 *error = -EIO;
Alexander Couzens6a161492020-07-12 13:45:50 +0200365 goto out_err;
366 break;
367 case GRE_PTYPE_FR:
368 /* continue as usual */
369 break;
370 default:
Harald Weltef2949742021-01-20 14:54:14 +0100371 LOGBIND(bind, LOGL_NOTICE, "Unknown GRE protocol 0x%04x != FR\n", osmo_ntohs(greh->ptype));
Alexander Couzens6a161492020-07-12 13:45:50 +0200372 *error = -EIO;
373 goto out_err;
374 break;
375 }
376
377 if (msg->len < sizeof(*greh) + 2) {
Harald Weltef2949742021-01-20 14:54:14 +0100378 LOGBIND(bind, LOGL_ERROR, "Short FR header: %u bytes\n", msg->len);
Alexander Couzens6a161492020-07-12 13:45:50 +0200379 *error = -EIO;
380 goto out_err;
381 }
382
383 frh = (uint8_t *)greh + sizeof(*greh);
384 if (frh[0] & 0x01) {
Harald Weltef2949742021-01-20 14:54:14 +0100385 LOGBIND(bind, LOGL_NOTICE, "Unsupported single-byte FR address\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200386 *error = -EIO;
387 goto out_err;
388 }
389 *dlci = ((frh[0] & 0xfc) << 2);
390 if ((frh[1] & 0x0f) != 0x01) {
Harald Weltef2949742021-01-20 14:54:14 +0100391 LOGBIND(bind, LOGL_NOTICE, "Unknown second FR octet 0x%02x\n", frh[1]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200392 *error = -EIO;
393 goto out_err;
394 }
395 *dlci |= (frh[1] >> 4);
396
397 msg->l2h = frh+2;
398
399 return msg;
400
401out_err:
402 msgb_free(msg);
403 return NULL;
404}
405
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100406static int ns2_find_vc_by_dlci(struct gprs_ns2_vc_bind *bind,
407 uint16_t dlci,
408 struct gprs_ns2_vc **result)
Alexander Couzens6a161492020-07-12 13:45:50 +0200409{
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
427static 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 Weltef2949742021-01-20 14:54:14 +0100437 msg = read_nsfrgre_msg(bfd, &rc, &saddr, &dlci, bind);
Alexander Couzens6a161492020-07-12 13:45:50 +0200438 if (!msg)
439 return rc;
440
441 if (dlci == 0 || dlci == 1023) {
Harald Weltef2949742021-01-20 14:54:14 +0100442 LOGBIND(bind, LOGL_INFO, "Received FR on LMI DLCI %u - ignoring\n", dlci);
Alexander Couzens6a161492020-07-12 13:45:50 +0200443 rc = 0;
444 goto out;
445 }
446
Alexander Couzensf7e2cac2021-01-25 16:18:21 +0100447 rc = ns2_find_vc_by_dlci(bind, dlci, &nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200448 if (rc) {
449 /* VC not found */
Harald Welte7d0daac2021-03-02 23:08:43 +0100450 rc = ns2_create_vc(bind, msg, &saddr, "newconnection", &reject, &nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200451 switch (rc) {
Alexander Couzensba5a9922021-01-25 16:03:23 +0100452 case NS2_CS_FOUND:
Alexander Couzens6a161492020-07-12 13:45:50 +0200453 break;
Alexander Couzensba5a9922021-01-25 16:03:23 +0100454 case NS2_CS_ERROR:
455 case NS2_CS_SKIPPED:
Alexander Couzens6a161492020-07-12 13:45:50 +0200456 rc = 0;
Alexander Couzens13010122020-09-24 00:54:51 +0200457 goto out;
Alexander Couzensba5a9922021-01-25 16:03:23 +0100458 case NS2_CS_REJECTED:
Alexander Couzens6a161492020-07-12 13:45:50 +0200459 /* nsip_sendmsg will free reject */
Alexander Couzens772ca612020-09-24 16:19:02 +0200460 rc = frgre_sendmsg(bind, reject, &saddr);
461 goto out;
Alexander Couzensba5a9922021-01-25 16:03:23 +0100462 case NS2_CS_CREATED:
Alexander Couzens6a161492020-07-12 13:45:50 +0200463 frgre_alloc_vc(bind, nsvc, &saddr, dlci);
Alexander Couzens8dfc24c2021-01-25 16:09:23 +0100464 ns2_vc_fsm_start(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200465 break;
466 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200467 }
Alexander Couzens13010122020-09-24 00:54:51 +0200468
469 rc = ns2_recv_vc(nsvc, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200470out:
471 msgb_free(msg);
472
473 return rc;
474}
475
476static 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
482static 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
497static 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;
Pau Espin Pedrol8f026bf2023-04-27 16:00:01 +0200506 unsigned int vc_len = msgb_length(msg);
507 int rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200508
509 /* Prepend the FR header */
510 frh = msgb_push(msg, 2);
511 frh[0] = (dlci >> 2) & 0xfc;
512 frh[1] = ((dlci & 0xf)<<4) | 0x01;
513
514 /* Prepend the GRE header */
515 greh = (struct gre_hdr *) msgb_push(msg, sizeof(*greh));
516 greh->flags = 0;
517 greh->ptype = osmo_htons(GRE_PTYPE_FR);
518
Pau Espin Pedrol8f026bf2023-04-27 16:00:01 +0200519 rc = frgre_sendmsg(bind, msg, &vcpriv->remote);
520 if (OSMO_LIKELY(rc >= 0)) {
521 RATE_CTR_INC_NS(nsvc, NS_CTR_PKTS_OUT);
522 RATE_CTR_ADD_NS(nsvc, NS_CTR_BYTES_OUT, rc);
523 } else {
524 RATE_CTR_INC_NS(nsvc, NS_CTR_PKTS_OUT_DROP);
525 RATE_CTR_ADD_NS(nsvc, NS_CTR_BYTES_OUT_DROP, vc_len);
526 }
527 return rc;
Alexander Couzens6a161492020-07-12 13:45:50 +0200528}
529
530static int frgre_fd_cb(struct osmo_fd *bfd, unsigned int what)
531{
532 int rc = 0;
533
534 if (what & OSMO_FD_READ)
535 rc = handle_nsfrgre_read(bfd);
536 if (what & OSMO_FD_WRITE)
537 rc = handle_nsfrgre_write(bfd);
538
539 return rc;
540}
541
Harald Welte5bef2cc2020-09-18 22:33:24 +0200542/*! determine if given bind is for FR-GRE encapsulation. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200543int gprs_ns2_is_frgre_bind(struct gprs_ns2_vc_bind *bind)
544{
545 return (bind->driver == &vc_driver_frgre);
546}
547
Harald Welte5bef2cc2020-09-18 22:33:24 +0200548/*! Create a new bind for NS over FR-GRE.
549 * \param[in] nsi NS instance in which to create the bind
550 * \param[in] local local address on which to bind
551 * \param[in] dscp DSCP/TOS bits to use for transmitted data on this bind
Alexander Couzensc80a8742021-02-03 11:27:52 +0100552 * \param[out] result pointer to the created bind or if a bind with the name exists return the bind.
553 * \return 0 on success; negative on error. -EALREADY returned in case a bind with the name exists */
Alexander Couzens6a161492020-07-12 13:45:50 +0200554int gprs_ns2_frgre_bind(struct gprs_ns2_inst *nsi,
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100555 const char *name,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700556 const struct osmo_sockaddr *local,
Alexander Couzens6a161492020-07-12 13:45:50 +0200557 int dscp,
558 struct gprs_ns2_vc_bind **result)
559{
Harald Weltec3aa8f92021-01-31 11:41:34 +0100560 struct gprs_ns2_vc_bind *bind;
Alexander Couzens6a161492020-07-12 13:45:50 +0200561 struct priv_bind *priv;
562 int rc;
563
Harald Weltec3aa8f92021-01-31 11:41:34 +0100564 if (local->u.sa.sa_family != AF_INET && local->u.sa.sa_family != AF_INET6)
Alexander Couzens6a161492020-07-12 13:45:50 +0200565 return -EINVAL;
Harald Weltec3aa8f92021-01-31 11:41:34 +0100566
Harald Weltef8a4e132021-04-28 19:44:48 +0200567 if (dscp < 0 || dscp > 63)
568 return -EINVAL;
569
Harald Weltec3aa8f92021-01-31 11:41:34 +0100570 bind = gprs_ns2_bind_by_name(nsi, name);
571 if (bind) {
Alexander Couzensc80a8742021-02-03 11:27:52 +0100572 if (result)
573 *result = bind;
Harald Weltec3aa8f92021-01-31 11:41:34 +0100574 return -EALREADY;
Alexander Couzens6a161492020-07-12 13:45:50 +0200575 }
576
Harald Weltec3aa8f92021-01-31 11:41:34 +0100577 rc = ns2_bind_alloc(nsi, name, &bind);
578 if (rc < 0)
579 return rc;
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100580
Alexander Couzens6a161492020-07-12 13:45:50 +0200581 bind->driver = &vc_driver_frgre;
Alexander Couzensaac90162020-11-19 02:44:04 +0100582 bind->ll = GPRS_NS2_LL_FR_GRE;
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100583 /* 2 mbit transfer capability. Counting should be done different for this. */
584 bind->transfer_capability = 2;
Alexander Couzens6a161492020-07-12 13:45:50 +0200585 bind->send_vc = frgre_vc_sendmsg;
586 bind->free_vc = free_vc;
587 bind->nsi = nsi;
Alexander Couzens4f1128f2021-01-20 17:42:48 +0100588 /* TODO: allow to set the MTU via vty. It can not be automatic detected because it goes over an
589 * ethernet device and the MTU here must match the FR side of the FR-to-GRE gateway.
590 */
591 bind->mtu = FRAME_RELAY_SDU;
Alexander Couzens6a161492020-07-12 13:45:50 +0200592
593 priv = bind->priv = talloc_zero(bind, struct priv_bind);
594 if (!priv) {
Harald Weltec3aa8f92021-01-31 11:41:34 +0100595 gprs_ns2_free_bind(bind);
Harald Weltebdfb8b92021-01-31 11:44:57 +0100596 return -ENOMEM;
Alexander Couzens6a161492020-07-12 13:45:50 +0200597 }
598 priv->fd.cb = frgre_fd_cb;
599 priv->fd.data = bind;
600 priv->addr = *local;
601 INIT_LLIST_HEAD(&bind->nsvc);
Harald Welte485b3f72021-04-28 19:45:34 +0200602 priv->dscp = dscp;
Alexander Couzens6a161492020-07-12 13:45:50 +0200603
Alexander Couzens6a161492020-07-12 13:45:50 +0200604 rc = osmo_sock_init_osa_ofd(&priv->fd, SOCK_RAW, IPPROTO_GRE,
605 local, NULL,
Harald Welte485b3f72021-04-28 19:45:34 +0200606 OSMO_SOCK_F_BIND | OSMO_SOCK_F_DSCP(priv->dscp));
Alexander Couzens6a161492020-07-12 13:45:50 +0200607 if (rc < 0) {
Harald Weltec3aa8f92021-01-31 11:41:34 +0100608 gprs_ns2_free_bind(bind);
Alexander Couzens6a161492020-07-12 13:45:50 +0200609 return rc;
610 }
611
Alexander Couzens6a161492020-07-12 13:45:50 +0200612 if (result)
613 *result = bind;
614
615 return rc;
616}