blob: 2b19157b84c43ae2a186fe9ef8302a7612466844 [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)) {
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 */
225static 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
279static 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
396out_err:
397 msgb_free(msg);
398 return NULL;
399}
400
401static 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
422static 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:
449 rc = ns2_recv_vc(bind->nsi, nsvc, msg);
450 break;
451 case GPRS_NS2_CS_ERROR:
452 case GPRS_NS2_CS_SKIPPED:
453 rc = 0;
454 break;
455 case GPRS_NS2_CS_REJECTED:
456 /* nsip_sendmsg will free reject */
457 frgre_sendmsg(bind, reject, &saddr);
458 return 0;
459 case GPRS_NS2_CS_CREATED:
460 frgre_alloc_vc(bind, nsvc, &saddr, dlci);
461 gprs_ns2_vc_fsm_start(nsvc);
462 rc = ns2_recv_vc(bind->nsi, nsvc, msg);
463 break;
464 }
465 } else {
466 /* VC found */
467 rc = ns2_recv_vc(bind->nsi, nsvc, msg);
468 }
469out:
470 msgb_free(msg);
471
472 return rc;
473}
474
475static int handle_nsfrgre_write(struct osmo_fd *bfd)
476{
477 /* FIXME: actually send the data here instead of nsip_sendmsg() */
478 return -EIO;
479}
480
481static inline int frgre_sendmsg(struct gprs_ns2_vc_bind *bind,
482 struct msgb *msg,
483 struct osmo_sockaddr *dest)
484{
485 int rc;
486 struct priv_bind *priv = bind->priv;
487
488 rc = sendto(priv->fd.fd, msg->data, msg->len, 0,
489 &dest->u.sa, sizeof(*dest));
490
491 msgb_free(msg);
492
493 return rc;
494}
495
496static int frgre_vc_sendmsg(struct gprs_ns2_vc *nsvc, struct msgb *msg)
497{
498 struct gprs_ns2_vc_bind *bind = nsvc->bind;
499 struct priv_vc *vcpriv = nsvc->priv;
500 struct priv_bind *bindpriv = bind->priv;
501
502 uint16_t dlci = osmo_htons(bindpriv->dlci);
503 uint8_t *frh;
504 struct gre_hdr *greh;
505
506 /* Prepend the FR header */
507 frh = msgb_push(msg, 2);
508 frh[0] = (dlci >> 2) & 0xfc;
509 frh[1] = ((dlci & 0xf)<<4) | 0x01;
510
511 /* Prepend the GRE header */
512 greh = (struct gre_hdr *) msgb_push(msg, sizeof(*greh));
513 greh->flags = 0;
514 greh->ptype = osmo_htons(GRE_PTYPE_FR);
515
516 return frgre_sendmsg(bind, msg, &vcpriv->remote);
517}
518
519static int frgre_fd_cb(struct osmo_fd *bfd, unsigned int what)
520{
521 int rc = 0;
522
523 if (what & OSMO_FD_READ)
524 rc = handle_nsfrgre_read(bfd);
525 if (what & OSMO_FD_WRITE)
526 rc = handle_nsfrgre_write(bfd);
527
528 return rc;
529}
530
531int gprs_ns2_is_frgre_bind(struct gprs_ns2_vc_bind *bind)
532{
533 return (bind->driver == &vc_driver_frgre);
534}
535
536int gprs_ns2_frgre_bind(struct gprs_ns2_inst *nsi,
537 struct osmo_sockaddr *local,
538 int dscp,
539 struct gprs_ns2_vc_bind **result)
540{
541 struct gprs_ns2_vc_bind *bind = talloc_zero(nsi, struct gprs_ns2_vc_bind);
542 struct priv_bind *priv;
543 int rc;
544
545 if (!bind)
546 return -ENOSPC;
547
548 if (local->u.sa.sa_family != AF_INET && local->u.sa.sa_family != AF_INET6) {
549 talloc_free(bind);
550 return -EINVAL;
551 }
552
553 bind->driver = &vc_driver_frgre;
554 bind->send_vc = frgre_vc_sendmsg;
555 bind->free_vc = free_vc;
556 bind->nsi = nsi;
557
558 priv = bind->priv = talloc_zero(bind, struct priv_bind);
559 if (!priv) {
560 talloc_free(bind);
561 return -ENOSPC;
562 }
563 priv->fd.cb = frgre_fd_cb;
564 priv->fd.data = bind;
565 priv->addr = *local;
566 INIT_LLIST_HEAD(&bind->nsvc);
567
568 llist_add(&bind->list, &nsi->binding);
569
570 rc = osmo_sock_init_osa_ofd(&priv->fd, SOCK_RAW, IPPROTO_GRE,
571 local, NULL,
572 OSMO_SOCK_F_BIND);
573 if (rc < 0) {
574 talloc_free(priv);
575 talloc_free(bind);
576 return rc;
577 }
578
579 if (dscp > 0) {
580 priv->dscp = dscp;
581
582 rc = setsockopt(priv->fd.fd, IPPROTO_IP, IP_TOS,
583 &dscp, sizeof(dscp));
584 if (rc < 0)
585 LOGP(DLNS, LOGL_ERROR,
586 "Failed to set the DSCP to %d with ret(%d) errno(%d)\n",
587 dscp, rc, errno);
588 }
589
590 ns2_vty_bind_apply(bind);
591
592 if (result)
593 *result = bind;
594
595 return rc;
596}