Pau Espin Pedrol | fdd732b | 2017-10-13 14:32:24 +0200 | [diff] [blame] | 1 | /* |
Harald Welte | 632e843 | 2017-09-05 18:12:14 +0200 | [diff] [blame] | 2 | * OsmoGGSN - Gateway GPRS Support Node |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 3 | * Copyright (C) 2002, 2003, 2004 Mondru AB. |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 4 | * Copyright (C) 2017 by Harald Welte <laforge@gnumonks.org> |
Pau Espin Pedrol | fdd732b | 2017-10-13 14:32:24 +0200 | [diff] [blame] | 5 | * |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 6 | * The contents of this file may be used under the terms of the GNU |
| 7 | * General Public License Version 2, provided that the above copyright |
| 8 | * notice and this permission notice is included in all copies or |
| 9 | * substantial portions of the software. |
Pau Espin Pedrol | fdd732b | 2017-10-13 14:32:24 +0200 | [diff] [blame] | 10 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /* ggsn.c |
| 14 | * |
| 15 | */ |
| 16 | |
| 17 | #ifdef __linux__ |
| 18 | #define _GNU_SOURCE 1 /* strdup() prototype, broken arpa/inet.h */ |
| 19 | #endif |
| 20 | |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 21 | #include "../config.h" |
| 22 | |
| 23 | #ifdef HAVE_STDINT_H |
| 24 | #include <stdint.h> |
| 25 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 26 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 27 | #include <getopt.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 28 | #include <ctype.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 29 | #include <signal.h> |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | #include <stdlib.h> |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 33 | #include <unistd.h> |
| 34 | #include <inttypes.h> |
| 35 | #include <errno.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 36 | #include <sys/types.h> |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 37 | #include <sys/ioctl.h> |
| 38 | |
| 39 | #include <net/if.h> |
| 40 | #include <arpa/inet.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 41 | #include <netinet/in.h> |
Harald Welte | 63ebccd | 2017-08-02 21:10:09 +0200 | [diff] [blame] | 42 | #include <netinet/ip.h> |
Harald Welte | a0d281d | 2017-08-02 21:48:16 +0200 | [diff] [blame] | 43 | #include <netinet/ip6.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 44 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 45 | #include <osmocom/core/application.h> |
Max | 727417d | 2016-08-02 17:10:38 +0200 | [diff] [blame] | 46 | #include <osmocom/core/select.h> |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 47 | #include <osmocom/core/stats.h> |
| 48 | #include <osmocom/core/rate_ctr.h> |
| 49 | #include <osmocom/core/timer.h> |
Max | 727417d | 2016-08-02 17:10:38 +0200 | [diff] [blame] | 50 | #include <osmocom/ctrl/control_if.h> |
| 51 | #include <osmocom/ctrl/control_cmd.h> |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 52 | #include <osmocom/ctrl/control_vty.h> |
Max | 727417d | 2016-08-02 17:10:38 +0200 | [diff] [blame] | 53 | #include <osmocom/ctrl/ports.h> |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 54 | #include <osmocom/vty/telnet_interface.h> |
| 55 | #include <osmocom/vty/logging.h> |
| 56 | #include <osmocom/vty/stats.h> |
| 57 | #include <osmocom/vty/ports.h> |
| 58 | #include <osmocom/vty/command.h> |
| 59 | #include <osmocom/gsm/apn.h> |
Max | 727417d | 2016-08-02 17:10:38 +0200 | [diff] [blame] | 60 | |
Emmanuel Bretelle | 2a10368 | 2010-09-07 17:01:20 +0200 | [diff] [blame] | 61 | #include "../lib/tun.h" |
| 62 | #include "../lib/ippool.h" |
| 63 | #include "../lib/syserr.h" |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 64 | #include "../lib/in46_addr.h" |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 65 | #include "../gtp/pdp.h" |
| 66 | #include "../gtp/gtp.h" |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 67 | #include "gtp-kernel.h" |
Harald Welte | d46bcd2 | 2017-08-08 23:27:22 +0200 | [diff] [blame] | 68 | #include "icmpv6.h" |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 69 | #include "ggsn.h" |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 70 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 71 | void *tall_ggsn_ctx; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 72 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 73 | static int end = 0; |
| 74 | static int daemonize = 0; |
| 75 | static struct ctrl_handle *g_ctrlh; |
| 76 | |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 77 | struct ul255_t qos; |
| 78 | struct ul255_t apn; |
| 79 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 80 | #define LOGPAPN(level, apn, fmt, args...) \ |
| 81 | LOGP(DGGSN, level, "APN(%s): " fmt, (apn)->cfg.name, ## args) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 82 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 83 | #define LOGPGGSN(level, ggsn, fmt, args...) \ |
| 84 | LOGP(DGGSN, level, "GGSN(%s): " fmt, (ggsn)->cfg.name, ## args) |
| 85 | |
Max | 6a21527 | 2017-09-25 10:35:34 +0200 | [diff] [blame] | 86 | #define LOGPPDP(level, pdp, fmt, args...) LOGPDPX(DGGSN, level, pdp, fmt, ## args) |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 87 | |
| 88 | static int ggsn_tun_fd_cb(struct osmo_fd *fd, unsigned int what); |
| 89 | static int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len); |
| 90 | |
| 91 | |
| 92 | static void pool_close_all_pdp(struct ippool_t *pool) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 93 | { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 94 | unsigned int i; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 95 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 96 | if (!pool) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 97 | return; |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 98 | |
| 99 | for (i = 0; i < pool->listsize; i++) { |
| 100 | struct ippoolm_t *member = &pool->member[i]; |
| 101 | struct pdp_t *pdp; |
| 102 | |
| 103 | if (!member->inuse) |
| 104 | continue; |
| 105 | pdp = member->peer; |
| 106 | if (!pdp) |
| 107 | continue; |
| 108 | LOGPPDP(LOGL_DEBUG, pdp, "Sending DELETE PDP CTX due to shutdown\n"); |
| 109 | gtp_delete_context_req(pdp->gsn, pdp, NULL, 1); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 110 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 113 | int apn_stop(struct apn_ctx *apn, bool force) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 114 | { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 115 | LOGPAPN(LOGL_NOTICE, apn, "%sStopping\n", force ? "FORCED " : ""); |
| 116 | /* check if pools have any active PDP contexts and bail out */ |
| 117 | pool_close_all_pdp(apn->v4.pool); |
| 118 | pool_close_all_pdp(apn->v6.pool); |
| 119 | |
| 120 | /* shutdown whatever old state might be left */ |
| 121 | if (apn->tun.tun) { |
| 122 | /* run ip-down script */ |
| 123 | if (apn->tun.cfg.ipdown_script) { |
| 124 | LOGPAPN( LOGL_INFO, apn, "Running %s\n", apn->tun.cfg.ipdown_script); |
| 125 | tun_runscript(apn->tun.tun, apn->tun.cfg.ipdown_script); |
| 126 | } |
| 127 | /* release tun device */ |
Harald Welte | ed08eb1 | 2017-10-01 18:15:56 +0800 | [diff] [blame] | 128 | LOGPAPN(LOGL_INFO, apn, "Closing TUN device %s\n", apn->tun.tun->devname); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 129 | osmo_fd_unregister(&apn->tun.fd); |
| 130 | tun_free(apn->tun.tun); |
| 131 | apn->tun.tun = NULL; |
| 132 | } |
Harald Welte | 0d0e242 | 2017-11-08 15:49:15 +0900 | [diff] [blame] | 133 | gtp_kernel_stop(apn->tun.cfg.dev_name); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 134 | |
| 135 | if (apn->v4.pool) { |
| 136 | LOGPAPN(LOGL_INFO, apn, "Releasing IPv4 pool\n"); |
| 137 | ippool_free(apn->v4.pool); |
| 138 | apn->v4.pool = NULL; |
| 139 | } |
| 140 | if (apn->v6.pool) { |
| 141 | LOGPAPN(LOGL_INFO, apn, "Releasing IPv6 pool\n"); |
| 142 | ippool_free(apn->v6.pool); |
| 143 | apn->v6.pool = NULL; |
| 144 | } |
| 145 | |
| 146 | apn->started = false; |
| 147 | return 0; |
| 148 | } |
| 149 | |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 150 | |
Harald Welte | f55a039 | 2017-11-08 14:33:55 +0900 | [diff] [blame] | 151 | static int alloc_ippool_blacklist(struct apn_ctx *apn, struct in46_prefix **blacklist, bool ipv6) |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 152 | { |
| 153 | |
| 154 | int flags, len, len2, i; |
| 155 | |
Harald Welte | e2a1de5 | 2017-11-08 15:24:07 +0900 | [diff] [blame] | 156 | *blacklist = NULL; |
| 157 | |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 158 | if (ipv6) |
| 159 | flags = IP_TYPE_IPv6_NONLINK; |
| 160 | else |
| 161 | flags = IP_TYPE_IPv4; |
| 162 | |
| 163 | while (1) { |
Harald Welte | e2a1de5 | 2017-11-08 15:24:07 +0900 | [diff] [blame] | 164 | len = netdev_ip_local_get(apn->tun.cfg.dev_name, NULL, 0, flags); |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 165 | if (len < 1) |
| 166 | return len; |
| 167 | |
| 168 | *blacklist = talloc_zero_size(apn, len * sizeof(struct in46_prefix)); |
Harald Welte | e2a1de5 | 2017-11-08 15:24:07 +0900 | [diff] [blame] | 169 | len2 = netdev_ip_local_get(apn->tun.cfg.dev_name, *blacklist, len, flags); |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 170 | if (len2 < 1) { |
| 171 | talloc_free(*blacklist); |
Harald Welte | e2a1de5 | 2017-11-08 15:24:07 +0900 | [diff] [blame] | 172 | *blacklist = NULL; |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 173 | return len2; |
| 174 | } |
| 175 | |
Harald Welte | e2a1de5 | 2017-11-08 15:24:07 +0900 | [diff] [blame] | 176 | if (len2 > len) { /* iface was added between 2 calls, repeat operation */ |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 177 | talloc_free(*blacklist); |
Harald Welte | e2a1de5 | 2017-11-08 15:24:07 +0900 | [diff] [blame] | 178 | *blacklist = NULL; |
| 179 | } else |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 180 | break; |
| 181 | } |
| 182 | |
| 183 | for (i = 0; i < len2; i++) |
| 184 | LOGPAPN(LOGL_INFO, apn, "Blacklist tun IP %s\n", |
| 185 | in46p_ntoa(&(*blacklist)[i])); |
| 186 | |
| 187 | return len2; |
| 188 | } |
| 189 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 190 | /* actually start the APN with its current config */ |
| 191 | int apn_start(struct apn_ctx *apn) |
| 192 | { |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 193 | int ippool_flags = IPPOOL_NONETWORK | IPPOOL_NOBROADCAST; |
Pau Espin Pedrol | a037e59 | 2017-10-16 14:41:37 +0200 | [diff] [blame] | 194 | struct in46_prefix ipv6_tun_linklocal_ip; |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 195 | struct in46_prefix *blacklist; |
| 196 | int blacklist_size; |
Pau Espin Pedrol | bffc3f9 | 2017-12-14 11:19:10 +0100 | [diff] [blame] | 197 | int rc; |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 198 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 199 | if (apn->started) |
| 200 | return 0; |
| 201 | |
| 202 | LOGPAPN(LOGL_INFO, apn, "Starting\n"); |
| 203 | switch (apn->cfg.gtpu_mode) { |
| 204 | case APN_GTPU_MODE_TUN: |
| 205 | LOGPAPN(LOGL_INFO, apn, "Opening TUN device %s\n", apn->tun.cfg.dev_name); |
| 206 | if (tun_new(&apn->tun.tun, apn->tun.cfg.dev_name)) { |
| 207 | LOGPAPN(LOGL_ERROR, apn, "Failed to configure tun device\n"); |
| 208 | return -1; |
| 209 | } |
| 210 | LOGPAPN(LOGL_INFO, apn, "Opened TUN device %s\n", apn->tun.tun->devname); |
| 211 | |
| 212 | /* Register with libosmcoore */ |
| 213 | osmo_fd_setup(&apn->tun.fd, apn->tun.tun->fd, BSC_FD_READ, ggsn_tun_fd_cb, apn, 0); |
| 214 | osmo_fd_register(&apn->tun.fd); |
| 215 | |
| 216 | /* Set TUN library callback */ |
| 217 | tun_set_cb_ind(apn->tun.tun, cb_tun_ind); |
| 218 | |
| 219 | if (apn->v4.cfg.ifconfig_prefix.addr.len) { |
| 220 | LOGPAPN(LOGL_INFO, apn, "Setting tun IP address %s\n", |
| 221 | in46p_ntoa(&apn->v4.cfg.ifconfig_prefix)); |
| 222 | if (tun_setaddr(apn->tun.tun, &apn->v4.cfg.ifconfig_prefix.addr, NULL, |
| 223 | apn->v4.cfg.ifconfig_prefix.prefixlen)) { |
| 224 | LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv4 address %s: %s\n", |
| 225 | in46p_ntoa(&apn->v4.cfg.ifconfig_prefix), strerror(errno)); |
| 226 | apn_stop(apn, false); |
| 227 | return -1; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | if (apn->v6.cfg.ifconfig_prefix.addr.len) { |
| 232 | LOGPAPN(LOGL_INFO, apn, "Setting tun IPv6 address %s\n", |
| 233 | in46p_ntoa(&apn->v6.cfg.ifconfig_prefix)); |
| 234 | if (tun_setaddr(apn->tun.tun, &apn->v6.cfg.ifconfig_prefix.addr, NULL, |
| 235 | apn->v6.cfg.ifconfig_prefix.prefixlen)) { |
Harald Welte | ed1ba2c | 2017-10-01 18:16:17 +0800 | [diff] [blame] | 236 | LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv6 address %s: %s. " |
| 237 | "Ensure you have ipv6 support and not used the disable_ipv6 sysctl?\n", |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 238 | in46p_ntoa(&apn->v6.cfg.ifconfig_prefix), strerror(errno)); |
| 239 | apn_stop(apn, false); |
| 240 | return -1; |
| 241 | } |
| 242 | } |
| 243 | |
Pau Espin Pedrol | 37c45e3 | 2017-12-14 14:09:13 +0100 | [diff] [blame] | 244 | if (apn->v6.cfg.ll_prefix.addr.len) { |
| 245 | LOGPAPN(LOGL_INFO, apn, "Setting tun IPv6 link-local address %s\n", |
| 246 | in46p_ntoa(&apn->v6.cfg.ll_prefix)); |
| 247 | if (tun_addaddr(apn->tun.tun, &apn->v6.cfg.ll_prefix.addr, NULL, |
| 248 | apn->v6.cfg.ll_prefix.prefixlen)) { |
| 249 | LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv6 link-local address %s: %s. " |
| 250 | "Ensure you have ipv6 support and not used the disable_ipv6 sysctl?\n", |
| 251 | in46p_ntoa(&apn->v6.cfg.ll_prefix), strerror(errno)); |
| 252 | apn_stop(apn, false); |
| 253 | return -1; |
| 254 | } |
| 255 | apn->v6_lladdr = apn->v6.cfg.ll_prefix.addr.v6; |
| 256 | } |
| 257 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 258 | if (apn->tun.cfg.ipup_script) { |
| 259 | LOGPAPN(LOGL_INFO, apn, "Running ip-up script %s\n", |
| 260 | apn->tun.cfg.ipup_script); |
| 261 | tun_runscript(apn->tun.tun, apn->tun.cfg.ipup_script); |
| 262 | } |
Harald Welte | f85fe97 | 2017-09-24 20:00:34 +0800 | [diff] [blame] | 263 | |
Pau Espin Pedrol | 37c45e3 | 2017-12-14 14:09:13 +0100 | [diff] [blame] | 264 | if (apn->cfg.apn_type_mask & (APN_TYPE_IPv6|APN_TYPE_IPv4v6) && |
| 265 | apn->v6.cfg.ll_prefix.addr.len == 0) { |
Pau Espin Pedrol | bffc3f9 | 2017-12-14 11:19:10 +0100 | [diff] [blame] | 266 | rc = tun_ip_local_get(apn->tun.tun, &ipv6_tun_linklocal_ip, 1, IP_TYPE_IPv6_LINK); |
| 267 | if (rc < 1) { |
| 268 | LOGPAPN(LOGL_ERROR, apn, "Cannot obtain IPv6 link-local address of interface: %s\n", |
| 269 | rc ? strerror(errno) : "tun interface has no link-local IP assigned"); |
Harald Welte | f85fe97 | 2017-09-24 20:00:34 +0800 | [diff] [blame] | 270 | apn_stop(apn, false); |
| 271 | return -1; |
| 272 | } |
Pau Espin Pedrol | a037e59 | 2017-10-16 14:41:37 +0200 | [diff] [blame] | 273 | apn->v6_lladdr = ipv6_tun_linklocal_ip.addr.v6; |
Harald Welte | f85fe97 | 2017-09-24 20:00:34 +0800 | [diff] [blame] | 274 | } |
| 275 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 276 | /* set back-pointer from TUN device to APN */ |
| 277 | apn->tun.tun->priv = apn; |
| 278 | break; |
| 279 | case APN_GTPU_MODE_KERNEL_GTP: |
Harald Welte | 2fc2bc6 | 2017-11-08 15:50:53 +0900 | [diff] [blame] | 280 | LOGPAPN(LOGL_INFO, apn, "Opening Kernel GTP device %s\n", apn->tun.cfg.dev_name); |
Harald Welte | 490782d | 2017-11-08 14:09:51 +0900 | [diff] [blame] | 281 | if (apn->cfg.apn_type_mask & (APN_TYPE_IPv6|APN_TYPE_IPv4v6)) { |
| 282 | LOGPAPN(LOGL_ERROR, apn, "Kernel GTP currently supports only IPv4\n"); |
| 283 | apn_stop(apn, false); |
| 284 | return -1; |
| 285 | } |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 286 | /* use GTP kernel module for data packet encapsulation */ |
Harald Welte | 698a233 | 2017-11-08 15:09:58 +0900 | [diff] [blame] | 287 | if (gtp_kernel_init(apn->ggsn->gsn, apn->tun.cfg.dev_name, |
| 288 | &apn->v4.cfg.ifconfig_prefix, apn->tun.cfg.ipup_script) < 0) { |
Harald Welte | 490782d | 2017-11-08 14:09:51 +0900 | [diff] [blame] | 289 | return -1; |
| 290 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 291 | break; |
| 292 | default: |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 293 | LOGPAPN(LOGL_ERROR, apn, "Unknown GTPU Mode %d\n", apn->cfg.gtpu_mode); |
| 294 | return -1; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 295 | } |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 296 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 297 | /* Create IPv4 pool */ |
| 298 | if (apn->v4.cfg.dynamic_prefix.addr.len) { |
| 299 | LOGPAPN(LOGL_INFO, apn, "Creating IPv4 pool %s\n", |
| 300 | in46p_ntoa(&apn->v4.cfg.dynamic_prefix)); |
Harald Welte | f55a039 | 2017-11-08 14:33:55 +0900 | [diff] [blame] | 301 | if ((blacklist_size = alloc_ippool_blacklist(apn, &blacklist, false)) < 0) |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 302 | LOGPAPN(LOGL_ERROR, apn, "Failed obtaining IPv4 tun IPs\n"); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 303 | if (ippool_new(&apn->v4.pool, &apn->v4.cfg.dynamic_prefix, |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 304 | &apn->v4.cfg.static_prefix, ippool_flags, |
| 305 | blacklist, blacklist_size)) { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 306 | LOGPAPN(LOGL_ERROR, apn, "Failed to create IPv4 pool\n"); |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 307 | talloc_free(blacklist); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 308 | apn_stop(apn, false); |
| 309 | return -1; |
| 310 | } |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 311 | talloc_free(blacklist); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 312 | } |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 313 | |
| 314 | /* Create IPv6 pool */ |
| 315 | if (apn->v6.cfg.dynamic_prefix.addr.len) { |
| 316 | LOGPAPN(LOGL_INFO, apn, "Creating IPv6 pool %s\n", |
| 317 | in46p_ntoa(&apn->v6.cfg.dynamic_prefix)); |
Harald Welte | f55a039 | 2017-11-08 14:33:55 +0900 | [diff] [blame] | 318 | if ((blacklist_size = alloc_ippool_blacklist(apn, &blacklist, true)) < 0) |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 319 | LOGPAPN(LOGL_ERROR, apn, "Failed obtaining IPv6 tun IPs\n"); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 320 | if (ippool_new(&apn->v6.pool, &apn->v6.cfg.dynamic_prefix, |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 321 | &apn->v6.cfg.static_prefix, ippool_flags, |
| 322 | blacklist, blacklist_size)) { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 323 | LOGPAPN(LOGL_ERROR, apn, "Failed to create IPv6 pool\n"); |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 324 | talloc_free(blacklist); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 325 | apn_stop(apn, false); |
| 326 | return -1; |
| 327 | } |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 328 | talloc_free(blacklist); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | LOGPAPN(LOGL_NOTICE, apn, "Successfully started\n"); |
| 332 | apn->started = true; |
| 333 | return 0; |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 334 | } |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 335 | |
Max | 3142d8d | 2017-05-04 17:45:10 +0200 | [diff] [blame] | 336 | static bool send_trap(const struct gsn_t *gsn, const struct pdp_t *pdp, const struct ippoolm_t *member, const char *var) |
| 337 | { |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 338 | char addrbuf[256]; |
Max | 3142d8d | 2017-05-04 17:45:10 +0200 | [diff] [blame] | 339 | char val[NAMESIZE]; |
| 340 | |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 341 | const char *addrstr = in46a_ntop(&member->addr, addrbuf, sizeof(addrbuf)); |
| 342 | |
Harald Welte | b10ee08 | 2017-08-12 19:29:16 +0200 | [diff] [blame] | 343 | snprintf(val, sizeof(val), "%s,%s", imsi_gtp2str(&pdp->imsi), addrstr); |
Max | 3142d8d | 2017-05-04 17:45:10 +0200 | [diff] [blame] | 344 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 345 | if (ctrl_cmd_send_trap(g_ctrlh, var, val) < 0) { |
| 346 | LOGPPDP(LOGL_ERROR, pdp, "Failed to create and send TRAP %s\n", var); |
Max | 3142d8d | 2017-05-04 17:45:10 +0200 | [diff] [blame] | 347 | return false; |
| 348 | } |
| 349 | return true; |
| 350 | } |
| 351 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 352 | static int delete_context(struct pdp_t *pdp) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 353 | { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 354 | struct gsn_t *gsn = pdp->gsn; |
Harald Welte | 698a233 | 2017-11-08 15:09:58 +0900 | [diff] [blame] | 355 | struct apn_ctx *apn = pdp->priv; |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 356 | struct ippoolm_t *member; |
| 357 | int i; |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 358 | |
| 359 | LOGPPDP(LOGL_INFO, pdp, "Deleting PDP context\n"); |
Max | dbd7024 | 2016-10-14 13:38:05 +0200 | [diff] [blame] | 360 | |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 361 | for (i = 0; i < 2; i++) { |
| 362 | if (pdp->peer[i]) { |
| 363 | member = pdp->peer[i]; |
| 364 | send_trap(gsn, pdp, member, "imsi-rem-ip"); /* TRAP with IP removal */ |
| 365 | ippool_freeip(member->pool, member); |
| 366 | } else if(i == 0) |
| 367 | LOGPPDP(LOGL_ERROR, pdp, "Cannot find/free IP Pool member\n"); |
| 368 | } |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 369 | |
Harald Welte | 698a233 | 2017-11-08 15:09:58 +0900 | [diff] [blame] | 370 | if (gtp_kernel_tunnel_del(pdp, apn->tun.cfg.dev_name)) { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 371 | LOGPPDP(LOGL_ERROR, pdp, "Cannot delete tunnel from kernel:%s\n", |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 372 | strerror(errno)); |
| 373 | } |
| 374 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 375 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Harald Welte | 1ae9877 | 2017-08-09 20:28:52 +0200 | [diff] [blame] | 378 | #include <osmocom/gsm/tlv.h> |
| 379 | |
| 380 | /* 3GPP TS 24.008 10.6.5.3 */ |
| 381 | enum pco_protocols { |
| 382 | PCO_P_LCP = 0xC021, |
| 383 | PCO_P_PAP = 0xC023, |
| 384 | PCO_P_CHAP = 0xC223, |
| 385 | PCO_P_IPCP = 0x8021, |
| 386 | PCO_P_PCSCF_ADDR = 0x0001, |
| 387 | PCO_P_IM_CN_SS_F = 0x0002, |
| 388 | PCO_P_DNS_IPv6_ADDR = 0x0003, |
| 389 | PCO_P_POLICY_CTRL_REJ = 0x0004, /* only in Network->MS */ |
| 390 | PCO_P_MS_SUP_NETREQ_BCI = 0x0005, |
| 391 | /* reserved */ |
| 392 | PCO_P_DSMIPv6_HA_ADDR = 0x0007, |
| 393 | PCO_P_DSMIPv6_HN_PREF = 0x0008, |
| 394 | PCO_P_DSMIPv6_v4_HA_ADDR= 0x0009, |
| 395 | PCO_P_IP_ADDR_VIA_NAS = 0x000a, /* only MS->Network */ |
| 396 | PCO_P_IPv4_ADDR_VIA_DHCP= 0x000b, /* only MS->Netowrk */ |
| 397 | PCO_P_PCSCF_IPv4_ADDR = 0x000c, |
| 398 | PCO_P_DNS_IPv4_ADDR = 0x000d, |
| 399 | PCO_P_MSISDN = 0x000e, |
| 400 | PCO_P_IFOM_SUPPORT = 0x000f, |
| 401 | PCO_P_IPv4_LINK_MTU = 0x0010, |
| 402 | PCO_P_MS_SUPP_LOC_A_TFT = 0x0011, |
| 403 | PCO_P_PCSCF_RESEL_SUP = 0x0012, /* only MS->Network */ |
| 404 | PCO_P_NBIFOM_REQ = 0x0013, |
| 405 | PCO_P_NBIFOM_MODE = 0x0014, |
| 406 | PCO_P_NONIP_LINK_MTU = 0x0015, |
| 407 | PCO_P_APN_RATE_CTRL_SUP = 0x0016, |
| 408 | PCO_P_PS_DATA_OFF_UE = 0x0017, |
| 409 | PCO_P_REL_DATA_SVC = 0x0018, |
| 410 | }; |
| 411 | |
| 412 | /* determine if PCO contains given protocol */ |
| 413 | static bool pco_contains_proto(struct ul255_t *pco, uint16_t prot) |
| 414 | { |
| 415 | uint8_t *cur = pco->v + 1; |
| 416 | |
| 417 | /* iterate over PCO and check if protocol contained */ |
Pau Espin Pedrol | 0ab62fe | 2017-08-30 15:51:24 +0200 | [diff] [blame] | 418 | while (cur + 3 <= pco->v + pco->l) { |
Harald Welte | 1ae9877 | 2017-08-09 20:28:52 +0200 | [diff] [blame] | 419 | uint16_t cur_prot = osmo_load16be(cur); |
| 420 | uint8_t cur_len = cur[2]; |
| 421 | if (cur_prot == prot) |
| 422 | return true; |
Pau Espin Pedrol | 0ab62fe | 2017-08-30 15:51:24 +0200 | [diff] [blame] | 423 | cur += cur_len + 3; |
Harald Welte | 1ae9877 | 2017-08-09 20:28:52 +0200 | [diff] [blame] | 424 | } |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | /* determine if PDP context has IPv6 support */ |
| 429 | static bool pdp_has_v4(struct pdp_t *pdp) |
| 430 | { |
| 431 | if (pdp->eua.l == 4+2) |
| 432 | return true; |
| 433 | else |
| 434 | return false; |
| 435 | } |
| 436 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 437 | /* construct an IPCP PCO from up to two given DNS addreses */ |
| 438 | static int build_ipcp_pco(struct msgb *msg, uint8_t id, const struct in46_addr *dns1, |
| 439 | const struct in46_addr *dns2) |
| 440 | { |
| 441 | uint8_t *len1, *len2; |
| 442 | uint8_t *start = msg->tail; |
| 443 | unsigned int len_appended; |
| 444 | |
| 445 | /* Three byte T16L header */ |
| 446 | msgb_put_u16(msg, 0x8021); /* IPCP */ |
| 447 | len1 = msgb_put(msg, 1); /* Length of contents: delay */ |
| 448 | |
| 449 | msgb_put_u8(msg, 0x02); /* ACK */ |
| 450 | msgb_put_u8(msg, id); /* ID: Needs to match request */ |
| 451 | msgb_put_u8(msg, 0x00); /* Length MSB */ |
| 452 | len2 = msgb_put(msg, 1); /* Length LSB: delay */ |
| 453 | |
| 454 | if (dns1 && dns1->len == 4) { |
| 455 | msgb_put_u8(msg, 0x81); /* DNS1 Tag */ |
| 456 | msgb_put_u8(msg, 2 + dns1->len);/* DNS1 Length, incl. TL */ |
Harald Welte | bcab7fb | 2017-12-03 21:43:50 +0100 | [diff] [blame] | 457 | msgb_put_u32(msg, ntohl(dns1->v4.s_addr)); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | if (dns2 && dns2->len == 4) { |
| 461 | msgb_put_u8(msg, 0x83); /* DNS2 Tag */ |
| 462 | msgb_put_u8(msg, 2 + dns2->len);/* DNS2 Length, incl. TL */ |
Harald Welte | bcab7fb | 2017-12-03 21:43:50 +0100 | [diff] [blame] | 463 | msgb_put_u32(msg, ntohl(dns2->v4.s_addr)); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* patch in length values */ |
| 467 | len_appended = msg->tail - start; |
| 468 | *len1 = len_appended - 3; |
| 469 | *len2 = len_appended - 3; |
| 470 | |
| 471 | return 0; |
| 472 | } |
| 473 | |
Harald Welte | 1ae9877 | 2017-08-09 20:28:52 +0200 | [diff] [blame] | 474 | /* process one PCO request from a MS/UE, putting together the proper responses */ |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 475 | static void process_pco(struct apn_ctx *apn, struct pdp_t *pdp) |
Harald Welte | 1ae9877 | 2017-08-09 20:28:52 +0200 | [diff] [blame] | 476 | { |
| 477 | struct msgb *msg = msgb_alloc(256, "PCO"); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 478 | unsigned int i; |
| 479 | |
| 480 | OSMO_ASSERT(msg); |
Harald Welte | 1ae9877 | 2017-08-09 20:28:52 +0200 | [diff] [blame] | 481 | msgb_put_u8(msg, 0x80); /* ext-bit + configuration protocol byte */ |
| 482 | |
| 483 | /* FIXME: also check if primary / secondary DNS was requested */ |
| 484 | if (pdp_has_v4(pdp) && pco_contains_proto(&pdp->pco_req, PCO_P_IPCP)) { |
| 485 | /* FIXME: properly implement this for IPCP */ |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 486 | build_ipcp_pco(msg, 0, &apn->v4.cfg.dns[0], &apn->v4.cfg.dns[1]); |
Harald Welte | 1ae9877 | 2017-08-09 20:28:52 +0200 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | if (pco_contains_proto(&pdp->pco_req, PCO_P_DNS_IPv6_ADDR)) { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 490 | for (i = 0; i < ARRAY_SIZE(apn->v6.cfg.dns); i++) { |
| 491 | struct in46_addr *i46a = &apn->v6.cfg.dns[i]; |
| 492 | if (i46a->len != 16) |
| 493 | continue; |
| 494 | msgb_t16lv_put(msg, PCO_P_DNS_IPv6_ADDR, i46a->len, i46a->v6.s6_addr); |
| 495 | } |
Harald Welte | 1ae9877 | 2017-08-09 20:28:52 +0200 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | if (pco_contains_proto(&pdp->pco_req, PCO_P_DNS_IPv4_ADDR)) { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 499 | for (i = 0; i < ARRAY_SIZE(apn->v4.cfg.dns); i++) { |
| 500 | struct in46_addr *i46a = &apn->v4.cfg.dns[i]; |
| 501 | if (i46a->len != 4) |
| 502 | continue; |
| 503 | msgb_t16lv_put(msg, PCO_P_DNS_IPv4_ADDR, i46a->len, (uint8_t *)&i46a->v4); |
| 504 | } |
Harald Welte | 1ae9877 | 2017-08-09 20:28:52 +0200 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | if (msgb_length(msg) > 1) { |
| 508 | memcpy(pdp->pco_neg.v, msgb_data(msg), msgb_length(msg)); |
| 509 | pdp->pco_neg.l = msgb_length(msg); |
| 510 | } else |
| 511 | pdp->pco_neg.l = 0; |
| 512 | |
| 513 | msgb_free(msg); |
| 514 | } |
| 515 | |
Harald Welte | 9d9d91b | 2017-10-14 16:22:16 +0200 | [diff] [blame] | 516 | static bool apn_supports_ipv4(const struct apn_ctx *apn) |
| 517 | { |
| 518 | if (apn->v4.cfg.static_prefix.addr.len || apn->v4.cfg.dynamic_prefix.addr.len) |
| 519 | return true; |
| 520 | return false; |
| 521 | } |
| 522 | |
| 523 | static bool apn_supports_ipv6(const struct apn_ctx *apn) |
| 524 | { |
| 525 | if (apn->v6.cfg.static_prefix.addr.len || apn->v6.cfg.dynamic_prefix.addr.len) |
| 526 | return true; |
| 527 | return false; |
| 528 | } |
| 529 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 530 | int create_context_ind(struct pdp_t *pdp) |
| 531 | { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 532 | static char name_buf[256]; |
| 533 | struct gsn_t *gsn = pdp->gsn; |
| 534 | struct ggsn_ctx *ggsn = gsn->priv; |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 535 | struct in46_addr addr[2]; |
| 536 | struct ippoolm_t *member = NULL; |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 537 | struct apn_ctx *apn; |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 538 | int rc, num_addr, i; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 539 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 540 | osmo_apn_to_str(name_buf, pdp->apn_req.v, pdp->apn_req.l); |
| 541 | |
| 542 | LOGPPDP(LOGL_DEBUG, pdp, "Processing create PDP context request for APN '%s'\n", name_buf); |
| 543 | |
| 544 | /* First find an exact APN name match */ |
| 545 | apn = ggsn_find_apn(ggsn, name_buf); |
Harald Welte | 2e84d2c | 2017-10-01 13:36:52 +0800 | [diff] [blame] | 546 | /* ignore if the APN has not been started */ |
Pau Espin Pedrol | 958256f | 2017-10-11 20:32:55 +0200 | [diff] [blame] | 547 | if (apn && !apn->started) |
Harald Welte | 2e84d2c | 2017-10-01 13:36:52 +0800 | [diff] [blame] | 548 | apn = NULL; |
Harald Welte | b16c46b | 2017-10-01 18:28:18 +0800 | [diff] [blame] | 549 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 550 | /* then try default (if any) */ |
| 551 | if (!apn) |
| 552 | apn = ggsn->cfg.default_apn; |
Harald Welte | b16c46b | 2017-10-01 18:28:18 +0800 | [diff] [blame] | 553 | /* ignore if the APN has not been started */ |
Pau Espin Pedrol | 958256f | 2017-10-11 20:32:55 +0200 | [diff] [blame] | 554 | if (apn && !apn->started) |
Harald Welte | b16c46b | 2017-10-01 18:28:18 +0800 | [diff] [blame] | 555 | apn = NULL; |
| 556 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 557 | if (!apn) { |
| 558 | /* no APN found for what user requested */ |
| 559 | LOGPPDP(LOGL_NOTICE, pdp, "Unknown APN '%s', rejecting\n", name_buf); |
| 560 | gtp_create_context_resp(gsn, pdp, GTPCAUSE_MISSING_APN); |
| 561 | return 0; |
| 562 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 563 | |
Harald Welte | d9d8862 | 2017-08-04 00:22:35 +0200 | [diff] [blame] | 564 | /* FIXME: we manually force all context requests to dynamic here! */ |
| 565 | if (pdp->eua.l > 2) |
| 566 | pdp->eua.l = 2; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 567 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 568 | memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_req0)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 569 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 570 | memcpy(pdp->qos_neg.v, pdp->qos_req.v, pdp->qos_req.l); /* TODO */ |
| 571 | pdp->qos_neg.l = pdp->qos_req.l; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 572 | |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 573 | memset(addr, 0, sizeof(addr)); |
| 574 | if ((num_addr = in46a_from_eua(&pdp->eua, addr)) < 0) { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 575 | LOGPPDP(LOGL_ERROR, pdp, "Cannot decode EUA from MS/SGSN: %s\n", |
Harald Welte | d1bf1e1 | 2017-08-03 00:00:23 +0200 | [diff] [blame] | 576 | osmo_hexdump(pdp->eua.v, pdp->eua.l)); |
| 577 | gtp_create_context_resp(gsn, pdp, GTPCAUSE_UNKNOWN_PDP); |
| 578 | return 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 579 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 580 | |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 581 | /* Allocate dynamic addresses from the pool */ |
| 582 | for (i = 0; i < num_addr; i++) { |
| 583 | if (addr[i].len == sizeof(struct in_addr)) { |
| 584 | /* does this APN actually have an IPv4 pool? */ |
| 585 | if (!apn_supports_ipv4(apn)) |
| 586 | goto err_wrong_af; |
Harald Welte | 9d9d91b | 2017-10-14 16:22:16 +0200 | [diff] [blame] | 587 | |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 588 | rc = ippool_newip(apn->v4.pool, &member, &addr[i], 0); |
| 589 | if (rc < 0) |
| 590 | goto err_pool_full; |
| 591 | /* copy back */ |
| 592 | memcpy(&addr[i].v4.s_addr, &member->addr.v4, 4); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 593 | |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 594 | } else if (addr[i].len == sizeof(struct in6_addr)) { |
| 595 | |
| 596 | /* does this APN actually have an IPv6 pool? */ |
| 597 | if (!apn_supports_ipv6(apn)) |
| 598 | goto err_wrong_af; |
| 599 | |
| 600 | rc = ippool_newip(apn->v6.pool, &member, &addr[i], 0); |
| 601 | if (rc < 0) |
| 602 | goto err_pool_full; |
| 603 | |
| 604 | /* IPv6 doesn't really send the real/allocated address at this point, but just |
| 605 | * the link-identifier which the MS shall use for router solicitation */ |
| 606 | /* initialize upper 64 bits to prefix, they are discarded by MS anyway */ |
| 607 | memcpy(addr[i].v6.s6_addr, &member->addr.v6, 8); |
| 608 | /* use allocated 64bit prefix as lower 64bit, used as link id by MS */ |
| 609 | memcpy(addr[i].v6.s6_addr+8, &member->addr.v6, 8); |
| 610 | } else |
| 611 | OSMO_ASSERT(0); |
| 612 | |
| 613 | pdp->peer[i] = member; |
| 614 | member->peer = pdp; |
| 615 | } |
| 616 | |
| 617 | in46a_to_eua(addr, num_addr, &pdp->eua); |
| 618 | |
| 619 | if (apn_supports_ipv4(apn)) { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 620 | /* TODO: In IPv6, EUA doesn't contain the actual IP addr/prefix! */ |
Harald Welte | 698a233 | 2017-11-08 15:09:58 +0900 | [diff] [blame] | 621 | if (gtp_kernel_tunnel_add(pdp, apn->tun.cfg.dev_name) < 0) { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 622 | LOGPPDP(LOGL_ERROR, pdp, "Cannot add tunnel to kernel: %s\n", strerror(errno)); |
| 623 | gtp_create_context_resp(gsn, pdp, GTPCAUSE_SYS_FAIL); |
| 624 | return 0; |
| 625 | } |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 626 | } |
Harald Welte | 9d9d91b | 2017-10-14 16:22:16 +0200 | [diff] [blame] | 627 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 628 | pdp->ipif = apn->tun.tun; /* TODO */ |
Harald Welte | 698a233 | 2017-11-08 15:09:58 +0900 | [diff] [blame] | 629 | pdp->priv = apn; |
Max | 3142d8d | 2017-05-04 17:45:10 +0200 | [diff] [blame] | 630 | |
| 631 | if (!send_trap(gsn, pdp, member, "imsi-ass-ip")) { /* TRAP with IP assignment */ |
Max | 727417d | 2016-08-02 17:10:38 +0200 | [diff] [blame] | 632 | gtp_create_context_resp(gsn, pdp, GTPCAUSE_NO_RESOURCES); |
| 633 | return 0; |
| 634 | } |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 635 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 636 | process_pco(apn, pdp); |
Harald Welte | 1ae9877 | 2017-08-09 20:28:52 +0200 | [diff] [blame] | 637 | |
Harald Welte | 93fed3b | 2017-09-24 11:43:17 +0800 | [diff] [blame] | 638 | /* Transmit G-PDU sequence numbers (only) if configured in APN */ |
| 639 | pdp->tx_gpdu_seq = apn->cfg.tx_gpdu_seq; |
| 640 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 641 | LOGPPDP(LOGL_INFO, pdp, "Successful PDP Context Creation: APN=%s(%s), TEIC=%u, IP=%s\n", |
| 642 | name_buf, apn->cfg.name, pdp->teic_own, in46a_ntoa(&member->addr)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 643 | gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ); |
| 644 | return 0; /* Success */ |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 645 | |
| 646 | err_pool_full: |
| 647 | LOGPPDP(LOGL_ERROR, pdp, "Cannot allocate IP address from pool (full!)\n"); |
| 648 | gtp_create_context_resp(gsn, pdp, -rc); |
| 649 | return 0; /* Already in use, or no more available */ |
Harald Welte | 9d9d91b | 2017-10-14 16:22:16 +0200 | [diff] [blame] | 650 | |
| 651 | err_wrong_af: |
| 652 | LOGPPDP(LOGL_ERROR, pdp, "APN doesn't support requested EUA / AF type\n"); |
| 653 | gtp_create_context_resp(gsn, pdp, GTPCAUSE_UNKNOWN_PDP); |
| 654 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 657 | /* Internet-originated IP packet, needs to be sent via GTP towards MS */ |
| 658 | static int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 659 | { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 660 | struct apn_ctx *apn = tun->priv; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 661 | struct ippoolm_t *ipm; |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 662 | struct in46_addr dst; |
Harald Welte | 63ebccd | 2017-08-02 21:10:09 +0200 | [diff] [blame] | 663 | struct iphdr *iph = (struct iphdr *)pack; |
Harald Welte | a0d281d | 2017-08-02 21:48:16 +0200 | [diff] [blame] | 664 | struct ip6_hdr *ip6h = (struct ip6_hdr *)pack; |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 665 | struct ippool_t *pool; |
jjako | c6762cf | 2004-04-28 14:52:58 +0000 | [diff] [blame] | 666 | |
Harald Welte | 63ebccd | 2017-08-02 21:10:09 +0200 | [diff] [blame] | 667 | if (iph->version == 4) { |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 668 | if (len < sizeof(*iph) || len < 4*iph->ihl) |
| 669 | return -1; |
| 670 | dst.len = 4; |
Harald Welte | 63ebccd | 2017-08-02 21:10:09 +0200 | [diff] [blame] | 671 | dst.v4.s_addr = iph->daddr; |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 672 | pool = apn->v4.pool; |
Harald Welte | a0d281d | 2017-08-02 21:48:16 +0200 | [diff] [blame] | 673 | } else if (iph->version == 6) { |
Harald Welte | d4d6e09 | 2017-08-08 18:10:43 +0200 | [diff] [blame] | 674 | /* Due to the fact that 3GPP requires an allocation of a |
| 675 | * /64 prefix to each MS, we must instruct |
| 676 | * ippool_getip() below to match only the leading /64 |
| 677 | * prefix, i.e. the first 8 bytes of the address */ |
| 678 | dst.len = 8; |
Harald Welte | a0d281d | 2017-08-02 21:48:16 +0200 | [diff] [blame] | 679 | dst.v6 = ip6h->ip6_dst; |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 680 | pool = apn->v6.pool; |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 681 | } else { |
Pau Espin Pedrol | 55d639f | 2017-12-04 13:17:07 +0100 | [diff] [blame] | 682 | LOGP(DTUN, LOGL_NOTICE, "non-IPv%u packet received from tun\n", iph->version); |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 683 | return -1; |
| 684 | } |
jjako | c6762cf | 2004-04-28 14:52:58 +0000 | [diff] [blame] | 685 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 686 | /* IPv6 packet but no IPv6 pool, or IPv4 packet with no IPv4 pool */ |
| 687 | if (!pool) |
| 688 | return 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 689 | |
Max | 427699e | 2017-12-05 16:30:37 +0100 | [diff] [blame] | 690 | DEBUGP(DTUN, "Received packet for APN(%s) from tun %s", apn->cfg.name, tun->devname); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 691 | |
| 692 | if (ippool_getip(pool, &ipm, &dst)) { |
Max | 427699e | 2017-12-05 16:30:37 +0100 | [diff] [blame] | 693 | DEBUGPC(DTUN, " with no PDP contex!!\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 694 | return 0; |
| 695 | } |
Max | 427699e | 2017-12-05 16:30:37 +0100 | [diff] [blame] | 696 | DEBUGPC(DTUN, "\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 697 | |
| 698 | if (ipm->peer) /* Check if a peer protocol is defined */ |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 699 | gtp_data_req(apn->ggsn->gsn, (struct pdp_t *)ipm->peer, pack, len); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 700 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Harald Welte | d46bcd2 | 2017-08-08 23:27:22 +0200 | [diff] [blame] | 703 | /* RFC3307 link-local scope multicast address */ |
| 704 | static const struct in6_addr all_router_mcast_addr = { |
| 705 | .s6_addr = { 0xff,0x02,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,2 } |
| 706 | }; |
| 707 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 708 | /* MS-originated GTP1-U packet, needs to be sent via TUN device */ |
| 709 | static int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 710 | { |
Harald Welte | d46bcd2 | 2017-08-08 23:27:22 +0200 | [diff] [blame] | 711 | struct iphdr *iph = (struct iphdr *)pack; |
| 712 | struct ip6_hdr *ip6h = (struct ip6_hdr *)pack; |
Harald Welte | f85fe97 | 2017-09-24 20:00:34 +0800 | [diff] [blame] | 713 | struct tun_t *tun = (struct tun_t *)pdp->ipif; |
| 714 | struct apn_ctx *apn = tun->priv; |
| 715 | |
| 716 | OSMO_ASSERT(tun); |
| 717 | OSMO_ASSERT(apn); |
Harald Welte | d46bcd2 | 2017-08-08 23:27:22 +0200 | [diff] [blame] | 718 | |
Max | 427699e | 2017-12-05 16:30:37 +0100 | [diff] [blame] | 719 | LOGPPDP(LOGL_DEBUG, pdp, "Packet received on APN(%s): forwarding to tun %s\n", apn->cfg.name, tun->devname); |
Harald Welte | d46bcd2 | 2017-08-08 23:27:22 +0200 | [diff] [blame] | 720 | |
| 721 | switch (iph->version) { |
| 722 | case 6: |
| 723 | /* daddr: all-routers multicast addr */ |
| 724 | if (IN6_ARE_ADDR_EQUAL(&ip6h->ip6_dst, &all_router_mcast_addr)) |
Harald Welte | f85fe97 | 2017-09-24 20:00:34 +0800 | [diff] [blame] | 725 | return handle_router_mcast(pdp->gsn, pdp, &apn->v6_lladdr, pack, len); |
Harald Welte | d46bcd2 | 2017-08-08 23:27:22 +0200 | [diff] [blame] | 726 | break; |
| 727 | case 4: |
| 728 | break; |
| 729 | default: |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 730 | LOGPPDP(LOGL_ERROR, pdp, "Packet from MS is neither IPv4 nor IPv6: %s\n", |
| 731 | osmo_hexdump(pack, len)); |
Harald Welte | d46bcd2 | 2017-08-08 23:27:22 +0200 | [diff] [blame] | 732 | return -1; |
| 733 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 734 | return tun_encaps((struct tun_t *)pdp->ipif, pack, len); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Harald Welte | 632e843 | 2017-09-05 18:12:14 +0200 | [diff] [blame] | 737 | static char *config_file = "osmo-ggsn.cfg"; |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 738 | |
| 739 | /* callback for tun device osmocom select loop integration */ |
| 740 | static int ggsn_tun_fd_cb(struct osmo_fd *fd, unsigned int what) |
| 741 | { |
| 742 | struct apn_ctx *apn = fd->data; |
| 743 | |
| 744 | OSMO_ASSERT(what & BSC_FD_READ); |
| 745 | |
| 746 | return tun_decaps(apn->tun.tun); |
| 747 | } |
| 748 | |
| 749 | /* callback for libgtp osmocom select loop integration */ |
| 750 | static int ggsn_gtp_fd_cb(struct osmo_fd *fd, unsigned int what) |
| 751 | { |
| 752 | struct ggsn_ctx *ggsn = fd->data; |
| 753 | int rc; |
| 754 | |
| 755 | OSMO_ASSERT(what & BSC_FD_READ); |
| 756 | |
| 757 | switch (fd->priv_nr) { |
| 758 | case 0: |
| 759 | rc = gtp_decaps0(ggsn->gsn); |
| 760 | break; |
| 761 | case 1: |
| 762 | rc = gtp_decaps1c(ggsn->gsn); |
| 763 | break; |
| 764 | case 2: |
| 765 | rc = gtp_decaps1u(ggsn->gsn); |
| 766 | break; |
| 767 | default: |
| 768 | OSMO_ASSERT(0); |
| 769 | break; |
| 770 | } |
| 771 | return rc; |
| 772 | } |
| 773 | |
| 774 | static void ggsn_gtp_tmr_start(struct ggsn_ctx *ggsn) |
| 775 | { |
| 776 | struct timeval next; |
| 777 | |
| 778 | /* Retrieve next retransmission as timeval */ |
| 779 | gtp_retranstimeout(ggsn->gsn, &next); |
| 780 | |
| 781 | /* re-schedule the timer */ |
| 782 | osmo_timer_schedule(&ggsn->gtp_timer, next.tv_sec, next.tv_usec/1000); |
| 783 | } |
| 784 | |
| 785 | /* timer callback for libgtp retransmission and ping */ |
| 786 | static void ggsn_gtp_tmr_cb(void *data) |
| 787 | { |
| 788 | struct ggsn_ctx *ggsn = data; |
| 789 | |
| 790 | /* do all the retransmissions as needed */ |
| 791 | gtp_retrans(ggsn->gsn); |
| 792 | |
| 793 | ggsn_gtp_tmr_start(ggsn); |
| 794 | } |
| 795 | |
| 796 | /* To exit gracefully. Used with GCC compilation flag -pg and gprof */ |
| 797 | static void signal_handler(int s) |
| 798 | { |
| 799 | LOGP(DGGSN, LOGL_NOTICE, "signal %d received\n", s); |
| 800 | switch (s) { |
| 801 | case SIGINT: |
Harald Welte | e804947 | 2017-08-20 12:44:21 +0200 | [diff] [blame] | 802 | case SIGTERM: |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 803 | LOGP(DGGSN, LOGL_NOTICE, "SIGINT received, shutting down\n"); |
| 804 | end = 1; |
| 805 | break; |
| 806 | case SIGABRT: |
| 807 | case SIGUSR1: |
| 808 | talloc_report(tall_vty_ctx, stderr); |
| 809 | talloc_report_full(tall_ggsn_ctx, stderr); |
| 810 | break; |
| 811 | case SIGUSR2: |
| 812 | talloc_report_full(tall_vty_ctx, stderr); |
| 813 | break; |
| 814 | default: |
| 815 | break; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | |
| 820 | /* Start a given GGSN */ |
| 821 | int ggsn_start(struct ggsn_ctx *ggsn) |
| 822 | { |
| 823 | struct apn_ctx *apn; |
| 824 | int rc; |
| 825 | |
| 826 | if (ggsn->started) |
| 827 | return 0; |
| 828 | |
| 829 | LOGPGGSN(LOGL_INFO, ggsn, "Starting GGSN\n"); |
| 830 | |
| 831 | /* Start libgtp listener */ |
| 832 | if (gtp_new(&ggsn->gsn, ggsn->cfg.state_dir, &ggsn->cfg.listen_addr.v4, GTP_MODE_GGSN)) { |
| 833 | LOGPGGSN(LOGL_ERROR, ggsn, "Failed to create GTP: %s\n", strerror(errno)); |
| 834 | return -1; |
| 835 | } |
| 836 | ggsn->gsn->priv = ggsn; |
| 837 | |
Harald Welte | 9814677 | 2017-09-05 17:41:20 +0200 | [diff] [blame] | 838 | /* patch in different addresses to use (in case we're behind NAT, the listen |
| 839 | * address is different from what we advertise externally) */ |
| 840 | if (ggsn->cfg.gtpc_addr.v4.s_addr) |
| 841 | ggsn->gsn->gsnc = ggsn->cfg.gtpc_addr.v4; |
| 842 | |
| 843 | if (ggsn->cfg.gtpu_addr.v4.s_addr) |
| 844 | ggsn->gsn->gsnu = ggsn->cfg.gtpu_addr.v4; |
| 845 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 846 | /* Register File Descriptors */ |
| 847 | osmo_fd_setup(&ggsn->gtp_fd0, ggsn->gsn->fd0, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 0); |
| 848 | rc = osmo_fd_register(&ggsn->gtp_fd0); |
| 849 | OSMO_ASSERT(rc == 0); |
| 850 | |
| 851 | osmo_fd_setup(&ggsn->gtp_fd1c, ggsn->gsn->fd1c, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 1); |
| 852 | rc = osmo_fd_register(&ggsn->gtp_fd1c); |
| 853 | OSMO_ASSERT(rc == 0); |
| 854 | |
| 855 | osmo_fd_setup(&ggsn->gtp_fd1u, ggsn->gsn->fd1u, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 2); |
| 856 | rc = osmo_fd_register(&ggsn->gtp_fd1u); |
| 857 | OSMO_ASSERT(rc == 0); |
| 858 | |
| 859 | /* Start GTP re-transmission timer */ |
| 860 | osmo_timer_setup(&ggsn->gtp_timer, ggsn_gtp_tmr_cb, ggsn); |
| 861 | |
| 862 | gtp_set_cb_data_ind(ggsn->gsn, encaps_tun); |
| 863 | gtp_set_cb_delete_context(ggsn->gsn, delete_context); |
| 864 | gtp_set_cb_create_context_ind(ggsn->gsn, create_context_ind); |
| 865 | |
| 866 | LOGPGGSN(LOGL_NOTICE, ggsn, "Successfully started\n"); |
| 867 | ggsn->started = true; |
| 868 | |
| 869 | llist_for_each_entry(apn, &ggsn->apn_list, list) |
| 870 | apn_start(apn); |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | /* Stop a given GGSN */ |
| 876 | int ggsn_stop(struct ggsn_ctx *ggsn) |
| 877 | { |
| 878 | struct apn_ctx *apn; |
| 879 | |
| 880 | if (!ggsn->started) |
| 881 | return 0; |
| 882 | |
| 883 | /* iterate over all APNs and stop them */ |
| 884 | llist_for_each_entry(apn, &ggsn->apn_list, list) |
| 885 | apn_stop(apn, true); |
| 886 | |
| 887 | osmo_timer_del(&ggsn->gtp_timer); |
| 888 | |
| 889 | osmo_fd_unregister(&ggsn->gtp_fd1u); |
| 890 | osmo_fd_unregister(&ggsn->gtp_fd1c); |
| 891 | osmo_fd_unregister(&ggsn->gtp_fd0); |
| 892 | |
| 893 | if (ggsn->gsn) { |
| 894 | gtp_free(ggsn->gsn); |
| 895 | ggsn->gsn = NULL; |
| 896 | } |
| 897 | |
| 898 | ggsn->started = false; |
| 899 | return 0; |
| 900 | } |
| 901 | |
| 902 | static void print_usage() |
| 903 | { |
| 904 | printf("Usage: osmo-ggsn [-h] [-D] [-c configfile] [-V]\n"); |
| 905 | } |
| 906 | |
| 907 | static void print_help() |
| 908 | { |
| 909 | printf( " Some useful help...\n" |
| 910 | " -h --help This help text\n" |
| 911 | " -D --daemonize Fork the process into a background daemon\n" |
| 912 | " -c --config-file filename The config file to use\n" |
| 913 | " -V --version Print the version of OsmoGGSN\n" |
| 914 | ); |
| 915 | } |
| 916 | |
| 917 | static void handle_options(int argc, char **argv) |
| 918 | { |
| 919 | while (1) { |
| 920 | int option_index = 0, c; |
| 921 | static struct option long_options[] = { |
| 922 | { "help", 0, 0, 'h' }, |
| 923 | { "daemonize", 0, 0, 'D' }, |
| 924 | { "config-file", 1, 0, 'c' }, |
| 925 | { "version", 0, 0, 'V' }, |
| 926 | { 0, 0, 0, 0 } |
| 927 | }; |
| 928 | |
| 929 | c = getopt_long(argc, argv, "hdc:V", long_options, &option_index); |
| 930 | if (c == -1) |
| 931 | break; |
| 932 | |
| 933 | switch (c) { |
| 934 | case 'h': |
| 935 | print_usage(); |
| 936 | print_help(); |
| 937 | exit(0); |
| 938 | case 'D': |
| 939 | daemonize = 1; |
| 940 | break; |
| 941 | case 'c': |
| 942 | config_file = optarg; |
| 943 | break; |
| 944 | case 'V': |
| 945 | print_version(1); |
| 946 | exit(0); |
| 947 | break; |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 952 | int main(int argc, char **argv) |
| 953 | { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 954 | struct ggsn_ctx *ggsn; |
| 955 | int rc; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 956 | |
Harald Welte | 632e843 | 2017-09-05 18:12:14 +0200 | [diff] [blame] | 957 | tall_ggsn_ctx = talloc_named_const(NULL, 0, "OsmoGGSN"); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 958 | msgb_talloc_ctx_init(tall_ggsn_ctx, 0); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 959 | |
Harald Welte | e804947 | 2017-08-20 12:44:21 +0200 | [diff] [blame] | 960 | /* Handle keyboard interrupt SIGINT */ |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 961 | signal(SIGINT, &signal_handler); |
Harald Welte | e804947 | 2017-08-20 12:44:21 +0200 | [diff] [blame] | 962 | signal(SIGTERM, &signal_handler); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 963 | signal(SIGABRT, &signal_handler); |
| 964 | signal(SIGUSR1, &signal_handler); |
| 965 | signal(SIGUSR2, &signal_handler); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 966 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 967 | osmo_init_ignore_signals(); |
Holger Hans Peter Freyther | 9c7fd8e | 2014-12-04 16:32:37 +0100 | [diff] [blame] | 968 | osmo_init_logging(&log_info); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 969 | osmo_stats_init(tall_ggsn_ctx); |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 970 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 971 | vty_init(&g_vty_info); |
| 972 | logging_vty_add_cmds(NULL); |
| 973 | osmo_stats_vty_add_cmds(&log_info); |
| 974 | ggsn_vty_init(); |
| 975 | ctrl_vty_init(tall_ggsn_ctx); |
| 976 | |
| 977 | handle_options(argc, argv); |
| 978 | |
| 979 | rate_ctr_init(tall_ggsn_ctx); |
| 980 | |
| 981 | rc = vty_read_config_file(config_file, NULL); |
| 982 | if (rc < 0) { |
| 983 | fprintf(stderr, "Failed to open config file: '%s'\n", config_file); |
| 984 | exit(2); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 985 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 986 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 987 | rc = telnet_init_dynif(tall_ggsn_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_GGSN); |
| 988 | if (rc < 0) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 989 | exit(1); |
Holger Hans Peter Freyther | 9c0ff4f | 2014-03-23 10:07:26 +0100 | [diff] [blame] | 990 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 991 | g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_GGSN, NULL); |
| 992 | if (!g_ctrlh) { |
| 993 | LOGP(DGGSN, LOGL_ERROR, "Failed to create CTRL interface.\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 994 | exit(1); |
| 995 | } |
jjako | 88c2216 | 2003-07-06 19:33:18 +0000 | [diff] [blame] | 996 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 997 | if (daemonize) { |
| 998 | rc = osmo_daemonize(); |
| 999 | if (rc < 0) { |
| 1000 | perror("Error during daemonize"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1001 | exit(1); |
| 1002 | } |
| 1003 | } |
jjako | 1d3db97 | 2004-01-16 09:56:56 +0000 | [diff] [blame] | 1004 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 1005 | #if 0 |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1006 | /* qos */ |
| 1007 | qos.l = 3; |
| 1008 | qos.v[2] = (args_info.qos_arg) & 0xff; |
| 1009 | qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff; |
| 1010 | qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff; |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 1011 | #endif |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 1012 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 1013 | /* Main select loop */ |
| 1014 | while (!end) { |
| 1015 | osmo_select_main(0); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1016 | } |
jjako | e014978 | 2003-07-06 17:07:04 +0000 | [diff] [blame] | 1017 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 1018 | llist_for_each_entry(ggsn, &g_ggsn_list, list) |
| 1019 | ggsn_stop(ggsn); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1020 | |
| 1021 | return 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1022 | } |