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 | 7bdc80d | 2019-04-11 18:47:59 +0200 | [diff] [blame] | 4 | * Copyright (C) 2017-2019 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/timer.h> |
Max | 727417d | 2016-08-02 17:10:38 +0200 | [diff] [blame] | 46 | #include <osmocom/ctrl/control_if.h> |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 47 | #include <osmocom/gsm/apn.h> |
Max | 727417d | 2016-08-02 17:10:38 +0200 | [diff] [blame] | 48 | |
Emmanuel Bretelle | 2a10368 | 2010-09-07 17:01:20 +0200 | [diff] [blame] | 49 | #include "../lib/tun.h" |
| 50 | #include "../lib/ippool.h" |
| 51 | #include "../lib/syserr.h" |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 52 | #include "../lib/in46_addr.h" |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 53 | #include "../lib/gtp-kernel.h" |
Pau Espin Pedrol | f612ffe | 2019-08-20 13:24:55 +0200 | [diff] [blame] | 54 | #include "../lib/util.h" |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 55 | #include "../gtp/pdp.h" |
| 56 | #include "../gtp/gtp.h" |
Pau Espin Pedrol | 1c8ae66 | 2020-04-09 18:51:05 +0200 | [diff] [blame] | 57 | #include "../lib/icmpv6.h" |
Pau Espin Pedrol | f7884e8 | 2019-08-20 12:06:13 +0200 | [diff] [blame] | 58 | #include "pco.h" |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 59 | #include "ggsn.h" |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 60 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 61 | static int ggsn_tun_fd_cb(struct osmo_fd *fd, unsigned int what); |
| 62 | static int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len); |
| 63 | |
Pau Espin Pedrol | f5fbb41 | 2019-08-21 18:49:44 +0200 | [diff] [blame] | 64 | void ggsn_close_one_pdp(struct pdp_t *pdp) |
| 65 | { |
| 66 | LOGPPDP(LOGL_DEBUG, pdp, "Sending DELETE PDP CTX due to shutdown\n"); |
| 67 | gtp_delete_context_req2(pdp->gsn, pdp, NULL, 1); |
| 68 | /* We have nothing more to do with pdp ctx, free it. Upon cb_delete_context |
| 69 | called during this call we'll clean up ggsn related stuff attached to this |
| 70 | pdp context. After this call, ippool member is cleared so |
| 71 | data is no longer valid and should not be accessed anymore. */ |
| 72 | gtp_freepdp_teardown(pdp->gsn, pdp); |
| 73 | } |
| 74 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 75 | static void pool_close_all_pdp(struct ippool_t *pool) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 76 | { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 77 | unsigned int i; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 78 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 79 | if (!pool) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 80 | return; |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 81 | |
| 82 | for (i = 0; i < pool->listsize; i++) { |
| 83 | struct ippoolm_t *member = &pool->member[i]; |
| 84 | struct pdp_t *pdp; |
| 85 | |
| 86 | if (!member->inuse) |
| 87 | continue; |
| 88 | pdp = member->peer; |
| 89 | if (!pdp) |
| 90 | continue; |
Pau Espin Pedrol | f5fbb41 | 2019-08-21 18:49:44 +0200 | [diff] [blame] | 91 | ggsn_close_one_pdp(pdp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 92 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Pau Espin Pedrol | 72ab4bc | 2019-05-29 19:08:26 +0200 | [diff] [blame] | 95 | int apn_stop(struct apn_ctx *apn) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 96 | { |
Pau Espin Pedrol | 72ab4bc | 2019-05-29 19:08:26 +0200 | [diff] [blame] | 97 | LOGPAPN(LOGL_NOTICE, apn, "Stopping\n"); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 98 | /* check if pools have any active PDP contexts and bail out */ |
| 99 | pool_close_all_pdp(apn->v4.pool); |
| 100 | pool_close_all_pdp(apn->v6.pool); |
| 101 | |
| 102 | /* shutdown whatever old state might be left */ |
| 103 | if (apn->tun.tun) { |
| 104 | /* run ip-down script */ |
| 105 | if (apn->tun.cfg.ipdown_script) { |
| 106 | LOGPAPN( LOGL_INFO, apn, "Running %s\n", apn->tun.cfg.ipdown_script); |
| 107 | tun_runscript(apn->tun.tun, apn->tun.cfg.ipdown_script); |
| 108 | } |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 109 | if (apn->cfg.gtpu_mode == APN_GTPU_MODE_TUN) { |
| 110 | /* release tun device */ |
| 111 | LOGPAPN(LOGL_INFO, apn, "Closing TUN device %s\n", apn->tun.tun->devname); |
| 112 | osmo_fd_unregister(&apn->tun.fd); |
| 113 | } |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 114 | tun_free(apn->tun.tun); |
| 115 | apn->tun.tun = NULL; |
| 116 | } |
| 117 | |
| 118 | if (apn->v4.pool) { |
| 119 | LOGPAPN(LOGL_INFO, apn, "Releasing IPv4 pool\n"); |
| 120 | ippool_free(apn->v4.pool); |
| 121 | apn->v4.pool = NULL; |
| 122 | } |
| 123 | if (apn->v6.pool) { |
| 124 | LOGPAPN(LOGL_INFO, apn, "Releasing IPv6 pool\n"); |
| 125 | ippool_free(apn->v6.pool); |
| 126 | apn->v6.pool = NULL; |
| 127 | } |
| 128 | |
| 129 | apn->started = false; |
| 130 | return 0; |
| 131 | } |
| 132 | |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 133 | |
Harald Welte | f55a039 | 2017-11-08 14:33:55 +0900 | [diff] [blame] | 134 | 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] | 135 | { |
| 136 | |
| 137 | int flags, len, len2, i; |
| 138 | |
Harald Welte | e2a1de5 | 2017-11-08 15:24:07 +0900 | [diff] [blame] | 139 | *blacklist = NULL; |
| 140 | |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 141 | if (ipv6) |
| 142 | flags = IP_TYPE_IPv6_NONLINK; |
| 143 | else |
| 144 | flags = IP_TYPE_IPv4; |
| 145 | |
| 146 | while (1) { |
Harald Welte | e2a1de5 | 2017-11-08 15:24:07 +0900 | [diff] [blame] | 147 | 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] | 148 | if (len < 1) |
| 149 | return len; |
| 150 | |
| 151 | *blacklist = talloc_zero_size(apn, len * sizeof(struct in46_prefix)); |
Harald Welte | e2a1de5 | 2017-11-08 15:24:07 +0900 | [diff] [blame] | 152 | 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] | 153 | if (len2 < 1) { |
| 154 | talloc_free(*blacklist); |
Harald Welte | e2a1de5 | 2017-11-08 15:24:07 +0900 | [diff] [blame] | 155 | *blacklist = NULL; |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 156 | return len2; |
| 157 | } |
| 158 | |
Harald Welte | e2a1de5 | 2017-11-08 15:24:07 +0900 | [diff] [blame] | 159 | if (len2 > len) { /* iface was added between 2 calls, repeat operation */ |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 160 | talloc_free(*blacklist); |
Harald Welte | e2a1de5 | 2017-11-08 15:24:07 +0900 | [diff] [blame] | 161 | *blacklist = NULL; |
| 162 | } else |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 163 | break; |
| 164 | } |
| 165 | |
| 166 | for (i = 0; i < len2; i++) |
| 167 | LOGPAPN(LOGL_INFO, apn, "Blacklist tun IP %s\n", |
| 168 | in46p_ntoa(&(*blacklist)[i])); |
| 169 | |
| 170 | return len2; |
| 171 | } |
| 172 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 173 | /* actually start the APN with its current config */ |
| 174 | int apn_start(struct apn_ctx *apn) |
| 175 | { |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 176 | int ippool_flags = IPPOOL_NONETWORK | IPPOOL_NOBROADCAST; |
Pau Espin Pedrol | a037e59 | 2017-10-16 14:41:37 +0200 | [diff] [blame] | 177 | struct in46_prefix ipv6_tun_linklocal_ip; |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 178 | struct in46_prefix *blacklist; |
| 179 | int blacklist_size; |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 180 | struct gsn_t *gsn = apn->ggsn->gsn; |
Pau Espin Pedrol | bffc3f9 | 2017-12-14 11:19:10 +0100 | [diff] [blame] | 181 | int rc; |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 182 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 183 | if (apn->started) |
| 184 | return 0; |
| 185 | |
| 186 | LOGPAPN(LOGL_INFO, apn, "Starting\n"); |
| 187 | switch (apn->cfg.gtpu_mode) { |
| 188 | case APN_GTPU_MODE_TUN: |
| 189 | LOGPAPN(LOGL_INFO, apn, "Opening TUN device %s\n", apn->tun.cfg.dev_name); |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 190 | if (tun_new(&apn->tun.tun, apn->tun.cfg.dev_name, false, -1, -1)) { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 191 | LOGPAPN(LOGL_ERROR, apn, "Failed to configure tun device\n"); |
| 192 | return -1; |
| 193 | } |
| 194 | LOGPAPN(LOGL_INFO, apn, "Opened TUN device %s\n", apn->tun.tun->devname); |
| 195 | |
| 196 | /* Register with libosmcoore */ |
| 197 | osmo_fd_setup(&apn->tun.fd, apn->tun.tun->fd, BSC_FD_READ, ggsn_tun_fd_cb, apn, 0); |
| 198 | osmo_fd_register(&apn->tun.fd); |
| 199 | |
| 200 | /* Set TUN library callback */ |
| 201 | tun_set_cb_ind(apn->tun.tun, cb_tun_ind); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 202 | break; |
| 203 | case APN_GTPU_MODE_KERNEL_GTP: |
Harald Welte | 2fc2bc6 | 2017-11-08 15:50:53 +0900 | [diff] [blame] | 204 | 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] | 205 | if (apn->cfg.apn_type_mask & (APN_TYPE_IPv6|APN_TYPE_IPv4v6)) { |
| 206 | LOGPAPN(LOGL_ERROR, apn, "Kernel GTP currently supports only IPv4\n"); |
Pau Espin Pedrol | 72ab4bc | 2019-05-29 19:08:26 +0200 | [diff] [blame] | 207 | apn_stop(apn); |
Harald Welte | 490782d | 2017-11-08 14:09:51 +0900 | [diff] [blame] | 208 | return -1; |
| 209 | } |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 210 | if (gsn == NULL) { |
Harald Welte | 0757504 | 2018-02-14 01:04:04 +0100 | [diff] [blame] | 211 | /* skip bringing up the APN now if the GSN is not initialized yet. |
| 212 | * This happens during initial load of the config file, as the |
| 213 | * "no shutdown" in the ggsn node only happens after the "apn" nodes |
| 214 | * are brought up */ |
| 215 | LOGPAPN(LOGL_NOTICE, apn, "Skipping APN start\n"); |
| 216 | return 0; |
| 217 | } |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 218 | /* use GTP kernel module for data packet encapsulation */ |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 219 | if (tun_new(&apn->tun.tun, apn->tun.cfg.dev_name, true, gsn->fd0, gsn->fd1u)) { |
| 220 | LOGPAPN(LOGL_ERROR, apn, "Failed to configure Kernel GTP device\n"); |
Harald Welte | 490782d | 2017-11-08 14:09:51 +0900 | [diff] [blame] | 221 | return -1; |
| 222 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 223 | break; |
| 224 | default: |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 225 | LOGPAPN(LOGL_ERROR, apn, "Unknown GTPU Mode %d\n", apn->cfg.gtpu_mode); |
| 226 | return -1; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 227 | } |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 228 | |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 229 | /* common initialization below */ |
| 230 | |
| 231 | /* set back-pointer from TUN device to APN */ |
| 232 | apn->tun.tun->priv = apn; |
| 233 | |
| 234 | if (apn->v4.cfg.ifconfig_prefix.addr.len) { |
| 235 | LOGPAPN(LOGL_INFO, apn, "Setting tun IP address %s\n", |
| 236 | in46p_ntoa(&apn->v4.cfg.ifconfig_prefix)); |
| 237 | if (tun_addaddr(apn->tun.tun, &apn->v4.cfg.ifconfig_prefix.addr, NULL, |
| 238 | apn->v4.cfg.ifconfig_prefix.prefixlen)) { |
| 239 | LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv4 address %s: %s\n", |
| 240 | in46p_ntoa(&apn->v4.cfg.ifconfig_prefix), strerror(errno)); |
Pau Espin Pedrol | 72ab4bc | 2019-05-29 19:08:26 +0200 | [diff] [blame] | 241 | apn_stop(apn); |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 242 | return -1; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | if (apn->v6.cfg.ifconfig_prefix.addr.len) { |
| 247 | LOGPAPN(LOGL_INFO, apn, "Setting tun IPv6 address %s\n", |
| 248 | in46p_ntoa(&apn->v6.cfg.ifconfig_prefix)); |
| 249 | if (tun_addaddr(apn->tun.tun, &apn->v6.cfg.ifconfig_prefix.addr, NULL, |
| 250 | apn->v6.cfg.ifconfig_prefix.prefixlen)) { |
| 251 | LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv6 address %s: %s. " |
| 252 | "Ensure you have ipv6 support and not used the disable_ipv6 sysctl?\n", |
| 253 | in46p_ntoa(&apn->v6.cfg.ifconfig_prefix), strerror(errno)); |
Pau Espin Pedrol | 72ab4bc | 2019-05-29 19:08:26 +0200 | [diff] [blame] | 254 | apn_stop(apn); |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 255 | return -1; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | if (apn->v6.cfg.ll_prefix.addr.len) { |
| 260 | LOGPAPN(LOGL_INFO, apn, "Setting tun IPv6 link-local address %s\n", |
| 261 | in46p_ntoa(&apn->v6.cfg.ll_prefix)); |
| 262 | if (tun_addaddr(apn->tun.tun, &apn->v6.cfg.ll_prefix.addr, NULL, |
| 263 | apn->v6.cfg.ll_prefix.prefixlen)) { |
| 264 | LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv6 link-local address %s: %s. " |
| 265 | "Ensure you have ipv6 support and not used the disable_ipv6 sysctl?\n", |
| 266 | in46p_ntoa(&apn->v6.cfg.ll_prefix), strerror(errno)); |
Pau Espin Pedrol | 72ab4bc | 2019-05-29 19:08:26 +0200 | [diff] [blame] | 267 | apn_stop(apn); |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 268 | return -1; |
| 269 | } |
| 270 | apn->v6_lladdr = apn->v6.cfg.ll_prefix.addr.v6; |
| 271 | } |
| 272 | |
| 273 | if (apn->tun.cfg.ipup_script) { |
| 274 | LOGPAPN(LOGL_INFO, apn, "Running ip-up script %s\n", |
| 275 | apn->tun.cfg.ipup_script); |
| 276 | tun_runscript(apn->tun.tun, apn->tun.cfg.ipup_script); |
| 277 | } |
| 278 | |
| 279 | if (apn->cfg.apn_type_mask & (APN_TYPE_IPv6|APN_TYPE_IPv4v6) && |
| 280 | apn->v6.cfg.ll_prefix.addr.len == 0) { |
| 281 | rc = tun_ip_local_get(apn->tun.tun, &ipv6_tun_linklocal_ip, 1, IP_TYPE_IPv6_LINK); |
| 282 | if (rc < 1) { |
| 283 | LOGPAPN(LOGL_ERROR, apn, "Cannot obtain IPv6 link-local address of interface: %s\n", |
| 284 | rc ? strerror(errno) : "tun interface has no link-local IP assigned"); |
Pau Espin Pedrol | 72ab4bc | 2019-05-29 19:08:26 +0200 | [diff] [blame] | 285 | apn_stop(apn); |
Harald Welte | f228639 | 2018-04-25 19:02:31 +0200 | [diff] [blame] | 286 | return -1; |
| 287 | } |
| 288 | apn->v6_lladdr = ipv6_tun_linklocal_ip.addr.v6; |
| 289 | } |
| 290 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 291 | /* Create IPv4 pool */ |
| 292 | if (apn->v4.cfg.dynamic_prefix.addr.len) { |
| 293 | LOGPAPN(LOGL_INFO, apn, "Creating IPv4 pool %s\n", |
| 294 | in46p_ntoa(&apn->v4.cfg.dynamic_prefix)); |
Harald Welte | f55a039 | 2017-11-08 14:33:55 +0900 | [diff] [blame] | 295 | if ((blacklist_size = alloc_ippool_blacklist(apn, &blacklist, false)) < 0) |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 296 | LOGPAPN(LOGL_ERROR, apn, "Failed obtaining IPv4 tun IPs\n"); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 297 | if (ippool_new(&apn->v4.pool, &apn->v4.cfg.dynamic_prefix, |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 298 | &apn->v4.cfg.static_prefix, ippool_flags, |
| 299 | blacklist, blacklist_size)) { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 300 | LOGPAPN(LOGL_ERROR, apn, "Failed to create IPv4 pool\n"); |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 301 | talloc_free(blacklist); |
Pau Espin Pedrol | 72ab4bc | 2019-05-29 19:08:26 +0200 | [diff] [blame] | 302 | apn_stop(apn); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 303 | return -1; |
| 304 | } |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 305 | talloc_free(blacklist); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 306 | } |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 307 | |
| 308 | /* Create IPv6 pool */ |
| 309 | if (apn->v6.cfg.dynamic_prefix.addr.len) { |
| 310 | LOGPAPN(LOGL_INFO, apn, "Creating IPv6 pool %s\n", |
| 311 | in46p_ntoa(&apn->v6.cfg.dynamic_prefix)); |
Harald Welte | f55a039 | 2017-11-08 14:33:55 +0900 | [diff] [blame] | 312 | if ((blacklist_size = alloc_ippool_blacklist(apn, &blacklist, true)) < 0) |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 313 | LOGPAPN(LOGL_ERROR, apn, "Failed obtaining IPv6 tun IPs\n"); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 314 | if (ippool_new(&apn->v6.pool, &apn->v6.cfg.dynamic_prefix, |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 315 | &apn->v6.cfg.static_prefix, ippool_flags, |
| 316 | blacklist, blacklist_size)) { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 317 | LOGPAPN(LOGL_ERROR, apn, "Failed to create IPv6 pool\n"); |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 318 | talloc_free(blacklist); |
Pau Espin Pedrol | 72ab4bc | 2019-05-29 19:08:26 +0200 | [diff] [blame] | 319 | apn_stop(apn); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 320 | return -1; |
| 321 | } |
Pau Espin Pedrol | 859f9b0 | 2017-10-16 14:52:25 +0200 | [diff] [blame] | 322 | talloc_free(blacklist); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | LOGPAPN(LOGL_NOTICE, apn, "Successfully started\n"); |
| 326 | apn->started = true; |
| 327 | return 0; |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 328 | } |
jjako | 0141d20 | 2004-01-09 15:19:20 +0000 | [diff] [blame] | 329 | |
Max | 3142d8d | 2017-05-04 17:45:10 +0200 | [diff] [blame] | 330 | static bool send_trap(const struct gsn_t *gsn, const struct pdp_t *pdp, const struct ippoolm_t *member, const char *var) |
| 331 | { |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 332 | char addrbuf[256]; |
Max | 3142d8d | 2017-05-04 17:45:10 +0200 | [diff] [blame] | 333 | char val[NAMESIZE]; |
| 334 | |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 335 | const char *addrstr = in46a_ntop(&member->addr, addrbuf, sizeof(addrbuf)); |
| 336 | |
Harald Welte | b10ee08 | 2017-08-12 19:29:16 +0200 | [diff] [blame] | 337 | snprintf(val, sizeof(val), "%s,%s", imsi_gtp2str(&pdp->imsi), addrstr); |
Max | 3142d8d | 2017-05-04 17:45:10 +0200 | [diff] [blame] | 338 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 339 | if (ctrl_cmd_send_trap(g_ctrlh, var, val) < 0) { |
| 340 | LOGPPDP(LOGL_ERROR, pdp, "Failed to create and send TRAP %s\n", var); |
Max | 3142d8d | 2017-05-04 17:45:10 +0200 | [diff] [blame] | 341 | return false; |
| 342 | } |
| 343 | return true; |
| 344 | } |
| 345 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 346 | static int delete_context(struct pdp_t *pdp) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 347 | { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 348 | struct gsn_t *gsn = pdp->gsn; |
Pau Espin Pedrol | f5fbb41 | 2019-08-21 18:49:44 +0200 | [diff] [blame] | 349 | struct pdp_priv_t *pdp_priv = pdp->priv; |
| 350 | struct apn_ctx *apn; |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 351 | struct ippoolm_t *member; |
| 352 | int i; |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 353 | |
| 354 | LOGPPDP(LOGL_INFO, pdp, "Deleting PDP context\n"); |
Max | dbd7024 | 2016-10-14 13:38:05 +0200 | [diff] [blame] | 355 | |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 356 | for (i = 0; i < 2; i++) { |
| 357 | if (pdp->peer[i]) { |
| 358 | member = pdp->peer[i]; |
| 359 | send_trap(gsn, pdp, member, "imsi-rem-ip"); /* TRAP with IP removal */ |
| 360 | ippool_freeip(member->pool, member); |
Pau Espin Pedrol | 3eb05d2 | 2019-08-28 11:17:08 +0200 | [diff] [blame] | 361 | } else if (i == 0) { |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 362 | LOGPPDP(LOGL_ERROR, pdp, "Cannot find/free IP Pool member\n"); |
Pau Espin Pedrol | 3eb05d2 | 2019-08-28 11:17:08 +0200 | [diff] [blame] | 363 | } |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 364 | } |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 365 | |
Pau Espin Pedrol | f5fbb41 | 2019-08-21 18:49:44 +0200 | [diff] [blame] | 366 | if (!pdp_priv) { |
| 367 | LOGPPDP(LOGL_NOTICE, pdp, "Deleting PDP context: without private structure!\n"); |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | /* Remove from SGSN */ |
| 372 | sgsn_peer_remove_pdp_priv(pdp_priv); |
| 373 | |
| 374 | apn = pdp_priv->apn; |
Pau Espin Pedrol | d950134 | 2019-08-21 15:24:29 +0200 | [diff] [blame] | 375 | if (apn && apn->cfg.gtpu_mode == APN_GTPU_MODE_KERNEL_GTP) { |
Harald Welte | 546884d | 2018-04-25 21:13:06 +0200 | [diff] [blame] | 376 | if (gtp_kernel_tunnel_del(pdp, apn->tun.cfg.dev_name)) { |
| 377 | LOGPPDP(LOGL_ERROR, pdp, "Cannot delete tunnel from kernel:%s\n", |
| 378 | strerror(errno)); |
| 379 | } |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 380 | } |
| 381 | |
Pau Espin Pedrol | f5fbb41 | 2019-08-21 18:49:44 +0200 | [diff] [blame] | 382 | talloc_free(pdp_priv); |
| 383 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 384 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Harald Welte | 9d9d91b | 2017-10-14 16:22:16 +0200 | [diff] [blame] | 387 | static bool apn_supports_ipv4(const struct apn_ctx *apn) |
| 388 | { |
| 389 | if (apn->v4.cfg.static_prefix.addr.len || apn->v4.cfg.dynamic_prefix.addr.len) |
| 390 | return true; |
| 391 | return false; |
| 392 | } |
| 393 | |
| 394 | static bool apn_supports_ipv6(const struct apn_ctx *apn) |
| 395 | { |
| 396 | if (apn->v6.cfg.static_prefix.addr.len || apn->v6.cfg.dynamic_prefix.addr.len) |
| 397 | return true; |
| 398 | return false; |
| 399 | } |
| 400 | |
Pau Espin Pedrol | f5fbb41 | 2019-08-21 18:49:44 +0200 | [diff] [blame] | 401 | static struct sgsn_peer* ggsn_find_sgsn(struct ggsn_ctx *ggsn, struct in_addr *peer_addr) |
| 402 | { |
| 403 | struct sgsn_peer *sgsn; |
| 404 | |
| 405 | llist_for_each_entry(sgsn, &ggsn->sgsn_list, entry) { |
| 406 | if (memcmp(&sgsn->addr, peer_addr, sizeof(*peer_addr)) == 0) |
| 407 | return sgsn; |
| 408 | } |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | static struct sgsn_peer* ggsn_find_or_create_sgsn(struct ggsn_ctx *ggsn, struct pdp_t *pdp) |
| 413 | { |
| 414 | struct sgsn_peer *sgsn; |
| 415 | struct in_addr ia; |
| 416 | |
| 417 | if (gsna2in_addr(&ia, &pdp->gsnrc)) { |
| 418 | LOGPPDP(LOGL_ERROR, pdp, "Failed parsing gsnrc (len=%u) to discover SGSN\n", |
| 419 | pdp->gsnrc.l); |
| 420 | return NULL; |
| 421 | } |
| 422 | |
| 423 | if ((sgsn = ggsn_find_sgsn(ggsn, &ia))) |
| 424 | return sgsn; |
| 425 | |
| 426 | sgsn = sgsn_peer_allocate(ggsn, &ia, pdp->version); |
| 427 | llist_add(&sgsn->entry, &ggsn->sgsn_list); |
| 428 | return sgsn; |
| 429 | } |
| 430 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 431 | int create_context_ind(struct pdp_t *pdp) |
| 432 | { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 433 | static char name_buf[256]; |
| 434 | struct gsn_t *gsn = pdp->gsn; |
| 435 | struct ggsn_ctx *ggsn = gsn->priv; |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 436 | struct in46_addr addr[2]; |
Pau Espin Pedrol | 4e43ef5 | 2018-01-26 18:12:19 +0100 | [diff] [blame] | 437 | struct ippoolm_t *member = NULL, *addrv4 = NULL, *addrv6 = NULL; |
| 438 | char straddrv4[INET_ADDRSTRLEN], straddrv6[INET6_ADDRSTRLEN]; |
Vadim Yanitskiy | 2e8e57a | 2019-05-13 22:09:15 +0700 | [diff] [blame] | 439 | struct apn_ctx *apn = NULL; |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 440 | int rc, num_addr, i; |
Vadim Yanitskiy | 2e8e57a | 2019-05-13 22:09:15 +0700 | [diff] [blame] | 441 | char *apn_name; |
Pau Espin Pedrol | f5fbb41 | 2019-08-21 18:49:44 +0200 | [diff] [blame] | 442 | struct sgsn_peer *sgsn; |
| 443 | struct pdp_priv_t *pdp_priv; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 444 | |
Vadim Yanitskiy | 2e8e57a | 2019-05-13 22:09:15 +0700 | [diff] [blame] | 445 | apn_name = osmo_apn_to_str(name_buf, pdp->apn_req.v, pdp->apn_req.l); |
| 446 | LOGPPDP(LOGL_DEBUG, pdp, "Processing create PDP context request for APN '%s'\n", |
| 447 | apn_name ? name_buf : "(NONE)"); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 448 | |
| 449 | /* First find an exact APN name match */ |
Vadim Yanitskiy | 2e8e57a | 2019-05-13 22:09:15 +0700 | [diff] [blame] | 450 | if (apn_name != NULL) |
| 451 | apn = ggsn_find_apn(ggsn, name_buf); |
Harald Welte | 2e84d2c | 2017-10-01 13:36:52 +0800 | [diff] [blame] | 452 | /* ignore if the APN has not been started */ |
Pau Espin Pedrol | 958256f | 2017-10-11 20:32:55 +0200 | [diff] [blame] | 453 | if (apn && !apn->started) |
Harald Welte | 2e84d2c | 2017-10-01 13:36:52 +0800 | [diff] [blame] | 454 | apn = NULL; |
Harald Welte | b16c46b | 2017-10-01 18:28:18 +0800 | [diff] [blame] | 455 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 456 | /* then try default (if any) */ |
| 457 | if (!apn) |
| 458 | apn = ggsn->cfg.default_apn; |
Harald Welte | b16c46b | 2017-10-01 18:28:18 +0800 | [diff] [blame] | 459 | /* ignore if the APN has not been started */ |
Pau Espin Pedrol | 958256f | 2017-10-11 20:32:55 +0200 | [diff] [blame] | 460 | if (apn && !apn->started) |
Harald Welte | b16c46b | 2017-10-01 18:28:18 +0800 | [diff] [blame] | 461 | apn = NULL; |
| 462 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 463 | if (!apn) { |
| 464 | /* no APN found for what user requested */ |
| 465 | LOGPPDP(LOGL_NOTICE, pdp, "Unknown APN '%s', rejecting\n", name_buf); |
| 466 | gtp_create_context_resp(gsn, pdp, GTPCAUSE_MISSING_APN); |
| 467 | return 0; |
| 468 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 469 | |
Harald Welte | d9d8862 | 2017-08-04 00:22:35 +0200 | [diff] [blame] | 470 | /* FIXME: we manually force all context requests to dynamic here! */ |
| 471 | if (pdp->eua.l > 2) |
| 472 | pdp->eua.l = 2; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 473 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 474 | memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_req0)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 475 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 476 | memcpy(pdp->qos_neg.v, pdp->qos_req.v, pdp->qos_req.l); /* TODO */ |
| 477 | pdp->qos_neg.l = pdp->qos_req.l; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 478 | |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 479 | memset(addr, 0, sizeof(addr)); |
| 480 | if ((num_addr = in46a_from_eua(&pdp->eua, addr)) < 0) { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 481 | LOGPPDP(LOGL_ERROR, pdp, "Cannot decode EUA from MS/SGSN: %s\n", |
Harald Welte | d1bf1e1 | 2017-08-03 00:00:23 +0200 | [diff] [blame] | 482 | osmo_hexdump(pdp->eua.v, pdp->eua.l)); |
| 483 | gtp_create_context_resp(gsn, pdp, GTPCAUSE_UNKNOWN_PDP); |
| 484 | return 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 485 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 486 | |
Vadim Yanitskiy | d7030d2 | 2019-05-13 22:10:24 +0700 | [diff] [blame] | 487 | /* Store the actual APN for logging and the VTY */ |
| 488 | rc = osmo_apn_from_str(pdp->apn_use.v, sizeof(pdp->apn_use.v), apn->cfg.name); |
| 489 | if (rc < 0) /* Unlikely this would happen, but anyway... */ |
| 490 | LOGPPDP(LOGL_ERROR, pdp, "Failed to store APN '%s'\n", apn->cfg.name); |
| 491 | pdp->apn_use.l = rc; |
| 492 | |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 493 | /* Allocate dynamic addresses from the pool */ |
| 494 | for (i = 0; i < num_addr; i++) { |
Pau Espin Pedrol | 60ee0db | 2019-08-20 11:43:25 +0200 | [diff] [blame] | 495 | if (in46a_is_v4(&addr[i])) { |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 496 | /* does this APN actually have an IPv4 pool? */ |
| 497 | if (!apn_supports_ipv4(apn)) |
| 498 | goto err_wrong_af; |
Harald Welte | 9d9d91b | 2017-10-14 16:22:16 +0200 | [diff] [blame] | 499 | |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 500 | rc = ippool_newip(apn->v4.pool, &member, &addr[i], 0); |
| 501 | if (rc < 0) |
| 502 | goto err_pool_full; |
| 503 | /* copy back */ |
| 504 | memcpy(&addr[i].v4.s_addr, &member->addr.v4, 4); |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 505 | |
Pau Espin Pedrol | 4e43ef5 | 2018-01-26 18:12:19 +0100 | [diff] [blame] | 506 | addrv4 = member; |
| 507 | |
Pau Espin Pedrol | 60ee0db | 2019-08-20 11:43:25 +0200 | [diff] [blame] | 508 | } else if (in46a_is_v6(&addr[i])) { |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 509 | |
| 510 | /* does this APN actually have an IPv6 pool? */ |
| 511 | if (!apn_supports_ipv6(apn)) |
| 512 | goto err_wrong_af; |
| 513 | |
| 514 | rc = ippool_newip(apn->v6.pool, &member, &addr[i], 0); |
| 515 | if (rc < 0) |
| 516 | goto err_pool_full; |
| 517 | |
| 518 | /* IPv6 doesn't really send the real/allocated address at this point, but just |
| 519 | * the link-identifier which the MS shall use for router solicitation */ |
| 520 | /* initialize upper 64 bits to prefix, they are discarded by MS anyway */ |
| 521 | memcpy(addr[i].v6.s6_addr, &member->addr.v6, 8); |
| 522 | /* use allocated 64bit prefix as lower 64bit, used as link id by MS */ |
| 523 | memcpy(addr[i].v6.s6_addr+8, &member->addr.v6, 8); |
Pau Espin Pedrol | 4e43ef5 | 2018-01-26 18:12:19 +0100 | [diff] [blame] | 524 | |
| 525 | addrv6 = member; |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 526 | } else |
| 527 | OSMO_ASSERT(0); |
| 528 | |
| 529 | pdp->peer[i] = member; |
| 530 | member->peer = pdp; |
| 531 | } |
| 532 | |
| 533 | in46a_to_eua(addr, num_addr, &pdp->eua); |
| 534 | |
Harald Welte | 546884d | 2018-04-25 21:13:06 +0200 | [diff] [blame] | 535 | if (apn->cfg.gtpu_mode == APN_GTPU_MODE_KERNEL_GTP && apn_supports_ipv4(apn)) { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 536 | /* TODO: In IPv6, EUA doesn't contain the actual IP addr/prefix! */ |
Harald Welte | 698a233 | 2017-11-08 15:09:58 +0900 | [diff] [blame] | 537 | if (gtp_kernel_tunnel_add(pdp, apn->tun.cfg.dev_name) < 0) { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 538 | LOGPPDP(LOGL_ERROR, pdp, "Cannot add tunnel to kernel: %s\n", strerror(errno)); |
| 539 | gtp_create_context_resp(gsn, pdp, GTPCAUSE_SYS_FAIL); |
| 540 | return 0; |
| 541 | } |
Pau Espin Pedrol | 2d6a69e | 2017-12-06 19:26:25 +0100 | [diff] [blame] | 542 | } |
Harald Welte | 9d9d91b | 2017-10-14 16:22:16 +0200 | [diff] [blame] | 543 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 544 | pdp->ipif = apn->tun.tun; /* TODO */ |
Pau Espin Pedrol | f5fbb41 | 2019-08-21 18:49:44 +0200 | [diff] [blame] | 545 | |
| 546 | pdp_priv = talloc_zero(ggsn, struct pdp_priv_t); |
| 547 | pdp->priv = pdp_priv; |
| 548 | pdp_priv->lib = pdp; |
| 549 | /* Create sgsn and assign pdp to it */ |
| 550 | sgsn = ggsn_find_or_create_sgsn(ggsn, pdp); |
| 551 | sgsn_peer_add_pdp_priv(sgsn, pdp_priv); |
| 552 | pdp_priv->apn = apn; |
Max | 3142d8d | 2017-05-04 17:45:10 +0200 | [diff] [blame] | 553 | |
Pau Espin Pedrol | 4e43ef5 | 2018-01-26 18:12:19 +0100 | [diff] [blame] | 554 | /* TODO: change trap to send 2 IPs */ |
Max | 3142d8d | 2017-05-04 17:45:10 +0200 | [diff] [blame] | 555 | if (!send_trap(gsn, pdp, member, "imsi-ass-ip")) { /* TRAP with IP assignment */ |
Max | 727417d | 2016-08-02 17:10:38 +0200 | [diff] [blame] | 556 | gtp_create_context_resp(gsn, pdp, GTPCAUSE_NO_RESOURCES); |
| 557 | return 0; |
| 558 | } |
Pablo Neira Ayuso | 4b075b6 | 2015-11-17 12:22:42 +0100 | [diff] [blame] | 559 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 560 | process_pco(apn, pdp); |
Harald Welte | 1ae9877 | 2017-08-09 20:28:52 +0200 | [diff] [blame] | 561 | |
Harald Welte | 93fed3b | 2017-09-24 11:43:17 +0800 | [diff] [blame] | 562 | /* Transmit G-PDU sequence numbers (only) if configured in APN */ |
| 563 | pdp->tx_gpdu_seq = apn->cfg.tx_gpdu_seq; |
| 564 | |
Pau Espin Pedrol | 4e43ef5 | 2018-01-26 18:12:19 +0100 | [diff] [blame] | 565 | LOGPPDP(LOGL_INFO, pdp, "Successful PDP Context Creation: APN=%s(%s), TEIC=%u, IPv4=%s, IPv6=%s\n", |
| 566 | name_buf, apn->cfg.name, pdp->teic_own, |
| 567 | addrv4 ? inet_ntop(AF_INET, &addrv4->addr.v4, straddrv4, sizeof(straddrv4)) : "none", |
| 568 | addrv6 ? inet_ntop(AF_INET6, &addrv6->addr.v6, straddrv6, sizeof(straddrv6)) : "none"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 569 | gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ); |
| 570 | return 0; /* Success */ |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 571 | |
| 572 | err_pool_full: |
| 573 | LOGPPDP(LOGL_ERROR, pdp, "Cannot allocate IP address from pool (full!)\n"); |
| 574 | gtp_create_context_resp(gsn, pdp, -rc); |
| 575 | return 0; /* Already in use, or no more available */ |
Harald Welte | 9d9d91b | 2017-10-14 16:22:16 +0200 | [diff] [blame] | 576 | |
| 577 | err_wrong_af: |
| 578 | LOGPPDP(LOGL_ERROR, pdp, "APN doesn't support requested EUA / AF type\n"); |
| 579 | gtp_create_context_resp(gsn, pdp, GTPCAUSE_UNKNOWN_PDP); |
| 580 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 583 | /* Internet-originated IP packet, needs to be sent via GTP towards MS */ |
| 584 | 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] | 585 | { |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 586 | struct apn_ctx *apn = tun->priv; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 587 | struct ippoolm_t *ipm; |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 588 | struct in46_addr dst; |
Harald Welte | 63ebccd | 2017-08-02 21:10:09 +0200 | [diff] [blame] | 589 | struct iphdr *iph = (struct iphdr *)pack; |
Harald Welte | a0d281d | 2017-08-02 21:48:16 +0200 | [diff] [blame] | 590 | struct ip6_hdr *ip6h = (struct ip6_hdr *)pack; |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 591 | struct ippool_t *pool; |
Pau Espin Pedrol | 134855c | 2018-01-30 16:04:53 +0100 | [diff] [blame] | 592 | char straddr[INET6_ADDRSTRLEN]; |
Pau Espin Pedrol | dddbbaa | 2018-01-30 16:16:33 +0100 | [diff] [blame] | 593 | uint8_t pref_offset; |
jjako | c6762cf | 2004-04-28 14:52:58 +0000 | [diff] [blame] | 594 | |
Pau Espin Pedrol | a4942e6 | 2018-01-30 16:01:27 +0100 | [diff] [blame] | 595 | switch (iph->version) { |
| 596 | case 4: |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 597 | if (len < sizeof(*iph) || len < 4*iph->ihl) |
| 598 | return -1; |
| 599 | dst.len = 4; |
Harald Welte | 63ebccd | 2017-08-02 21:10:09 +0200 | [diff] [blame] | 600 | dst.v4.s_addr = iph->daddr; |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 601 | pool = apn->v4.pool; |
Pau Espin Pedrol | a4942e6 | 2018-01-30 16:01:27 +0100 | [diff] [blame] | 602 | break; |
| 603 | case 6: |
Harald Welte | d4d6e09 | 2017-08-08 18:10:43 +0200 | [diff] [blame] | 604 | /* Due to the fact that 3GPP requires an allocation of a |
| 605 | * /64 prefix to each MS, we must instruct |
| 606 | * ippool_getip() below to match only the leading /64 |
Pau Espin Pedrol | dddbbaa | 2018-01-30 16:16:33 +0100 | [diff] [blame] | 607 | * prefix, i.e. the first 8 bytes of the address. If the ll addr |
| 608 | * is used, then the match should be done on the trailing 64 |
| 609 | * bits. */ |
Harald Welte | d4d6e09 | 2017-08-08 18:10:43 +0200 | [diff] [blame] | 610 | dst.len = 8; |
Pau Espin Pedrol | dddbbaa | 2018-01-30 16:16:33 +0100 | [diff] [blame] | 611 | pref_offset = IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_dst) ? 8 : 0; |
| 612 | memcpy(&dst.v6, ((uint8_t*)&ip6h->ip6_dst) + pref_offset, 8); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 613 | pool = apn->v6.pool; |
Pau Espin Pedrol | a4942e6 | 2018-01-30 16:01:27 +0100 | [diff] [blame] | 614 | break; |
| 615 | default: |
Pau Espin Pedrol | 012d51e | 2019-08-20 13:43:38 +0200 | [diff] [blame] | 616 | LOGTUN(LOGL_NOTICE, tun, "non-IPv%u packet received\n", iph->version); |
Harald Welte | d12eab9 | 2017-08-02 19:49:47 +0200 | [diff] [blame] | 617 | return -1; |
| 618 | } |
jjako | c6762cf | 2004-04-28 14:52:58 +0000 | [diff] [blame] | 619 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 620 | /* IPv6 packet but no IPv6 pool, or IPv4 packet with no IPv4 pool */ |
| 621 | if (!pool) |
| 622 | return 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 623 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 624 | if (ippool_getip(pool, &ipm, &dst)) { |
Pau Espin Pedrol | 012d51e | 2019-08-20 13:43:38 +0200 | [diff] [blame] | 625 | LOGTUN(LOGL_DEBUG, tun, "Received packet for APN(%s) with no PDP contex! (%s)\n", |
| 626 | apn->cfg.name, |
| 627 | iph->version == 4 ? |
Pau Espin Pedrol | 134855c | 2018-01-30 16:04:53 +0100 | [diff] [blame] | 628 | inet_ntop(AF_INET, &iph->saddr, straddr, sizeof(straddr)) : |
| 629 | inet_ntop(AF_INET6, &ip6h->ip6_src, straddr, sizeof(straddr))); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 630 | return 0; |
| 631 | } |
Pau Espin Pedrol | 012d51e | 2019-08-20 13:43:38 +0200 | [diff] [blame] | 632 | LOGTUN(LOGL_DEBUG, tun, "Received packet for APN(%s)\n", apn->cfg.name); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 633 | |
| 634 | if (ipm->peer) /* Check if a peer protocol is defined */ |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 635 | 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] | 636 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Harald Welte | d46bcd2 | 2017-08-08 23:27:22 +0200 | [diff] [blame] | 639 | /* RFC3307 link-local scope multicast address */ |
| 640 | static const struct in6_addr all_router_mcast_addr = { |
| 641 | .s6_addr = { 0xff,0x02,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,2 } |
| 642 | }; |
| 643 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 644 | /* MS-originated GTP1-U packet, needs to be sent via TUN device */ |
| 645 | static int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 646 | { |
Harald Welte | d46bcd2 | 2017-08-08 23:27:22 +0200 | [diff] [blame] | 647 | struct iphdr *iph = (struct iphdr *)pack; |
| 648 | struct ip6_hdr *ip6h = (struct ip6_hdr *)pack; |
Harald Welte | f85fe97 | 2017-09-24 20:00:34 +0800 | [diff] [blame] | 649 | struct tun_t *tun = (struct tun_t *)pdp->ipif; |
| 650 | struct apn_ctx *apn = tun->priv; |
Pau Espin Pedrol | 5b1ef95 | 2018-01-25 20:50:59 +0100 | [diff] [blame] | 651 | char straddr[INET6_ADDRSTRLEN]; |
Pau Espin Pedrol | 7d54ed4 | 2018-01-25 20:09:16 +0100 | [diff] [blame] | 652 | struct ippoolm_t *peer; |
Pau Espin Pedrol | 5b1ef95 | 2018-01-25 20:50:59 +0100 | [diff] [blame] | 653 | uint8_t pref_offset; |
Harald Welte | f85fe97 | 2017-09-24 20:00:34 +0800 | [diff] [blame] | 654 | |
| 655 | OSMO_ASSERT(tun); |
| 656 | OSMO_ASSERT(apn); |
Harald Welte | d46bcd2 | 2017-08-08 23:27:22 +0200 | [diff] [blame] | 657 | |
Max | 427699e | 2017-12-05 16:30:37 +0100 | [diff] [blame] | 658 | 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] | 659 | |
| 660 | switch (iph->version) { |
| 661 | case 6: |
Pau Espin Pedrol | 7d54ed4 | 2018-01-25 20:09:16 +0100 | [diff] [blame] | 662 | peer = pdp_get_peer_ipv(pdp, true); |
| 663 | if (!peer) { |
| 664 | LOGPPDP(LOGL_ERROR, pdp, "Packet from MS IPv6 with unassigned EUA: %s\n", |
| 665 | osmo_hexdump(pack, len)); |
| 666 | return -1; |
| 667 | } |
| 668 | |
Pau Espin Pedrol | 5b1ef95 | 2018-01-25 20:50:59 +0100 | [diff] [blame] | 669 | /* Validate packet comes from IPaddr assigned to the pdp ctx. |
| 670 | If packet is a LL addr, then EUA is in the lower 64 bits, |
| 671 | otherwise it's used as the 64 prefix */ |
| 672 | pref_offset = IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src) ? 8 : 0; |
| 673 | if (memcmp(((uint8_t*)&ip6h->ip6_src) + pref_offset, &peer->addr.v6, 8)) { |
| 674 | LOGPPDP(LOGL_ERROR, pdp, "Packet from MS using unassigned src IPv6: %s\n", |
| 675 | inet_ntop(AF_INET6, &ip6h->ip6_src, straddr, sizeof(straddr))); |
| 676 | return -1; |
| 677 | } |
| 678 | |
Harald Welte | d46bcd2 | 2017-08-08 23:27:22 +0200 | [diff] [blame] | 679 | /* daddr: all-routers multicast addr */ |
| 680 | if (IN6_ARE_ADDR_EQUAL(&ip6h->ip6_dst, &all_router_mcast_addr)) |
Pau Espin Pedrol | 7d54ed4 | 2018-01-25 20:09:16 +0100 | [diff] [blame] | 681 | return handle_router_mcast(pdp->gsn, pdp, &peer->addr.v6, |
| 682 | &apn->v6_lladdr, pack, len); |
Harald Welte | d46bcd2 | 2017-08-08 23:27:22 +0200 | [diff] [blame] | 683 | break; |
| 684 | case 4: |
Pau Espin Pedrol | 7d54ed4 | 2018-01-25 20:09:16 +0100 | [diff] [blame] | 685 | peer = pdp_get_peer_ipv(pdp, false); |
| 686 | if (!peer) { |
| 687 | LOGPPDP(LOGL_ERROR, pdp, "Packet from MS IPv4 with unassigned EUA: %s\n", |
| 688 | osmo_hexdump(pack, len)); |
| 689 | return -1; |
| 690 | } |
Pau Espin Pedrol | 5b1ef95 | 2018-01-25 20:50:59 +0100 | [diff] [blame] | 691 | |
| 692 | /* Validate packet comes from IPaddr assigned to the pdp ctx */ |
| 693 | if (memcmp(&iph->saddr, &peer->addr.v4, sizeof(peer->addr.v4))) { |
| 694 | LOGPPDP(LOGL_ERROR, pdp, "Packet from MS using unassigned src IPv4: %s\n", |
| 695 | inet_ntop(AF_INET, &iph->saddr, straddr, sizeof(straddr))); |
| 696 | return -1; |
| 697 | } |
Harald Welte | d46bcd2 | 2017-08-08 23:27:22 +0200 | [diff] [blame] | 698 | break; |
| 699 | default: |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 700 | LOGPPDP(LOGL_ERROR, pdp, "Packet from MS is neither IPv4 nor IPv6: %s\n", |
| 701 | osmo_hexdump(pack, len)); |
Harald Welte | d46bcd2 | 2017-08-08 23:27:22 +0200 | [diff] [blame] | 702 | return -1; |
| 703 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 704 | return tun_encaps((struct tun_t *)pdp->ipif, pack, len); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 707 | /* callback for tun device osmocom select loop integration */ |
| 708 | static int ggsn_tun_fd_cb(struct osmo_fd *fd, unsigned int what) |
| 709 | { |
| 710 | struct apn_ctx *apn = fd->data; |
| 711 | |
| 712 | OSMO_ASSERT(what & BSC_FD_READ); |
| 713 | |
| 714 | return tun_decaps(apn->tun.tun); |
| 715 | } |
| 716 | |
| 717 | /* callback for libgtp osmocom select loop integration */ |
| 718 | static int ggsn_gtp_fd_cb(struct osmo_fd *fd, unsigned int what) |
| 719 | { |
| 720 | struct ggsn_ctx *ggsn = fd->data; |
| 721 | int rc; |
| 722 | |
| 723 | OSMO_ASSERT(what & BSC_FD_READ); |
| 724 | |
| 725 | switch (fd->priv_nr) { |
| 726 | case 0: |
| 727 | rc = gtp_decaps0(ggsn->gsn); |
| 728 | break; |
| 729 | case 1: |
| 730 | rc = gtp_decaps1c(ggsn->gsn); |
| 731 | break; |
| 732 | case 2: |
| 733 | rc = gtp_decaps1u(ggsn->gsn); |
| 734 | break; |
| 735 | default: |
| 736 | OSMO_ASSERT(0); |
| 737 | break; |
| 738 | } |
| 739 | return rc; |
| 740 | } |
| 741 | |
Oliver Smith | 1cde2c1 | 2019-05-13 11:35:03 +0200 | [diff] [blame] | 742 | /* libgtp callback for confirmations */ |
| 743 | static int cb_conf(int type, int cause, struct pdp_t *pdp, void *cbp) |
| 744 | { |
Pau Espin Pedrol | f5fbb41 | 2019-08-21 18:49:44 +0200 | [diff] [blame] | 745 | struct sgsn_peer *sgsn; |
Oliver Smith | 1cde2c1 | 2019-05-13 11:35:03 +0200 | [diff] [blame] | 746 | int rc = 0; |
| 747 | |
| 748 | if (cause == EOF) |
| 749 | LOGP(DGGSN, LOGL_NOTICE, "libgtp EOF (type=%u, pdp=%p, cbp=%p)\n", |
| 750 | type, pdp, cbp); |
| 751 | |
| 752 | switch (type) { |
| 753 | case GTP_DELETE_PDP_REQ: |
| 754 | /* Remark: We actually never reach this path nowadays because |
| 755 | only place where we call gtp_delete_context_req2() is during |
Pau Espin Pedrol | 26e300f | 2019-08-29 14:02:28 +0200 | [diff] [blame] | 756 | ggsn_close_one_pdp() path, and in that case we free all pdp |
| 757 | contexts immediatelly without waiting for confirmation |
| 758 | (through gtp_freepdp_teardown()) since we want to tear down |
| 759 | the whole APN anyways. As a result, DeleteCtxResponse will |
| 760 | never reach here since it will be dropped at some point in |
| 761 | lower layers in the Rx path. This code is nevertheless left |
| 762 | here in order to ease future developent and avoid possible |
| 763 | future memleaks once more scenarios where GGSN sends a |
| 764 | DeleteCtxRequest are introduced. */ |
Pau Espin Pedrol | f5fbb41 | 2019-08-21 18:49:44 +0200 | [diff] [blame] | 765 | if (pdp) |
Pau Espin Pedrol | 26e300f | 2019-08-29 14:02:28 +0200 | [diff] [blame] | 766 | rc = gtp_freepdp(pdp->gsn, pdp); |
Pau Espin Pedrol | f5fbb41 | 2019-08-21 18:49:44 +0200 | [diff] [blame] | 767 | break; |
| 768 | case GTP_ECHO_REQ: |
| 769 | sgsn = (struct sgsn_peer *)cbp; |
| 770 | sgsn_peer_echo_resp(sgsn, cause == EOF); |
| 771 | break; |
Oliver Smith | 1cde2c1 | 2019-05-13 11:35:03 +0200 | [diff] [blame] | 772 | } |
| 773 | return rc; |
| 774 | } |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 775 | |
Pau Espin Pedrol | f5fbb41 | 2019-08-21 18:49:44 +0200 | [diff] [blame] | 776 | static int cb_recovery3(struct gsn_t *gsn, struct sockaddr_in *peer, struct pdp_t *pdp, uint8_t recovery) |
| 777 | { |
| 778 | struct ggsn_ctx *ggsn = (struct ggsn_ctx *)gsn->priv; |
| 779 | struct sgsn_peer *sgsn; |
| 780 | |
| 781 | sgsn = ggsn_find_sgsn(ggsn, &peer->sin_addr); |
| 782 | if (!sgsn) { |
| 783 | LOGPGGSN(LOGL_NOTICE, ggsn, "Received Recovery IE for unknown SGSN (no PDP contexts active)\n"); |
| 784 | return -EINVAL; |
| 785 | } |
| 786 | |
| 787 | return sgsn_peer_handle_recovery(sgsn, pdp, recovery); |
| 788 | } |
| 789 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 790 | /* Start a given GGSN */ |
| 791 | int ggsn_start(struct ggsn_ctx *ggsn) |
| 792 | { |
| 793 | struct apn_ctx *apn; |
| 794 | int rc; |
| 795 | |
| 796 | if (ggsn->started) |
| 797 | return 0; |
| 798 | |
| 799 | LOGPGGSN(LOGL_INFO, ggsn, "Starting GGSN\n"); |
| 800 | |
| 801 | /* Start libgtp listener */ |
| 802 | if (gtp_new(&ggsn->gsn, ggsn->cfg.state_dir, &ggsn->cfg.listen_addr.v4, GTP_MODE_GGSN)) { |
| 803 | LOGPGGSN(LOGL_ERROR, ggsn, "Failed to create GTP: %s\n", strerror(errno)); |
| 804 | return -1; |
| 805 | } |
| 806 | ggsn->gsn->priv = ggsn; |
| 807 | |
Harald Welte | 9814677 | 2017-09-05 17:41:20 +0200 | [diff] [blame] | 808 | /* patch in different addresses to use (in case we're behind NAT, the listen |
| 809 | * address is different from what we advertise externally) */ |
| 810 | if (ggsn->cfg.gtpc_addr.v4.s_addr) |
| 811 | ggsn->gsn->gsnc = ggsn->cfg.gtpc_addr.v4; |
| 812 | |
| 813 | if (ggsn->cfg.gtpu_addr.v4.s_addr) |
| 814 | ggsn->gsn->gsnu = ggsn->cfg.gtpu_addr.v4; |
| 815 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 816 | /* Register File Descriptors */ |
| 817 | osmo_fd_setup(&ggsn->gtp_fd0, ggsn->gsn->fd0, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 0); |
| 818 | rc = osmo_fd_register(&ggsn->gtp_fd0); |
| 819 | OSMO_ASSERT(rc == 0); |
| 820 | |
| 821 | osmo_fd_setup(&ggsn->gtp_fd1c, ggsn->gsn->fd1c, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 1); |
| 822 | rc = osmo_fd_register(&ggsn->gtp_fd1c); |
| 823 | OSMO_ASSERT(rc == 0); |
| 824 | |
| 825 | osmo_fd_setup(&ggsn->gtp_fd1u, ggsn->gsn->fd1u, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 2); |
| 826 | rc = osmo_fd_register(&ggsn->gtp_fd1u); |
| 827 | OSMO_ASSERT(rc == 0); |
| 828 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 829 | gtp_set_cb_data_ind(ggsn->gsn, encaps_tun); |
| 830 | gtp_set_cb_delete_context(ggsn->gsn, delete_context); |
| 831 | gtp_set_cb_create_context_ind(ggsn->gsn, create_context_ind); |
Oliver Smith | 1cde2c1 | 2019-05-13 11:35:03 +0200 | [diff] [blame] | 832 | gtp_set_cb_conf(ggsn->gsn, cb_conf); |
Pau Espin Pedrol | f5fbb41 | 2019-08-21 18:49:44 +0200 | [diff] [blame] | 833 | gtp_set_cb_recovery3(ggsn->gsn, cb_recovery3); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 834 | |
| 835 | LOGPGGSN(LOGL_NOTICE, ggsn, "Successfully started\n"); |
| 836 | ggsn->started = true; |
| 837 | |
| 838 | llist_for_each_entry(apn, &ggsn->apn_list, list) |
| 839 | apn_start(apn); |
| 840 | |
| 841 | return 0; |
| 842 | } |
| 843 | |
| 844 | /* Stop a given GGSN */ |
| 845 | int ggsn_stop(struct ggsn_ctx *ggsn) |
| 846 | { |
| 847 | struct apn_ctx *apn; |
| 848 | |
| 849 | if (!ggsn->started) |
| 850 | return 0; |
| 851 | |
| 852 | /* iterate over all APNs and stop them */ |
| 853 | llist_for_each_entry(apn, &ggsn->apn_list, list) |
Pau Espin Pedrol | 72ab4bc | 2019-05-29 19:08:26 +0200 | [diff] [blame] | 854 | apn_stop(apn); |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 855 | |
Harald Welte | dda21ed | 2017-08-12 15:07:02 +0200 | [diff] [blame] | 856 | osmo_fd_unregister(&ggsn->gtp_fd1u); |
| 857 | osmo_fd_unregister(&ggsn->gtp_fd1c); |
| 858 | osmo_fd_unregister(&ggsn->gtp_fd0); |
| 859 | |
| 860 | if (ggsn->gsn) { |
| 861 | gtp_free(ggsn->gsn); |
| 862 | ggsn->gsn = NULL; |
| 863 | } |
| 864 | |
| 865 | ggsn->started = false; |
| 866 | return 0; |
| 867 | } |