blob: f6df408815be32faaa71c7d7be0c6650ed408f56 [file] [log] [blame]
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +01001#ifdef __linux__
2#define _GNU_SOURCE 1 /* strdup() prototype, broken arpa/inet.h */
3#endif
4
5#include "../config.h"
6
7#ifdef HAVE_STDINT_H
8#include <stdint.h>
9#endif
10
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010011#include <stdio.h>
12#include <string.h>
13#include <stdlib.h>
Harald Welte51127ea2017-11-06 02:42:22 +090014#include <inttypes.h>
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010015#include <sys/types.h>
16#include <arpa/inet.h>
17#include <net/if.h>
18
19#include <libgtpnl/gtp.h>
20#include <libgtpnl/gtpnl.h>
21#include <libmnl/libmnl.h>
22
23#include <errno.h>
24
25#include <time.h>
26
27#include "../lib/tun.h"
28#include "../lib/syserr.h"
Pau Espin Pedrol1ef26212019-08-20 13:26:14 +020029#include "../lib/util.h"
30#include "../lib/ippool.h"
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010031#include "../gtp/pdp.h"
32#include "../gtp/gtp.h"
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010033
34#include <libgtpnl/gtp.h>
35#include <libgtpnl/gtpnl.h>
36#include <libmnl/libmnl.h>
37
38#include "gtp-kernel.h"
39
Harald Weltefd30bd12017-11-12 18:26:59 +090040static void pdp_debug(const char *prefix, const char *devname, struct pdp_t *pdp)
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010041{
Pau Espin Pedrol1ef26212019-08-20 13:26:14 +020042 char buf4[INET_ADDRSTRLEN], buf6[INET6_ADDRSTRLEN];
43 struct ippoolm_t *peer;
Harald Welte51127ea2017-11-06 02:42:22 +090044 struct in_addr ia;
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010045
Pau Espin Pedrol1ef26212019-08-20 13:26:14 +020046 buf4[0] = '\0';
47 if ((peer = pdp_get_peer_ipv(pdp, false)))
48 in46a_ntop(&peer->addr, buf4, sizeof(buf4));
49 buf6[0] = '\0';
50 if ((peer = pdp_get_peer_ipv(pdp, true)))
51 in46a_ntop(&peer->addr, buf6, sizeof(buf6));
52
Harald Welte51127ea2017-11-06 02:42:22 +090053 gsna2in_addr(&ia, &pdp->gsnrc);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010054
Pau Espin Pedrol1ef26212019-08-20 13:26:14 +020055 LOGPDPX(DGGSN, LOGL_DEBUG, pdp, "%s %s v%u TEID %"PRIx64" EUA=(%s,%s) SGSN=%s\n", prefix,
Harald Weltefd30bd12017-11-12 18:26:59 +090056 devname, pdp->version,
Harald Welte51127ea2017-11-06 02:42:22 +090057 pdp->version == 0 ? pdp_gettid(pdp->imsi, pdp->nsapi) : pdp->teid_gn,
Pau Espin Pedrol1ef26212019-08-20 13:26:14 +020058 buf4, buf6, inet_ntoa(ia));
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010059}
60
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010061static struct {
62 int genl_id;
63 struct mnl_socket *nl;
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010064} gtp_nl;
65
Harald Welte22e15732017-11-08 16:07:12 +090066static int gtp_kernel_init_once(void)
67{
68 /* only initialize once */
69 if (gtp_nl.nl)
70 return 0;
71
72 gtp_nl.nl = genl_socket_open();
73 if (gtp_nl.nl == NULL) {
Harald Welte3dad9512017-11-08 16:53:47 +090074 LOGP(DGGSN, LOGL_ERROR, "cannot create genetlink socket\n");
Harald Welte22e15732017-11-08 16:07:12 +090075 return -1;
76 }
77 gtp_nl.genl_id = genl_lookup_family(gtp_nl.nl, "gtp");
78 if (gtp_nl.genl_id < 0) {
Harald Welte3dad9512017-11-08 16:53:47 +090079 LOGP(DGGSN, LOGL_ERROR, "cannot lookup GTP genetlink ID\n");
Harald Welte31879562017-11-08 16:08:20 +090080 genl_socket_close(gtp_nl.nl);
81 gtp_nl.nl = NULL;
Harald Welte22e15732017-11-08 16:07:12 +090082 return -1;
83 }
Harald Welte3dad9512017-11-08 16:53:47 +090084 LOGP(DGGSN, LOGL_NOTICE, "Initialized GTP kernel mode (genl ID is %d)\n", gtp_nl.genl_id);
Harald Welte22e15732017-11-08 16:07:12 +090085
86 return 0;
87}
88
Harald Weltef2286392018-04-25 19:02:31 +020089int gtp_kernel_create(int dest_ns, const char *devname, int fd0, int fd1u)
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010090{
Harald Weltef2286392018-04-25 19:02:31 +020091 if (gtp_kernel_init_once() < 0)
Harald Weltee3c59182017-11-08 14:08:24 +090092 return -1;
Harald Weltee3c59182017-11-08 14:08:24 +090093
Harald Weltef2286392018-04-25 19:02:31 +020094 return gtp_dev_create(dest_ns, devname, fd0, fd1u);
95}
96
97int gtp_kernel_create_sgsn(int dest_ns, const char *devname, int fd0, int fd1u)
98{
99 if (gtp_kernel_init_once() < 0)
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100100 return -1;
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100101
Harald Weltef2286392018-04-25 19:02:31 +0200102 return gtp_dev_create_sgsn(dest_ns, devname, fd0, fd1u);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100103}
104
Harald Welte698a2332017-11-08 15:09:58 +0900105void gtp_kernel_stop(const char *devname)
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100106{
Harald Welte698a2332017-11-08 15:09:58 +0900107 gtp_dev_destroy(devname);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100108}
109
Harald Welte698a2332017-11-08 15:09:58 +0900110int gtp_kernel_tunnel_add(struct pdp_t *pdp, const char *devname)
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100111{
112 struct in_addr ms, sgsn;
113 struct gtp_tunnel *t;
114 int ret;
115
Harald Weltefd30bd12017-11-12 18:26:59 +0900116 pdp_debug(__func__, devname, pdp);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100117
118 t = gtp_tunnel_alloc();
119 if (t == NULL)
120 return -1;
121
122 memcpy(&ms, &pdp->eua.v[2], sizeof(struct in_addr));
123 memcpy(&sgsn, &pdp->gsnrc.v[0], sizeof(struct in_addr));
124
Harald Welte698a2332017-11-08 15:09:58 +0900125 gtp_tunnel_set_ifidx(t, if_nametoindex(devname));
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100126 gtp_tunnel_set_version(t, pdp->version);
127 gtp_tunnel_set_ms_ip4(t, &ms);
128 gtp_tunnel_set_sgsn_ip4(t, &sgsn);
129 if (pdp->version == 0) {
130 gtp_tunnel_set_tid(t, pdp_gettid(pdp->imsi, pdp->nsapi));
131 gtp_tunnel_set_flowid(t, pdp->flru);
132 } else {
Harald Welte875e4dc2017-02-23 20:26:19 +0100133 gtp_tunnel_set_i_tei(t, pdp->teid_own);
134 /* use the TEI advertised by SGSN when sending packets
135 * towards the SGSN */
136 gtp_tunnel_set_o_tei(t, pdp->teid_gn);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100137 }
138
139 ret = gtp_add_tunnel(gtp_nl.genl_id, gtp_nl.nl, t);
140 gtp_tunnel_free(t);
141
142 return ret;
143}
144
Harald Welte698a2332017-11-08 15:09:58 +0900145int gtp_kernel_tunnel_del(struct pdp_t *pdp, const char *devname)
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100146{
147 struct gtp_tunnel *t;
148 int ret;
149
Harald Weltefd30bd12017-11-12 18:26:59 +0900150 pdp_debug(__func__, devname, pdp);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100151
152 t = gtp_tunnel_alloc();
153 if (t == NULL)
154 return -1;
155
Harald Welte698a2332017-11-08 15:09:58 +0900156 gtp_tunnel_set_ifidx(t, if_nametoindex(devname));
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100157 gtp_tunnel_set_version(t, pdp->version);
158 if (pdp->version == 0) {
159 gtp_tunnel_set_tid(t, pdp_gettid(pdp->imsi, pdp->nsapi));
160 gtp_tunnel_set_flowid(t, pdp->flru);
161 } else {
Harald Welte875e4dc2017-02-23 20:26:19 +0100162 gtp_tunnel_set_i_tei(t, pdp->teid_own);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100163 }
164
165 ret = gtp_del_tunnel(gtp_nl.genl_id, gtp_nl.nl, t);
166 gtp_tunnel_free(t);
167
168 return ret;
169}