blob: 8b576ab35bf34c061c6d0b3209ca1dec7fe7a3f1 [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>
59#include <osmocom/gsm/apn.h>
Max727417d2016-08-02 17:10:38 +020060
Emmanuel Bretelle2a103682010-09-07 17:01:20 +020061#include "../lib/tun.h"
62#include "../lib/ippool.h"
63#include "../lib/syserr.h"
Harald Welted12eab92017-08-02 19:49:47 +020064#include "../lib/in46_addr.h"
jjako52c24142002-12-16 13:33:51 +000065#include "../gtp/pdp.h"
66#include "../gtp/gtp.h"
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010067#include "gtp-kernel.h"
Harald Welted46bcd22017-08-08 23:27:22 +020068#include "icmpv6.h"
Harald Weltedda21ed2017-08-12 15:07:02 +020069#include "ggsn.h"
jjako52c24142002-12-16 13:33:51 +000070
Harald Weltedda21ed2017-08-12 15:07:02 +020071void *tall_ggsn_ctx;
jjakoa7cd2492003-04-11 09:40:12 +000072
Harald Weltedda21ed2017-08-12 15:07:02 +020073static int end = 0;
74static int daemonize = 0;
75static struct ctrl_handle *g_ctrlh;
76
jjakoa7cd2492003-04-11 09:40:12 +000077struct ul255_t qos;
78struct ul255_t apn;
79
Harald Weltedda21ed2017-08-12 15:07:02 +020080#define LOGPAPN(level, apn, fmt, args...) \
81 LOGP(DGGSN, level, "APN(%s): " fmt, (apn)->cfg.name, ## args)
jjako52c24142002-12-16 13:33:51 +000082
Harald Weltedda21ed2017-08-12 15:07:02 +020083#define LOGPGGSN(level, ggsn, fmt, args...) \
84 LOGP(DGGSN, level, "GGSN(%s): " fmt, (ggsn)->cfg.name, ## args)
85
Max6a215272017-09-25 10:35:34 +020086#define LOGPPDP(level, pdp, fmt, args...) LOGPDPX(DGGSN, level, pdp, fmt, ## args)
Harald Weltedda21ed2017-08-12 15:07:02 +020087
88static int ggsn_tun_fd_cb(struct osmo_fd *fd, unsigned int what);
89static int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len);
90
91
92static void pool_close_all_pdp(struct ippool_t *pool)
Harald Weltebed35df2011-11-02 13:06:18 +010093{
Harald Weltedda21ed2017-08-12 15:07:02 +020094 unsigned int i;
jjako52c24142002-12-16 13:33:51 +000095
Harald Weltedda21ed2017-08-12 15:07:02 +020096 if (!pool)
Harald Weltebed35df2011-11-02 13:06:18 +010097 return;
Harald Weltedda21ed2017-08-12 15:07:02 +020098
99 for (i = 0; i < pool->listsize; i++) {
100 struct ippoolm_t *member = &pool->member[i];
101 struct pdp_t *pdp;
102
103 if (!member->inuse)
104 continue;
105 pdp = member->peer;
106 if (!pdp)
107 continue;
108 LOGPPDP(LOGL_DEBUG, pdp, "Sending DELETE PDP CTX due to shutdown\n");
109 gtp_delete_context_req(pdp->gsn, pdp, NULL, 1);
Harald Weltebed35df2011-11-02 13:06:18 +0100110 }
jjako52c24142002-12-16 13:33:51 +0000111}
112
Harald Weltedda21ed2017-08-12 15:07:02 +0200113int apn_stop(struct apn_ctx *apn, bool force)
Harald Weltebed35df2011-11-02 13:06:18 +0100114{
Harald Weltedda21ed2017-08-12 15:07:02 +0200115 LOGPAPN(LOGL_NOTICE, apn, "%sStopping\n", force ? "FORCED " : "");
116 /* check if pools have any active PDP contexts and bail out */
117 pool_close_all_pdp(apn->v4.pool);
118 pool_close_all_pdp(apn->v6.pool);
119
120 /* shutdown whatever old state might be left */
121 if (apn->tun.tun) {
122 /* run ip-down script */
123 if (apn->tun.cfg.ipdown_script) {
124 LOGPAPN( LOGL_INFO, apn, "Running %s\n", apn->tun.cfg.ipdown_script);
125 tun_runscript(apn->tun.tun, apn->tun.cfg.ipdown_script);
126 }
127 /* release tun device */
Harald Welteed08eb12017-10-01 18:15:56 +0800128 LOGPAPN(LOGL_INFO, apn, "Closing TUN device %s\n", apn->tun.tun->devname);
Harald Weltedda21ed2017-08-12 15:07:02 +0200129 osmo_fd_unregister(&apn->tun.fd);
130 tun_free(apn->tun.tun);
131 apn->tun.tun = NULL;
132 }
133
134 if (apn->v4.pool) {
135 LOGPAPN(LOGL_INFO, apn, "Releasing IPv4 pool\n");
136 ippool_free(apn->v4.pool);
137 apn->v4.pool = NULL;
138 }
139 if (apn->v6.pool) {
140 LOGPAPN(LOGL_INFO, apn, "Releasing IPv6 pool\n");
141 ippool_free(apn->v6.pool);
142 apn->v6.pool = NULL;
143 }
144
145 apn->started = false;
146 return 0;
147}
148
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200149
150static int alloc_ippool_blacklist(struct apn_ctx *apn, const struct tun_t *tun, struct in46_prefix **blacklist, bool ipv6)
151{
152
153 int flags, len, len2, i;
154
155 if (ipv6)
156 flags = IP_TYPE_IPv6_NONLINK;
157 else
158 flags = IP_TYPE_IPv4;
159
160 while (1) {
161 len = tun_ip_local_get(apn->tun.tun, NULL, 0, flags);
162 if (len < 1)
163 return len;
164
165 *blacklist = talloc_zero_size(apn, len * sizeof(struct in46_prefix));
166 len2 = tun_ip_local_get(apn->tun.tun, *blacklist, len, flags);
167 if (len2 < 1) {
168 talloc_free(*blacklist);
169 return len2;
170 }
171
172 if (len2 > len) /* iface was added between 2 calls, repeat operation */
173 talloc_free(*blacklist);
174 else
175 break;
176 }
177
178 for (i = 0; i < len2; i++)
179 LOGPAPN(LOGL_INFO, apn, "Blacklist tun IP %s\n",
180 in46p_ntoa(&(*blacklist)[i]));
181
182 return len2;
183}
184
Harald Weltedda21ed2017-08-12 15:07:02 +0200185/* actually start the APN with its current config */
186int apn_start(struct apn_ctx *apn)
187{
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200188 int ippool_flags = IPPOOL_NONETWORK | IPPOOL_NOBROADCAST;
Pau Espin Pedrola037e592017-10-16 14:41:37 +0200189 struct in46_prefix ipv6_tun_linklocal_ip;
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200190 struct in46_prefix *blacklist;
191 int blacklist_size;
192
Harald Weltedda21ed2017-08-12 15:07:02 +0200193 if (apn->started)
194 return 0;
195
196 LOGPAPN(LOGL_INFO, apn, "Starting\n");
197 switch (apn->cfg.gtpu_mode) {
198 case APN_GTPU_MODE_TUN:
199 LOGPAPN(LOGL_INFO, apn, "Opening TUN device %s\n", apn->tun.cfg.dev_name);
200 if (tun_new(&apn->tun.tun, apn->tun.cfg.dev_name)) {
201 LOGPAPN(LOGL_ERROR, apn, "Failed to configure tun device\n");
202 return -1;
203 }
204 LOGPAPN(LOGL_INFO, apn, "Opened TUN device %s\n", apn->tun.tun->devname);
205
206 /* Register with libosmcoore */
207 osmo_fd_setup(&apn->tun.fd, apn->tun.tun->fd, BSC_FD_READ, ggsn_tun_fd_cb, apn, 0);
208 osmo_fd_register(&apn->tun.fd);
209
210 /* Set TUN library callback */
211 tun_set_cb_ind(apn->tun.tun, cb_tun_ind);
212
213 if (apn->v4.cfg.ifconfig_prefix.addr.len) {
214 LOGPAPN(LOGL_INFO, apn, "Setting tun IP address %s\n",
215 in46p_ntoa(&apn->v4.cfg.ifconfig_prefix));
216 if (tun_setaddr(apn->tun.tun, &apn->v4.cfg.ifconfig_prefix.addr, NULL,
217 apn->v4.cfg.ifconfig_prefix.prefixlen)) {
218 LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv4 address %s: %s\n",
219 in46p_ntoa(&apn->v4.cfg.ifconfig_prefix), strerror(errno));
220 apn_stop(apn, false);
221 return -1;
222 }
223 }
224
225 if (apn->v6.cfg.ifconfig_prefix.addr.len) {
226 LOGPAPN(LOGL_INFO, apn, "Setting tun IPv6 address %s\n",
227 in46p_ntoa(&apn->v6.cfg.ifconfig_prefix));
228 if (tun_setaddr(apn->tun.tun, &apn->v6.cfg.ifconfig_prefix.addr, NULL,
229 apn->v6.cfg.ifconfig_prefix.prefixlen)) {
Harald Welteed1ba2c2017-10-01 18:16:17 +0800230 LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv6 address %s: %s. "
231 "Ensure you have ipv6 support and not used the disable_ipv6 sysctl?\n",
Harald Weltedda21ed2017-08-12 15:07:02 +0200232 in46p_ntoa(&apn->v6.cfg.ifconfig_prefix), strerror(errno));
233 apn_stop(apn, false);
234 return -1;
235 }
236 }
237
238 if (apn->tun.cfg.ipup_script) {
239 LOGPAPN(LOGL_INFO, apn, "Running ip-up script %s\n",
240 apn->tun.cfg.ipup_script);
241 tun_runscript(apn->tun.tun, apn->tun.cfg.ipup_script);
242 }
Harald Weltef85fe972017-09-24 20:00:34 +0800243
244 if (apn->cfg.apn_type_mask & (APN_TYPE_IPv6|APN_TYPE_IPv4v6)) {
Pau Espin Pedrola037e592017-10-16 14:41:37 +0200245 if (tun_ip_local_get(apn->tun.tun, &ipv6_tun_linklocal_ip, 1, IP_TYPE_IPv6_LINK) < 1) {
Harald Weltef85fe972017-09-24 20:00:34 +0800246 LOGPAPN(LOGL_ERROR, apn, "Cannot obtain IPv6 link-local address of "
247 "interface: %s\n", strerror(errno));
248 apn_stop(apn, false);
249 return -1;
250 }
Pau Espin Pedrola037e592017-10-16 14:41:37 +0200251 apn->v6_lladdr = ipv6_tun_linklocal_ip.addr.v6;
Harald Weltef85fe972017-09-24 20:00:34 +0800252 }
253
Harald Weltedda21ed2017-08-12 15:07:02 +0200254 /* set back-pointer from TUN device to APN */
255 apn->tun.tun->priv = apn;
256 break;
257 case APN_GTPU_MODE_KERNEL_GTP:
258 LOGPAPN(LOGL_ERROR, apn, "FIXME: Kernel GTP\n");
259#if 0
260 /* use GTP kernel module for data packet encapsulation */
261 if (gtp_kernel_init(gsn, &net.v4, prefixlen, net_arg) < 0)
262 goto err;
263#endif
Harald Weltebed35df2011-11-02 13:06:18 +0100264 break;
265 default:
Harald Weltedda21ed2017-08-12 15:07:02 +0200266 LOGPAPN(LOGL_ERROR, apn, "Unknown GTPU Mode %d\n", apn->cfg.gtpu_mode);
267 return -1;
Harald Weltebed35df2011-11-02 13:06:18 +0100268 }
jjako0141d202004-01-09 15:19:20 +0000269
Harald Weltedda21ed2017-08-12 15:07:02 +0200270 /* Create IPv4 pool */
271 if (apn->v4.cfg.dynamic_prefix.addr.len) {
272 LOGPAPN(LOGL_INFO, apn, "Creating IPv4 pool %s\n",
273 in46p_ntoa(&apn->v4.cfg.dynamic_prefix));
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200274 if ((blacklist_size = alloc_ippool_blacklist(apn, apn->tun.tun, &blacklist, false)) < 0)
275 LOGPAPN(LOGL_ERROR, apn, "Failed obtaining IPv4 tun IPs\n");
Harald Weltedda21ed2017-08-12 15:07:02 +0200276 if (ippool_new(&apn->v4.pool, &apn->v4.cfg.dynamic_prefix,
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200277 &apn->v4.cfg.static_prefix, ippool_flags,
278 blacklist, blacklist_size)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200279 LOGPAPN(LOGL_ERROR, apn, "Failed to create IPv4 pool\n");
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200280 talloc_free(blacklist);
Harald Weltedda21ed2017-08-12 15:07:02 +0200281 apn_stop(apn, false);
282 return -1;
283 }
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200284 talloc_free(blacklist);
Harald Weltebed35df2011-11-02 13:06:18 +0100285 }
Harald Weltedda21ed2017-08-12 15:07:02 +0200286
287 /* Create IPv6 pool */
288 if (apn->v6.cfg.dynamic_prefix.addr.len) {
289 LOGPAPN(LOGL_INFO, apn, "Creating IPv6 pool %s\n",
290 in46p_ntoa(&apn->v6.cfg.dynamic_prefix));
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200291 if ((blacklist_size = alloc_ippool_blacklist(apn, apn->tun.tun, &blacklist, true)) < 0)
292 LOGPAPN(LOGL_ERROR, apn, "Failed obtaining IPv6 tun IPs\n");
Harald Weltedda21ed2017-08-12 15:07:02 +0200293 if (ippool_new(&apn->v6.pool, &apn->v6.cfg.dynamic_prefix,
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200294 &apn->v6.cfg.static_prefix, ippool_flags,
295 blacklist, blacklist_size)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200296 LOGPAPN(LOGL_ERROR, apn, "Failed to create IPv6 pool\n");
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200297 talloc_free(blacklist);
Harald Weltedda21ed2017-08-12 15:07:02 +0200298 apn_stop(apn, false);
299 return -1;
300 }
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200301 talloc_free(blacklist);
Harald Weltedda21ed2017-08-12 15:07:02 +0200302 }
303
304 LOGPAPN(LOGL_NOTICE, apn, "Successfully started\n");
305 apn->started = true;
306 return 0;
jjako0141d202004-01-09 15:19:20 +0000307}
jjako0141d202004-01-09 15:19:20 +0000308
Max3142d8d2017-05-04 17:45:10 +0200309static bool send_trap(const struct gsn_t *gsn, const struct pdp_t *pdp, const struct ippoolm_t *member, const char *var)
310{
Harald Welted12eab92017-08-02 19:49:47 +0200311 char addrbuf[256];
Max3142d8d2017-05-04 17:45:10 +0200312 char val[NAMESIZE];
313
Harald Welted12eab92017-08-02 19:49:47 +0200314 const char *addrstr = in46a_ntop(&member->addr, addrbuf, sizeof(addrbuf));
315
Harald Welteb10ee082017-08-12 19:29:16 +0200316 snprintf(val, sizeof(val), "%s,%s", imsi_gtp2str(&pdp->imsi), addrstr);
Max3142d8d2017-05-04 17:45:10 +0200317
Harald Weltedda21ed2017-08-12 15:07:02 +0200318 if (ctrl_cmd_send_trap(g_ctrlh, var, val) < 0) {
319 LOGPPDP(LOGL_ERROR, pdp, "Failed to create and send TRAP %s\n", var);
Max3142d8d2017-05-04 17:45:10 +0200320 return false;
321 }
322 return true;
323}
324
Harald Weltedda21ed2017-08-12 15:07:02 +0200325static int delete_context(struct pdp_t *pdp)
Harald Weltebed35df2011-11-02 13:06:18 +0100326{
Harald Weltedda21ed2017-08-12 15:07:02 +0200327 struct gsn_t *gsn = pdp->gsn;
328 struct ippoolm_t *ipp = (struct ippoolm_t *)pdp->peer;
329
330 LOGPPDP(LOGL_INFO, pdp, "Deleting PDP context\n");
Max727417d2016-08-02 17:10:38 +0200331 struct ippoolm_t *member = pdp->peer;
Maxdbd70242016-10-14 13:38:05 +0200332
333 if (pdp->peer) {
Max3142d8d2017-05-04 17:45:10 +0200334 send_trap(gsn, pdp, member, "imsi-rem-ip"); /* TRAP with IP removal */
Harald Weltedda21ed2017-08-12 15:07:02 +0200335 ippool_freeip(ipp->pool, ipp);
Maxdbd70242016-10-14 13:38:05 +0200336 } else
Harald Weltedda21ed2017-08-12 15:07:02 +0200337 LOGPPDP(LOGL_ERROR, pdp, "Cannot find/free IP Pool member\n");
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100338
339 if (gtp_kernel_tunnel_del(pdp)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200340 LOGPPDP(LOGL_ERROR, pdp, "Cannot delete tunnel from kernel:%s\n",
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100341 strerror(errno));
342 }
343
Harald Weltebed35df2011-11-02 13:06:18 +0100344 return 0;
jjako52c24142002-12-16 13:33:51 +0000345}
346
Harald Welte1ae98772017-08-09 20:28:52 +0200347#include <osmocom/gsm/tlv.h>
348
349/* 3GPP TS 24.008 10.6.5.3 */
350enum pco_protocols {
351 PCO_P_LCP = 0xC021,
352 PCO_P_PAP = 0xC023,
353 PCO_P_CHAP = 0xC223,
354 PCO_P_IPCP = 0x8021,
355 PCO_P_PCSCF_ADDR = 0x0001,
356 PCO_P_IM_CN_SS_F = 0x0002,
357 PCO_P_DNS_IPv6_ADDR = 0x0003,
358 PCO_P_POLICY_CTRL_REJ = 0x0004, /* only in Network->MS */
359 PCO_P_MS_SUP_NETREQ_BCI = 0x0005,
360 /* reserved */
361 PCO_P_DSMIPv6_HA_ADDR = 0x0007,
362 PCO_P_DSMIPv6_HN_PREF = 0x0008,
363 PCO_P_DSMIPv6_v4_HA_ADDR= 0x0009,
364 PCO_P_IP_ADDR_VIA_NAS = 0x000a, /* only MS->Network */
365 PCO_P_IPv4_ADDR_VIA_DHCP= 0x000b, /* only MS->Netowrk */
366 PCO_P_PCSCF_IPv4_ADDR = 0x000c,
367 PCO_P_DNS_IPv4_ADDR = 0x000d,
368 PCO_P_MSISDN = 0x000e,
369 PCO_P_IFOM_SUPPORT = 0x000f,
370 PCO_P_IPv4_LINK_MTU = 0x0010,
371 PCO_P_MS_SUPP_LOC_A_TFT = 0x0011,
372 PCO_P_PCSCF_RESEL_SUP = 0x0012, /* only MS->Network */
373 PCO_P_NBIFOM_REQ = 0x0013,
374 PCO_P_NBIFOM_MODE = 0x0014,
375 PCO_P_NONIP_LINK_MTU = 0x0015,
376 PCO_P_APN_RATE_CTRL_SUP = 0x0016,
377 PCO_P_PS_DATA_OFF_UE = 0x0017,
378 PCO_P_REL_DATA_SVC = 0x0018,
379};
380
381/* determine if PCO contains given protocol */
382static bool pco_contains_proto(struct ul255_t *pco, uint16_t prot)
383{
384 uint8_t *cur = pco->v + 1;
385
386 /* iterate over PCO and check if protocol contained */
Pau Espin Pedrol0ab62fe2017-08-30 15:51:24 +0200387 while (cur + 3 <= pco->v + pco->l) {
Harald Welte1ae98772017-08-09 20:28:52 +0200388 uint16_t cur_prot = osmo_load16be(cur);
389 uint8_t cur_len = cur[2];
390 if (cur_prot == prot)
391 return true;
392 if (cur_len == 0)
393 break;
Pau Espin Pedrol0ab62fe2017-08-30 15:51:24 +0200394 cur += cur_len + 3;
Harald Welte1ae98772017-08-09 20:28:52 +0200395 }
396 return false;
397}
398
399/* determine if PDP context has IPv6 support */
400static bool pdp_has_v4(struct pdp_t *pdp)
401{
402 if (pdp->eua.l == 4+2)
403 return true;
404 else
405 return false;
406}
407
Harald Weltedda21ed2017-08-12 15:07:02 +0200408/* construct an IPCP PCO from up to two given DNS addreses */
409static int build_ipcp_pco(struct msgb *msg, uint8_t id, const struct in46_addr *dns1,
410 const struct in46_addr *dns2)
411{
412 uint8_t *len1, *len2;
413 uint8_t *start = msg->tail;
414 unsigned int len_appended;
415
416 /* Three byte T16L header */
417 msgb_put_u16(msg, 0x8021); /* IPCP */
418 len1 = msgb_put(msg, 1); /* Length of contents: delay */
419
420 msgb_put_u8(msg, 0x02); /* ACK */
421 msgb_put_u8(msg, id); /* ID: Needs to match request */
422 msgb_put_u8(msg, 0x00); /* Length MSB */
423 len2 = msgb_put(msg, 1); /* Length LSB: delay */
424
425 if (dns1 && dns1->len == 4) {
426 msgb_put_u8(msg, 0x81); /* DNS1 Tag */
427 msgb_put_u8(msg, 2 + dns1->len);/* DNS1 Length, incl. TL */
428 msgb_put_u32(msg, dns1->v4.s_addr);
429 }
430
431 if (dns2 && dns2->len == 4) {
432 msgb_put_u8(msg, 0x83); /* DNS2 Tag */
433 msgb_put_u8(msg, 2 + dns2->len);/* DNS2 Length, incl. TL */
434 msgb_put_u32(msg, dns2->v4.s_addr);
435 }
436
437 /* patch in length values */
438 len_appended = msg->tail - start;
439 *len1 = len_appended - 3;
440 *len2 = len_appended - 3;
441
442 return 0;
443}
444
Harald Welte1ae98772017-08-09 20:28:52 +0200445/* process one PCO request from a MS/UE, putting together the proper responses */
Harald Weltedda21ed2017-08-12 15:07:02 +0200446static void process_pco(struct apn_ctx *apn, struct pdp_t *pdp)
Harald Welte1ae98772017-08-09 20:28:52 +0200447{
448 struct msgb *msg = msgb_alloc(256, "PCO");
Harald Weltedda21ed2017-08-12 15:07:02 +0200449 unsigned int i;
450
451 OSMO_ASSERT(msg);
Harald Welte1ae98772017-08-09 20:28:52 +0200452 msgb_put_u8(msg, 0x80); /* ext-bit + configuration protocol byte */
453
454 /* FIXME: also check if primary / secondary DNS was requested */
455 if (pdp_has_v4(pdp) && pco_contains_proto(&pdp->pco_req, PCO_P_IPCP)) {
456 /* FIXME: properly implement this for IPCP */
Harald Weltedda21ed2017-08-12 15:07:02 +0200457 build_ipcp_pco(msg, 0, &apn->v4.cfg.dns[0], &apn->v4.cfg.dns[1]);
Harald Welte1ae98772017-08-09 20:28:52 +0200458 }
459
460 if (pco_contains_proto(&pdp->pco_req, PCO_P_DNS_IPv6_ADDR)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200461 for (i = 0; i < ARRAY_SIZE(apn->v6.cfg.dns); i++) {
462 struct in46_addr *i46a = &apn->v6.cfg.dns[i];
463 if (i46a->len != 16)
464 continue;
465 msgb_t16lv_put(msg, PCO_P_DNS_IPv6_ADDR, i46a->len, i46a->v6.s6_addr);
466 }
Harald Welte1ae98772017-08-09 20:28:52 +0200467 }
468
469 if (pco_contains_proto(&pdp->pco_req, PCO_P_DNS_IPv4_ADDR)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200470 for (i = 0; i < ARRAY_SIZE(apn->v4.cfg.dns); i++) {
471 struct in46_addr *i46a = &apn->v4.cfg.dns[i];
472 if (i46a->len != 4)
473 continue;
474 msgb_t16lv_put(msg, PCO_P_DNS_IPv4_ADDR, i46a->len, (uint8_t *)&i46a->v4);
475 }
Harald Welte1ae98772017-08-09 20:28:52 +0200476 }
477
478 if (msgb_length(msg) > 1) {
479 memcpy(pdp->pco_neg.v, msgb_data(msg), msgb_length(msg));
480 pdp->pco_neg.l = msgb_length(msg);
481 } else
482 pdp->pco_neg.l = 0;
483
484 msgb_free(msg);
485}
486
Harald Welte9d9d91b2017-10-14 16:22:16 +0200487static bool apn_supports_ipv4(const struct apn_ctx *apn)
488{
489 if (apn->v4.cfg.static_prefix.addr.len || apn->v4.cfg.dynamic_prefix.addr.len)
490 return true;
491 return false;
492}
493
494static bool apn_supports_ipv6(const struct apn_ctx *apn)
495{
496 if (apn->v6.cfg.static_prefix.addr.len || apn->v6.cfg.dynamic_prefix.addr.len)
497 return true;
498 return false;
499}
500
Harald Weltebed35df2011-11-02 13:06:18 +0100501int create_context_ind(struct pdp_t *pdp)
502{
Harald Weltedda21ed2017-08-12 15:07:02 +0200503 static char name_buf[256];
504 struct gsn_t *gsn = pdp->gsn;
505 struct ggsn_ctx *ggsn = gsn->priv;
Harald Welted12eab92017-08-02 19:49:47 +0200506 struct in46_addr addr;
Harald Weltebed35df2011-11-02 13:06:18 +0100507 struct ippoolm_t *member;
Harald Weltedda21ed2017-08-12 15:07:02 +0200508 struct apn_ctx *apn;
Harald Welted1bf1e12017-08-03 00:00:23 +0200509 int rc;
jjako52c24142002-12-16 13:33:51 +0000510
Harald Weltedda21ed2017-08-12 15:07:02 +0200511 osmo_apn_to_str(name_buf, pdp->apn_req.v, pdp->apn_req.l);
512
513 LOGPPDP(LOGL_DEBUG, pdp, "Processing create PDP context request for APN '%s'\n", name_buf);
514
515 /* First find an exact APN name match */
516 apn = ggsn_find_apn(ggsn, name_buf);
Harald Welte2e84d2c2017-10-01 13:36:52 +0800517 /* ignore if the APN has not been started */
Pau Espin Pedrol958256f2017-10-11 20:32:55 +0200518 if (apn && !apn->started)
Harald Welte2e84d2c2017-10-01 13:36:52 +0800519 apn = NULL;
Harald Welteb16c46b2017-10-01 18:28:18 +0800520
Harald Weltedda21ed2017-08-12 15:07:02 +0200521 /* then try default (if any) */
522 if (!apn)
523 apn = ggsn->cfg.default_apn;
Harald Welteb16c46b2017-10-01 18:28:18 +0800524 /* ignore if the APN has not been started */
Pau Espin Pedrol958256f2017-10-11 20:32:55 +0200525 if (apn && !apn->started)
Harald Welteb16c46b2017-10-01 18:28:18 +0800526 apn = NULL;
527
Harald Weltedda21ed2017-08-12 15:07:02 +0200528 if (!apn) {
529 /* no APN found for what user requested */
530 LOGPPDP(LOGL_NOTICE, pdp, "Unknown APN '%s', rejecting\n", name_buf);
531 gtp_create_context_resp(gsn, pdp, GTPCAUSE_MISSING_APN);
532 return 0;
533 }
jjako52c24142002-12-16 13:33:51 +0000534
Harald Welted9d88622017-08-04 00:22:35 +0200535 /* FIXME: we manually force all context requests to dynamic here! */
536 if (pdp->eua.l > 2)
537 pdp->eua.l = 2;
jjako52c24142002-12-16 13:33:51 +0000538
Harald Weltebed35df2011-11-02 13:06:18 +0100539 memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_req0));
jjako52c24142002-12-16 13:33:51 +0000540
Harald Weltebed35df2011-11-02 13:06:18 +0100541 memcpy(pdp->qos_neg.v, pdp->qos_req.v, pdp->qos_req.l); /* TODO */
542 pdp->qos_neg.l = pdp->qos_req.l;
jjako52c24142002-12-16 13:33:51 +0000543
Harald Weltea0d281d2017-08-02 21:48:16 +0200544 if (in46a_from_eua(&pdp->eua, &addr)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200545 LOGPPDP(LOGL_ERROR, pdp, "Cannot decode EUA from MS/SGSN: %s\n",
Harald Welted1bf1e12017-08-03 00:00:23 +0200546 osmo_hexdump(pdp->eua.v, pdp->eua.l));
547 gtp_create_context_resp(gsn, pdp, GTPCAUSE_UNKNOWN_PDP);
548 return 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100549 }
jjakoa7cd2492003-04-11 09:40:12 +0000550
Harald Weltedda21ed2017-08-12 15:07:02 +0200551 if (addr.len == sizeof(struct in_addr)) {
Harald Welte9d9d91b2017-10-14 16:22:16 +0200552 /* does this APN actually have an IPv4 pool? */
553 if (!apn_supports_ipv4(apn))
554 goto err_wrong_af;
555
Harald Weltedda21ed2017-08-12 15:07:02 +0200556 rc = ippool_newip(apn->v4.pool, &member, &addr, 0);
557 if (rc < 0)
558 goto err_pool_full;
559 in46a_to_eua(&member->addr, &pdp->eua);
jjakoa7cd2492003-04-11 09:40:12 +0000560
Harald Weltedda21ed2017-08-12 15:07:02 +0200561 /* TODO: In IPv6, EUA doesn't contain the actual IP addr/prefix! */
562 if (gtp_kernel_tunnel_add(pdp) < 0) {
563 LOGPPDP(LOGL_ERROR, pdp, "Cannot add tunnel to kernel: %s\n", strerror(errno));
564 gtp_create_context_resp(gsn, pdp, GTPCAUSE_SYS_FAIL);
565 return 0;
566 }
567 } else if (addr.len == sizeof(struct in6_addr)) {
Harald Welted46bcd22017-08-08 23:27:22 +0200568 struct in46_addr tmp;
Harald Welte9d9d91b2017-10-14 16:22:16 +0200569
570 /* does this APN actually have an IPv6 pool? */
571 if (!apn_supports_ipv6(apn))
572 goto err_wrong_af;
573
Harald Weltedda21ed2017-08-12 15:07:02 +0200574 rc = ippool_newip(apn->v6.pool, &member, &addr, 0);
575 if (rc < 0)
576 goto err_pool_full;
577
Harald Welted46bcd22017-08-08 23:27:22 +0200578 /* IPv6 doesn't really send the real/allocated address at this point, but just
579 * the link-identifier which the MS shall use for router solicitation */
580 tmp.len = addr.len;
581 /* initialize upper 64 bits to prefix, they are discarded by MS anyway */
582 memcpy(tmp.v6.s6_addr, &member->addr.v6, 8);
583 /* use allocated 64bit prefix as lower 64bit, used as link id by MS */
584 memcpy(tmp.v6.s6_addr+8, &member->addr.v6, 8);
585 in46a_to_eua(&tmp, &pdp->eua);
586 } else
Harald Weltedda21ed2017-08-12 15:07:02 +0200587 OSMO_ASSERT(0);
jjako52c24142002-12-16 13:33:51 +0000588
Harald Weltedda21ed2017-08-12 15:07:02 +0200589 pdp->peer = member;
590 pdp->ipif = apn->tun.tun; /* TODO */
591 member->peer = pdp;
Max3142d8d2017-05-04 17:45:10 +0200592
593 if (!send_trap(gsn, pdp, member, "imsi-ass-ip")) { /* TRAP with IP assignment */
Max727417d2016-08-02 17:10:38 +0200594 gtp_create_context_resp(gsn, pdp, GTPCAUSE_NO_RESOURCES);
595 return 0;
596 }
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100597
Harald Weltedda21ed2017-08-12 15:07:02 +0200598 process_pco(apn, pdp);
Harald Welte1ae98772017-08-09 20:28:52 +0200599
Harald Welte93fed3b2017-09-24 11:43:17 +0800600 /* Transmit G-PDU sequence numbers (only) if configured in APN */
601 pdp->tx_gpdu_seq = apn->cfg.tx_gpdu_seq;
602
Harald Weltedda21ed2017-08-12 15:07:02 +0200603 LOGPPDP(LOGL_INFO, pdp, "Successful PDP Context Creation: APN=%s(%s), TEIC=%u, IP=%s\n",
604 name_buf, apn->cfg.name, pdp->teic_own, in46a_ntoa(&member->addr));
Harald Weltebed35df2011-11-02 13:06:18 +0100605 gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ);
606 return 0; /* Success */
Harald Weltedda21ed2017-08-12 15:07:02 +0200607
608err_pool_full:
609 LOGPPDP(LOGL_ERROR, pdp, "Cannot allocate IP address from pool (full!)\n");
610 gtp_create_context_resp(gsn, pdp, -rc);
611 return 0; /* Already in use, or no more available */
Harald Welte9d9d91b2017-10-14 16:22:16 +0200612
613err_wrong_af:
614 LOGPPDP(LOGL_ERROR, pdp, "APN doesn't support requested EUA / AF type\n");
615 gtp_create_context_resp(gsn, pdp, GTPCAUSE_UNKNOWN_PDP);
616 return 0;
jjako52c24142002-12-16 13:33:51 +0000617}
618
Harald Weltedda21ed2017-08-12 15:07:02 +0200619/* Internet-originated IP packet, needs to be sent via GTP towards MS */
620static int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len)
Harald Weltebed35df2011-11-02 13:06:18 +0100621{
Harald Weltedda21ed2017-08-12 15:07:02 +0200622 struct apn_ctx *apn = tun->priv;
Harald Weltebed35df2011-11-02 13:06:18 +0100623 struct ippoolm_t *ipm;
Harald Welted12eab92017-08-02 19:49:47 +0200624 struct in46_addr dst;
Harald Welte63ebccd2017-08-02 21:10:09 +0200625 struct iphdr *iph = (struct iphdr *)pack;
Harald Weltea0d281d2017-08-02 21:48:16 +0200626 struct ip6_hdr *ip6h = (struct ip6_hdr *)pack;
Harald Weltedda21ed2017-08-12 15:07:02 +0200627 struct ippool_t *pool;
jjakoc6762cf2004-04-28 14:52:58 +0000628
Harald Welte63ebccd2017-08-02 21:10:09 +0200629 if (iph->version == 4) {
Harald Welted12eab92017-08-02 19:49:47 +0200630 if (len < sizeof(*iph) || len < 4*iph->ihl)
631 return -1;
632 dst.len = 4;
Harald Welte63ebccd2017-08-02 21:10:09 +0200633 dst.v4.s_addr = iph->daddr;
Harald Weltedda21ed2017-08-12 15:07:02 +0200634 pool = apn->v4.pool;
Harald Weltea0d281d2017-08-02 21:48:16 +0200635 } else if (iph->version == 6) {
Harald Welted4d6e092017-08-08 18:10:43 +0200636 /* Due to the fact that 3GPP requires an allocation of a
637 * /64 prefix to each MS, we must instruct
638 * ippool_getip() below to match only the leading /64
639 * prefix, i.e. the first 8 bytes of the address */
640 dst.len = 8;
Harald Weltea0d281d2017-08-02 21:48:16 +0200641 dst.v6 = ip6h->ip6_dst;
Harald Weltedda21ed2017-08-12 15:07:02 +0200642 pool = apn->v6.pool;
Harald Welted12eab92017-08-02 19:49:47 +0200643 } else {
Harald Weltedda21ed2017-08-12 15:07:02 +0200644 LOGP(DTUN, LOGL_NOTICE, "non-IPv packet received from tun\n");
Harald Welted12eab92017-08-02 19:49:47 +0200645 return -1;
646 }
jjakoc6762cf2004-04-28 14:52:58 +0000647
Harald Weltedda21ed2017-08-12 15:07:02 +0200648 /* IPv6 packet but no IPv6 pool, or IPv4 packet with no IPv4 pool */
649 if (!pool)
650 return 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100651
Harald Weltedda21ed2017-08-12 15:07:02 +0200652 DEBUGP(DTUN, "Received packet from tun!\n");
653
654 if (ippool_getip(pool, &ipm, &dst)) {
655 DEBUGP(DTUN, "Received packet with no PDP contex!!\n");
Harald Weltebed35df2011-11-02 13:06:18 +0100656 return 0;
657 }
658
659 if (ipm->peer) /* Check if a peer protocol is defined */
Harald Weltedda21ed2017-08-12 15:07:02 +0200660 gtp_data_req(apn->ggsn->gsn, (struct pdp_t *)ipm->peer, pack, len);
Harald Weltebed35df2011-11-02 13:06:18 +0100661 return 0;
jjako52c24142002-12-16 13:33:51 +0000662}
663
Harald Welted46bcd22017-08-08 23:27:22 +0200664/* RFC3307 link-local scope multicast address */
665static const struct in6_addr all_router_mcast_addr = {
666 .s6_addr = { 0xff,0x02,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,2 }
667};
668
Harald Weltedda21ed2017-08-12 15:07:02 +0200669/* MS-originated GTP1-U packet, needs to be sent via TUN device */
670static int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len)
Harald Weltebed35df2011-11-02 13:06:18 +0100671{
Harald Welted46bcd22017-08-08 23:27:22 +0200672 struct iphdr *iph = (struct iphdr *)pack;
673 struct ip6_hdr *ip6h = (struct ip6_hdr *)pack;
Harald Weltef85fe972017-09-24 20:00:34 +0800674 struct tun_t *tun = (struct tun_t *)pdp->ipif;
675 struct apn_ctx *apn = tun->priv;
676
677 OSMO_ASSERT(tun);
678 OSMO_ASSERT(apn);
Harald Welted46bcd22017-08-08 23:27:22 +0200679
Harald Weltedda21ed2017-08-12 15:07:02 +0200680 LOGPPDP(LOGL_DEBUG, pdp, "Packet received: forwarding to tun\n");
Harald Welted46bcd22017-08-08 23:27:22 +0200681
682 switch (iph->version) {
683 case 6:
684 /* daddr: all-routers multicast addr */
685 if (IN6_ARE_ADDR_EQUAL(&ip6h->ip6_dst, &all_router_mcast_addr))
Harald Weltef85fe972017-09-24 20:00:34 +0800686 return handle_router_mcast(pdp->gsn, pdp, &apn->v6_lladdr, pack, len);
Harald Welted46bcd22017-08-08 23:27:22 +0200687 break;
688 case 4:
689 break;
690 default:
Harald Weltedda21ed2017-08-12 15:07:02 +0200691 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS is neither IPv4 nor IPv6: %s\n",
692 osmo_hexdump(pack, len));
Harald Welted46bcd22017-08-08 23:27:22 +0200693 return -1;
694 }
Harald Weltebed35df2011-11-02 13:06:18 +0100695 return tun_encaps((struct tun_t *)pdp->ipif, pack, len);
jjako52c24142002-12-16 13:33:51 +0000696}
697
Harald Welte632e8432017-09-05 18:12:14 +0200698static char *config_file = "osmo-ggsn.cfg";
Harald Weltedda21ed2017-08-12 15:07:02 +0200699
700/* callback for tun device osmocom select loop integration */
701static int ggsn_tun_fd_cb(struct osmo_fd *fd, unsigned int what)
702{
703 struct apn_ctx *apn = fd->data;
704
705 OSMO_ASSERT(what & BSC_FD_READ);
706
707 return tun_decaps(apn->tun.tun);
708}
709
710/* callback for libgtp osmocom select loop integration */
711static int ggsn_gtp_fd_cb(struct osmo_fd *fd, unsigned int what)
712{
713 struct ggsn_ctx *ggsn = fd->data;
714 int rc;
715
716 OSMO_ASSERT(what & BSC_FD_READ);
717
718 switch (fd->priv_nr) {
719 case 0:
720 rc = gtp_decaps0(ggsn->gsn);
721 break;
722 case 1:
723 rc = gtp_decaps1c(ggsn->gsn);
724 break;
725 case 2:
726 rc = gtp_decaps1u(ggsn->gsn);
727 break;
728 default:
729 OSMO_ASSERT(0);
730 break;
731 }
732 return rc;
733}
734
735static void ggsn_gtp_tmr_start(struct ggsn_ctx *ggsn)
736{
737 struct timeval next;
738
739 /* Retrieve next retransmission as timeval */
740 gtp_retranstimeout(ggsn->gsn, &next);
741
742 /* re-schedule the timer */
743 osmo_timer_schedule(&ggsn->gtp_timer, next.tv_sec, next.tv_usec/1000);
744}
745
746/* timer callback for libgtp retransmission and ping */
747static void ggsn_gtp_tmr_cb(void *data)
748{
749 struct ggsn_ctx *ggsn = data;
750
751 /* do all the retransmissions as needed */
752 gtp_retrans(ggsn->gsn);
753
754 ggsn_gtp_tmr_start(ggsn);
755}
756
757/* To exit gracefully. Used with GCC compilation flag -pg and gprof */
758static void signal_handler(int s)
759{
760 LOGP(DGGSN, LOGL_NOTICE, "signal %d received\n", s);
761 switch (s) {
762 case SIGINT:
Harald Weltee8049472017-08-20 12:44:21 +0200763 case SIGTERM:
Harald Weltedda21ed2017-08-12 15:07:02 +0200764 LOGP(DGGSN, LOGL_NOTICE, "SIGINT received, shutting down\n");
765 end = 1;
766 break;
767 case SIGABRT:
768 case SIGUSR1:
769 talloc_report(tall_vty_ctx, stderr);
770 talloc_report_full(tall_ggsn_ctx, stderr);
771 break;
772 case SIGUSR2:
773 talloc_report_full(tall_vty_ctx, stderr);
774 break;
775 default:
776 break;
777 }
778}
779
780
781/* Start a given GGSN */
782int ggsn_start(struct ggsn_ctx *ggsn)
783{
784 struct apn_ctx *apn;
785 int rc;
786
787 if (ggsn->started)
788 return 0;
789
790 LOGPGGSN(LOGL_INFO, ggsn, "Starting GGSN\n");
791
792 /* Start libgtp listener */
793 if (gtp_new(&ggsn->gsn, ggsn->cfg.state_dir, &ggsn->cfg.listen_addr.v4, GTP_MODE_GGSN)) {
794 LOGPGGSN(LOGL_ERROR, ggsn, "Failed to create GTP: %s\n", strerror(errno));
795 return -1;
796 }
797 ggsn->gsn->priv = ggsn;
798
Harald Welte98146772017-09-05 17:41:20 +0200799 /* patch in different addresses to use (in case we're behind NAT, the listen
800 * address is different from what we advertise externally) */
801 if (ggsn->cfg.gtpc_addr.v4.s_addr)
802 ggsn->gsn->gsnc = ggsn->cfg.gtpc_addr.v4;
803
804 if (ggsn->cfg.gtpu_addr.v4.s_addr)
805 ggsn->gsn->gsnu = ggsn->cfg.gtpu_addr.v4;
806
Harald Weltedda21ed2017-08-12 15:07:02 +0200807 /* Register File Descriptors */
808 osmo_fd_setup(&ggsn->gtp_fd0, ggsn->gsn->fd0, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 0);
809 rc = osmo_fd_register(&ggsn->gtp_fd0);
810 OSMO_ASSERT(rc == 0);
811
812 osmo_fd_setup(&ggsn->gtp_fd1c, ggsn->gsn->fd1c, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 1);
813 rc = osmo_fd_register(&ggsn->gtp_fd1c);
814 OSMO_ASSERT(rc == 0);
815
816 osmo_fd_setup(&ggsn->gtp_fd1u, ggsn->gsn->fd1u, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 2);
817 rc = osmo_fd_register(&ggsn->gtp_fd1u);
818 OSMO_ASSERT(rc == 0);
819
820 /* Start GTP re-transmission timer */
821 osmo_timer_setup(&ggsn->gtp_timer, ggsn_gtp_tmr_cb, ggsn);
822
823 gtp_set_cb_data_ind(ggsn->gsn, encaps_tun);
824 gtp_set_cb_delete_context(ggsn->gsn, delete_context);
825 gtp_set_cb_create_context_ind(ggsn->gsn, create_context_ind);
826
827 LOGPGGSN(LOGL_NOTICE, ggsn, "Successfully started\n");
828 ggsn->started = true;
829
830 llist_for_each_entry(apn, &ggsn->apn_list, list)
831 apn_start(apn);
832
833 return 0;
834}
835
836/* Stop a given GGSN */
837int ggsn_stop(struct ggsn_ctx *ggsn)
838{
839 struct apn_ctx *apn;
840
841 if (!ggsn->started)
842 return 0;
843
844 /* iterate over all APNs and stop them */
845 llist_for_each_entry(apn, &ggsn->apn_list, list)
846 apn_stop(apn, true);
847
848 osmo_timer_del(&ggsn->gtp_timer);
849
850 osmo_fd_unregister(&ggsn->gtp_fd1u);
851 osmo_fd_unregister(&ggsn->gtp_fd1c);
852 osmo_fd_unregister(&ggsn->gtp_fd0);
853
854 if (ggsn->gsn) {
855 gtp_free(ggsn->gsn);
856 ggsn->gsn = NULL;
857 }
858
859 ggsn->started = false;
860 return 0;
861}
862
863static void print_usage()
864{
865 printf("Usage: osmo-ggsn [-h] [-D] [-c configfile] [-V]\n");
866}
867
868static void print_help()
869{
870 printf( " Some useful help...\n"
871 " -h --help This help text\n"
872 " -D --daemonize Fork the process into a background daemon\n"
873 " -c --config-file filename The config file to use\n"
874 " -V --version Print the version of OsmoGGSN\n"
875 );
876}
877
878static void handle_options(int argc, char **argv)
879{
880 while (1) {
881 int option_index = 0, c;
882 static struct option long_options[] = {
883 { "help", 0, 0, 'h' },
884 { "daemonize", 0, 0, 'D' },
885 { "config-file", 1, 0, 'c' },
886 { "version", 0, 0, 'V' },
887 { 0, 0, 0, 0 }
888 };
889
890 c = getopt_long(argc, argv, "hdc:V", long_options, &option_index);
891 if (c == -1)
892 break;
893
894 switch (c) {
895 case 'h':
896 print_usage();
897 print_help();
898 exit(0);
899 case 'D':
900 daemonize = 1;
901 break;
902 case 'c':
903 config_file = optarg;
904 break;
905 case 'V':
906 print_version(1);
907 exit(0);
908 break;
909 }
910 }
911}
912
jjako52c24142002-12-16 13:33:51 +0000913int main(int argc, char **argv)
914{
Harald Weltedda21ed2017-08-12 15:07:02 +0200915 struct ggsn_ctx *ggsn;
916 int rc;
jjako52c24142002-12-16 13:33:51 +0000917
Harald Welte632e8432017-09-05 18:12:14 +0200918 tall_ggsn_ctx = talloc_named_const(NULL, 0, "OsmoGGSN");
Harald Weltedda21ed2017-08-12 15:07:02 +0200919 msgb_talloc_ctx_init(tall_ggsn_ctx, 0);
jjako52c24142002-12-16 13:33:51 +0000920
Harald Weltee8049472017-08-20 12:44:21 +0200921 /* Handle keyboard interrupt SIGINT */
Harald Weltedda21ed2017-08-12 15:07:02 +0200922 signal(SIGINT, &signal_handler);
Harald Weltee8049472017-08-20 12:44:21 +0200923 signal(SIGTERM, &signal_handler);
Harald Weltedda21ed2017-08-12 15:07:02 +0200924 signal(SIGABRT, &signal_handler);
925 signal(SIGUSR1, &signal_handler);
926 signal(SIGUSR2, &signal_handler);
jjako52c24142002-12-16 13:33:51 +0000927
Harald Weltedda21ed2017-08-12 15:07:02 +0200928 osmo_init_ignore_signals();
Holger Hans Peter Freyther9c7fd8e2014-12-04 16:32:37 +0100929 osmo_init_logging(&log_info);
Harald Weltedda21ed2017-08-12 15:07:02 +0200930 osmo_stats_init(tall_ggsn_ctx);
jjako0141d202004-01-09 15:19:20 +0000931
Harald Weltedda21ed2017-08-12 15:07:02 +0200932 vty_init(&g_vty_info);
933 logging_vty_add_cmds(NULL);
934 osmo_stats_vty_add_cmds(&log_info);
935 ggsn_vty_init();
936 ctrl_vty_init(tall_ggsn_ctx);
937
938 handle_options(argc, argv);
939
940 rate_ctr_init(tall_ggsn_ctx);
941
942 rc = vty_read_config_file(config_file, NULL);
943 if (rc < 0) {
944 fprintf(stderr, "Failed to open config file: '%s'\n", config_file);
945 exit(2);
Harald Weltebed35df2011-11-02 13:06:18 +0100946 }
jjako52c24142002-12-16 13:33:51 +0000947
Harald Weltedda21ed2017-08-12 15:07:02 +0200948 rc = telnet_init_dynif(tall_ggsn_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_GGSN);
949 if (rc < 0)
Harald Weltebed35df2011-11-02 13:06:18 +0100950 exit(1);
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100951
Harald Weltedda21ed2017-08-12 15:07:02 +0200952 g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_GGSN, NULL);
953 if (!g_ctrlh) {
954 LOGP(DGGSN, LOGL_ERROR, "Failed to create CTRL interface.\n");
Harald Weltebed35df2011-11-02 13:06:18 +0100955 exit(1);
956 }
jjako88c22162003-07-06 19:33:18 +0000957
Harald Weltedda21ed2017-08-12 15:07:02 +0200958 if (daemonize) {
959 rc = osmo_daemonize();
960 if (rc < 0) {
961 perror("Error during daemonize");
Harald Weltebed35df2011-11-02 13:06:18 +0100962 exit(1);
963 }
964 }
jjako1d3db972004-01-16 09:56:56 +0000965
Harald Weltedda21ed2017-08-12 15:07:02 +0200966#if 0
Harald Weltebed35df2011-11-02 13:06:18 +0100967 /* qos */
968 qos.l = 3;
969 qos.v[2] = (args_info.qos_arg) & 0xff;
970 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
971 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
Harald Weltedda21ed2017-08-12 15:07:02 +0200972#endif
jjakoa7cd2492003-04-11 09:40:12 +0000973
Harald Weltedda21ed2017-08-12 15:07:02 +0200974 /* Main select loop */
975 while (!end) {
976 osmo_select_main(0);
Harald Weltebed35df2011-11-02 13:06:18 +0100977 }
jjakoe0149782003-07-06 17:07:04 +0000978
Harald Weltedda21ed2017-08-12 15:07:02 +0200979 llist_for_each_entry(ggsn, &g_ggsn_list, list)
980 ggsn_stop(ggsn);
Harald Weltebed35df2011-11-02 13:06:18 +0100981
982 return 1;
jjako52c24142002-12-16 13:33:51 +0000983}