Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 1 | #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 Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <string.h> |
| 13 | #include <stdlib.h> |
Harald Welte | 51127ea | 2017-11-06 02:42:22 +0900 | [diff] [blame] | 14 | #include <inttypes.h> |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 15 | #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 Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 31 | |
| 32 | #include <libgtpnl/gtp.h> |
| 33 | #include <libgtpnl/gtpnl.h> |
| 34 | #include <libmnl/libmnl.h> |
| 35 | |
| 36 | #include "gtp-kernel.h" |
| 37 | |
| 38 | static void pdp_debug(struct pdp_t *pdp) |
| 39 | { |
Harald Welte | 51127ea | 2017-11-06 02:42:22 +0900 | [diff] [blame] | 40 | struct in46_addr ia46; |
| 41 | struct in_addr ia; |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 42 | |
Harald Welte | 51127ea | 2017-11-06 02:42:22 +0900 | [diff] [blame] | 43 | in46a_from_eua(&pdp->eua, &ia46); |
| 44 | gsna2in_addr(&ia, &pdp->gsnrc); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 45 | |
Harald Welte | 51127ea | 2017-11-06 02:42:22 +0900 | [diff] [blame] | 46 | 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 Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 49 | } |
| 50 | |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 51 | static 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 Welte | e3c5918 | 2017-11-08 14:08:24 +0900 | [diff] [blame^] | 60 | int gtp_kernel_init(struct gsn_t *gsn, struct in46_prefix *prefix, const char *ipup) |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 61 | { |
Harald Welte | e3c5918 | 2017-11-08 14:08:24 +0900 | [diff] [blame^] | 62 | 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 Ayuso | 7b31987 | 2016-05-10 18:38:30 +0200 | [diff] [blame] | 72 | if (gtp_dev_create(-1, GTP_DEVNAME, gsn->fd0, gsn->fd1u) < 0) { |
Andreas Schultz | c5fbf9b | 2015-11-17 12:22:43 +0100 | [diff] [blame] | 73 | SYS_ERR(DGGSN, LOGL_ERROR, 0, |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 74 | "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 Schultz | c5fbf9b | 2015-11-17 12:22:43 +0100 | [diff] [blame] | 82 | SYS_ERR(DGGSN, LOGL_ERROR, 0, |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 83 | "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 Schultz | c5fbf9b | 2015-11-17 12:22:43 +0100 | [diff] [blame] | 88 | SYS_ERR(DGGSN, LOGL_ERROR, 0, |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 89 | "cannot lookup GTP genetlink ID\n"); |
| 90 | return -1; |
| 91 | } |
Harald Welte | 51127ea | 2017-11-06 02:42:22 +0900 | [diff] [blame] | 92 | SYS_ERR(DGGSN, LOGL_DEBUG, 0, |
| 93 | "Using the GTP kernel mode (genl ID is %d)\n", gtp_nl.genl_id); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 94 | |
Harald Welte | e3c5918 | 2017-11-08 14:08:24 +0900 | [diff] [blame^] | 95 | net_arg = in46p_ntoa(prefix); |
| 96 | |
Andreas Schultz | c5fbf9b | 2015-11-17 12:22:43 +0100 | [diff] [blame] | 97 | DEBUGP(DGGSN, "Setting route to reach %s via %s\n", |
Harald Welte | 8ffd7fc | 2017-08-12 14:52:15 +0200 | [diff] [blame] | 98 | net_arg, GTP_DEVNAME); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 99 | |
Harald Welte | e3c5918 | 2017-11-08 14:08:24 +0900 | [diff] [blame^] | 100 | if (gtp_dev_config(GTP_DEVNAME, &net, prefix->prefixlen) < 0) { |
Andreas Schultz | c5fbf9b | 2015-11-17 12:22:43 +0100 | [diff] [blame] | 101 | SYS_ERR(DGGSN, LOGL_ERROR, 0, |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 102 | "Cannot add route to reach network %s\n", |
Harald Welte | 8ffd7fc | 2017-08-12 14:52:15 +0200 | [diff] [blame] | 103 | net_arg); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 104 | } |
| 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 Welte | 8ffd7fc | 2017-08-12 14:52:15 +0200 | [diff] [blame] | 117 | ipup, GTP_DEVNAME, net_arg); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 118 | cmd[sizeof(cmd)-1] = '\0'; |
| 119 | |
| 120 | err = system(cmd); |
| 121 | if (err < 0) { |
Andreas Schultz | c5fbf9b | 2015-11-17 12:22:43 +0100 | [diff] [blame] | 122 | SYS_ERR(DGGSN, LOGL_ERROR, 0, |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 123 | "Failed to launch script `%s'", ipup); |
| 124 | return -1; |
| 125 | } |
| 126 | } |
Andreas Schultz | c5fbf9b | 2015-11-17 12:22:43 +0100 | [diff] [blame] | 127 | SYS_ERR(DGGSN, LOGL_NOTICE, 0, "GTP kernel configured\n"); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | void gtp_kernel_stop(void) |
| 133 | { |
| 134 | if (!gtp_nl.enabled) |
| 135 | return; |
| 136 | |
| 137 | gtp_dev_destroy(GTP_DEVNAME); |
| 138 | } |
| 139 | |
| 140 | int 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 Welte | 875e4dc | 2017-02-23 20:26:19 +0100 | [diff] [blame] | 166 | 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 Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 170 | } |
| 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 | |
| 178 | int 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 Welte | 875e4dc | 2017-02-23 20:26:19 +0100 | [diff] [blame] | 198 | gtp_tunnel_set_i_tei(t, pdp->teid_own); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 199 | } |
| 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 | |
| 207 | int gtp_kernel_enabled(void) |
| 208 | { |
| 209 | return gtp_nl.enabled; |
| 210 | } |