blob: 9b298efc6521e053d6b2103d54884ca43157b4bd [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>
14#include <sys/types.h>
15#include <arpa/inet.h>
16#include <net/if.h>
17
18#include <libgtpnl/gtp.h>
19#include <libgtpnl/gtpnl.h>
20#include <libmnl/libmnl.h>
21
22#include <errno.h>
23
24#include <time.h>
25
26#include "../lib/tun.h"
27#include "../lib/syserr.h"
28#include "../gtp/pdp.h"
29#include "../gtp/gtp.h"
30#include "cmdline.h"
31
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{
40 int i;
41 uint64_t teid;
42
43 if (!debug)
44 return;
45
46 printf("version %u\n", pdp->version);
47 if (pdp->version == 0) {
48 teid = pdp_gettid(pdp->imsi, pdp->nsapi);
49 printf("flowid %u\n", pdp->flru);
50 } else {
51 teid = pdp->teid_gn; /* GTPIE_TEI_DI */
52 }
53
54 printf("teid %llx\n", teid);
55 printf("address (%u)\n", pdp->eua.l);
56
57 /* Byte 0: 0xf1 == IETF */
58 /* Byte 1: 0x21 == IPv4 */
59 /* Byte 2-6: IPv4 address */
60
61 for (i = 0; i < 6; i++)
62 printf("%x ", pdp->eua.v[i] & 0xff); /* GTPIE_EUA */
63
64 printf("\n");
65 printf("sgsn-addr (%u)\n", pdp->gsnrc.l);
66
67 for (i = 0; i < 4; i++)
68 printf("%x ", pdp->gsnrc.v[i] & 0xff); /* GTPIE_GSN_ADDR */
69
70 printf("\n");
71}
72
73static int mask2prefix(struct in_addr *mask)
74{
75 uint32_t tmp = ntohl(mask->s_addr);
76 int k;
77
78 for (k=0; tmp > 0; k++)
79 tmp = (tmp << 1);
80
81 return k;
82}
83
84static struct {
85 int genl_id;
86 struct mnl_socket *nl;
87 bool enabled;
88} gtp_nl;
89
90/* Always forces the kernel to allocate gtp0. If it exists it hits EEXIST */
91#define GTP_DEVNAME "gtp0"
92
93int gtp_kernel_init(struct gsn_t *gsn, struct in_addr *net,
94 struct in_addr *mask,
95 struct gengetopt_args_info *args_info)
96{
97 if (!args_info->gtpnl_given)
98 return 0;
99
Pablo Neira Ayuso7b319872016-05-10 18:38:30 +0200100 if (gtp_dev_create(-1, GTP_DEVNAME, gsn->fd0, gsn->fd1u) < 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 create GTP tunnel device: %s\n",
103 strerror(errno));
104 return -1;
105 }
106 gtp_nl.enabled = true;
107
108 gtp_nl.nl = genl_socket_open();
109 if (gtp_nl.nl == NULL) {
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +0100110 SYS_ERR(DGGSN, LOGL_ERROR, 0,
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100111 "cannot create genetlink socket\n");
112 return -1;
113 }
114 gtp_nl.genl_id = genl_lookup_family(gtp_nl.nl, "gtp");
115 if (gtp_nl.genl_id < 0) {
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +0100116 SYS_ERR(DGGSN, LOGL_ERROR, 0,
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100117 "cannot lookup GTP genetlink ID\n");
118 return -1;
119 }
120 if (debug) {
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +0100121 SYS_ERR(DGGSN, LOGL_NOTICE, 0,
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100122 "Using the GTP kernel mode (genl ID is %d)\n",
123 gtp_nl.genl_id);
124 }
125
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +0100126 DEBUGP(DGGSN, "Setting route to reach %s via %s\n",
127 args_info->net_arg, GTP_DEVNAME);
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100128
129 if (gtp_dev_config(GTP_DEVNAME, net, mask2prefix(mask)) < 0) {
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +0100130 SYS_ERR(DGGSN, LOGL_ERROR, 0,
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100131 "Cannot add route to reach network %s\n",
132 args_info->net_arg);
133 }
134
135 /* launch script if it is set to bring up the route to reach
136 * the MS, eg. ip ro add 10.0.0.0/8 dev gtp0. Better add this
137 * using native rtnetlink interface given that we know the
138 * MS network mask, later.
139 */
140 if (ipup) {
141 char cmd[1024];
142 int err;
143
144 /* eg. /home/ggsn/ipup gtp0 10.0.0.0/8 */
145 snprintf(cmd, sizeof(cmd), "%s %s %s",
146 ipup, GTP_DEVNAME, args_info->net_arg);
147 cmd[sizeof(cmd)-1] = '\0';
148
149 err = system(cmd);
150 if (err < 0) {
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +0100151 SYS_ERR(DGGSN, LOGL_ERROR, 0,
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100152 "Failed to launch script `%s'", ipup);
153 return -1;
154 }
155 }
Andreas Schultzc5fbf9b2015-11-17 12:22:43 +0100156 SYS_ERR(DGGSN, LOGL_NOTICE, 0, "GTP kernel configured\n");
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100157
158 return 0;
159}
160
161void gtp_kernel_stop(void)
162{
163 if (!gtp_nl.enabled)
164 return;
165
166 gtp_dev_destroy(GTP_DEVNAME);
167}
168
169int gtp_kernel_tunnel_add(struct pdp_t *pdp)
170{
171 struct in_addr ms, sgsn;
172 struct gtp_tunnel *t;
173 int ret;
174
175 if (!gtp_nl.enabled)
176 return 0;
177
178 pdp_debug(pdp);
179
180 t = gtp_tunnel_alloc();
181 if (t == NULL)
182 return -1;
183
184 memcpy(&ms, &pdp->eua.v[2], sizeof(struct in_addr));
185 memcpy(&sgsn, &pdp->gsnrc.v[0], sizeof(struct in_addr));
186
187 gtp_tunnel_set_ifidx(t, if_nametoindex(GTP_DEVNAME));
188 gtp_tunnel_set_version(t, pdp->version);
189 gtp_tunnel_set_ms_ip4(t, &ms);
190 gtp_tunnel_set_sgsn_ip4(t, &sgsn);
191 if (pdp->version == 0) {
192 gtp_tunnel_set_tid(t, pdp_gettid(pdp->imsi, pdp->nsapi));
193 gtp_tunnel_set_flowid(t, pdp->flru);
194 } else {
195 gtp_tunnel_set_tid(t, pdp->teid_gn); /* GTPIE_TEI_DI */
196 }
197
198 ret = gtp_add_tunnel(gtp_nl.genl_id, gtp_nl.nl, t);
199 gtp_tunnel_free(t);
200
201 return ret;
202}
203
204int gtp_kernel_tunnel_del(struct pdp_t *pdp)
205{
206 struct gtp_tunnel *t;
207 int ret;
208
209 if (!gtp_nl.enabled)
210 return 0;
211
212 pdp_debug(pdp);
213
214 t = gtp_tunnel_alloc();
215 if (t == NULL)
216 return -1;
217
218 gtp_tunnel_set_ifidx(t, if_nametoindex(GTP_DEVNAME));
219 gtp_tunnel_set_version(t, pdp->version);
220 if (pdp->version == 0) {
221 gtp_tunnel_set_tid(t, pdp_gettid(pdp->imsi, pdp->nsapi));
222 gtp_tunnel_set_flowid(t, pdp->flru);
223 } else {
224 gtp_tunnel_set_tid(t, pdp->teid_gn); /* GTPIE_TEI_DI */
225 }
226
227 ret = gtp_del_tunnel(gtp_nl.genl_id, gtp_nl.nl, t);
228 gtp_tunnel_free(t);
229
230 return ret;
231}
232
233int gtp_kernel_enabled(void)
234{
235 return gtp_nl.enabled;
236}