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" |
Pau Espin Pedrol | 1ef2621 | 2019-08-20 13:26:14 +0200 | [diff] [blame] | 29 | #include "../lib/util.h" |
| 30 | #include "../lib/ippool.h" |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 31 | #include "../gtp/pdp.h" |
| 32 | #include "../gtp/gtp.h" |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 33 | |
| 34 | #include <libgtpnl/gtp.h> |
| 35 | #include <libgtpnl/gtpnl.h> |
| 36 | #include <libmnl/libmnl.h> |
| 37 | |
| 38 | #include "gtp-kernel.h" |
| 39 | |
Harald Welte | fd30bd1 | 2017-11-12 18:26:59 +0900 | [diff] [blame] | 40 | static void pdp_debug(const char *prefix, const char *devname, struct pdp_t *pdp) |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 41 | { |
Pau Espin Pedrol | 1ef2621 | 2019-08-20 13:26:14 +0200 | [diff] [blame] | 42 | char buf4[INET_ADDRSTRLEN], buf6[INET6_ADDRSTRLEN]; |
| 43 | struct ippoolm_t *peer; |
Harald Welte | 51127ea | 2017-11-06 02:42:22 +0900 | [diff] [blame] | 44 | struct in_addr ia; |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 45 | |
Pau Espin Pedrol | 1ef2621 | 2019-08-20 13:26:14 +0200 | [diff] [blame] | 46 | 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 Welte | 51127ea | 2017-11-06 02:42:22 +0900 | [diff] [blame] | 53 | gsna2in_addr(&ia, &pdp->gsnrc); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 54 | |
Pau Espin Pedrol | 1ef2621 | 2019-08-20 13:26:14 +0200 | [diff] [blame] | 55 | LOGPDPX(DGGSN, LOGL_DEBUG, pdp, "%s %s v%u TEID %"PRIx64" EUA=(%s,%s) SGSN=%s\n", prefix, |
Harald Welte | fd30bd1 | 2017-11-12 18:26:59 +0900 | [diff] [blame] | 56 | devname, pdp->version, |
Harald Welte | 51127ea | 2017-11-06 02:42:22 +0900 | [diff] [blame] | 57 | pdp->version == 0 ? pdp_gettid(pdp->imsi, pdp->nsapi) : pdp->teid_gn, |
Pau Espin Pedrol | 1ef2621 | 2019-08-20 13:26:14 +0200 | [diff] [blame] | 58 | buf4, buf6, inet_ntoa(ia)); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 61 | static struct { |
| 62 | int genl_id; |
| 63 | struct mnl_socket *nl; |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 64 | } gtp_nl; |
| 65 | |
Harald Welte | 22e1573 | 2017-11-08 16:07:12 +0900 | [diff] [blame] | 66 | static 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 Welte | 3dad951 | 2017-11-08 16:53:47 +0900 | [diff] [blame] | 74 | LOGP(DGGSN, LOGL_ERROR, "cannot create genetlink socket\n"); |
Harald Welte | 22e1573 | 2017-11-08 16:07:12 +0900 | [diff] [blame] | 75 | return -1; |
| 76 | } |
| 77 | gtp_nl.genl_id = genl_lookup_family(gtp_nl.nl, "gtp"); |
| 78 | if (gtp_nl.genl_id < 0) { |
Harald Welte | 3dad951 | 2017-11-08 16:53:47 +0900 | [diff] [blame] | 79 | LOGP(DGGSN, LOGL_ERROR, "cannot lookup GTP genetlink ID\n"); |
Harald Welte | 3187956 | 2017-11-08 16:08:20 +0900 | [diff] [blame] | 80 | genl_socket_close(gtp_nl.nl); |
| 81 | gtp_nl.nl = NULL; |
Harald Welte | 22e1573 | 2017-11-08 16:07:12 +0900 | [diff] [blame] | 82 | return -1; |
| 83 | } |
Harald Welte | 3dad951 | 2017-11-08 16:53:47 +0900 | [diff] [blame] | 84 | LOGP(DGGSN, LOGL_NOTICE, "Initialized GTP kernel mode (genl ID is %d)\n", gtp_nl.genl_id); |
Harald Welte | 22e1573 | 2017-11-08 16:07:12 +0900 | [diff] [blame] | 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 89 | int gtp_kernel_create(int dest_ns, const char *devname, int fd0, int fd1u) |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 90 | { |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 91 | if (gtp_kernel_init_once() < 0) |
Harald Welte | e3c5918 | 2017-11-08 14:08:24 +0900 | [diff] [blame] | 92 | return -1; |
Harald Welte | e3c5918 | 2017-11-08 14:08:24 +0900 | [diff] [blame] | 93 | |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 94 | return gtp_dev_create(dest_ns, devname, fd0, fd1u); |
| 95 | } |
| 96 | |
| 97 | int gtp_kernel_create_sgsn(int dest_ns, const char *devname, int fd0, int fd1u) |
| 98 | { |
| 99 | if (gtp_kernel_init_once() < 0) |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 100 | return -1; |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 101 | |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 102 | return gtp_dev_create_sgsn(dest_ns, devname, fd0, fd1u); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 103 | } |
| 104 | |
Harald Welte | 698a233 | 2017-11-08 15:09:58 +0900 | [diff] [blame] | 105 | void gtp_kernel_stop(const char *devname) |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 106 | { |
Harald Welte | 698a233 | 2017-11-08 15:09:58 +0900 | [diff] [blame] | 107 | gtp_dev_destroy(devname); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Harald Welte | 698a233 | 2017-11-08 15:09:58 +0900 | [diff] [blame] | 110 | int gtp_kernel_tunnel_add(struct pdp_t *pdp, const char *devname) |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 111 | { |
| 112 | struct in_addr ms, sgsn; |
| 113 | struct gtp_tunnel *t; |
| 114 | int ret; |
| 115 | |
Harald Welte | fd30bd1 | 2017-11-12 18:26:59 +0900 | [diff] [blame] | 116 | pdp_debug(__func__, devname, pdp); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 117 | |
| 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 Welte | 698a233 | 2017-11-08 15:09:58 +0900 | [diff] [blame] | 125 | gtp_tunnel_set_ifidx(t, if_nametoindex(devname)); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 126 | 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 Welte | 875e4dc | 2017-02-23 20:26:19 +0100 | [diff] [blame] | 133 | 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 Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 137 | } |
| 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 Welte | 698a233 | 2017-11-08 15:09:58 +0900 | [diff] [blame] | 145 | int gtp_kernel_tunnel_del(struct pdp_t *pdp, const char *devname) |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 146 | { |
| 147 | struct gtp_tunnel *t; |
| 148 | int ret; |
| 149 | |
Harald Welte | fd30bd1 | 2017-11-12 18:26:59 +0900 | [diff] [blame] | 150 | pdp_debug(__func__, devname, pdp); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 151 | |
| 152 | t = gtp_tunnel_alloc(); |
| 153 | if (t == NULL) |
| 154 | return -1; |
| 155 | |
Harald Welte | 698a233 | 2017-11-08 15:09:58 +0900 | [diff] [blame] | 156 | gtp_tunnel_set_ifidx(t, if_nametoindex(devname)); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 157 | 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 Welte | 875e4dc | 2017-02-23 20:26:19 +0100 | [diff] [blame] | 162 | gtp_tunnel_set_i_tei(t, pdp->teid_own); |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | ret = gtp_del_tunnel(gtp_nl.genl_id, gtp_nl.nl, t); |
| 166 | gtp_tunnel_free(t); |
| 167 | |
| 168 | return ret; |
| 169 | } |