blob: a5708fc2ab07bfd2d778f6c1c177b5c377d40fa2 [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"
29#include "../gtp/pdp.h"
30#include "../gtp/gtp.h"
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010031
32#include <libgtpnl/gtp.h>
33#include <libgtpnl/gtpnl.h>
34#include <libmnl/libmnl.h>
35
36#include "gtp-kernel.h"
37
38static void pdp_debug(struct pdp_t *pdp)
39{
Harald Welte51127ea2017-11-06 02:42:22 +090040 struct in46_addr ia46;
41 struct in_addr ia;
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010042
Harald Welte51127ea2017-11-06 02:42:22 +090043 in46a_from_eua(&pdp->eua, &ia46);
44 gsna2in_addr(&ia, &pdp->gsnrc);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010045
Harald Welte51127ea2017-11-06 02:42:22 +090046 LOGPDPX(DGGSN, LOGL_DEBUG, pdp, "v%u TEID %"PRIu64"x EUA=%s SGSN=%s\n", pdp->version,
47 pdp->version == 0 ? pdp_gettid(pdp->imsi, pdp->nsapi) : pdp->teid_gn,
48 in46a_ntoa(&ia46), inet_ntoa(ia));
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010049}
50
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010051static struct {
52 int genl_id;
53 struct mnl_socket *nl;
54 bool enabled;
55} gtp_nl;
56
57/* Always forces the kernel to allocate gtp0. If it exists it hits EEXIST */
58#define GTP_DEVNAME "gtp0"
59
Harald Weltee3c59182017-11-08 14:08:24 +090060int gtp_kernel_init(struct gsn_t *gsn, struct in46_prefix *prefix, const char *ipup)
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010061{
Harald Weltee3c59182017-11-08 14:08:24 +090062 struct in_addr net;
63 const char *net_arg;
64
65 if (prefix->addr.len != 4) {
66 SYS_ERR(DGGSN, LOGL_ERROR, 0,
67 "we only support IPv4 in this path :/");
68 return -1;
69 }
70 net = prefix->addr.v4;
71
Pablo Neira Ayuso7b319872016-05-10 18:38:30 +020072 if (gtp_dev_create(-1, GTP_DEVNAME, gsn->fd0, gsn->fd1u) < 0) {
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +010073 SYS_ERR(DGGSN, LOGL_ERROR, 0,
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010074 "cannot create GTP tunnel device: %s\n",
75 strerror(errno));
76 return -1;
77 }
78 gtp_nl.enabled = true;
79
80 gtp_nl.nl = genl_socket_open();
81 if (gtp_nl.nl == NULL) {
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +010082 SYS_ERR(DGGSN, LOGL_ERROR, 0,
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010083 "cannot create genetlink socket\n");
84 return -1;
85 }
86 gtp_nl.genl_id = genl_lookup_family(gtp_nl.nl, "gtp");
87 if (gtp_nl.genl_id < 0) {
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +010088 SYS_ERR(DGGSN, LOGL_ERROR, 0,
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010089 "cannot lookup GTP genetlink ID\n");
90 return -1;
91 }
Harald Welte51127ea2017-11-06 02:42:22 +090092 SYS_ERR(DGGSN, LOGL_DEBUG, 0,
93 "Using the GTP kernel mode (genl ID is %d)\n", gtp_nl.genl_id);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010094
Harald Weltee3c59182017-11-08 14:08:24 +090095 net_arg = in46p_ntoa(prefix);
96
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +010097 DEBUGP(DGGSN, "Setting route to reach %s via %s\n",
Harald Welte8ffd7fc2017-08-12 14:52:15 +020098 net_arg, GTP_DEVNAME);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010099
Harald Weltee3c59182017-11-08 14:08:24 +0900100 if (gtp_dev_config(GTP_DEVNAME, &net, prefix->prefixlen) < 0) {
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +0100101 SYS_ERR(DGGSN, LOGL_ERROR, 0,
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100102 "Cannot add route to reach network %s\n",
Harald Welte8ffd7fc2017-08-12 14:52:15 +0200103 net_arg);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100104 }
105
106 /* launch script if it is set to bring up the route to reach
107 * the MS, eg. ip ro add 10.0.0.0/8 dev gtp0. Better add this
108 * using native rtnetlink interface given that we know the
109 * MS network mask, later.
110 */
111 if (ipup) {
112 char cmd[1024];
113 int err;
114
115 /* eg. /home/ggsn/ipup gtp0 10.0.0.0/8 */
116 snprintf(cmd, sizeof(cmd), "%s %s %s",
Harald Welte8ffd7fc2017-08-12 14:52:15 +0200117 ipup, GTP_DEVNAME, net_arg);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100118 cmd[sizeof(cmd)-1] = '\0';
119
120 err = system(cmd);
121 if (err < 0) {
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +0100122 SYS_ERR(DGGSN, LOGL_ERROR, 0,
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100123 "Failed to launch script `%s'", ipup);
124 return -1;
125 }
126 }
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +0100127 SYS_ERR(DGGSN, LOGL_NOTICE, 0, "GTP kernel configured\n");
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100128
129 return 0;
130}
131
132void gtp_kernel_stop(void)
133{
134 if (!gtp_nl.enabled)
135 return;
136
137 gtp_dev_destroy(GTP_DEVNAME);
138}
139
140int gtp_kernel_tunnel_add(struct pdp_t *pdp)
141{
142 struct in_addr ms, sgsn;
143 struct gtp_tunnel *t;
144 int ret;
145
146 if (!gtp_nl.enabled)
147 return 0;
148
149 pdp_debug(pdp);
150
151 t = gtp_tunnel_alloc();
152 if (t == NULL)
153 return -1;
154
155 memcpy(&ms, &pdp->eua.v[2], sizeof(struct in_addr));
156 memcpy(&sgsn, &pdp->gsnrc.v[0], sizeof(struct in_addr));
157
158 gtp_tunnel_set_ifidx(t, if_nametoindex(GTP_DEVNAME));
159 gtp_tunnel_set_version(t, pdp->version);
160 gtp_tunnel_set_ms_ip4(t, &ms);
161 gtp_tunnel_set_sgsn_ip4(t, &sgsn);
162 if (pdp->version == 0) {
163 gtp_tunnel_set_tid(t, pdp_gettid(pdp->imsi, pdp->nsapi));
164 gtp_tunnel_set_flowid(t, pdp->flru);
165 } else {
Harald Welte875e4dc2017-02-23 20:26:19 +0100166 gtp_tunnel_set_i_tei(t, pdp->teid_own);
167 /* use the TEI advertised by SGSN when sending packets
168 * towards the SGSN */
169 gtp_tunnel_set_o_tei(t, pdp->teid_gn);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100170 }
171
172 ret = gtp_add_tunnel(gtp_nl.genl_id, gtp_nl.nl, t);
173 gtp_tunnel_free(t);
174
175 return ret;
176}
177
178int gtp_kernel_tunnel_del(struct pdp_t *pdp)
179{
180 struct gtp_tunnel *t;
181 int ret;
182
183 if (!gtp_nl.enabled)
184 return 0;
185
186 pdp_debug(pdp);
187
188 t = gtp_tunnel_alloc();
189 if (t == NULL)
190 return -1;
191
192 gtp_tunnel_set_ifidx(t, if_nametoindex(GTP_DEVNAME));
193 gtp_tunnel_set_version(t, pdp->version);
194 if (pdp->version == 0) {
195 gtp_tunnel_set_tid(t, pdp_gettid(pdp->imsi, pdp->nsapi));
196 gtp_tunnel_set_flowid(t, pdp->flru);
197 } else {
Harald Welte875e4dc2017-02-23 20:26:19 +0100198 gtp_tunnel_set_i_tei(t, pdp->teid_own);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100199 }
200
201 ret = gtp_del_tunnel(gtp_nl.genl_id, gtp_nl.nl, t);
202 gtp_tunnel_free(t);
203
204 return ret;
205}
206
207int gtp_kernel_enabled(void)
208{
209 return gtp_nl.enabled;
210}