blob: da7e53fe14a187081e3c106b8d76d0542c669540 [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
342 greh = (struct gre_hdr *) (msg->data + iph->ihl*4);
343 if (greh->flags) {
Harald Weltef2949742021-01-20 14:54:14 +0100344 LOGBIND(bind, LOGL_NOTICE, "Unknown GRE flags 0x%04x\n", osmo_ntohs(greh->flags));
Alexander Couzens6a161492020-07-12 13:45:50 +0200345 }
346
347 switch (osmo_ntohs(greh->ptype)) {
348 case GRE_PTYPE_IPv4:
349 /* IPv4 messages might be GRE keepalives */
350 *error = handle_rx_gre_ipv4(bfd, msg, iph, greh);
351 goto out_err;
352 break;
353 case GRE_PTYPE_IPv6:
354 *error = handle_rx_gre_ipv6(bfd, msg, ip6h, greh);
355 goto out_err;
356 break;
357 case GRE_PTYPE_FR:
358 /* continue as usual */
359 break;
360 default:
Harald Weltef2949742021-01-20 14:54:14 +0100361 LOGBIND(bind, LOGL_NOTICE, "Unknown GRE protocol 0x%04x != FR\n", osmo_ntohs(greh->ptype));
Alexander Couzens6a161492020-07-12 13:45:50 +0200362 *error = -EIO;
363 goto out_err;
364 break;
365 }
366
367 if (msg->len < sizeof(*greh) + 2) {
Harald Weltef2949742021-01-20 14:54:14 +0100368 LOGBIND(bind, LOGL_ERROR, "Short FR header: %u bytes\n", msg->len);
Alexander Couzens6a161492020-07-12 13:45:50 +0200369 *error = -EIO;
370 goto out_err;
371 }
372
373 frh = (uint8_t *)greh + sizeof(*greh);
374 if (frh[0] & 0x01) {
Harald Weltef2949742021-01-20 14:54:14 +0100375 LOGBIND(bind, LOGL_NOTICE, "Unsupported single-byte FR address\n");
Alexander Couzens6a161492020-07-12 13:45:50 +0200376 *error = -EIO;
377 goto out_err;
378 }
379 *dlci = ((frh[0] & 0xfc) << 2);
380 if ((frh[1] & 0x0f) != 0x01) {
Harald Weltef2949742021-01-20 14:54:14 +0100381 LOGBIND(bind, LOGL_NOTICE, "Unknown second FR octet 0x%02x\n", frh[1]);
Alexander Couzens6a161492020-07-12 13:45:50 +0200382 *error = -EIO;
383 goto out_err;
384 }
385 *dlci |= (frh[1] >> 4);
386
387 msg->l2h = frh+2;
388
389 return msg;
390
391out_err:
392 msgb_free(msg);
393 return NULL;
394}
395
396static int gprs_ns2_find_vc_by_dlci(struct gprs_ns2_vc_bind *bind,
397 uint16_t dlci,
398 struct gprs_ns2_vc **result)
399{
400 struct gprs_ns2_vc *nsvc;
401 struct priv_vc *vcpriv;
402
403 if (!result)
404 return -EINVAL;
405
406 llist_for_each_entry(nsvc, &bind->nsvc, blist) {
407 vcpriv = nsvc->priv;
408 if (vcpriv->dlci != dlci) {
409 *result = nsvc;
410 return 0;
411 }
412 }
413
414 return 1;
415}
416
417static int handle_nsfrgre_read(struct osmo_fd *bfd)
418{
419 int rc;
420 struct osmo_sockaddr saddr;
421 struct gprs_ns2_vc *nsvc;
422 struct gprs_ns2_vc_bind *bind = bfd->data;
423 struct msgb *msg;
424 struct msgb *reject;
425 uint16_t dlci;
426
Harald Weltef2949742021-01-20 14:54:14 +0100427 msg = read_nsfrgre_msg(bfd, &rc, &saddr, &dlci, bind);
Alexander Couzens6a161492020-07-12 13:45:50 +0200428 if (!msg)
429 return rc;
430
431 if (dlci == 0 || dlci == 1023) {
Harald Weltef2949742021-01-20 14:54:14 +0100432 LOGBIND(bind, LOGL_INFO, "Received FR on LMI DLCI %u - ignoring\n", dlci);
Alexander Couzens6a161492020-07-12 13:45:50 +0200433 rc = 0;
434 goto out;
435 }
436
437 rc = gprs_ns2_find_vc_by_dlci(bind, dlci, &nsvc);
438 if (rc) {
439 /* VC not found */
440 rc = ns2_create_vc(bind, msg, "newconnection", &reject, &nsvc);
441 switch (rc) {
Alexander Couzensba5a9922021-01-25 16:03:23 +0100442 case NS2_CS_FOUND:
Alexander Couzens6a161492020-07-12 13:45:50 +0200443 break;
Alexander Couzensba5a9922021-01-25 16:03:23 +0100444 case NS2_CS_ERROR:
445 case NS2_CS_SKIPPED:
Alexander Couzens6a161492020-07-12 13:45:50 +0200446 rc = 0;
Alexander Couzens13010122020-09-24 00:54:51 +0200447 goto out;
Alexander Couzensba5a9922021-01-25 16:03:23 +0100448 case NS2_CS_REJECTED:
Alexander Couzens6a161492020-07-12 13:45:50 +0200449 /* nsip_sendmsg will free reject */
Alexander Couzens772ca612020-09-24 16:19:02 +0200450 rc = frgre_sendmsg(bind, reject, &saddr);
451 goto out;
Alexander Couzensba5a9922021-01-25 16:03:23 +0100452 case NS2_CS_CREATED:
Alexander Couzens6a161492020-07-12 13:45:50 +0200453 frgre_alloc_vc(bind, nsvc, &saddr, dlci);
454 gprs_ns2_vc_fsm_start(nsvc);
Alexander Couzens6a161492020-07-12 13:45:50 +0200455 break;
456 }
Alexander Couzens6a161492020-07-12 13:45:50 +0200457 }
Alexander Couzens13010122020-09-24 00:54:51 +0200458
459 rc = ns2_recv_vc(nsvc, msg);
Alexander Couzens6a161492020-07-12 13:45:50 +0200460out:
461 msgb_free(msg);
462
463 return rc;
464}
465
466static int handle_nsfrgre_write(struct osmo_fd *bfd)
467{
468 /* FIXME: actually send the data here instead of nsip_sendmsg() */
469 return -EIO;
470}
471
472static inline int frgre_sendmsg(struct gprs_ns2_vc_bind *bind,
473 struct msgb *msg,
474 struct osmo_sockaddr *dest)
475{
476 int rc;
477 struct priv_bind *priv = bind->priv;
478
479 rc = sendto(priv->fd.fd, msg->data, msg->len, 0,
480 &dest->u.sa, sizeof(*dest));
481
482 msgb_free(msg);
483
484 return rc;
485}
486
487static int frgre_vc_sendmsg(struct gprs_ns2_vc *nsvc, struct msgb *msg)
488{
489 struct gprs_ns2_vc_bind *bind = nsvc->bind;
490 struct priv_vc *vcpriv = nsvc->priv;
491 struct priv_bind *bindpriv = bind->priv;
492
493 uint16_t dlci = osmo_htons(bindpriv->dlci);
494 uint8_t *frh;
495 struct gre_hdr *greh;
496
497 /* Prepend the FR header */
498 frh = msgb_push(msg, 2);
499 frh[0] = (dlci >> 2) & 0xfc;
500 frh[1] = ((dlci & 0xf)<<4) | 0x01;
501
502 /* Prepend the GRE header */
503 greh = (struct gre_hdr *) msgb_push(msg, sizeof(*greh));
504 greh->flags = 0;
505 greh->ptype = osmo_htons(GRE_PTYPE_FR);
506
507 return frgre_sendmsg(bind, msg, &vcpriv->remote);
508}
509
510static int frgre_fd_cb(struct osmo_fd *bfd, unsigned int what)
511{
512 int rc = 0;
513
514 if (what & OSMO_FD_READ)
515 rc = handle_nsfrgre_read(bfd);
516 if (what & OSMO_FD_WRITE)
517 rc = handle_nsfrgre_write(bfd);
518
519 return rc;
520}
521
Harald Welte5bef2cc2020-09-18 22:33:24 +0200522/*! determine if given bind is for FR-GRE encapsulation. */
Alexander Couzens6a161492020-07-12 13:45:50 +0200523int gprs_ns2_is_frgre_bind(struct gprs_ns2_vc_bind *bind)
524{
525 return (bind->driver == &vc_driver_frgre);
526}
527
Harald Welte5bef2cc2020-09-18 22:33:24 +0200528/*! Create a new bind for NS over FR-GRE.
529 * \param[in] nsi NS instance in which to create the bind
530 * \param[in] local local address on which to bind
531 * \param[in] dscp DSCP/TOS bits to use for transmitted data on this bind
532 * \param[out] result pointer to created bind
533 * \return 0 on success; negative on error */
Alexander Couzens6a161492020-07-12 13:45:50 +0200534int gprs_ns2_frgre_bind(struct gprs_ns2_inst *nsi,
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100535 const char *name,
Vadim Yanitskiya07f25e2020-10-09 21:47:01 +0700536 const struct osmo_sockaddr *local,
Alexander Couzens6a161492020-07-12 13:45:50 +0200537 int dscp,
538 struct gprs_ns2_vc_bind **result)
539{
540 struct gprs_ns2_vc_bind *bind = talloc_zero(nsi, struct gprs_ns2_vc_bind);
541 struct priv_bind *priv;
542 int rc;
543
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100544 if (!name)
545 return -ENOSPC;
546
547 if (gprs_ns2_bind_by_name(nsi, name))
548 return -EALREADY;
549
Alexander Couzens6a161492020-07-12 13:45:50 +0200550 if (!bind)
551 return -ENOSPC;
552
553 if (local->u.sa.sa_family != AF_INET && local->u.sa.sa_family != AF_INET6) {
554 talloc_free(bind);
555 return -EINVAL;
556 }
557
Alexander Couzensaaa55a62020-12-03 06:02:03 +0100558 bind->name = talloc_strdup(bind, name);
559 if (!bind->name) {
560 talloc_free(bind);
561 return -ENOSPC;
562 }
563
Alexander Couzens6a161492020-07-12 13:45:50 +0200564 bind->driver = &vc_driver_frgre;
Alexander Couzensaac90162020-11-19 02:44:04 +0100565 bind->ll = GPRS_NS2_LL_FR_GRE;
Alexander Couzens1c8785d2020-12-17 06:58:53 +0100566 /* 2 mbit transfer capability. Counting should be done different for this. */
567 bind->transfer_capability = 2;
Alexander Couzens6a161492020-07-12 13:45:50 +0200568 bind->send_vc = frgre_vc_sendmsg;
569 bind->free_vc = free_vc;
570 bind->nsi = nsi;
571
572 priv = bind->priv = talloc_zero(bind, struct priv_bind);
573 if (!priv) {
574 talloc_free(bind);
575 return -ENOSPC;
576 }
577 priv->fd.cb = frgre_fd_cb;
578 priv->fd.data = bind;
579 priv->addr = *local;
580 INIT_LLIST_HEAD(&bind->nsvc);
581
582 llist_add(&bind->list, &nsi->binding);
583
584 rc = osmo_sock_init_osa_ofd(&priv->fd, SOCK_RAW, IPPROTO_GRE,
585 local, NULL,
586 OSMO_SOCK_F_BIND);
587 if (rc < 0) {
588 talloc_free(priv);
589 talloc_free(bind);
590 return rc;
591 }
592
593 if (dscp > 0) {
594 priv->dscp = dscp;
595
596 rc = setsockopt(priv->fd.fd, IPPROTO_IP, IP_TOS,
597 &dscp, sizeof(dscp));
598 if (rc < 0)
Harald Weltef2949742021-01-20 14:54:14 +0100599 LOGBIND(bind, LOGL_ERROR, "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
Alexander Couzens6a161492020-07-12 13:45:50 +0200600 dscp, rc, errno);
601 }
602
Alexander Couzens6a161492020-07-12 13:45:50 +0200603 if (result)
604 *result = bind;
605
606 return rc;
607}