blob: 423f0c02e4c8413f44c850c2ea95b15254afc800 [file] [log] [blame]
Pau Espin Pedrolfdd732b2017-10-13 14:32:24 +02001/*
Harald Welte632e8432017-09-05 18:12:14 +02002 * OsmoGGSN - Gateway GPRS Support Node
jjako0fe0df02004-09-17 11:30:40 +00003 * Copyright (C) 2002, 2003, 2004 Mondru AB.
Harald Weltedda21ed2017-08-12 15:07:02 +02004 * Copyright (C) 2017 by Harald Welte <laforge@gnumonks.org>
Pau Espin Pedrolfdd732b2017-10-13 14:32:24 +02005 *
jjakoa7cd2492003-04-11 09:40:12 +00006 * 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 Pedrolfdd732b2017-10-13 14:32:24 +020010 *
jjako52c24142002-12-16 13:33:51 +000011 */
12
13/* ggsn.c
14 *
15 */
16
17#ifdef __linux__
18#define _GNU_SOURCE 1 /* strdup() prototype, broken arpa/inet.h */
19#endif
20
jjako0fe0df02004-09-17 11:30:40 +000021#include "../config.h"
22
23#ifdef HAVE_STDINT_H
24#include <stdint.h>
25#endif
jjako52c24142002-12-16 13:33:51 +000026
Harald Weltedda21ed2017-08-12 15:07:02 +020027#include <getopt.h>
jjako52c24142002-12-16 13:33:51 +000028#include <ctype.h>
jjako52c24142002-12-16 13:33:51 +000029#include <signal.h>
30#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
Harald Weltedda21ed2017-08-12 15:07:02 +020033#include <unistd.h>
34#include <inttypes.h>
35#include <errno.h>
jjako52c24142002-12-16 13:33:51 +000036#include <sys/types.h>
Harald Weltedda21ed2017-08-12 15:07:02 +020037#include <sys/ioctl.h>
38
39#include <net/if.h>
40#include <arpa/inet.h>
jjako52c24142002-12-16 13:33:51 +000041#include <netinet/in.h>
Harald Welte63ebccd2017-08-02 21:10:09 +020042#include <netinet/ip.h>
Harald Weltea0d281d2017-08-02 21:48:16 +020043#include <netinet/ip6.h>
jjako52c24142002-12-16 13:33:51 +000044
Harald Weltedda21ed2017-08-12 15:07:02 +020045#include <osmocom/core/application.h>
Max727417d2016-08-02 17:10:38 +020046#include <osmocom/core/select.h>
Harald Weltedda21ed2017-08-12 15:07:02 +020047#include <osmocom/core/stats.h>
48#include <osmocom/core/rate_ctr.h>
49#include <osmocom/core/timer.h>
Max727417d2016-08-02 17:10:38 +020050#include <osmocom/ctrl/control_if.h>
51#include <osmocom/ctrl/control_cmd.h>
Harald Weltedda21ed2017-08-12 15:07:02 +020052#include <osmocom/ctrl/control_vty.h>
Max727417d2016-08-02 17:10:38 +020053#include <osmocom/ctrl/ports.h>
Harald Weltedda21ed2017-08-12 15:07:02 +020054#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>
Harald Welte3e443ca2018-02-14 01:04:04 +010059#include <osmocom/vty/misc.h>
Harald Weltedda21ed2017-08-12 15:07:02 +020060#include <osmocom/gsm/apn.h>
Max727417d2016-08-02 17:10:38 +020061
Emmanuel Bretelle2a103682010-09-07 17:01:20 +020062#include "../lib/tun.h"
63#include "../lib/ippool.h"
64#include "../lib/syserr.h"
Harald Welted12eab92017-08-02 19:49:47 +020065#include "../lib/in46_addr.h"
Harald Weltef2286392018-04-25 19:02:31 +020066#include "../lib/gtp-kernel.h"
jjako52c24142002-12-16 13:33:51 +000067#include "../gtp/pdp.h"
68#include "../gtp/gtp.h"
Harald Welted46bcd22017-08-08 23:27:22 +020069#include "icmpv6.h"
Harald Weltedda21ed2017-08-12 15:07:02 +020070#include "ggsn.h"
jjako52c24142002-12-16 13:33:51 +000071
Harald Weltedda21ed2017-08-12 15:07:02 +020072void *tall_ggsn_ctx;
jjakoa7cd2492003-04-11 09:40:12 +000073
Harald Weltedda21ed2017-08-12 15:07:02 +020074static int end = 0;
75static int daemonize = 0;
76static struct ctrl_handle *g_ctrlh;
77
jjakoa7cd2492003-04-11 09:40:12 +000078struct ul255_t qos;
79struct ul255_t apn;
80
Harald Weltedda21ed2017-08-12 15:07:02 +020081#define LOGPAPN(level, apn, fmt, args...) \
82 LOGP(DGGSN, level, "APN(%s): " fmt, (apn)->cfg.name, ## args)
jjako52c24142002-12-16 13:33:51 +000083
Harald Weltedda21ed2017-08-12 15:07:02 +020084#define LOGPGGSN(level, ggsn, fmt, args...) \
85 LOGP(DGGSN, level, "GGSN(%s): " fmt, (ggsn)->cfg.name, ## args)
86
Max6a215272017-09-25 10:35:34 +020087#define LOGPPDP(level, pdp, fmt, args...) LOGPDPX(DGGSN, level, pdp, fmt, ## args)
Harald Weltedda21ed2017-08-12 15:07:02 +020088
89static int ggsn_tun_fd_cb(struct osmo_fd *fd, unsigned int what);
90static int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len);
91
92
93static void pool_close_all_pdp(struct ippool_t *pool)
Harald Weltebed35df2011-11-02 13:06:18 +010094{
Harald Weltedda21ed2017-08-12 15:07:02 +020095 unsigned int i;
jjako52c24142002-12-16 13:33:51 +000096
Harald Weltedda21ed2017-08-12 15:07:02 +020097 if (!pool)
Harald Weltebed35df2011-11-02 13:06:18 +010098 return;
Harald Weltedda21ed2017-08-12 15:07:02 +020099
100 for (i = 0; i < pool->listsize; i++) {
101 struct ippoolm_t *member = &pool->member[i];
102 struct pdp_t *pdp;
103
104 if (!member->inuse)
105 continue;
106 pdp = member->peer;
107 if (!pdp)
108 continue;
109 LOGPPDP(LOGL_DEBUG, pdp, "Sending DELETE PDP CTX due to shutdown\n");
110 gtp_delete_context_req(pdp->gsn, pdp, NULL, 1);
Harald Weltebed35df2011-11-02 13:06:18 +0100111 }
jjako52c24142002-12-16 13:33:51 +0000112}
113
Harald Weltedda21ed2017-08-12 15:07:02 +0200114int apn_stop(struct apn_ctx *apn, bool force)
Harald Weltebed35df2011-11-02 13:06:18 +0100115{
Harald Weltedda21ed2017-08-12 15:07:02 +0200116 LOGPAPN(LOGL_NOTICE, apn, "%sStopping\n", force ? "FORCED " : "");
117 /* check if pools have any active PDP contexts and bail out */
118 pool_close_all_pdp(apn->v4.pool);
119 pool_close_all_pdp(apn->v6.pool);
120
121 /* shutdown whatever old state might be left */
122 if (apn->tun.tun) {
123 /* run ip-down script */
124 if (apn->tun.cfg.ipdown_script) {
125 LOGPAPN( LOGL_INFO, apn, "Running %s\n", apn->tun.cfg.ipdown_script);
126 tun_runscript(apn->tun.tun, apn->tun.cfg.ipdown_script);
127 }
Harald Weltef2286392018-04-25 19:02:31 +0200128 if (apn->cfg.gtpu_mode == APN_GTPU_MODE_TUN) {
129 /* release tun device */
130 LOGPAPN(LOGL_INFO, apn, "Closing TUN device %s\n", apn->tun.tun->devname);
131 osmo_fd_unregister(&apn->tun.fd);
132 }
Harald Weltedda21ed2017-08-12 15:07:02 +0200133 tun_free(apn->tun.tun);
134 apn->tun.tun = NULL;
135 }
136
137 if (apn->v4.pool) {
138 LOGPAPN(LOGL_INFO, apn, "Releasing IPv4 pool\n");
139 ippool_free(apn->v4.pool);
140 apn->v4.pool = NULL;
141 }
142 if (apn->v6.pool) {
143 LOGPAPN(LOGL_INFO, apn, "Releasing IPv6 pool\n");
144 ippool_free(apn->v6.pool);
145 apn->v6.pool = NULL;
146 }
147
148 apn->started = false;
149 return 0;
150}
151
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200152
Harald Weltef55a0392017-11-08 14:33:55 +0900153static int alloc_ippool_blacklist(struct apn_ctx *apn, struct in46_prefix **blacklist, bool ipv6)
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200154{
155
156 int flags, len, len2, i;
157
Harald Weltee2a1de52017-11-08 15:24:07 +0900158 *blacklist = NULL;
159
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200160 if (ipv6)
161 flags = IP_TYPE_IPv6_NONLINK;
162 else
163 flags = IP_TYPE_IPv4;
164
165 while (1) {
Harald Weltee2a1de52017-11-08 15:24:07 +0900166 len = netdev_ip_local_get(apn->tun.cfg.dev_name, NULL, 0, flags);
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200167 if (len < 1)
168 return len;
169
170 *blacklist = talloc_zero_size(apn, len * sizeof(struct in46_prefix));
Harald Weltee2a1de52017-11-08 15:24:07 +0900171 len2 = netdev_ip_local_get(apn->tun.cfg.dev_name, *blacklist, len, flags);
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200172 if (len2 < 1) {
173 talloc_free(*blacklist);
Harald Weltee2a1de52017-11-08 15:24:07 +0900174 *blacklist = NULL;
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200175 return len2;
176 }
177
Harald Weltee2a1de52017-11-08 15:24:07 +0900178 if (len2 > len) { /* iface was added between 2 calls, repeat operation */
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200179 talloc_free(*blacklist);
Harald Weltee2a1de52017-11-08 15:24:07 +0900180 *blacklist = NULL;
181 } else
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200182 break;
183 }
184
185 for (i = 0; i < len2; i++)
186 LOGPAPN(LOGL_INFO, apn, "Blacklist tun IP %s\n",
187 in46p_ntoa(&(*blacklist)[i]));
188
189 return len2;
190}
191
Harald Weltedda21ed2017-08-12 15:07:02 +0200192/* actually start the APN with its current config */
193int apn_start(struct apn_ctx *apn)
194{
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200195 int ippool_flags = IPPOOL_NONETWORK | IPPOOL_NOBROADCAST;
Pau Espin Pedrola037e592017-10-16 14:41:37 +0200196 struct in46_prefix ipv6_tun_linklocal_ip;
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200197 struct in46_prefix *blacklist;
198 int blacklist_size;
Harald Weltef2286392018-04-25 19:02:31 +0200199 struct gsn_t *gsn = apn->ggsn->gsn;
Pau Espin Pedrolbffc3f92017-12-14 11:19:10 +0100200 int rc;
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200201
Harald Weltedda21ed2017-08-12 15:07:02 +0200202 if (apn->started)
203 return 0;
204
205 LOGPAPN(LOGL_INFO, apn, "Starting\n");
206 switch (apn->cfg.gtpu_mode) {
207 case APN_GTPU_MODE_TUN:
208 LOGPAPN(LOGL_INFO, apn, "Opening TUN device %s\n", apn->tun.cfg.dev_name);
Harald Weltef2286392018-04-25 19:02:31 +0200209 if (tun_new(&apn->tun.tun, apn->tun.cfg.dev_name, false, -1, -1)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200210 LOGPAPN(LOGL_ERROR, apn, "Failed to configure tun device\n");
211 return -1;
212 }
213 LOGPAPN(LOGL_INFO, apn, "Opened TUN device %s\n", apn->tun.tun->devname);
214
215 /* Register with libosmcoore */
216 osmo_fd_setup(&apn->tun.fd, apn->tun.tun->fd, BSC_FD_READ, ggsn_tun_fd_cb, apn, 0);
217 osmo_fd_register(&apn->tun.fd);
218
219 /* Set TUN library callback */
220 tun_set_cb_ind(apn->tun.tun, cb_tun_ind);
Harald Weltedda21ed2017-08-12 15:07:02 +0200221 break;
222 case APN_GTPU_MODE_KERNEL_GTP:
Harald Welte2fc2bc62017-11-08 15:50:53 +0900223 LOGPAPN(LOGL_INFO, apn, "Opening Kernel GTP device %s\n", apn->tun.cfg.dev_name);
Harald Welte490782d2017-11-08 14:09:51 +0900224 if (apn->cfg.apn_type_mask & (APN_TYPE_IPv6|APN_TYPE_IPv4v6)) {
225 LOGPAPN(LOGL_ERROR, apn, "Kernel GTP currently supports only IPv4\n");
226 apn_stop(apn, false);
227 return -1;
228 }
Harald Weltef2286392018-04-25 19:02:31 +0200229 if (gsn == NULL) {
Harald Welte07575042018-02-14 01:04:04 +0100230 /* skip bringing up the APN now if the GSN is not initialized yet.
231 * This happens during initial load of the config file, as the
232 * "no shutdown" in the ggsn node only happens after the "apn" nodes
233 * are brought up */
234 LOGPAPN(LOGL_NOTICE, apn, "Skipping APN start\n");
235 return 0;
236 }
Harald Weltedda21ed2017-08-12 15:07:02 +0200237 /* use GTP kernel module for data packet encapsulation */
Harald Weltef2286392018-04-25 19:02:31 +0200238 if (tun_new(&apn->tun.tun, apn->tun.cfg.dev_name, true, gsn->fd0, gsn->fd1u)) {
239 LOGPAPN(LOGL_ERROR, apn, "Failed to configure Kernel GTP device\n");
Harald Welte490782d2017-11-08 14:09:51 +0900240 return -1;
241 }
Harald Weltebed35df2011-11-02 13:06:18 +0100242 break;
243 default:
Harald Weltedda21ed2017-08-12 15:07:02 +0200244 LOGPAPN(LOGL_ERROR, apn, "Unknown GTPU Mode %d\n", apn->cfg.gtpu_mode);
245 return -1;
Harald Weltebed35df2011-11-02 13:06:18 +0100246 }
jjako0141d202004-01-09 15:19:20 +0000247
Harald Weltef2286392018-04-25 19:02:31 +0200248 /* common initialization below */
249
250 /* set back-pointer from TUN device to APN */
251 apn->tun.tun->priv = apn;
252
253 if (apn->v4.cfg.ifconfig_prefix.addr.len) {
254 LOGPAPN(LOGL_INFO, apn, "Setting tun IP address %s\n",
255 in46p_ntoa(&apn->v4.cfg.ifconfig_prefix));
256 if (tun_addaddr(apn->tun.tun, &apn->v4.cfg.ifconfig_prefix.addr, NULL,
257 apn->v4.cfg.ifconfig_prefix.prefixlen)) {
258 LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv4 address %s: %s\n",
259 in46p_ntoa(&apn->v4.cfg.ifconfig_prefix), strerror(errno));
260 apn_stop(apn, false);
261 return -1;
262 }
263 }
264
265 if (apn->v6.cfg.ifconfig_prefix.addr.len) {
266 LOGPAPN(LOGL_INFO, apn, "Setting tun IPv6 address %s\n",
267 in46p_ntoa(&apn->v6.cfg.ifconfig_prefix));
268 if (tun_addaddr(apn->tun.tun, &apn->v6.cfg.ifconfig_prefix.addr, NULL,
269 apn->v6.cfg.ifconfig_prefix.prefixlen)) {
270 LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv6 address %s: %s. "
271 "Ensure you have ipv6 support and not used the disable_ipv6 sysctl?\n",
272 in46p_ntoa(&apn->v6.cfg.ifconfig_prefix), strerror(errno));
273 apn_stop(apn, false);
274 return -1;
275 }
276 }
277
278 if (apn->v6.cfg.ll_prefix.addr.len) {
279 LOGPAPN(LOGL_INFO, apn, "Setting tun IPv6 link-local address %s\n",
280 in46p_ntoa(&apn->v6.cfg.ll_prefix));
281 if (tun_addaddr(apn->tun.tun, &apn->v6.cfg.ll_prefix.addr, NULL,
282 apn->v6.cfg.ll_prefix.prefixlen)) {
283 LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv6 link-local address %s: %s. "
284 "Ensure you have ipv6 support and not used the disable_ipv6 sysctl?\n",
285 in46p_ntoa(&apn->v6.cfg.ll_prefix), strerror(errno));
286 apn_stop(apn, false);
287 return -1;
288 }
289 apn->v6_lladdr = apn->v6.cfg.ll_prefix.addr.v6;
290 }
291
292 if (apn->tun.cfg.ipup_script) {
293 LOGPAPN(LOGL_INFO, apn, "Running ip-up script %s\n",
294 apn->tun.cfg.ipup_script);
295 tun_runscript(apn->tun.tun, apn->tun.cfg.ipup_script);
296 }
297
298 if (apn->cfg.apn_type_mask & (APN_TYPE_IPv6|APN_TYPE_IPv4v6) &&
299 apn->v6.cfg.ll_prefix.addr.len == 0) {
300 rc = tun_ip_local_get(apn->tun.tun, &ipv6_tun_linklocal_ip, 1, IP_TYPE_IPv6_LINK);
301 if (rc < 1) {
302 LOGPAPN(LOGL_ERROR, apn, "Cannot obtain IPv6 link-local address of interface: %s\n",
303 rc ? strerror(errno) : "tun interface has no link-local IP assigned");
304 apn_stop(apn, false);
305 return -1;
306 }
307 apn->v6_lladdr = ipv6_tun_linklocal_ip.addr.v6;
308 }
309
Harald Weltedda21ed2017-08-12 15:07:02 +0200310 /* Create IPv4 pool */
311 if (apn->v4.cfg.dynamic_prefix.addr.len) {
312 LOGPAPN(LOGL_INFO, apn, "Creating IPv4 pool %s\n",
313 in46p_ntoa(&apn->v4.cfg.dynamic_prefix));
Harald Weltef55a0392017-11-08 14:33:55 +0900314 if ((blacklist_size = alloc_ippool_blacklist(apn, &blacklist, false)) < 0)
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200315 LOGPAPN(LOGL_ERROR, apn, "Failed obtaining IPv4 tun IPs\n");
Harald Weltedda21ed2017-08-12 15:07:02 +0200316 if (ippool_new(&apn->v4.pool, &apn->v4.cfg.dynamic_prefix,
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200317 &apn->v4.cfg.static_prefix, ippool_flags,
318 blacklist, blacklist_size)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200319 LOGPAPN(LOGL_ERROR, apn, "Failed to create IPv4 pool\n");
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200320 talloc_free(blacklist);
Harald Weltedda21ed2017-08-12 15:07:02 +0200321 apn_stop(apn, false);
322 return -1;
323 }
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200324 talloc_free(blacklist);
Harald Weltebed35df2011-11-02 13:06:18 +0100325 }
Harald Weltedda21ed2017-08-12 15:07:02 +0200326
327 /* Create IPv6 pool */
328 if (apn->v6.cfg.dynamic_prefix.addr.len) {
329 LOGPAPN(LOGL_INFO, apn, "Creating IPv6 pool %s\n",
330 in46p_ntoa(&apn->v6.cfg.dynamic_prefix));
Harald Weltef55a0392017-11-08 14:33:55 +0900331 if ((blacklist_size = alloc_ippool_blacklist(apn, &blacklist, true)) < 0)
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200332 LOGPAPN(LOGL_ERROR, apn, "Failed obtaining IPv6 tun IPs\n");
Harald Weltedda21ed2017-08-12 15:07:02 +0200333 if (ippool_new(&apn->v6.pool, &apn->v6.cfg.dynamic_prefix,
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200334 &apn->v6.cfg.static_prefix, ippool_flags,
335 blacklist, blacklist_size)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200336 LOGPAPN(LOGL_ERROR, apn, "Failed to create IPv6 pool\n");
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200337 talloc_free(blacklist);
Harald Weltedda21ed2017-08-12 15:07:02 +0200338 apn_stop(apn, false);
339 return -1;
340 }
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200341 talloc_free(blacklist);
Harald Weltedda21ed2017-08-12 15:07:02 +0200342 }
343
344 LOGPAPN(LOGL_NOTICE, apn, "Successfully started\n");
345 apn->started = true;
346 return 0;
jjako0141d202004-01-09 15:19:20 +0000347}
jjako0141d202004-01-09 15:19:20 +0000348
Max3142d8d2017-05-04 17:45:10 +0200349static bool send_trap(const struct gsn_t *gsn, const struct pdp_t *pdp, const struct ippoolm_t *member, const char *var)
350{
Harald Welted12eab92017-08-02 19:49:47 +0200351 char addrbuf[256];
Max3142d8d2017-05-04 17:45:10 +0200352 char val[NAMESIZE];
353
Harald Welted12eab92017-08-02 19:49:47 +0200354 const char *addrstr = in46a_ntop(&member->addr, addrbuf, sizeof(addrbuf));
355
Harald Welteb10ee082017-08-12 19:29:16 +0200356 snprintf(val, sizeof(val), "%s,%s", imsi_gtp2str(&pdp->imsi), addrstr);
Max3142d8d2017-05-04 17:45:10 +0200357
Harald Weltedda21ed2017-08-12 15:07:02 +0200358 if (ctrl_cmd_send_trap(g_ctrlh, var, val) < 0) {
359 LOGPPDP(LOGL_ERROR, pdp, "Failed to create and send TRAP %s\n", var);
Max3142d8d2017-05-04 17:45:10 +0200360 return false;
361 }
362 return true;
363}
364
Harald Weltedda21ed2017-08-12 15:07:02 +0200365static int delete_context(struct pdp_t *pdp)
Harald Weltebed35df2011-11-02 13:06:18 +0100366{
Harald Weltedda21ed2017-08-12 15:07:02 +0200367 struct gsn_t *gsn = pdp->gsn;
Harald Welte698a2332017-11-08 15:09:58 +0900368 struct apn_ctx *apn = pdp->priv;
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100369 struct ippoolm_t *member;
370 int i;
Harald Weltedda21ed2017-08-12 15:07:02 +0200371
372 LOGPPDP(LOGL_INFO, pdp, "Deleting PDP context\n");
Maxdbd70242016-10-14 13:38:05 +0200373
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100374 for (i = 0; i < 2; i++) {
375 if (pdp->peer[i]) {
376 member = pdp->peer[i];
377 send_trap(gsn, pdp, member, "imsi-rem-ip"); /* TRAP with IP removal */
378 ippool_freeip(member->pool, member);
379 } else if(i == 0)
380 LOGPPDP(LOGL_ERROR, pdp, "Cannot find/free IP Pool member\n");
381 }
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100382
Harald Welte546884d2018-04-25 21:13:06 +0200383 if (apn->cfg.gtpu_mode == APN_GTPU_MODE_KERNEL_GTP) {
384 if (gtp_kernel_tunnel_del(pdp, apn->tun.cfg.dev_name)) {
385 LOGPPDP(LOGL_ERROR, pdp, "Cannot delete tunnel from kernel:%s\n",
386 strerror(errno));
387 }
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100388 }
389
Harald Weltebed35df2011-11-02 13:06:18 +0100390 return 0;
jjako52c24142002-12-16 13:33:51 +0000391}
392
Harald Welte1ae98772017-08-09 20:28:52 +0200393#include <osmocom/gsm/tlv.h>
394
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100395/* RFC 1332 */
396enum ipcp_options {
397 IPCP_OPT_IPADDR = 3,
398 IPCP_OPT_PRIMARY_DNS = 129,
399 IPCP_OPT_SECONDARY_DNS = 131,
400};
401
402struct ipcp_option_hdr {
403 uint8_t type;
404 uint8_t len;
405 uint8_t data[0];
Philipp Maier6a2856b2018-05-28 17:50:09 +0200406} __attribute__ ((packed));
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100407
408struct ipcp_hdr {
409 uint8_t code;
410 uint8_t id;
411 uint16_t len;
412 uint8_t options[0];
Philipp Maier6a2856b2018-05-28 17:50:09 +0200413} __attribute__ ((packed));
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100414
415/* determine if IPCP contains given option */
Stefan Sperlingd70ab972018-07-19 15:25:47 +0200416static uint8_t *ipcp_contains_option(uint8_t *ipcp, size_t ipcp_len, enum ipcp_options opt, size_t opt_minlen)
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100417{
Stefan Sperlingd70ab972018-07-19 15:25:47 +0200418 uint8_t *cur_opt = ipcp + sizeof(struct ipcp_hdr);
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100419
420 /* iterate over Options and check if protocol contained */
Stefan Sperlingd70ab972018-07-19 15:25:47 +0200421 while (cur_opt + 2 <= ipcp + ipcp_len) {
422 uint8_t type = cur_opt[0];
423 uint8_t len = cur_opt[1]; /* length value includes 2 bytes type/length */
424 if (len < 2)
425 return NULL;
426 if (type == opt && len >= 2 + opt_minlen)
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100427 return cur_opt;
Stefan Sperlingd70ab972018-07-19 15:25:47 +0200428 cur_opt += len;
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100429 }
430 return NULL;
431}
432
Harald Welte1ae98772017-08-09 20:28:52 +0200433/* 3GPP TS 24.008 10.6.5.3 */
434enum pco_protocols {
435 PCO_P_LCP = 0xC021,
436 PCO_P_PAP = 0xC023,
437 PCO_P_CHAP = 0xC223,
438 PCO_P_IPCP = 0x8021,
439 PCO_P_PCSCF_ADDR = 0x0001,
440 PCO_P_IM_CN_SS_F = 0x0002,
441 PCO_P_DNS_IPv6_ADDR = 0x0003,
442 PCO_P_POLICY_CTRL_REJ = 0x0004, /* only in Network->MS */
443 PCO_P_MS_SUP_NETREQ_BCI = 0x0005,
444 /* reserved */
445 PCO_P_DSMIPv6_HA_ADDR = 0x0007,
446 PCO_P_DSMIPv6_HN_PREF = 0x0008,
447 PCO_P_DSMIPv6_v4_HA_ADDR= 0x0009,
448 PCO_P_IP_ADDR_VIA_NAS = 0x000a, /* only MS->Network */
449 PCO_P_IPv4_ADDR_VIA_DHCP= 0x000b, /* only MS->Netowrk */
450 PCO_P_PCSCF_IPv4_ADDR = 0x000c,
451 PCO_P_DNS_IPv4_ADDR = 0x000d,
452 PCO_P_MSISDN = 0x000e,
453 PCO_P_IFOM_SUPPORT = 0x000f,
454 PCO_P_IPv4_LINK_MTU = 0x0010,
455 PCO_P_MS_SUPP_LOC_A_TFT = 0x0011,
456 PCO_P_PCSCF_RESEL_SUP = 0x0012, /* only MS->Network */
457 PCO_P_NBIFOM_REQ = 0x0013,
458 PCO_P_NBIFOM_MODE = 0x0014,
459 PCO_P_NONIP_LINK_MTU = 0x0015,
460 PCO_P_APN_RATE_CTRL_SUP = 0x0016,
461 PCO_P_PS_DATA_OFF_UE = 0x0017,
462 PCO_P_REL_DATA_SVC = 0x0018,
463};
464
465/* determine if PCO contains given protocol */
Stefan Sperling57238882018-07-19 19:27:59 +0200466static uint8_t *pco_contains_proto(struct ul255_t *pco, size_t offset, uint16_t prot, size_t prot_minlen)
Harald Welte1ae98772017-08-09 20:28:52 +0200467{
Stefan Sperling57238882018-07-19 19:27:59 +0200468 uint8_t *cur = pco->v + 1 + offset;
Harald Welte1ae98772017-08-09 20:28:52 +0200469
470 /* iterate over PCO and check if protocol contained */
Pau Espin Pedrol0ab62fe2017-08-30 15:51:24 +0200471 while (cur + 3 <= pco->v + pco->l) {
Harald Welte1ae98772017-08-09 20:28:52 +0200472 uint16_t cur_prot = osmo_load16be(cur);
473 uint8_t cur_len = cur[2];
Stefan Sperlingd70ab972018-07-19 15:25:47 +0200474 if (cur_prot == prot && cur_len >= prot_minlen)
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100475 return cur;
Pau Espin Pedrol0ab62fe2017-08-30 15:51:24 +0200476 cur += cur_len + 3;
Harald Welte1ae98772017-08-09 20:28:52 +0200477 }
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100478 return NULL;
Harald Welte1ae98772017-08-09 20:28:52 +0200479}
480
Pau Espin Pedrol7d54ed42018-01-25 20:09:16 +0100481/*! Get the peer of pdp based on IP version used.
482 * \param[in] pdp PDP context to select the peer from.
483 * \param[in] v4v6 IP version to select. Valid values are 4 and 6.
484 * \returns The selected peer matching the given IP version. NULL if not present.
485 */
486static struct ippoolm_t *pdp_get_peer_ipv(struct pdp_t *pdp, bool is_ipv6) {
487 uint8_t len1, len2, i;
488
489 if (is_ipv6) {
490 len1 = 8;
491 len2 = 16;
492 } else {
493 len1 = sizeof(struct in_addr);
494 len2 = len1;
495 }
496
497 for (i = 0; i < 2; i++) {
498 struct ippoolm_t * ippool = pdp->peer[i];
499 if (ippool && (ippool->addr.len == len1 || ippool->addr.len == len2))
500 return ippool;
501 }
502 return NULL;
503}
504
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100505/* construct an IPCP PCO response from request*/
Harald Welteffa22732019-04-10 14:30:21 +0200506static void build_ipcp_pco(const struct apn_ctx *apn, struct pdp_t *pdp, struct msgb *msg)
Harald Weltedda21ed2017-08-12 15:07:02 +0200507{
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100508 const struct in46_addr *dns1 = &apn->v4.cfg.dns[0];
509 const struct in46_addr *dns2 = &apn->v4.cfg.dns[1];
Stefan Sperlingd70ab972018-07-19 15:25:47 +0200510 uint8_t *ipcp;
511 uint16_t ipcp_len;
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100512 uint8_t *len1, *len2, *pco_ipcp;
Harald Weltedda21ed2017-08-12 15:07:02 +0200513 unsigned int len_appended;
Stefan Sperlingd70ab972018-07-19 15:25:47 +0200514 ptrdiff_t consumed;
Stefan Sperling57238882018-07-19 19:27:59 +0200515 size_t remain, offset = 0;
Harald Weltedda21ed2017-08-12 15:07:02 +0200516
Stefan Sperlingd70ab972018-07-19 15:25:47 +0200517 /* pco_contains_proto() returns a potentially unaligned pointer into pco_req->v (see OS#3194) */
Stefan Sperling57238882018-07-19 19:27:59 +0200518 pco_ipcp = pco_contains_proto(&pdp->pco_req, offset, PCO_P_IPCP, sizeof(struct ipcp_hdr));
519 while (pco_ipcp) {
520 uint8_t *start = msg->tail;
Stefan Sperlingd70ab972018-07-19 15:25:47 +0200521
Stefan Sperling57238882018-07-19 19:27:59 +0200522 ipcp = (pco_ipcp + 3); /* 2=type + 1=len */
523 consumed = (ipcp - &pdp->pco_req.v[0]);
524 remain = sizeof(pdp->pco_req.v) - consumed;
525 ipcp_len = osmo_load16be(ipcp + 2); /* 1=code + 1=id */
526 if (remain < 0 || remain < ipcp_len)
527 return;
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100528
Stefan Sperling57238882018-07-19 19:27:59 +0200529 /* Three byte T16L header */
530 msgb_put_u16(msg, 0x8021); /* IPCP */
531 len1 = msgb_put(msg, 1); /* Length of contents: delay */
Harald Weltedda21ed2017-08-12 15:07:02 +0200532
Stefan Sperling57238882018-07-19 19:27:59 +0200533 msgb_put_u8(msg, 0x02); /* ACK */
534 msgb_put_u8(msg, ipcp[1]); /* ID: Needs to match request */
535 msgb_put_u8(msg, 0x00); /* Length MSB */
536 len2 = msgb_put(msg, 1); /* Length LSB: delay */
Harald Weltedda21ed2017-08-12 15:07:02 +0200537
Stefan Sperling57238882018-07-19 19:27:59 +0200538 if (dns1->len == 4 && ipcp_contains_option(ipcp, ipcp_len, IPCP_OPT_PRIMARY_DNS, 4)) {
539 msgb_put_u8(msg, 0x81); /* DNS1 Tag */
540 msgb_put_u8(msg, 2 + dns1->len);/* DNS1 Length, incl. TL */
541 msgb_put_u32(msg, ntohl(dns1->v4.s_addr));
542 }
543
544 if (dns2->len == 4 && ipcp_contains_option(ipcp, ipcp_len, IPCP_OPT_SECONDARY_DNS, 4)) {
545 msgb_put_u8(msg, 0x83); /* DNS2 Tag */
546 msgb_put_u8(msg, 2 + dns2->len);/* DNS2 Length, incl. TL */
547 msgb_put_u32(msg, ntohl(dns2->v4.s_addr));
548 }
549
550 /* patch in length values */
551 len_appended = msg->tail - start;
552 *len1 = len_appended - 3;
553 *len2 = len_appended - 3;
554
555 offset += 3 + ipcp_len;
556 pco_ipcp = pco_contains_proto(&pdp->pco_req, offset, PCO_P_IPCP, sizeof(struct ipcp_hdr));
Harald Weltedda21ed2017-08-12 15:07:02 +0200557 }
558
Harald Weltedda21ed2017-08-12 15:07:02 +0200559}
560
Harald Welte1ae98772017-08-09 20:28:52 +0200561/* process one PCO request from a MS/UE, putting together the proper responses */
Harald Welteffa22732019-04-10 14:30:21 +0200562static void process_pco(const struct apn_ctx *apn, struct pdp_t *pdp)
Harald Welte1ae98772017-08-09 20:28:52 +0200563{
564 struct msgb *msg = msgb_alloc(256, "PCO");
Pau Espin Pedrol4ae8d822018-01-26 17:51:55 +0100565 struct ippoolm_t *peer_v4 = pdp_get_peer_ipv(pdp, false);
Harald Weltedda21ed2017-08-12 15:07:02 +0200566 unsigned int i;
567
568 OSMO_ASSERT(msg);
Harald Welte1ae98772017-08-09 20:28:52 +0200569 msgb_put_u8(msg, 0x80); /* ext-bit + configuration protocol byte */
570
Pau Espin Pedrol4ae8d822018-01-26 17:51:55 +0100571 if (peer_v4)
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100572 build_ipcp_pco(apn, pdp, msg);
Harald Welte1ae98772017-08-09 20:28:52 +0200573
Stefan Sperling57238882018-07-19 19:27:59 +0200574 if (pco_contains_proto(&pdp->pco_req, 0, PCO_P_DNS_IPv6_ADDR, 0)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200575 for (i = 0; i < ARRAY_SIZE(apn->v6.cfg.dns); i++) {
Harald Welteffa22732019-04-10 14:30:21 +0200576 const struct in46_addr *i46a = &apn->v6.cfg.dns[i];
Harald Weltedda21ed2017-08-12 15:07:02 +0200577 if (i46a->len != 16)
578 continue;
579 msgb_t16lv_put(msg, PCO_P_DNS_IPv6_ADDR, i46a->len, i46a->v6.s6_addr);
580 }
Harald Welte1ae98772017-08-09 20:28:52 +0200581 }
582
Stefan Sperling57238882018-07-19 19:27:59 +0200583 if (pco_contains_proto(&pdp->pco_req, 0, PCO_P_DNS_IPv4_ADDR, 0)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200584 for (i = 0; i < ARRAY_SIZE(apn->v4.cfg.dns); i++) {
Harald Welteffa22732019-04-10 14:30:21 +0200585 const struct in46_addr *i46a = &apn->v4.cfg.dns[i];
Harald Weltedda21ed2017-08-12 15:07:02 +0200586 if (i46a->len != 4)
587 continue;
588 msgb_t16lv_put(msg, PCO_P_DNS_IPv4_ADDR, i46a->len, (uint8_t *)&i46a->v4);
589 }
Harald Welte1ae98772017-08-09 20:28:52 +0200590 }
591
592 if (msgb_length(msg) > 1) {
593 memcpy(pdp->pco_neg.v, msgb_data(msg), msgb_length(msg));
594 pdp->pco_neg.l = msgb_length(msg);
595 } else
596 pdp->pco_neg.l = 0;
597
598 msgb_free(msg);
599}
600
Harald Welte9d9d91b2017-10-14 16:22:16 +0200601static bool apn_supports_ipv4(const struct apn_ctx *apn)
602{
603 if (apn->v4.cfg.static_prefix.addr.len || apn->v4.cfg.dynamic_prefix.addr.len)
604 return true;
605 return false;
606}
607
608static bool apn_supports_ipv6(const struct apn_ctx *apn)
609{
610 if (apn->v6.cfg.static_prefix.addr.len || apn->v6.cfg.dynamic_prefix.addr.len)
611 return true;
612 return false;
613}
614
Harald Weltebed35df2011-11-02 13:06:18 +0100615int create_context_ind(struct pdp_t *pdp)
616{
Harald Weltedda21ed2017-08-12 15:07:02 +0200617 static char name_buf[256];
618 struct gsn_t *gsn = pdp->gsn;
619 struct ggsn_ctx *ggsn = gsn->priv;
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100620 struct in46_addr addr[2];
Pau Espin Pedrol4e43ef52018-01-26 18:12:19 +0100621 struct ippoolm_t *member = NULL, *addrv4 = NULL, *addrv6 = NULL;
622 char straddrv4[INET_ADDRSTRLEN], straddrv6[INET6_ADDRSTRLEN];
Harald Weltedda21ed2017-08-12 15:07:02 +0200623 struct apn_ctx *apn;
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100624 int rc, num_addr, i;
jjako52c24142002-12-16 13:33:51 +0000625
Harald Weltedda21ed2017-08-12 15:07:02 +0200626 osmo_apn_to_str(name_buf, pdp->apn_req.v, pdp->apn_req.l);
627
628 LOGPPDP(LOGL_DEBUG, pdp, "Processing create PDP context request for APN '%s'\n", name_buf);
629
630 /* First find an exact APN name match */
631 apn = ggsn_find_apn(ggsn, name_buf);
Harald Welte2e84d2c2017-10-01 13:36:52 +0800632 /* ignore if the APN has not been started */
Pau Espin Pedrol958256f2017-10-11 20:32:55 +0200633 if (apn && !apn->started)
Harald Welte2e84d2c2017-10-01 13:36:52 +0800634 apn = NULL;
Harald Welteb16c46b2017-10-01 18:28:18 +0800635
Harald Weltedda21ed2017-08-12 15:07:02 +0200636 /* then try default (if any) */
637 if (!apn)
638 apn = ggsn->cfg.default_apn;
Harald Welteb16c46b2017-10-01 18:28:18 +0800639 /* ignore if the APN has not been started */
Pau Espin Pedrol958256f2017-10-11 20:32:55 +0200640 if (apn && !apn->started)
Harald Welteb16c46b2017-10-01 18:28:18 +0800641 apn = NULL;
642
Harald Weltedda21ed2017-08-12 15:07:02 +0200643 if (!apn) {
644 /* no APN found for what user requested */
645 LOGPPDP(LOGL_NOTICE, pdp, "Unknown APN '%s', rejecting\n", name_buf);
646 gtp_create_context_resp(gsn, pdp, GTPCAUSE_MISSING_APN);
647 return 0;
648 }
jjako52c24142002-12-16 13:33:51 +0000649
Harald Welted9d88622017-08-04 00:22:35 +0200650 /* FIXME: we manually force all context requests to dynamic here! */
651 if (pdp->eua.l > 2)
652 pdp->eua.l = 2;
jjako52c24142002-12-16 13:33:51 +0000653
Harald Weltebed35df2011-11-02 13:06:18 +0100654 memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_req0));
jjako52c24142002-12-16 13:33:51 +0000655
Harald Weltebed35df2011-11-02 13:06:18 +0100656 memcpy(pdp->qos_neg.v, pdp->qos_req.v, pdp->qos_req.l); /* TODO */
657 pdp->qos_neg.l = pdp->qos_req.l;
jjako52c24142002-12-16 13:33:51 +0000658
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100659 memset(addr, 0, sizeof(addr));
660 if ((num_addr = in46a_from_eua(&pdp->eua, addr)) < 0) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200661 LOGPPDP(LOGL_ERROR, pdp, "Cannot decode EUA from MS/SGSN: %s\n",
Harald Welted1bf1e12017-08-03 00:00:23 +0200662 osmo_hexdump(pdp->eua.v, pdp->eua.l));
663 gtp_create_context_resp(gsn, pdp, GTPCAUSE_UNKNOWN_PDP);
664 return 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100665 }
jjakoa7cd2492003-04-11 09:40:12 +0000666
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100667 /* Allocate dynamic addresses from the pool */
668 for (i = 0; i < num_addr; i++) {
669 if (addr[i].len == sizeof(struct in_addr)) {
670 /* does this APN actually have an IPv4 pool? */
671 if (!apn_supports_ipv4(apn))
672 goto err_wrong_af;
Harald Welte9d9d91b2017-10-14 16:22:16 +0200673
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100674 rc = ippool_newip(apn->v4.pool, &member, &addr[i], 0);
675 if (rc < 0)
676 goto err_pool_full;
677 /* copy back */
678 memcpy(&addr[i].v4.s_addr, &member->addr.v4, 4);
jjakoa7cd2492003-04-11 09:40:12 +0000679
Pau Espin Pedrol4e43ef52018-01-26 18:12:19 +0100680 addrv4 = member;
681
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100682 } else if (addr[i].len == sizeof(struct in6_addr)) {
683
684 /* does this APN actually have an IPv6 pool? */
685 if (!apn_supports_ipv6(apn))
686 goto err_wrong_af;
687
688 rc = ippool_newip(apn->v6.pool, &member, &addr[i], 0);
689 if (rc < 0)
690 goto err_pool_full;
691
692 /* IPv6 doesn't really send the real/allocated address at this point, but just
693 * the link-identifier which the MS shall use for router solicitation */
694 /* initialize upper 64 bits to prefix, they are discarded by MS anyway */
695 memcpy(addr[i].v6.s6_addr, &member->addr.v6, 8);
696 /* use allocated 64bit prefix as lower 64bit, used as link id by MS */
697 memcpy(addr[i].v6.s6_addr+8, &member->addr.v6, 8);
Pau Espin Pedrol4e43ef52018-01-26 18:12:19 +0100698
699 addrv6 = member;
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100700 } else
701 OSMO_ASSERT(0);
702
703 pdp->peer[i] = member;
704 member->peer = pdp;
705 }
706
707 in46a_to_eua(addr, num_addr, &pdp->eua);
708
Harald Welte546884d2018-04-25 21:13:06 +0200709 if (apn->cfg.gtpu_mode == APN_GTPU_MODE_KERNEL_GTP && apn_supports_ipv4(apn)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200710 /* TODO: In IPv6, EUA doesn't contain the actual IP addr/prefix! */
Harald Welte698a2332017-11-08 15:09:58 +0900711 if (gtp_kernel_tunnel_add(pdp, apn->tun.cfg.dev_name) < 0) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200712 LOGPPDP(LOGL_ERROR, pdp, "Cannot add tunnel to kernel: %s\n", strerror(errno));
713 gtp_create_context_resp(gsn, pdp, GTPCAUSE_SYS_FAIL);
714 return 0;
715 }
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100716 }
Harald Welte9d9d91b2017-10-14 16:22:16 +0200717
Harald Weltedda21ed2017-08-12 15:07:02 +0200718 pdp->ipif = apn->tun.tun; /* TODO */
Harald Welte698a2332017-11-08 15:09:58 +0900719 pdp->priv = apn;
Max3142d8d2017-05-04 17:45:10 +0200720
Pau Espin Pedrol4e43ef52018-01-26 18:12:19 +0100721 /* TODO: change trap to send 2 IPs */
Max3142d8d2017-05-04 17:45:10 +0200722 if (!send_trap(gsn, pdp, member, "imsi-ass-ip")) { /* TRAP with IP assignment */
Max727417d2016-08-02 17:10:38 +0200723 gtp_create_context_resp(gsn, pdp, GTPCAUSE_NO_RESOURCES);
724 return 0;
725 }
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100726
Harald Weltedda21ed2017-08-12 15:07:02 +0200727 process_pco(apn, pdp);
Harald Welte1ae98772017-08-09 20:28:52 +0200728
Harald Welte93fed3b2017-09-24 11:43:17 +0800729 /* Transmit G-PDU sequence numbers (only) if configured in APN */
730 pdp->tx_gpdu_seq = apn->cfg.tx_gpdu_seq;
731
Pau Espin Pedrol4e43ef52018-01-26 18:12:19 +0100732 LOGPPDP(LOGL_INFO, pdp, "Successful PDP Context Creation: APN=%s(%s), TEIC=%u, IPv4=%s, IPv6=%s\n",
733 name_buf, apn->cfg.name, pdp->teic_own,
734 addrv4 ? inet_ntop(AF_INET, &addrv4->addr.v4, straddrv4, sizeof(straddrv4)) : "none",
735 addrv6 ? inet_ntop(AF_INET6, &addrv6->addr.v6, straddrv6, sizeof(straddrv6)) : "none");
Harald Weltebed35df2011-11-02 13:06:18 +0100736 gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ);
737 return 0; /* Success */
Harald Weltedda21ed2017-08-12 15:07:02 +0200738
739err_pool_full:
740 LOGPPDP(LOGL_ERROR, pdp, "Cannot allocate IP address from pool (full!)\n");
741 gtp_create_context_resp(gsn, pdp, -rc);
742 return 0; /* Already in use, or no more available */
Harald Welte9d9d91b2017-10-14 16:22:16 +0200743
744err_wrong_af:
745 LOGPPDP(LOGL_ERROR, pdp, "APN doesn't support requested EUA / AF type\n");
746 gtp_create_context_resp(gsn, pdp, GTPCAUSE_UNKNOWN_PDP);
747 return 0;
jjako52c24142002-12-16 13:33:51 +0000748}
749
Harald Weltedda21ed2017-08-12 15:07:02 +0200750/* Internet-originated IP packet, needs to be sent via GTP towards MS */
751static int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len)
Harald Weltebed35df2011-11-02 13:06:18 +0100752{
Harald Weltedda21ed2017-08-12 15:07:02 +0200753 struct apn_ctx *apn = tun->priv;
Harald Weltebed35df2011-11-02 13:06:18 +0100754 struct ippoolm_t *ipm;
Harald Welted12eab92017-08-02 19:49:47 +0200755 struct in46_addr dst;
Harald Welte63ebccd2017-08-02 21:10:09 +0200756 struct iphdr *iph = (struct iphdr *)pack;
Harald Weltea0d281d2017-08-02 21:48:16 +0200757 struct ip6_hdr *ip6h = (struct ip6_hdr *)pack;
Harald Weltedda21ed2017-08-12 15:07:02 +0200758 struct ippool_t *pool;
Pau Espin Pedrol134855c2018-01-30 16:04:53 +0100759 char straddr[INET6_ADDRSTRLEN];
Pau Espin Pedroldddbbaa2018-01-30 16:16:33 +0100760 uint8_t pref_offset;
jjakoc6762cf2004-04-28 14:52:58 +0000761
Pau Espin Pedrola4942e62018-01-30 16:01:27 +0100762 switch (iph->version) {
763 case 4:
Harald Welted12eab92017-08-02 19:49:47 +0200764 if (len < sizeof(*iph) || len < 4*iph->ihl)
765 return -1;
766 dst.len = 4;
Harald Welte63ebccd2017-08-02 21:10:09 +0200767 dst.v4.s_addr = iph->daddr;
Harald Weltedda21ed2017-08-12 15:07:02 +0200768 pool = apn->v4.pool;
Pau Espin Pedrola4942e62018-01-30 16:01:27 +0100769 break;
770 case 6:
Harald Welted4d6e092017-08-08 18:10:43 +0200771 /* Due to the fact that 3GPP requires an allocation of a
772 * /64 prefix to each MS, we must instruct
773 * ippool_getip() below to match only the leading /64
Pau Espin Pedroldddbbaa2018-01-30 16:16:33 +0100774 * prefix, i.e. the first 8 bytes of the address. If the ll addr
775 * is used, then the match should be done on the trailing 64
776 * bits. */
Harald Welted4d6e092017-08-08 18:10:43 +0200777 dst.len = 8;
Pau Espin Pedroldddbbaa2018-01-30 16:16:33 +0100778 pref_offset = IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_dst) ? 8 : 0;
779 memcpy(&dst.v6, ((uint8_t*)&ip6h->ip6_dst) + pref_offset, 8);
Harald Weltedda21ed2017-08-12 15:07:02 +0200780 pool = apn->v6.pool;
Pau Espin Pedrola4942e62018-01-30 16:01:27 +0100781 break;
782 default:
Pau Espin Pedrol55d639f2017-12-04 13:17:07 +0100783 LOGP(DTUN, LOGL_NOTICE, "non-IPv%u packet received from tun\n", iph->version);
Harald Welted12eab92017-08-02 19:49:47 +0200784 return -1;
785 }
jjakoc6762cf2004-04-28 14:52:58 +0000786
Harald Weltedda21ed2017-08-12 15:07:02 +0200787 /* IPv6 packet but no IPv6 pool, or IPv4 packet with no IPv4 pool */
788 if (!pool)
789 return 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100790
Max427699e2017-12-05 16:30:37 +0100791 DEBUGP(DTUN, "Received packet for APN(%s) from tun %s", apn->cfg.name, tun->devname);
Harald Weltedda21ed2017-08-12 15:07:02 +0200792
793 if (ippool_getip(pool, &ipm, &dst)) {
Pau Espin Pedrol134855c2018-01-30 16:04:53 +0100794 DEBUGPC(DTUN, " with no PDP contex! (%s)\n", iph->version == 4 ?
795 inet_ntop(AF_INET, &iph->saddr, straddr, sizeof(straddr)) :
796 inet_ntop(AF_INET6, &ip6h->ip6_src, straddr, sizeof(straddr)));
Harald Weltebed35df2011-11-02 13:06:18 +0100797 return 0;
798 }
Max427699e2017-12-05 16:30:37 +0100799 DEBUGPC(DTUN, "\n");
Harald Weltebed35df2011-11-02 13:06:18 +0100800
801 if (ipm->peer) /* Check if a peer protocol is defined */
Harald Weltedda21ed2017-08-12 15:07:02 +0200802 gtp_data_req(apn->ggsn->gsn, (struct pdp_t *)ipm->peer, pack, len);
Harald Weltebed35df2011-11-02 13:06:18 +0100803 return 0;
jjako52c24142002-12-16 13:33:51 +0000804}
805
Harald Welted46bcd22017-08-08 23:27:22 +0200806/* RFC3307 link-local scope multicast address */
807static const struct in6_addr all_router_mcast_addr = {
808 .s6_addr = { 0xff,0x02,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,2 }
809};
810
Harald Weltedda21ed2017-08-12 15:07:02 +0200811/* MS-originated GTP1-U packet, needs to be sent via TUN device */
812static int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len)
Harald Weltebed35df2011-11-02 13:06:18 +0100813{
Harald Welted46bcd22017-08-08 23:27:22 +0200814 struct iphdr *iph = (struct iphdr *)pack;
815 struct ip6_hdr *ip6h = (struct ip6_hdr *)pack;
Harald Weltef85fe972017-09-24 20:00:34 +0800816 struct tun_t *tun = (struct tun_t *)pdp->ipif;
817 struct apn_ctx *apn = tun->priv;
Pau Espin Pedrol5b1ef952018-01-25 20:50:59 +0100818 char straddr[INET6_ADDRSTRLEN];
Pau Espin Pedrol7d54ed42018-01-25 20:09:16 +0100819 struct ippoolm_t *peer;
Pau Espin Pedrol5b1ef952018-01-25 20:50:59 +0100820 uint8_t pref_offset;
Harald Weltef85fe972017-09-24 20:00:34 +0800821
822 OSMO_ASSERT(tun);
823 OSMO_ASSERT(apn);
Harald Welted46bcd22017-08-08 23:27:22 +0200824
Max427699e2017-12-05 16:30:37 +0100825 LOGPPDP(LOGL_DEBUG, pdp, "Packet received on APN(%s): forwarding to tun %s\n", apn->cfg.name, tun->devname);
Harald Welted46bcd22017-08-08 23:27:22 +0200826
827 switch (iph->version) {
828 case 6:
Pau Espin Pedrol7d54ed42018-01-25 20:09:16 +0100829 peer = pdp_get_peer_ipv(pdp, true);
830 if (!peer) {
831 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS IPv6 with unassigned EUA: %s\n",
832 osmo_hexdump(pack, len));
833 return -1;
834 }
835
Pau Espin Pedrol5b1ef952018-01-25 20:50:59 +0100836 /* Validate packet comes from IPaddr assigned to the pdp ctx.
837 If packet is a LL addr, then EUA is in the lower 64 bits,
838 otherwise it's used as the 64 prefix */
839 pref_offset = IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src) ? 8 : 0;
840 if (memcmp(((uint8_t*)&ip6h->ip6_src) + pref_offset, &peer->addr.v6, 8)) {
841 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS using unassigned src IPv6: %s\n",
842 inet_ntop(AF_INET6, &ip6h->ip6_src, straddr, sizeof(straddr)));
843 return -1;
844 }
845
Harald Welted46bcd22017-08-08 23:27:22 +0200846 /* daddr: all-routers multicast addr */
847 if (IN6_ARE_ADDR_EQUAL(&ip6h->ip6_dst, &all_router_mcast_addr))
Pau Espin Pedrol7d54ed42018-01-25 20:09:16 +0100848 return handle_router_mcast(pdp->gsn, pdp, &peer->addr.v6,
849 &apn->v6_lladdr, pack, len);
Harald Welted46bcd22017-08-08 23:27:22 +0200850 break;
851 case 4:
Pau Espin Pedrol7d54ed42018-01-25 20:09:16 +0100852 peer = pdp_get_peer_ipv(pdp, false);
853 if (!peer) {
854 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS IPv4 with unassigned EUA: %s\n",
855 osmo_hexdump(pack, len));
856 return -1;
857 }
Pau Espin Pedrol5b1ef952018-01-25 20:50:59 +0100858
859 /* Validate packet comes from IPaddr assigned to the pdp ctx */
860 if (memcmp(&iph->saddr, &peer->addr.v4, sizeof(peer->addr.v4))) {
861 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS using unassigned src IPv4: %s\n",
862 inet_ntop(AF_INET, &iph->saddr, straddr, sizeof(straddr)));
863 return -1;
864 }
Harald Welted46bcd22017-08-08 23:27:22 +0200865 break;
866 default:
Harald Weltedda21ed2017-08-12 15:07:02 +0200867 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS is neither IPv4 nor IPv6: %s\n",
868 osmo_hexdump(pack, len));
Harald Welted46bcd22017-08-08 23:27:22 +0200869 return -1;
870 }
Harald Weltebed35df2011-11-02 13:06:18 +0100871 return tun_encaps((struct tun_t *)pdp->ipif, pack, len);
jjako52c24142002-12-16 13:33:51 +0000872}
873
Harald Welte632e8432017-09-05 18:12:14 +0200874static char *config_file = "osmo-ggsn.cfg";
Harald Weltedda21ed2017-08-12 15:07:02 +0200875
876/* callback for tun device osmocom select loop integration */
877static int ggsn_tun_fd_cb(struct osmo_fd *fd, unsigned int what)
878{
879 struct apn_ctx *apn = fd->data;
880
881 OSMO_ASSERT(what & BSC_FD_READ);
882
883 return tun_decaps(apn->tun.tun);
884}
885
886/* callback for libgtp osmocom select loop integration */
887static int ggsn_gtp_fd_cb(struct osmo_fd *fd, unsigned int what)
888{
889 struct ggsn_ctx *ggsn = fd->data;
890 int rc;
891
892 OSMO_ASSERT(what & BSC_FD_READ);
893
894 switch (fd->priv_nr) {
895 case 0:
896 rc = gtp_decaps0(ggsn->gsn);
897 break;
898 case 1:
899 rc = gtp_decaps1c(ggsn->gsn);
900 break;
901 case 2:
902 rc = gtp_decaps1u(ggsn->gsn);
903 break;
904 default:
905 OSMO_ASSERT(0);
906 break;
907 }
908 return rc;
909}
910
911static void ggsn_gtp_tmr_start(struct ggsn_ctx *ggsn)
912{
913 struct timeval next;
914
915 /* Retrieve next retransmission as timeval */
916 gtp_retranstimeout(ggsn->gsn, &next);
917
918 /* re-schedule the timer */
919 osmo_timer_schedule(&ggsn->gtp_timer, next.tv_sec, next.tv_usec/1000);
920}
921
922/* timer callback for libgtp retransmission and ping */
923static void ggsn_gtp_tmr_cb(void *data)
924{
925 struct ggsn_ctx *ggsn = data;
926
927 /* do all the retransmissions as needed */
928 gtp_retrans(ggsn->gsn);
929
930 ggsn_gtp_tmr_start(ggsn);
931}
932
933/* To exit gracefully. Used with GCC compilation flag -pg and gprof */
934static void signal_handler(int s)
935{
936 LOGP(DGGSN, LOGL_NOTICE, "signal %d received\n", s);
937 switch (s) {
938 case SIGINT:
Harald Weltee8049472017-08-20 12:44:21 +0200939 case SIGTERM:
Harald Weltedda21ed2017-08-12 15:07:02 +0200940 LOGP(DGGSN, LOGL_NOTICE, "SIGINT received, shutting down\n");
941 end = 1;
942 break;
943 case SIGABRT:
944 case SIGUSR1:
945 talloc_report(tall_vty_ctx, stderr);
946 talloc_report_full(tall_ggsn_ctx, stderr);
947 break;
948 case SIGUSR2:
949 talloc_report_full(tall_vty_ctx, stderr);
950 break;
951 default:
952 break;
953 }
954}
955
956
957/* Start a given GGSN */
958int ggsn_start(struct ggsn_ctx *ggsn)
959{
960 struct apn_ctx *apn;
961 int rc;
962
963 if (ggsn->started)
964 return 0;
965
966 LOGPGGSN(LOGL_INFO, ggsn, "Starting GGSN\n");
967
968 /* Start libgtp listener */
969 if (gtp_new(&ggsn->gsn, ggsn->cfg.state_dir, &ggsn->cfg.listen_addr.v4, GTP_MODE_GGSN)) {
970 LOGPGGSN(LOGL_ERROR, ggsn, "Failed to create GTP: %s\n", strerror(errno));
971 return -1;
972 }
973 ggsn->gsn->priv = ggsn;
974
Harald Welte98146772017-09-05 17:41:20 +0200975 /* patch in different addresses to use (in case we're behind NAT, the listen
976 * address is different from what we advertise externally) */
977 if (ggsn->cfg.gtpc_addr.v4.s_addr)
978 ggsn->gsn->gsnc = ggsn->cfg.gtpc_addr.v4;
979
980 if (ggsn->cfg.gtpu_addr.v4.s_addr)
981 ggsn->gsn->gsnu = ggsn->cfg.gtpu_addr.v4;
982
Harald Weltedda21ed2017-08-12 15:07:02 +0200983 /* Register File Descriptors */
984 osmo_fd_setup(&ggsn->gtp_fd0, ggsn->gsn->fd0, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 0);
985 rc = osmo_fd_register(&ggsn->gtp_fd0);
986 OSMO_ASSERT(rc == 0);
987
988 osmo_fd_setup(&ggsn->gtp_fd1c, ggsn->gsn->fd1c, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 1);
989 rc = osmo_fd_register(&ggsn->gtp_fd1c);
990 OSMO_ASSERT(rc == 0);
991
992 osmo_fd_setup(&ggsn->gtp_fd1u, ggsn->gsn->fd1u, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 2);
993 rc = osmo_fd_register(&ggsn->gtp_fd1u);
994 OSMO_ASSERT(rc == 0);
995
996 /* Start GTP re-transmission timer */
997 osmo_timer_setup(&ggsn->gtp_timer, ggsn_gtp_tmr_cb, ggsn);
998
999 gtp_set_cb_data_ind(ggsn->gsn, encaps_tun);
1000 gtp_set_cb_delete_context(ggsn->gsn, delete_context);
1001 gtp_set_cb_create_context_ind(ggsn->gsn, create_context_ind);
1002
1003 LOGPGGSN(LOGL_NOTICE, ggsn, "Successfully started\n");
1004 ggsn->started = true;
1005
1006 llist_for_each_entry(apn, &ggsn->apn_list, list)
1007 apn_start(apn);
1008
1009 return 0;
1010}
1011
1012/* Stop a given GGSN */
1013int ggsn_stop(struct ggsn_ctx *ggsn)
1014{
1015 struct apn_ctx *apn;
1016
1017 if (!ggsn->started)
1018 return 0;
1019
1020 /* iterate over all APNs and stop them */
1021 llist_for_each_entry(apn, &ggsn->apn_list, list)
1022 apn_stop(apn, true);
1023
1024 osmo_timer_del(&ggsn->gtp_timer);
1025
1026 osmo_fd_unregister(&ggsn->gtp_fd1u);
1027 osmo_fd_unregister(&ggsn->gtp_fd1c);
1028 osmo_fd_unregister(&ggsn->gtp_fd0);
1029
1030 if (ggsn->gsn) {
1031 gtp_free(ggsn->gsn);
1032 ggsn->gsn = NULL;
1033 }
1034
1035 ggsn->started = false;
1036 return 0;
1037}
1038
1039static void print_usage()
1040{
1041 printf("Usage: osmo-ggsn [-h] [-D] [-c configfile] [-V]\n");
1042}
1043
1044static void print_help()
1045{
1046 printf( " Some useful help...\n"
1047 " -h --help This help text\n"
1048 " -D --daemonize Fork the process into a background daemon\n"
1049 " -c --config-file filename The config file to use\n"
1050 " -V --version Print the version of OsmoGGSN\n"
1051 );
1052}
1053
1054static void handle_options(int argc, char **argv)
1055{
1056 while (1) {
1057 int option_index = 0, c;
1058 static struct option long_options[] = {
1059 { "help", 0, 0, 'h' },
1060 { "daemonize", 0, 0, 'D' },
1061 { "config-file", 1, 0, 'c' },
1062 { "version", 0, 0, 'V' },
1063 { 0, 0, 0, 0 }
1064 };
1065
1066 c = getopt_long(argc, argv, "hdc:V", long_options, &option_index);
1067 if (c == -1)
1068 break;
1069
1070 switch (c) {
1071 case 'h':
1072 print_usage();
1073 print_help();
1074 exit(0);
1075 case 'D':
1076 daemonize = 1;
1077 break;
1078 case 'c':
1079 config_file = optarg;
1080 break;
1081 case 'V':
1082 print_version(1);
1083 exit(0);
1084 break;
1085 }
1086 }
1087}
1088
jjako52c24142002-12-16 13:33:51 +00001089int main(int argc, char **argv)
1090{
Harald Weltedda21ed2017-08-12 15:07:02 +02001091 struct ggsn_ctx *ggsn;
1092 int rc;
jjako52c24142002-12-16 13:33:51 +00001093
Harald Welte632e8432017-09-05 18:12:14 +02001094 tall_ggsn_ctx = talloc_named_const(NULL, 0, "OsmoGGSN");
Harald Weltedda21ed2017-08-12 15:07:02 +02001095 msgb_talloc_ctx_init(tall_ggsn_ctx, 0);
Harald Welte3e443ca2018-02-14 01:04:04 +01001096 g_vty_info.tall_ctx = tall_ggsn_ctx;
jjako52c24142002-12-16 13:33:51 +00001097
Harald Weltee8049472017-08-20 12:44:21 +02001098 /* Handle keyboard interrupt SIGINT */
Harald Weltedda21ed2017-08-12 15:07:02 +02001099 signal(SIGINT, &signal_handler);
Harald Weltee8049472017-08-20 12:44:21 +02001100 signal(SIGTERM, &signal_handler);
Harald Weltedda21ed2017-08-12 15:07:02 +02001101 signal(SIGABRT, &signal_handler);
1102 signal(SIGUSR1, &signal_handler);
1103 signal(SIGUSR2, &signal_handler);
jjako52c24142002-12-16 13:33:51 +00001104
Harald Weltedda21ed2017-08-12 15:07:02 +02001105 osmo_init_ignore_signals();
Pau Espin Pedrol042a4452018-04-17 14:31:42 +02001106 osmo_init_logging2(tall_ggsn_ctx, &log_info);
Harald Weltedda21ed2017-08-12 15:07:02 +02001107 osmo_stats_init(tall_ggsn_ctx);
jjako0141d202004-01-09 15:19:20 +00001108
Harald Weltedda21ed2017-08-12 15:07:02 +02001109 vty_init(&g_vty_info);
1110 logging_vty_add_cmds(NULL);
Harald Welte3e443ca2018-02-14 01:04:04 +01001111 osmo_talloc_vty_add_cmds();
Harald Weltedda21ed2017-08-12 15:07:02 +02001112 osmo_stats_vty_add_cmds(&log_info);
1113 ggsn_vty_init();
1114 ctrl_vty_init(tall_ggsn_ctx);
1115
1116 handle_options(argc, argv);
1117
1118 rate_ctr_init(tall_ggsn_ctx);
1119
1120 rc = vty_read_config_file(config_file, NULL);
1121 if (rc < 0) {
1122 fprintf(stderr, "Failed to open config file: '%s'\n", config_file);
1123 exit(2);
Harald Weltebed35df2011-11-02 13:06:18 +01001124 }
jjako52c24142002-12-16 13:33:51 +00001125
Harald Weltedda21ed2017-08-12 15:07:02 +02001126 rc = telnet_init_dynif(tall_ggsn_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_GGSN);
1127 if (rc < 0)
Harald Weltebed35df2011-11-02 13:06:18 +01001128 exit(1);
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +01001129
Pau Espin Pedrol3e0baa62018-06-19 11:50:02 +02001130 g_ctrlh = ctrl_interface_setup_dynip(NULL, ctrl_vty_get_bind_addr(),
1131 OSMO_CTRL_PORT_GGSN, NULL);
Harald Weltedda21ed2017-08-12 15:07:02 +02001132 if (!g_ctrlh) {
1133 LOGP(DGGSN, LOGL_ERROR, "Failed to create CTRL interface.\n");
Harald Weltebed35df2011-11-02 13:06:18 +01001134 exit(1);
1135 }
jjako88c22162003-07-06 19:33:18 +00001136
Harald Weltedda21ed2017-08-12 15:07:02 +02001137 if (daemonize) {
1138 rc = osmo_daemonize();
1139 if (rc < 0) {
1140 perror("Error during daemonize");
Harald Weltebed35df2011-11-02 13:06:18 +01001141 exit(1);
1142 }
1143 }
jjako1d3db972004-01-16 09:56:56 +00001144
Harald Weltedda21ed2017-08-12 15:07:02 +02001145#if 0
Harald Weltebed35df2011-11-02 13:06:18 +01001146 /* qos */
1147 qos.l = 3;
1148 qos.v[2] = (args_info.qos_arg) & 0xff;
1149 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
1150 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
Harald Weltedda21ed2017-08-12 15:07:02 +02001151#endif
jjakoa7cd2492003-04-11 09:40:12 +00001152
Harald Weltedda21ed2017-08-12 15:07:02 +02001153 /* Main select loop */
1154 while (!end) {
1155 osmo_select_main(0);
Harald Weltebed35df2011-11-02 13:06:18 +01001156 }
jjakoe0149782003-07-06 17:07:04 +00001157
Harald Weltedda21ed2017-08-12 15:07:02 +02001158 llist_for_each_entry(ggsn, &g_ggsn_list, list)
1159 ggsn_stop(ggsn);
Harald Weltebed35df2011-11-02 13:06:18 +01001160
Max3fc9cc92019-03-14 11:16:55 +01001161 return 0;
jjako52c24142002-12-16 13:33:51 +00001162}