blob: a35153e895cbcdbd74b616512c6b55b7496a5695 [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;
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010054} gtp_nl;
55
Harald Welte22e15732017-11-08 16:07:12 +090056static int gtp_kernel_init_once(void)
57{
58 /* only initialize once */
59 if (gtp_nl.nl)
60 return 0;
61
62 gtp_nl.nl = genl_socket_open();
63 if (gtp_nl.nl == NULL) {
64 SYS_ERR(DGGSN, LOGL_ERROR, 0, "cannot create genetlink socket\n");
65 return -1;
66 }
67 gtp_nl.genl_id = genl_lookup_family(gtp_nl.nl, "gtp");
68 if (gtp_nl.genl_id < 0) {
69 SYS_ERR(DGGSN, LOGL_ERROR, 0,
70 "cannot lookup GTP genetlink ID\n");
Harald Welte31879562017-11-08 16:08:20 +090071 genl_socket_close(gtp_nl.nl);
72 gtp_nl.nl = NULL;
Harald Welte22e15732017-11-08 16:07:12 +090073 return -1;
74 }
75 SYS_ERR(DGGSN, LOGL_DEBUG, 0, "Initialized GTP kernel mode (genl ID is %d)\n", gtp_nl.genl_id);
76
77 return 0;
78}
79
Harald Welte698a2332017-11-08 15:09:58 +090080int gtp_kernel_init(struct gsn_t *gsn, const char *devname, struct in46_prefix *prefix, const char *ipup)
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010081{
Harald Weltee3c59182017-11-08 14:08:24 +090082 struct in_addr net;
83 const char *net_arg;
84
Harald Welte22e15732017-11-08 16:07:12 +090085 if (!gtp_nl.nl)
86 gtp_kernel_init_once();
87
Harald Weltee3c59182017-11-08 14:08:24 +090088 if (prefix->addr.len != 4) {
89 SYS_ERR(DGGSN, LOGL_ERROR, 0,
90 "we only support IPv4 in this path :/");
91 return -1;
92 }
93 net = prefix->addr.v4;
94
Harald Welte698a2332017-11-08 15:09:58 +090095 if (gtp_dev_create(-1, devname, gsn->fd0, gsn->fd1u) < 0) {
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +010096 SYS_ERR(DGGSN, LOGL_ERROR, 0,
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010097 "cannot create GTP tunnel device: %s\n",
98 strerror(errno));
99 return -1;
100 }
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100101
Harald Weltee3c59182017-11-08 14:08:24 +0900102 net_arg = in46p_ntoa(prefix);
103
Harald Welte698a2332017-11-08 15:09:58 +0900104 DEBUGP(DGGSN, "Setting route to reach %s via %s\n", net_arg, devname);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100105
Harald Welte698a2332017-11-08 15:09:58 +0900106 if (gtp_dev_config(devname, &net, prefix->prefixlen) < 0) {
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +0100107 SYS_ERR(DGGSN, LOGL_ERROR, 0,
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100108 "Cannot add route to reach network %s\n",
Harald Welte8ffd7fc2017-08-12 14:52:15 +0200109 net_arg);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100110 }
111
112 /* launch script if it is set to bring up the route to reach
113 * the MS, eg. ip ro add 10.0.0.0/8 dev gtp0. Better add this
114 * using native rtnetlink interface given that we know the
115 * MS network mask, later.
116 */
117 if (ipup) {
118 char cmd[1024];
119 int err;
120
121 /* eg. /home/ggsn/ipup gtp0 10.0.0.0/8 */
Harald Welte698a2332017-11-08 15:09:58 +0900122 snprintf(cmd, sizeof(cmd), "%s %s %s", ipup, devname, net_arg);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100123 cmd[sizeof(cmd)-1] = '\0';
124
125 err = system(cmd);
126 if (err < 0) {
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +0100127 SYS_ERR(DGGSN, LOGL_ERROR, 0,
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100128 "Failed to launch script `%s'", ipup);
129 return -1;
130 }
131 }
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +0100132 SYS_ERR(DGGSN, LOGL_NOTICE, 0, "GTP kernel configured\n");
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100133
134 return 0;
135}
136
Harald Welte698a2332017-11-08 15:09:58 +0900137void gtp_kernel_stop(const char *devname)
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100138{
Harald Welte698a2332017-11-08 15:09:58 +0900139 gtp_dev_destroy(devname);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100140}
141
Harald Welte698a2332017-11-08 15:09:58 +0900142int gtp_kernel_tunnel_add(struct pdp_t *pdp, const char *devname)
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100143{
144 struct in_addr ms, sgsn;
145 struct gtp_tunnel *t;
146 int ret;
147
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100148 pdp_debug(pdp);
149
150 t = gtp_tunnel_alloc();
151 if (t == NULL)
152 return -1;
153
154 memcpy(&ms, &pdp->eua.v[2], sizeof(struct in_addr));
155 memcpy(&sgsn, &pdp->gsnrc.v[0], sizeof(struct in_addr));
156
Harald Welte698a2332017-11-08 15:09:58 +0900157 gtp_tunnel_set_ifidx(t, if_nametoindex(devname));
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100158 gtp_tunnel_set_version(t, pdp->version);
159 gtp_tunnel_set_ms_ip4(t, &ms);
160 gtp_tunnel_set_sgsn_ip4(t, &sgsn);
161 if (pdp->version == 0) {
162 gtp_tunnel_set_tid(t, pdp_gettid(pdp->imsi, pdp->nsapi));
163 gtp_tunnel_set_flowid(t, pdp->flru);
164 } else {
Harald Welte875e4dc2017-02-23 20:26:19 +0100165 gtp_tunnel_set_i_tei(t, pdp->teid_own);
166 /* use the TEI advertised by SGSN when sending packets
167 * towards the SGSN */
168 gtp_tunnel_set_o_tei(t, pdp->teid_gn);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100169 }
170
171 ret = gtp_add_tunnel(gtp_nl.genl_id, gtp_nl.nl, t);
172 gtp_tunnel_free(t);
173
174 return ret;
175}
176
Harald Welte698a2332017-11-08 15:09:58 +0900177int gtp_kernel_tunnel_del(struct pdp_t *pdp, const char *devname)
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100178{
179 struct gtp_tunnel *t;
180 int ret;
181
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100182 pdp_debug(pdp);
183
184 t = gtp_tunnel_alloc();
185 if (t == NULL)
186 return -1;
187
Harald Welte698a2332017-11-08 15:09:58 +0900188 gtp_tunnel_set_ifidx(t, if_nametoindex(devname));
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100189 gtp_tunnel_set_version(t, pdp->version);
190 if (pdp->version == 0) {
191 gtp_tunnel_set_tid(t, pdp_gettid(pdp->imsi, pdp->nsapi));
192 gtp_tunnel_set_flowid(t, pdp->flru);
193 } else {
Harald Welte875e4dc2017-02-23 20:26:19 +0100194 gtp_tunnel_set_i_tei(t, pdp->teid_own);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100195 }
196
197 ret = gtp_del_tunnel(gtp_nl.genl_id, gtp_nl.nl, t);
198 gtp_tunnel_free(t);
199
200 return ret;
201}