blob: 46850a61ff137b915e23db90597ab48a353d3f0b [file] [log] [blame]
jjako52c24142002-12-16 13:33:51 +00001/*
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>
jjako52c24142002-12-16 13:33:51 +00005 *
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.
jjako52c24142002-12-16 13:33:51 +000010 *
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
86#define LOGPPDP(level, pdp, fmt, args...) \
87 LOGP(DGGSN, level, "PDP(%s:%u): " fmt, imsi_gtp2str(&(pdp)->imsi), (pdp)->nsapi, ## args)
88
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 if (!apn->started)
117 return 0;
jjako0141d202004-01-09 15:19:20 +0000118
Harald Weltedda21ed2017-08-12 15:07:02 +0200119 LOGPAPN(LOGL_NOTICE, apn, "%sStopping\n", force ? "FORCED " : "");
120 /* check if pools have any active PDP contexts and bail out */
121 pool_close_all_pdp(apn->v4.pool);
122 pool_close_all_pdp(apn->v6.pool);
123
124 /* shutdown whatever old state might be left */
125 if (apn->tun.tun) {
126 /* run ip-down script */
127 if (apn->tun.cfg.ipdown_script) {
128 LOGPAPN( LOGL_INFO, apn, "Running %s\n", apn->tun.cfg.ipdown_script);
129 tun_runscript(apn->tun.tun, apn->tun.cfg.ipdown_script);
130 }
131 /* release tun device */
132 LOGPAPN(LOGL_INFO, apn, "Closing TUN device\n");
133 osmo_fd_unregister(&apn->tun.fd);
134 tun_free(apn->tun.tun);
135 apn->tun.tun = NULL;
136 }
137
138 if (apn->v4.pool) {
139 LOGPAPN(LOGL_INFO, apn, "Releasing IPv4 pool\n");
140 ippool_free(apn->v4.pool);
141 apn->v4.pool = NULL;
142 }
143 if (apn->v6.pool) {
144 LOGPAPN(LOGL_INFO, apn, "Releasing IPv6 pool\n");
145 ippool_free(apn->v6.pool);
146 apn->v6.pool = NULL;
147 }
148
149 apn->started = false;
150 return 0;
151}
152
153/* actually start the APN with its current config */
154int apn_start(struct apn_ctx *apn)
155{
156 if (apn->started)
157 return 0;
158
159 LOGPAPN(LOGL_INFO, apn, "Starting\n");
160 switch (apn->cfg.gtpu_mode) {
161 case APN_GTPU_MODE_TUN:
162 LOGPAPN(LOGL_INFO, apn, "Opening TUN device %s\n", apn->tun.cfg.dev_name);
163 if (tun_new(&apn->tun.tun, apn->tun.cfg.dev_name)) {
164 LOGPAPN(LOGL_ERROR, apn, "Failed to configure tun device\n");
165 return -1;
166 }
167 LOGPAPN(LOGL_INFO, apn, "Opened TUN device %s\n", apn->tun.tun->devname);
168
169 /* Register with libosmcoore */
170 osmo_fd_setup(&apn->tun.fd, apn->tun.tun->fd, BSC_FD_READ, ggsn_tun_fd_cb, apn, 0);
171 osmo_fd_register(&apn->tun.fd);
172
173 /* Set TUN library callback */
174 tun_set_cb_ind(apn->tun.tun, cb_tun_ind);
175
176 if (apn->v4.cfg.ifconfig_prefix.addr.len) {
177 LOGPAPN(LOGL_INFO, apn, "Setting tun IP address %s\n",
178 in46p_ntoa(&apn->v4.cfg.ifconfig_prefix));
179 if (tun_setaddr(apn->tun.tun, &apn->v4.cfg.ifconfig_prefix.addr, NULL,
180 apn->v4.cfg.ifconfig_prefix.prefixlen)) {
181 LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv4 address %s: %s\n",
182 in46p_ntoa(&apn->v4.cfg.ifconfig_prefix), strerror(errno));
183 apn_stop(apn, false);
184 return -1;
185 }
186 }
187
188 if (apn->v6.cfg.ifconfig_prefix.addr.len) {
189 LOGPAPN(LOGL_INFO, apn, "Setting tun IPv6 address %s\n",
190 in46p_ntoa(&apn->v6.cfg.ifconfig_prefix));
191 if (tun_setaddr(apn->tun.tun, &apn->v6.cfg.ifconfig_prefix.addr, NULL,
192 apn->v6.cfg.ifconfig_prefix.prefixlen)) {
193 LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv6 address %s: %s\n",
194 in46p_ntoa(&apn->v6.cfg.ifconfig_prefix), strerror(errno));
195 apn_stop(apn, false);
196 return -1;
197 }
198 }
199
200 if (apn->tun.cfg.ipup_script) {
201 LOGPAPN(LOGL_INFO, apn, "Running ip-up script %s\n",
202 apn->tun.cfg.ipup_script);
203 tun_runscript(apn->tun.tun, apn->tun.cfg.ipup_script);
204 }
205 /* set back-pointer from TUN device to APN */
206 apn->tun.tun->priv = apn;
207 break;
208 case APN_GTPU_MODE_KERNEL_GTP:
209 LOGPAPN(LOGL_ERROR, apn, "FIXME: Kernel GTP\n");
210#if 0
211 /* use GTP kernel module for data packet encapsulation */
212 if (gtp_kernel_init(gsn, &net.v4, prefixlen, net_arg) < 0)
213 goto err;
214#endif
Harald Weltebed35df2011-11-02 13:06:18 +0100215 break;
216 default:
Harald Weltedda21ed2017-08-12 15:07:02 +0200217 LOGPAPN(LOGL_ERROR, apn, "Unknown GTPU Mode %d\n", apn->cfg.gtpu_mode);
218 return -1;
Harald Weltebed35df2011-11-02 13:06:18 +0100219 }
jjako0141d202004-01-09 15:19:20 +0000220
Harald Weltedda21ed2017-08-12 15:07:02 +0200221 /* Create IPv4 pool */
222 if (apn->v4.cfg.dynamic_prefix.addr.len) {
223 LOGPAPN(LOGL_INFO, apn, "Creating IPv4 pool %s\n",
224 in46p_ntoa(&apn->v4.cfg.dynamic_prefix));
225 if (ippool_new(&apn->v4.pool, &apn->v4.cfg.dynamic_prefix,
226 &apn->v4.cfg.static_prefix, 0)) {
227 LOGPAPN(LOGL_ERROR, apn, "Failed to create IPv4 pool\n");
228 apn_stop(apn, false);
229 return -1;
230 }
Harald Weltebed35df2011-11-02 13:06:18 +0100231 }
Harald Weltedda21ed2017-08-12 15:07:02 +0200232
233 /* Create IPv6 pool */
234 if (apn->v6.cfg.dynamic_prefix.addr.len) {
235 LOGPAPN(LOGL_INFO, apn, "Creating IPv6 pool %s\n",
236 in46p_ntoa(&apn->v6.cfg.dynamic_prefix));
237 if (ippool_new(&apn->v6.pool, &apn->v6.cfg.dynamic_prefix,
238 &apn->v6.cfg.static_prefix, 0)) {
239 LOGPAPN(LOGL_ERROR, apn, "Failed to create IPv6 pool\n");
240 apn_stop(apn, false);
241 return -1;
242 }
243 }
244
245 LOGPAPN(LOGL_NOTICE, apn, "Successfully started\n");
246 apn->started = true;
247 return 0;
jjako0141d202004-01-09 15:19:20 +0000248}
jjako0141d202004-01-09 15:19:20 +0000249
Max3142d8d2017-05-04 17:45:10 +0200250static bool send_trap(const struct gsn_t *gsn, const struct pdp_t *pdp, const struct ippoolm_t *member, const char *var)
251{
Harald Welted12eab92017-08-02 19:49:47 +0200252 char addrbuf[256];
Max3142d8d2017-05-04 17:45:10 +0200253 char val[NAMESIZE];
254
Harald Welted12eab92017-08-02 19:49:47 +0200255 const char *addrstr = in46a_ntop(&member->addr, addrbuf, sizeof(addrbuf));
256
Harald Welteb10ee082017-08-12 19:29:16 +0200257 snprintf(val, sizeof(val), "%s,%s", imsi_gtp2str(&pdp->imsi), addrstr);
Max3142d8d2017-05-04 17:45:10 +0200258
Harald Weltedda21ed2017-08-12 15:07:02 +0200259 if (ctrl_cmd_send_trap(g_ctrlh, var, val) < 0) {
260 LOGPPDP(LOGL_ERROR, pdp, "Failed to create and send TRAP %s\n", var);
Max3142d8d2017-05-04 17:45:10 +0200261 return false;
262 }
263 return true;
264}
265
Harald Weltedda21ed2017-08-12 15:07:02 +0200266static int delete_context(struct pdp_t *pdp)
Harald Weltebed35df2011-11-02 13:06:18 +0100267{
Harald Weltedda21ed2017-08-12 15:07:02 +0200268 struct gsn_t *gsn = pdp->gsn;
269 struct ippoolm_t *ipp = (struct ippoolm_t *)pdp->peer;
270
271 LOGPPDP(LOGL_INFO, pdp, "Deleting PDP context\n");
Max727417d2016-08-02 17:10:38 +0200272 struct ippoolm_t *member = pdp->peer;
Maxdbd70242016-10-14 13:38:05 +0200273
274 if (pdp->peer) {
Max3142d8d2017-05-04 17:45:10 +0200275 send_trap(gsn, pdp, member, "imsi-rem-ip"); /* TRAP with IP removal */
Harald Weltedda21ed2017-08-12 15:07:02 +0200276 ippool_freeip(ipp->pool, ipp);
Maxdbd70242016-10-14 13:38:05 +0200277 } else
Harald Weltedda21ed2017-08-12 15:07:02 +0200278 LOGPPDP(LOGL_ERROR, pdp, "Cannot find/free IP Pool member\n");
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100279
280 if (gtp_kernel_tunnel_del(pdp)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200281 LOGPPDP(LOGL_ERROR, pdp, "Cannot delete tunnel from kernel:%s\n",
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100282 strerror(errno));
283 }
284
Harald Weltebed35df2011-11-02 13:06:18 +0100285 return 0;
jjako52c24142002-12-16 13:33:51 +0000286}
287
Harald Welte1ae98772017-08-09 20:28:52 +0200288#include <osmocom/gsm/tlv.h>
289
290/* 3GPP TS 24.008 10.6.5.3 */
291enum pco_protocols {
292 PCO_P_LCP = 0xC021,
293 PCO_P_PAP = 0xC023,
294 PCO_P_CHAP = 0xC223,
295 PCO_P_IPCP = 0x8021,
296 PCO_P_PCSCF_ADDR = 0x0001,
297 PCO_P_IM_CN_SS_F = 0x0002,
298 PCO_P_DNS_IPv6_ADDR = 0x0003,
299 PCO_P_POLICY_CTRL_REJ = 0x0004, /* only in Network->MS */
300 PCO_P_MS_SUP_NETREQ_BCI = 0x0005,
301 /* reserved */
302 PCO_P_DSMIPv6_HA_ADDR = 0x0007,
303 PCO_P_DSMIPv6_HN_PREF = 0x0008,
304 PCO_P_DSMIPv6_v4_HA_ADDR= 0x0009,
305 PCO_P_IP_ADDR_VIA_NAS = 0x000a, /* only MS->Network */
306 PCO_P_IPv4_ADDR_VIA_DHCP= 0x000b, /* only MS->Netowrk */
307 PCO_P_PCSCF_IPv4_ADDR = 0x000c,
308 PCO_P_DNS_IPv4_ADDR = 0x000d,
309 PCO_P_MSISDN = 0x000e,
310 PCO_P_IFOM_SUPPORT = 0x000f,
311 PCO_P_IPv4_LINK_MTU = 0x0010,
312 PCO_P_MS_SUPP_LOC_A_TFT = 0x0011,
313 PCO_P_PCSCF_RESEL_SUP = 0x0012, /* only MS->Network */
314 PCO_P_NBIFOM_REQ = 0x0013,
315 PCO_P_NBIFOM_MODE = 0x0014,
316 PCO_P_NONIP_LINK_MTU = 0x0015,
317 PCO_P_APN_RATE_CTRL_SUP = 0x0016,
318 PCO_P_PS_DATA_OFF_UE = 0x0017,
319 PCO_P_REL_DATA_SVC = 0x0018,
320};
321
322/* determine if PCO contains given protocol */
323static bool pco_contains_proto(struct ul255_t *pco, uint16_t prot)
324{
325 uint8_t *cur = pco->v + 1;
326
327 /* iterate over PCO and check if protocol contained */
Pau Espin Pedrol0ab62fe2017-08-30 15:51:24 +0200328 while (cur + 3 <= pco->v + pco->l) {
Harald Welte1ae98772017-08-09 20:28:52 +0200329 uint16_t cur_prot = osmo_load16be(cur);
330 uint8_t cur_len = cur[2];
331 if (cur_prot == prot)
332 return true;
333 if (cur_len == 0)
334 break;
Pau Espin Pedrol0ab62fe2017-08-30 15:51:24 +0200335 cur += cur_len + 3;
Harald Welte1ae98772017-08-09 20:28:52 +0200336 }
337 return false;
338}
339
340/* determine if PDP context has IPv6 support */
341static bool pdp_has_v4(struct pdp_t *pdp)
342{
343 if (pdp->eua.l == 4+2)
344 return true;
345 else
346 return false;
347}
348
Harald Weltedda21ed2017-08-12 15:07:02 +0200349/* construct an IPCP PCO from up to two given DNS addreses */
350static int build_ipcp_pco(struct msgb *msg, uint8_t id, const struct in46_addr *dns1,
351 const struct in46_addr *dns2)
352{
353 uint8_t *len1, *len2;
354 uint8_t *start = msg->tail;
355 unsigned int len_appended;
356
357 /* Three byte T16L header */
358 msgb_put_u16(msg, 0x8021); /* IPCP */
359 len1 = msgb_put(msg, 1); /* Length of contents: delay */
360
361 msgb_put_u8(msg, 0x02); /* ACK */
362 msgb_put_u8(msg, id); /* ID: Needs to match request */
363 msgb_put_u8(msg, 0x00); /* Length MSB */
364 len2 = msgb_put(msg, 1); /* Length LSB: delay */
365
366 if (dns1 && dns1->len == 4) {
367 msgb_put_u8(msg, 0x81); /* DNS1 Tag */
368 msgb_put_u8(msg, 2 + dns1->len);/* DNS1 Length, incl. TL */
369 msgb_put_u32(msg, dns1->v4.s_addr);
370 }
371
372 if (dns2 && dns2->len == 4) {
373 msgb_put_u8(msg, 0x83); /* DNS2 Tag */
374 msgb_put_u8(msg, 2 + dns2->len);/* DNS2 Length, incl. TL */
375 msgb_put_u32(msg, dns2->v4.s_addr);
376 }
377
378 /* patch in length values */
379 len_appended = msg->tail - start;
380 *len1 = len_appended - 3;
381 *len2 = len_appended - 3;
382
383 return 0;
384}
385
Harald Welte1ae98772017-08-09 20:28:52 +0200386/* process one PCO request from a MS/UE, putting together the proper responses */
Harald Weltedda21ed2017-08-12 15:07:02 +0200387static void process_pco(struct apn_ctx *apn, struct pdp_t *pdp)
Harald Welte1ae98772017-08-09 20:28:52 +0200388{
389 struct msgb *msg = msgb_alloc(256, "PCO");
Harald Weltedda21ed2017-08-12 15:07:02 +0200390 unsigned int i;
391
392 OSMO_ASSERT(msg);
Harald Welte1ae98772017-08-09 20:28:52 +0200393 msgb_put_u8(msg, 0x80); /* ext-bit + configuration protocol byte */
394
395 /* FIXME: also check if primary / secondary DNS was requested */
396 if (pdp_has_v4(pdp) && pco_contains_proto(&pdp->pco_req, PCO_P_IPCP)) {
397 /* FIXME: properly implement this for IPCP */
Harald Weltedda21ed2017-08-12 15:07:02 +0200398 build_ipcp_pco(msg, 0, &apn->v4.cfg.dns[0], &apn->v4.cfg.dns[1]);
Harald Welte1ae98772017-08-09 20:28:52 +0200399 }
400
401 if (pco_contains_proto(&pdp->pco_req, PCO_P_DNS_IPv6_ADDR)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200402 for (i = 0; i < ARRAY_SIZE(apn->v6.cfg.dns); i++) {
403 struct in46_addr *i46a = &apn->v6.cfg.dns[i];
404 if (i46a->len != 16)
405 continue;
406 msgb_t16lv_put(msg, PCO_P_DNS_IPv6_ADDR, i46a->len, i46a->v6.s6_addr);
407 }
Harald Welte1ae98772017-08-09 20:28:52 +0200408 }
409
410 if (pco_contains_proto(&pdp->pco_req, PCO_P_DNS_IPv4_ADDR)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200411 for (i = 0; i < ARRAY_SIZE(apn->v4.cfg.dns); i++) {
412 struct in46_addr *i46a = &apn->v4.cfg.dns[i];
413 if (i46a->len != 4)
414 continue;
415 msgb_t16lv_put(msg, PCO_P_DNS_IPv4_ADDR, i46a->len, (uint8_t *)&i46a->v4);
416 }
Harald Welte1ae98772017-08-09 20:28:52 +0200417 }
418
419 if (msgb_length(msg) > 1) {
420 memcpy(pdp->pco_neg.v, msgb_data(msg), msgb_length(msg));
421 pdp->pco_neg.l = msgb_length(msg);
422 } else
423 pdp->pco_neg.l = 0;
424
425 msgb_free(msg);
426}
427
Harald Weltebed35df2011-11-02 13:06:18 +0100428int create_context_ind(struct pdp_t *pdp)
429{
Harald Weltedda21ed2017-08-12 15:07:02 +0200430 static char name_buf[256];
431 struct gsn_t *gsn = pdp->gsn;
432 struct ggsn_ctx *ggsn = gsn->priv;
Harald Welted12eab92017-08-02 19:49:47 +0200433 struct in46_addr addr;
Harald Weltebed35df2011-11-02 13:06:18 +0100434 struct ippoolm_t *member;
Harald Weltedda21ed2017-08-12 15:07:02 +0200435 struct apn_ctx *apn;
Harald Welted1bf1e12017-08-03 00:00:23 +0200436 int rc;
jjako52c24142002-12-16 13:33:51 +0000437
Harald Weltedda21ed2017-08-12 15:07:02 +0200438 osmo_apn_to_str(name_buf, pdp->apn_req.v, pdp->apn_req.l);
439
440 LOGPPDP(LOGL_DEBUG, pdp, "Processing create PDP context request for APN '%s'\n", name_buf);
441
442 /* First find an exact APN name match */
443 apn = ggsn_find_apn(ggsn, name_buf);
444 /* then try default (if any) */
445 if (!apn)
446 apn = ggsn->cfg.default_apn;
447 if (!apn) {
448 /* no APN found for what user requested */
449 LOGPPDP(LOGL_NOTICE, pdp, "Unknown APN '%s', rejecting\n", name_buf);
450 gtp_create_context_resp(gsn, pdp, GTPCAUSE_MISSING_APN);
451 return 0;
452 }
jjako52c24142002-12-16 13:33:51 +0000453
Harald Welted9d88622017-08-04 00:22:35 +0200454 /* FIXME: we manually force all context requests to dynamic here! */
455 if (pdp->eua.l > 2)
456 pdp->eua.l = 2;
jjako52c24142002-12-16 13:33:51 +0000457
Harald Weltebed35df2011-11-02 13:06:18 +0100458 memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_req0));
jjako52c24142002-12-16 13:33:51 +0000459
Harald Weltebed35df2011-11-02 13:06:18 +0100460 memcpy(pdp->qos_neg.v, pdp->qos_req.v, pdp->qos_req.l); /* TODO */
461 pdp->qos_neg.l = pdp->qos_req.l;
jjako52c24142002-12-16 13:33:51 +0000462
Harald Weltea0d281d2017-08-02 21:48:16 +0200463 if (in46a_from_eua(&pdp->eua, &addr)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200464 LOGPPDP(LOGL_ERROR, pdp, "Cannot decode EUA from MS/SGSN: %s\n",
Harald Welted1bf1e12017-08-03 00:00:23 +0200465 osmo_hexdump(pdp->eua.v, pdp->eua.l));
466 gtp_create_context_resp(gsn, pdp, GTPCAUSE_UNKNOWN_PDP);
467 return 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100468 }
jjakoa7cd2492003-04-11 09:40:12 +0000469
Harald Weltedda21ed2017-08-12 15:07:02 +0200470 if (addr.len == sizeof(struct in_addr)) {
471 rc = ippool_newip(apn->v4.pool, &member, &addr, 0);
472 if (rc < 0)
473 goto err_pool_full;
474 in46a_to_eua(&member->addr, &pdp->eua);
jjakoa7cd2492003-04-11 09:40:12 +0000475
Harald Weltedda21ed2017-08-12 15:07:02 +0200476 /* TODO: In IPv6, EUA doesn't contain the actual IP addr/prefix! */
477 if (gtp_kernel_tunnel_add(pdp) < 0) {
478 LOGPPDP(LOGL_ERROR, pdp, "Cannot add tunnel to kernel: %s\n", strerror(errno));
479 gtp_create_context_resp(gsn, pdp, GTPCAUSE_SYS_FAIL);
480 return 0;
481 }
482 } else if (addr.len == sizeof(struct in6_addr)) {
Harald Welted46bcd22017-08-08 23:27:22 +0200483 struct in46_addr tmp;
Harald Weltedda21ed2017-08-12 15:07:02 +0200484 rc = ippool_newip(apn->v6.pool, &member, &addr, 0);
485 if (rc < 0)
486 goto err_pool_full;
487
Harald Welted46bcd22017-08-08 23:27:22 +0200488 /* IPv6 doesn't really send the real/allocated address at this point, but just
489 * the link-identifier which the MS shall use for router solicitation */
490 tmp.len = addr.len;
491 /* initialize upper 64 bits to prefix, they are discarded by MS anyway */
492 memcpy(tmp.v6.s6_addr, &member->addr.v6, 8);
493 /* use allocated 64bit prefix as lower 64bit, used as link id by MS */
494 memcpy(tmp.v6.s6_addr+8, &member->addr.v6, 8);
495 in46a_to_eua(&tmp, &pdp->eua);
496 } else
Harald Weltedda21ed2017-08-12 15:07:02 +0200497 OSMO_ASSERT(0);
jjako52c24142002-12-16 13:33:51 +0000498
Harald Weltedda21ed2017-08-12 15:07:02 +0200499 pdp->peer = member;
500 pdp->ipif = apn->tun.tun; /* TODO */
501 member->peer = pdp;
Max3142d8d2017-05-04 17:45:10 +0200502
503 if (!send_trap(gsn, pdp, member, "imsi-ass-ip")) { /* TRAP with IP assignment */
Max727417d2016-08-02 17:10:38 +0200504 gtp_create_context_resp(gsn, pdp, GTPCAUSE_NO_RESOURCES);
505 return 0;
506 }
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100507
Harald Weltedda21ed2017-08-12 15:07:02 +0200508 process_pco(apn, pdp);
Harald Welte1ae98772017-08-09 20:28:52 +0200509
Harald Weltedda21ed2017-08-12 15:07:02 +0200510 LOGPPDP(LOGL_INFO, pdp, "Successful PDP Context Creation: APN=%s(%s), TEIC=%u, IP=%s\n",
511 name_buf, apn->cfg.name, pdp->teic_own, in46a_ntoa(&member->addr));
Harald Weltebed35df2011-11-02 13:06:18 +0100512 gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ);
513 return 0; /* Success */
Harald Weltedda21ed2017-08-12 15:07:02 +0200514
515err_pool_full:
516 LOGPPDP(LOGL_ERROR, pdp, "Cannot allocate IP address from pool (full!)\n");
517 gtp_create_context_resp(gsn, pdp, -rc);
518 return 0; /* Already in use, or no more available */
jjako52c24142002-12-16 13:33:51 +0000519}
520
Harald Weltedda21ed2017-08-12 15:07:02 +0200521/* Internet-originated IP packet, needs to be sent via GTP towards MS */
522static int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len)
Harald Weltebed35df2011-11-02 13:06:18 +0100523{
Harald Weltedda21ed2017-08-12 15:07:02 +0200524 struct apn_ctx *apn = tun->priv;
Harald Weltebed35df2011-11-02 13:06:18 +0100525 struct ippoolm_t *ipm;
Harald Welted12eab92017-08-02 19:49:47 +0200526 struct in46_addr dst;
Harald Welte63ebccd2017-08-02 21:10:09 +0200527 struct iphdr *iph = (struct iphdr *)pack;
Harald Weltea0d281d2017-08-02 21:48:16 +0200528 struct ip6_hdr *ip6h = (struct ip6_hdr *)pack;
Harald Weltedda21ed2017-08-12 15:07:02 +0200529 struct ippool_t *pool;
jjakoc6762cf2004-04-28 14:52:58 +0000530
Harald Welte63ebccd2017-08-02 21:10:09 +0200531 if (iph->version == 4) {
Harald Welted12eab92017-08-02 19:49:47 +0200532 if (len < sizeof(*iph) || len < 4*iph->ihl)
533 return -1;
534 dst.len = 4;
Harald Welte63ebccd2017-08-02 21:10:09 +0200535 dst.v4.s_addr = iph->daddr;
Harald Weltedda21ed2017-08-12 15:07:02 +0200536 pool = apn->v4.pool;
Harald Weltea0d281d2017-08-02 21:48:16 +0200537 } else if (iph->version == 6) {
Harald Welted4d6e092017-08-08 18:10:43 +0200538 /* Due to the fact that 3GPP requires an allocation of a
539 * /64 prefix to each MS, we must instruct
540 * ippool_getip() below to match only the leading /64
541 * prefix, i.e. the first 8 bytes of the address */
542 dst.len = 8;
Harald Weltea0d281d2017-08-02 21:48:16 +0200543 dst.v6 = ip6h->ip6_dst;
Harald Weltedda21ed2017-08-12 15:07:02 +0200544 pool = apn->v6.pool;
Harald Welted12eab92017-08-02 19:49:47 +0200545 } else {
Harald Weltedda21ed2017-08-12 15:07:02 +0200546 LOGP(DTUN, LOGL_NOTICE, "non-IPv packet received from tun\n");
Harald Welted12eab92017-08-02 19:49:47 +0200547 return -1;
548 }
jjakoc6762cf2004-04-28 14:52:58 +0000549
Harald Weltedda21ed2017-08-12 15:07:02 +0200550 /* IPv6 packet but no IPv6 pool, or IPv4 packet with no IPv4 pool */
551 if (!pool)
552 return 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100553
Harald Weltedda21ed2017-08-12 15:07:02 +0200554 DEBUGP(DTUN, "Received packet from tun!\n");
555
556 if (ippool_getip(pool, &ipm, &dst)) {
557 DEBUGP(DTUN, "Received packet with no PDP contex!!\n");
Harald Weltebed35df2011-11-02 13:06:18 +0100558 return 0;
559 }
560
561 if (ipm->peer) /* Check if a peer protocol is defined */
Harald Weltedda21ed2017-08-12 15:07:02 +0200562 gtp_data_req(apn->ggsn->gsn, (struct pdp_t *)ipm->peer, pack, len);
Harald Weltebed35df2011-11-02 13:06:18 +0100563 return 0;
jjako52c24142002-12-16 13:33:51 +0000564}
565
Harald Welted46bcd22017-08-08 23:27:22 +0200566/* RFC3307 link-local scope multicast address */
567static const struct in6_addr all_router_mcast_addr = {
568 .s6_addr = { 0xff,0x02,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,2 }
569};
570
Harald Weltedda21ed2017-08-12 15:07:02 +0200571/* MS-originated GTP1-U packet, needs to be sent via TUN device */
572static int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len)
Harald Weltebed35df2011-11-02 13:06:18 +0100573{
Harald Welted46bcd22017-08-08 23:27:22 +0200574 struct iphdr *iph = (struct iphdr *)pack;
575 struct ip6_hdr *ip6h = (struct ip6_hdr *)pack;
576
Harald Weltedda21ed2017-08-12 15:07:02 +0200577 LOGPPDP(LOGL_DEBUG, pdp, "Packet received: forwarding to tun\n");
Harald Welted46bcd22017-08-08 23:27:22 +0200578
579 switch (iph->version) {
580 case 6:
581 /* daddr: all-routers multicast addr */
582 if (IN6_ARE_ADDR_EQUAL(&ip6h->ip6_dst, &all_router_mcast_addr))
Harald Weltedda21ed2017-08-12 15:07:02 +0200583 return handle_router_mcast(pdp->gsn, pdp, pack, len);
Harald Welted46bcd22017-08-08 23:27:22 +0200584 break;
585 case 4:
586 break;
587 default:
Harald Weltedda21ed2017-08-12 15:07:02 +0200588 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS is neither IPv4 nor IPv6: %s\n",
589 osmo_hexdump(pack, len));
Harald Welted46bcd22017-08-08 23:27:22 +0200590 return -1;
591 }
Harald Weltebed35df2011-11-02 13:06:18 +0100592 return tun_encaps((struct tun_t *)pdp->ipif, pack, len);
jjako52c24142002-12-16 13:33:51 +0000593}
594
Harald Welte632e8432017-09-05 18:12:14 +0200595static char *config_file = "osmo-ggsn.cfg";
Harald Weltedda21ed2017-08-12 15:07:02 +0200596
597/* callback for tun device osmocom select loop integration */
598static int ggsn_tun_fd_cb(struct osmo_fd *fd, unsigned int what)
599{
600 struct apn_ctx *apn = fd->data;
601
602 OSMO_ASSERT(what & BSC_FD_READ);
603
604 return tun_decaps(apn->tun.tun);
605}
606
607/* callback for libgtp osmocom select loop integration */
608static int ggsn_gtp_fd_cb(struct osmo_fd *fd, unsigned int what)
609{
610 struct ggsn_ctx *ggsn = fd->data;
611 int rc;
612
613 OSMO_ASSERT(what & BSC_FD_READ);
614
615 switch (fd->priv_nr) {
616 case 0:
617 rc = gtp_decaps0(ggsn->gsn);
618 break;
619 case 1:
620 rc = gtp_decaps1c(ggsn->gsn);
621 break;
622 case 2:
623 rc = gtp_decaps1u(ggsn->gsn);
624 break;
625 default:
626 OSMO_ASSERT(0);
627 break;
628 }
629 return rc;
630}
631
632static void ggsn_gtp_tmr_start(struct ggsn_ctx *ggsn)
633{
634 struct timeval next;
635
636 /* Retrieve next retransmission as timeval */
637 gtp_retranstimeout(ggsn->gsn, &next);
638
639 /* re-schedule the timer */
640 osmo_timer_schedule(&ggsn->gtp_timer, next.tv_sec, next.tv_usec/1000);
641}
642
643/* timer callback for libgtp retransmission and ping */
644static void ggsn_gtp_tmr_cb(void *data)
645{
646 struct ggsn_ctx *ggsn = data;
647
648 /* do all the retransmissions as needed */
649 gtp_retrans(ggsn->gsn);
650
651 ggsn_gtp_tmr_start(ggsn);
652}
653
654/* To exit gracefully. Used with GCC compilation flag -pg and gprof */
655static void signal_handler(int s)
656{
657 LOGP(DGGSN, LOGL_NOTICE, "signal %d received\n", s);
658 switch (s) {
659 case SIGINT:
Harald Weltee8049472017-08-20 12:44:21 +0200660 case SIGTERM:
Harald Weltedda21ed2017-08-12 15:07:02 +0200661 LOGP(DGGSN, LOGL_NOTICE, "SIGINT received, shutting down\n");
662 end = 1;
663 break;
664 case SIGABRT:
665 case SIGUSR1:
666 talloc_report(tall_vty_ctx, stderr);
667 talloc_report_full(tall_ggsn_ctx, stderr);
668 break;
669 case SIGUSR2:
670 talloc_report_full(tall_vty_ctx, stderr);
671 break;
672 default:
673 break;
674 }
675}
676
677
678/* Start a given GGSN */
679int ggsn_start(struct ggsn_ctx *ggsn)
680{
681 struct apn_ctx *apn;
682 int rc;
683
684 if (ggsn->started)
685 return 0;
686
687 LOGPGGSN(LOGL_INFO, ggsn, "Starting GGSN\n");
688
689 /* Start libgtp listener */
690 if (gtp_new(&ggsn->gsn, ggsn->cfg.state_dir, &ggsn->cfg.listen_addr.v4, GTP_MODE_GGSN)) {
691 LOGPGGSN(LOGL_ERROR, ggsn, "Failed to create GTP: %s\n", strerror(errno));
692 return -1;
693 }
694 ggsn->gsn->priv = ggsn;
695
Harald Welte98146772017-09-05 17:41:20 +0200696 /* patch in different addresses to use (in case we're behind NAT, the listen
697 * address is different from what we advertise externally) */
698 if (ggsn->cfg.gtpc_addr.v4.s_addr)
699 ggsn->gsn->gsnc = ggsn->cfg.gtpc_addr.v4;
700
701 if (ggsn->cfg.gtpu_addr.v4.s_addr)
702 ggsn->gsn->gsnu = ggsn->cfg.gtpu_addr.v4;
703
Harald Weltedda21ed2017-08-12 15:07:02 +0200704 /* Register File Descriptors */
705 osmo_fd_setup(&ggsn->gtp_fd0, ggsn->gsn->fd0, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 0);
706 rc = osmo_fd_register(&ggsn->gtp_fd0);
707 OSMO_ASSERT(rc == 0);
708
709 osmo_fd_setup(&ggsn->gtp_fd1c, ggsn->gsn->fd1c, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 1);
710 rc = osmo_fd_register(&ggsn->gtp_fd1c);
711 OSMO_ASSERT(rc == 0);
712
713 osmo_fd_setup(&ggsn->gtp_fd1u, ggsn->gsn->fd1u, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 2);
714 rc = osmo_fd_register(&ggsn->gtp_fd1u);
715 OSMO_ASSERT(rc == 0);
716
717 /* Start GTP re-transmission timer */
718 osmo_timer_setup(&ggsn->gtp_timer, ggsn_gtp_tmr_cb, ggsn);
719
720 gtp_set_cb_data_ind(ggsn->gsn, encaps_tun);
721 gtp_set_cb_delete_context(ggsn->gsn, delete_context);
722 gtp_set_cb_create_context_ind(ggsn->gsn, create_context_ind);
723
724 LOGPGGSN(LOGL_NOTICE, ggsn, "Successfully started\n");
725 ggsn->started = true;
726
727 llist_for_each_entry(apn, &ggsn->apn_list, list)
728 apn_start(apn);
729
730 return 0;
731}
732
733/* Stop a given GGSN */
734int ggsn_stop(struct ggsn_ctx *ggsn)
735{
736 struct apn_ctx *apn;
737
738 if (!ggsn->started)
739 return 0;
740
741 /* iterate over all APNs and stop them */
742 llist_for_each_entry(apn, &ggsn->apn_list, list)
743 apn_stop(apn, true);
744
745 osmo_timer_del(&ggsn->gtp_timer);
746
747 osmo_fd_unregister(&ggsn->gtp_fd1u);
748 osmo_fd_unregister(&ggsn->gtp_fd1c);
749 osmo_fd_unregister(&ggsn->gtp_fd0);
750
751 if (ggsn->gsn) {
752 gtp_free(ggsn->gsn);
753 ggsn->gsn = NULL;
754 }
755
756 ggsn->started = false;
757 return 0;
758}
759
760static void print_usage()
761{
762 printf("Usage: osmo-ggsn [-h] [-D] [-c configfile] [-V]\n");
763}
764
765static void print_help()
766{
767 printf( " Some useful help...\n"
768 " -h --help This help text\n"
769 " -D --daemonize Fork the process into a background daemon\n"
770 " -c --config-file filename The config file to use\n"
771 " -V --version Print the version of OsmoGGSN\n"
772 );
773}
774
775static void handle_options(int argc, char **argv)
776{
777 while (1) {
778 int option_index = 0, c;
779 static struct option long_options[] = {
780 { "help", 0, 0, 'h' },
781 { "daemonize", 0, 0, 'D' },
782 { "config-file", 1, 0, 'c' },
783 { "version", 0, 0, 'V' },
784 { 0, 0, 0, 0 }
785 };
786
787 c = getopt_long(argc, argv, "hdc:V", long_options, &option_index);
788 if (c == -1)
789 break;
790
791 switch (c) {
792 case 'h':
793 print_usage();
794 print_help();
795 exit(0);
796 case 'D':
797 daemonize = 1;
798 break;
799 case 'c':
800 config_file = optarg;
801 break;
802 case 'V':
803 print_version(1);
804 exit(0);
805 break;
806 }
807 }
808}
809
jjako52c24142002-12-16 13:33:51 +0000810int main(int argc, char **argv)
811{
Harald Weltedda21ed2017-08-12 15:07:02 +0200812 struct ggsn_ctx *ggsn;
813 int rc;
jjako52c24142002-12-16 13:33:51 +0000814
Harald Welte632e8432017-09-05 18:12:14 +0200815 tall_ggsn_ctx = talloc_named_const(NULL, 0, "OsmoGGSN");
Harald Weltedda21ed2017-08-12 15:07:02 +0200816 msgb_talloc_ctx_init(tall_ggsn_ctx, 0);
jjako52c24142002-12-16 13:33:51 +0000817
Harald Weltee8049472017-08-20 12:44:21 +0200818 /* Handle keyboard interrupt SIGINT */
Harald Weltedda21ed2017-08-12 15:07:02 +0200819 signal(SIGINT, &signal_handler);
Harald Weltee8049472017-08-20 12:44:21 +0200820 signal(SIGTERM, &signal_handler);
Harald Weltedda21ed2017-08-12 15:07:02 +0200821 signal(SIGABRT, &signal_handler);
822 signal(SIGUSR1, &signal_handler);
823 signal(SIGUSR2, &signal_handler);
jjako52c24142002-12-16 13:33:51 +0000824
Harald Weltedda21ed2017-08-12 15:07:02 +0200825 osmo_init_ignore_signals();
Holger Hans Peter Freyther9c7fd8e2014-12-04 16:32:37 +0100826 osmo_init_logging(&log_info);
Harald Weltedda21ed2017-08-12 15:07:02 +0200827 osmo_stats_init(tall_ggsn_ctx);
jjako0141d202004-01-09 15:19:20 +0000828
Harald Weltedda21ed2017-08-12 15:07:02 +0200829 vty_init(&g_vty_info);
830 logging_vty_add_cmds(NULL);
831 osmo_stats_vty_add_cmds(&log_info);
832 ggsn_vty_init();
833 ctrl_vty_init(tall_ggsn_ctx);
834
835 handle_options(argc, argv);
836
837 rate_ctr_init(tall_ggsn_ctx);
838
839 rc = vty_read_config_file(config_file, NULL);
840 if (rc < 0) {
841 fprintf(stderr, "Failed to open config file: '%s'\n", config_file);
842 exit(2);
Harald Weltebed35df2011-11-02 13:06:18 +0100843 }
jjako52c24142002-12-16 13:33:51 +0000844
Harald Weltedda21ed2017-08-12 15:07:02 +0200845 rc = telnet_init_dynif(tall_ggsn_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_GGSN);
846 if (rc < 0)
Harald Weltebed35df2011-11-02 13:06:18 +0100847 exit(1);
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +0100848
Harald Weltedda21ed2017-08-12 15:07:02 +0200849 g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_GGSN, NULL);
850 if (!g_ctrlh) {
851 LOGP(DGGSN, LOGL_ERROR, "Failed to create CTRL interface.\n");
Harald Weltebed35df2011-11-02 13:06:18 +0100852 exit(1);
853 }
jjako88c22162003-07-06 19:33:18 +0000854
Harald Weltedda21ed2017-08-12 15:07:02 +0200855 if (daemonize) {
856 rc = osmo_daemonize();
857 if (rc < 0) {
858 perror("Error during daemonize");
Harald Weltebed35df2011-11-02 13:06:18 +0100859 exit(1);
860 }
861 }
jjako1d3db972004-01-16 09:56:56 +0000862
Harald Weltedda21ed2017-08-12 15:07:02 +0200863#if 0
Harald Weltebed35df2011-11-02 13:06:18 +0100864 /* qos */
865 qos.l = 3;
866 qos.v[2] = (args_info.qos_arg) & 0xff;
867 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
868 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
Harald Weltedda21ed2017-08-12 15:07:02 +0200869#endif
jjakoa7cd2492003-04-11 09:40:12 +0000870
Harald Weltedda21ed2017-08-12 15:07:02 +0200871 /* Main select loop */
872 while (!end) {
873 osmo_select_main(0);
Harald Weltebed35df2011-11-02 13:06:18 +0100874 }
jjakoe0149782003-07-06 17:07:04 +0000875
Harald Weltedda21ed2017-08-12 15:07:02 +0200876 llist_for_each_entry(ggsn, &g_ggsn_list, list)
877 ggsn_stop(ggsn);
Harald Weltebed35df2011-11-02 13:06:18 +0100878
879 return 1;
jjako52c24142002-12-16 13:33:51 +0000880}