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