blob: cb11c49e1f719a7d40521fb847530a92ad933667 [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"
jjako52c24142002-12-16 13:33:51 +000066#include "../gtp/pdp.h"
67#include "../gtp/gtp.h"
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +010068#include "gtp-kernel.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 }
128 /* release tun device */
Harald Welteed08eb12017-10-01 18:15:56 +0800129 LOGPAPN(LOGL_INFO, apn, "Closing TUN device %s\n", apn->tun.tun->devname);
Harald Weltedda21ed2017-08-12 15:07:02 +0200130 osmo_fd_unregister(&apn->tun.fd);
131 tun_free(apn->tun.tun);
132 apn->tun.tun = NULL;
133 }
Harald Welte0d0e2422017-11-08 15:49:15 +0900134 gtp_kernel_stop(apn->tun.cfg.dev_name);
Harald Weltedda21ed2017-08-12 15:07:02 +0200135
136 if (apn->v4.pool) {
137 LOGPAPN(LOGL_INFO, apn, "Releasing IPv4 pool\n");
138 ippool_free(apn->v4.pool);
139 apn->v4.pool = NULL;
140 }
141 if (apn->v6.pool) {
142 LOGPAPN(LOGL_INFO, apn, "Releasing IPv6 pool\n");
143 ippool_free(apn->v6.pool);
144 apn->v6.pool = NULL;
145 }
146
147 apn->started = false;
148 return 0;
149}
150
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200151
Harald Weltef55a0392017-11-08 14:33:55 +0900152static int alloc_ippool_blacklist(struct apn_ctx *apn, struct in46_prefix **blacklist, bool ipv6)
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200153{
154
155 int flags, len, len2, i;
156
Harald Weltee2a1de52017-11-08 15:24:07 +0900157 *blacklist = NULL;
158
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200159 if (ipv6)
160 flags = IP_TYPE_IPv6_NONLINK;
161 else
162 flags = IP_TYPE_IPv4;
163
164 while (1) {
Harald Weltee2a1de52017-11-08 15:24:07 +0900165 len = netdev_ip_local_get(apn->tun.cfg.dev_name, NULL, 0, flags);
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200166 if (len < 1)
167 return len;
168
169 *blacklist = talloc_zero_size(apn, len * sizeof(struct in46_prefix));
Harald Weltee2a1de52017-11-08 15:24:07 +0900170 len2 = netdev_ip_local_get(apn->tun.cfg.dev_name, *blacklist, len, flags);
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200171 if (len2 < 1) {
172 talloc_free(*blacklist);
Harald Weltee2a1de52017-11-08 15:24:07 +0900173 *blacklist = NULL;
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200174 return len2;
175 }
176
Harald Weltee2a1de52017-11-08 15:24:07 +0900177 if (len2 > len) { /* iface was added between 2 calls, repeat operation */
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200178 talloc_free(*blacklist);
Harald Weltee2a1de52017-11-08 15:24:07 +0900179 *blacklist = NULL;
180 } else
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200181 break;
182 }
183
184 for (i = 0; i < len2; i++)
185 LOGPAPN(LOGL_INFO, apn, "Blacklist tun IP %s\n",
186 in46p_ntoa(&(*blacklist)[i]));
187
188 return len2;
189}
190
Harald Weltedda21ed2017-08-12 15:07:02 +0200191/* actually start the APN with its current config */
192int apn_start(struct apn_ctx *apn)
193{
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200194 int ippool_flags = IPPOOL_NONETWORK | IPPOOL_NOBROADCAST;
Pau Espin Pedrola037e592017-10-16 14:41:37 +0200195 struct in46_prefix ipv6_tun_linklocal_ip;
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200196 struct in46_prefix *blacklist;
197 int blacklist_size;
Pau Espin Pedrolbffc3f92017-12-14 11:19:10 +0100198 int rc;
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200199
Harald Weltedda21ed2017-08-12 15:07:02 +0200200 if (apn->started)
201 return 0;
202
203 LOGPAPN(LOGL_INFO, apn, "Starting\n");
204 switch (apn->cfg.gtpu_mode) {
205 case APN_GTPU_MODE_TUN:
206 LOGPAPN(LOGL_INFO, apn, "Opening TUN device %s\n", apn->tun.cfg.dev_name);
207 if (tun_new(&apn->tun.tun, apn->tun.cfg.dev_name)) {
208 LOGPAPN(LOGL_ERROR, apn, "Failed to configure tun device\n");
209 return -1;
210 }
211 LOGPAPN(LOGL_INFO, apn, "Opened TUN device %s\n", apn->tun.tun->devname);
212
213 /* Register with libosmcoore */
214 osmo_fd_setup(&apn->tun.fd, apn->tun.tun->fd, BSC_FD_READ, ggsn_tun_fd_cb, apn, 0);
215 osmo_fd_register(&apn->tun.fd);
216
217 /* Set TUN library callback */
218 tun_set_cb_ind(apn->tun.tun, cb_tun_ind);
219
220 if (apn->v4.cfg.ifconfig_prefix.addr.len) {
221 LOGPAPN(LOGL_INFO, apn, "Setting tun IP address %s\n",
222 in46p_ntoa(&apn->v4.cfg.ifconfig_prefix));
223 if (tun_setaddr(apn->tun.tun, &apn->v4.cfg.ifconfig_prefix.addr, NULL,
224 apn->v4.cfg.ifconfig_prefix.prefixlen)) {
225 LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv4 address %s: %s\n",
226 in46p_ntoa(&apn->v4.cfg.ifconfig_prefix), strerror(errno));
227 apn_stop(apn, false);
228 return -1;
229 }
230 }
231
232 if (apn->v6.cfg.ifconfig_prefix.addr.len) {
233 LOGPAPN(LOGL_INFO, apn, "Setting tun IPv6 address %s\n",
234 in46p_ntoa(&apn->v6.cfg.ifconfig_prefix));
235 if (tun_setaddr(apn->tun.tun, &apn->v6.cfg.ifconfig_prefix.addr, NULL,
236 apn->v6.cfg.ifconfig_prefix.prefixlen)) {
Harald Welteed1ba2c2017-10-01 18:16:17 +0800237 LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv6 address %s: %s. "
238 "Ensure you have ipv6 support and not used the disable_ipv6 sysctl?\n",
Harald Weltedda21ed2017-08-12 15:07:02 +0200239 in46p_ntoa(&apn->v6.cfg.ifconfig_prefix), strerror(errno));
240 apn_stop(apn, false);
241 return -1;
242 }
243 }
244
Pau Espin Pedrol37c45e32017-12-14 14:09:13 +0100245 if (apn->v6.cfg.ll_prefix.addr.len) {
246 LOGPAPN(LOGL_INFO, apn, "Setting tun IPv6 link-local address %s\n",
247 in46p_ntoa(&apn->v6.cfg.ll_prefix));
248 if (tun_addaddr(apn->tun.tun, &apn->v6.cfg.ll_prefix.addr, NULL,
249 apn->v6.cfg.ll_prefix.prefixlen)) {
250 LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv6 link-local address %s: %s. "
251 "Ensure you have ipv6 support and not used the disable_ipv6 sysctl?\n",
252 in46p_ntoa(&apn->v6.cfg.ll_prefix), strerror(errno));
253 apn_stop(apn, false);
254 return -1;
255 }
256 apn->v6_lladdr = apn->v6.cfg.ll_prefix.addr.v6;
257 }
258
Harald Weltedda21ed2017-08-12 15:07:02 +0200259 if (apn->tun.cfg.ipup_script) {
260 LOGPAPN(LOGL_INFO, apn, "Running ip-up script %s\n",
261 apn->tun.cfg.ipup_script);
262 tun_runscript(apn->tun.tun, apn->tun.cfg.ipup_script);
263 }
Harald Weltef85fe972017-09-24 20:00:34 +0800264
Pau Espin Pedrol37c45e32017-12-14 14:09:13 +0100265 if (apn->cfg.apn_type_mask & (APN_TYPE_IPv6|APN_TYPE_IPv4v6) &&
266 apn->v6.cfg.ll_prefix.addr.len == 0) {
Pau Espin Pedrolbffc3f92017-12-14 11:19:10 +0100267 rc = tun_ip_local_get(apn->tun.tun, &ipv6_tun_linklocal_ip, 1, IP_TYPE_IPv6_LINK);
268 if (rc < 1) {
269 LOGPAPN(LOGL_ERROR, apn, "Cannot obtain IPv6 link-local address of interface: %s\n",
270 rc ? strerror(errno) : "tun interface has no link-local IP assigned");
Harald Weltef85fe972017-09-24 20:00:34 +0800271 apn_stop(apn, false);
272 return -1;
273 }
Pau Espin Pedrola037e592017-10-16 14:41:37 +0200274 apn->v6_lladdr = ipv6_tun_linklocal_ip.addr.v6;
Harald Weltef85fe972017-09-24 20:00:34 +0800275 }
276
Harald Weltedda21ed2017-08-12 15:07:02 +0200277 /* set back-pointer from TUN device to APN */
278 apn->tun.tun->priv = apn;
279 break;
280 case APN_GTPU_MODE_KERNEL_GTP:
Harald Welte2fc2bc62017-11-08 15:50:53 +0900281 LOGPAPN(LOGL_INFO, apn, "Opening Kernel GTP device %s\n", apn->tun.cfg.dev_name);
Harald Welte490782d2017-11-08 14:09:51 +0900282 if (apn->cfg.apn_type_mask & (APN_TYPE_IPv6|APN_TYPE_IPv4v6)) {
283 LOGPAPN(LOGL_ERROR, apn, "Kernel GTP currently supports only IPv4\n");
284 apn_stop(apn, false);
285 return -1;
286 }
Harald Weltedda21ed2017-08-12 15:07:02 +0200287 /* use GTP kernel module for data packet encapsulation */
Harald Welte698a2332017-11-08 15:09:58 +0900288 if (gtp_kernel_init(apn->ggsn->gsn, apn->tun.cfg.dev_name,
289 &apn->v4.cfg.ifconfig_prefix, apn->tun.cfg.ipup_script) < 0) {
Harald Welte490782d2017-11-08 14:09:51 +0900290 return -1;
291 }
Harald Weltebed35df2011-11-02 13:06:18 +0100292 break;
293 default:
Harald Weltedda21ed2017-08-12 15:07:02 +0200294 LOGPAPN(LOGL_ERROR, apn, "Unknown GTPU Mode %d\n", apn->cfg.gtpu_mode);
295 return -1;
Harald Weltebed35df2011-11-02 13:06:18 +0100296 }
jjako0141d202004-01-09 15:19:20 +0000297
Harald Weltedda21ed2017-08-12 15:07:02 +0200298 /* Create IPv4 pool */
299 if (apn->v4.cfg.dynamic_prefix.addr.len) {
300 LOGPAPN(LOGL_INFO, apn, "Creating IPv4 pool %s\n",
301 in46p_ntoa(&apn->v4.cfg.dynamic_prefix));
Harald Weltef55a0392017-11-08 14:33:55 +0900302 if ((blacklist_size = alloc_ippool_blacklist(apn, &blacklist, false)) < 0)
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200303 LOGPAPN(LOGL_ERROR, apn, "Failed obtaining IPv4 tun IPs\n");
Harald Weltedda21ed2017-08-12 15:07:02 +0200304 if (ippool_new(&apn->v4.pool, &apn->v4.cfg.dynamic_prefix,
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200305 &apn->v4.cfg.static_prefix, ippool_flags,
306 blacklist, blacklist_size)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200307 LOGPAPN(LOGL_ERROR, apn, "Failed to create IPv4 pool\n");
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200308 talloc_free(blacklist);
Harald Weltedda21ed2017-08-12 15:07:02 +0200309 apn_stop(apn, false);
310 return -1;
311 }
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200312 talloc_free(blacklist);
Harald Weltebed35df2011-11-02 13:06:18 +0100313 }
Harald Weltedda21ed2017-08-12 15:07:02 +0200314
315 /* Create IPv6 pool */
316 if (apn->v6.cfg.dynamic_prefix.addr.len) {
317 LOGPAPN(LOGL_INFO, apn, "Creating IPv6 pool %s\n",
318 in46p_ntoa(&apn->v6.cfg.dynamic_prefix));
Harald Weltef55a0392017-11-08 14:33:55 +0900319 if ((blacklist_size = alloc_ippool_blacklist(apn, &blacklist, true)) < 0)
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200320 LOGPAPN(LOGL_ERROR, apn, "Failed obtaining IPv6 tun IPs\n");
Harald Weltedda21ed2017-08-12 15:07:02 +0200321 if (ippool_new(&apn->v6.pool, &apn->v6.cfg.dynamic_prefix,
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200322 &apn->v6.cfg.static_prefix, ippool_flags,
323 blacklist, blacklist_size)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200324 LOGPAPN(LOGL_ERROR, apn, "Failed to create IPv6 pool\n");
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200325 talloc_free(blacklist);
Harald Weltedda21ed2017-08-12 15:07:02 +0200326 apn_stop(apn, false);
327 return -1;
328 }
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200329 talloc_free(blacklist);
Harald Weltedda21ed2017-08-12 15:07:02 +0200330 }
331
332 LOGPAPN(LOGL_NOTICE, apn, "Successfully started\n");
333 apn->started = true;
334 return 0;
jjako0141d202004-01-09 15:19:20 +0000335}
jjako0141d202004-01-09 15:19:20 +0000336
Max3142d8d2017-05-04 17:45:10 +0200337static bool send_trap(const struct gsn_t *gsn, const struct pdp_t *pdp, const struct ippoolm_t *member, const char *var)
338{
Harald Welted12eab92017-08-02 19:49:47 +0200339 char addrbuf[256];
Max3142d8d2017-05-04 17:45:10 +0200340 char val[NAMESIZE];
341
Harald Welted12eab92017-08-02 19:49:47 +0200342 const char *addrstr = in46a_ntop(&member->addr, addrbuf, sizeof(addrbuf));
343
Harald Welteb10ee082017-08-12 19:29:16 +0200344 snprintf(val, sizeof(val), "%s,%s", imsi_gtp2str(&pdp->imsi), addrstr);
Max3142d8d2017-05-04 17:45:10 +0200345
Harald Weltedda21ed2017-08-12 15:07:02 +0200346 if (ctrl_cmd_send_trap(g_ctrlh, var, val) < 0) {
347 LOGPPDP(LOGL_ERROR, pdp, "Failed to create and send TRAP %s\n", var);
Max3142d8d2017-05-04 17:45:10 +0200348 return false;
349 }
350 return true;
351}
352
Harald Weltedda21ed2017-08-12 15:07:02 +0200353static int delete_context(struct pdp_t *pdp)
Harald Weltebed35df2011-11-02 13:06:18 +0100354{
Harald Weltedda21ed2017-08-12 15:07:02 +0200355 struct gsn_t *gsn = pdp->gsn;
Harald Welte698a2332017-11-08 15:09:58 +0900356 struct apn_ctx *apn = pdp->priv;
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100357 struct ippoolm_t *member;
358 int i;
Harald Weltedda21ed2017-08-12 15:07:02 +0200359
360 LOGPPDP(LOGL_INFO, pdp, "Deleting PDP context\n");
Maxdbd70242016-10-14 13:38:05 +0200361
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100362 for (i = 0; i < 2; i++) {
363 if (pdp->peer[i]) {
364 member = pdp->peer[i];
365 send_trap(gsn, pdp, member, "imsi-rem-ip"); /* TRAP with IP removal */
366 ippool_freeip(member->pool, member);
367 } else if(i == 0)
368 LOGPPDP(LOGL_ERROR, pdp, "Cannot find/free IP Pool member\n");
369 }
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100370
Harald Welte698a2332017-11-08 15:09:58 +0900371 if (gtp_kernel_tunnel_del(pdp, apn->tun.cfg.dev_name)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200372 LOGPPDP(LOGL_ERROR, pdp, "Cannot delete tunnel from kernel:%s\n",
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100373 strerror(errno));
374 }
375
Harald Weltebed35df2011-11-02 13:06:18 +0100376 return 0;
jjako52c24142002-12-16 13:33:51 +0000377}
378
Harald Welte1ae98772017-08-09 20:28:52 +0200379#include <osmocom/gsm/tlv.h>
380
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100381/* RFC 1332 */
382enum ipcp_options {
383 IPCP_OPT_IPADDR = 3,
384 IPCP_OPT_PRIMARY_DNS = 129,
385 IPCP_OPT_SECONDARY_DNS = 131,
386};
387
388struct ipcp_option_hdr {
389 uint8_t type;
390 uint8_t len;
391 uint8_t data[0];
392};
393
394struct ipcp_hdr {
395 uint8_t code;
396 uint8_t id;
397 uint16_t len;
398 uint8_t options[0];
399};
400
401/* determine if IPCP contains given option */
402static struct ipcp_option_hdr *ipcp_contains_option(struct ipcp_hdr *ipcp, enum ipcp_options opt)
403{
404 uint8_t *cur = ipcp->options;
405
406 /* iterate over Options and check if protocol contained */
407 while (cur + 2 <= ((uint8_t *)ipcp) + ipcp->len) {
408 struct ipcp_option_hdr *cur_opt = (struct ipcp_option_hdr *) cur;
409 if (cur_opt->type == opt)
410 return cur_opt;
411 cur += cur_opt->len;
412 }
413 return NULL;
414}
415
Harald Welte1ae98772017-08-09 20:28:52 +0200416/* 3GPP TS 24.008 10.6.5.3 */
417enum pco_protocols {
418 PCO_P_LCP = 0xC021,
419 PCO_P_PAP = 0xC023,
420 PCO_P_CHAP = 0xC223,
421 PCO_P_IPCP = 0x8021,
422 PCO_P_PCSCF_ADDR = 0x0001,
423 PCO_P_IM_CN_SS_F = 0x0002,
424 PCO_P_DNS_IPv6_ADDR = 0x0003,
425 PCO_P_POLICY_CTRL_REJ = 0x0004, /* only in Network->MS */
426 PCO_P_MS_SUP_NETREQ_BCI = 0x0005,
427 /* reserved */
428 PCO_P_DSMIPv6_HA_ADDR = 0x0007,
429 PCO_P_DSMIPv6_HN_PREF = 0x0008,
430 PCO_P_DSMIPv6_v4_HA_ADDR= 0x0009,
431 PCO_P_IP_ADDR_VIA_NAS = 0x000a, /* only MS->Network */
432 PCO_P_IPv4_ADDR_VIA_DHCP= 0x000b, /* only MS->Netowrk */
433 PCO_P_PCSCF_IPv4_ADDR = 0x000c,
434 PCO_P_DNS_IPv4_ADDR = 0x000d,
435 PCO_P_MSISDN = 0x000e,
436 PCO_P_IFOM_SUPPORT = 0x000f,
437 PCO_P_IPv4_LINK_MTU = 0x0010,
438 PCO_P_MS_SUPP_LOC_A_TFT = 0x0011,
439 PCO_P_PCSCF_RESEL_SUP = 0x0012, /* only MS->Network */
440 PCO_P_NBIFOM_REQ = 0x0013,
441 PCO_P_NBIFOM_MODE = 0x0014,
442 PCO_P_NONIP_LINK_MTU = 0x0015,
443 PCO_P_APN_RATE_CTRL_SUP = 0x0016,
444 PCO_P_PS_DATA_OFF_UE = 0x0017,
445 PCO_P_REL_DATA_SVC = 0x0018,
446};
447
448/* determine if PCO contains given protocol */
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100449static uint8_t *pco_contains_proto(struct ul255_t *pco, uint16_t prot)
Harald Welte1ae98772017-08-09 20:28:52 +0200450{
451 uint8_t *cur = pco->v + 1;
452
453 /* iterate over PCO and check if protocol contained */
Pau Espin Pedrol0ab62fe2017-08-30 15:51:24 +0200454 while (cur + 3 <= pco->v + pco->l) {
Harald Welte1ae98772017-08-09 20:28:52 +0200455 uint16_t cur_prot = osmo_load16be(cur);
456 uint8_t cur_len = cur[2];
457 if (cur_prot == prot)
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100458 return cur;
Pau Espin Pedrol0ab62fe2017-08-30 15:51:24 +0200459 cur += cur_len + 3;
Harald Welte1ae98772017-08-09 20:28:52 +0200460 }
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100461 return NULL;
Harald Welte1ae98772017-08-09 20:28:52 +0200462}
463
Pau Espin Pedrol7d54ed42018-01-25 20:09:16 +0100464/*! Get the peer of pdp based on IP version used.
465 * \param[in] pdp PDP context to select the peer from.
466 * \param[in] v4v6 IP version to select. Valid values are 4 and 6.
467 * \returns The selected peer matching the given IP version. NULL if not present.
468 */
469static struct ippoolm_t *pdp_get_peer_ipv(struct pdp_t *pdp, bool is_ipv6) {
470 uint8_t len1, len2, i;
471
472 if (is_ipv6) {
473 len1 = 8;
474 len2 = 16;
475 } else {
476 len1 = sizeof(struct in_addr);
477 len2 = len1;
478 }
479
480 for (i = 0; i < 2; i++) {
481 struct ippoolm_t * ippool = pdp->peer[i];
482 if (ippool && (ippool->addr.len == len1 || ippool->addr.len == len2))
483 return ippool;
484 }
485 return NULL;
486}
487
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100488/* construct an IPCP PCO response from request*/
489static int build_ipcp_pco(struct apn_ctx *apn, struct pdp_t *pdp, struct msgb *msg)
Harald Weltedda21ed2017-08-12 15:07:02 +0200490{
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100491 const struct in46_addr *dns1 = &apn->v4.cfg.dns[0];
492 const struct in46_addr *dns2 = &apn->v4.cfg.dns[1];
493 struct ipcp_hdr *ipcp;
494 uint8_t *len1, *len2, *pco_ipcp;
Harald Weltedda21ed2017-08-12 15:07:02 +0200495 uint8_t *start = msg->tail;
496 unsigned int len_appended;
497
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100498 if (!(pco_ipcp = pco_contains_proto(&pdp->pco_req, PCO_P_IPCP)))
499 return 0;
500 ipcp = (struct ipcp_hdr*) (pco_ipcp + 3); /* 2=type + 1=len */
501
Harald Weltedda21ed2017-08-12 15:07:02 +0200502 /* Three byte T16L header */
503 msgb_put_u16(msg, 0x8021); /* IPCP */
504 len1 = msgb_put(msg, 1); /* Length of contents: delay */
505
506 msgb_put_u8(msg, 0x02); /* ACK */
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100507 msgb_put_u8(msg, ipcp->id); /* ID: Needs to match request */
Harald Weltedda21ed2017-08-12 15:07:02 +0200508 msgb_put_u8(msg, 0x00); /* Length MSB */
509 len2 = msgb_put(msg, 1); /* Length LSB: delay */
510
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100511 if (dns1->len == 4 && ipcp_contains_option(ipcp, IPCP_OPT_PRIMARY_DNS)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200512 msgb_put_u8(msg, 0x81); /* DNS1 Tag */
513 msgb_put_u8(msg, 2 + dns1->len);/* DNS1 Length, incl. TL */
Harald Weltebcab7fb2017-12-03 21:43:50 +0100514 msgb_put_u32(msg, ntohl(dns1->v4.s_addr));
Harald Weltedda21ed2017-08-12 15:07:02 +0200515 }
516
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100517 if (dns2->len == 4 && ipcp_contains_option(ipcp, IPCP_OPT_SECONDARY_DNS)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200518 msgb_put_u8(msg, 0x83); /* DNS2 Tag */
519 msgb_put_u8(msg, 2 + dns2->len);/* DNS2 Length, incl. TL */
Harald Weltebcab7fb2017-12-03 21:43:50 +0100520 msgb_put_u32(msg, ntohl(dns2->v4.s_addr));
Harald Weltedda21ed2017-08-12 15:07:02 +0200521 }
522
523 /* patch in length values */
524 len_appended = msg->tail - start;
525 *len1 = len_appended - 3;
526 *len2 = len_appended - 3;
527
528 return 0;
529}
530
Harald Welte1ae98772017-08-09 20:28:52 +0200531/* process one PCO request from a MS/UE, putting together the proper responses */
Harald Weltedda21ed2017-08-12 15:07:02 +0200532static void process_pco(struct apn_ctx *apn, struct pdp_t *pdp)
Harald Welte1ae98772017-08-09 20:28:52 +0200533{
534 struct msgb *msg = msgb_alloc(256, "PCO");
Pau Espin Pedrol4ae8d822018-01-26 17:51:55 +0100535 struct ippoolm_t *peer_v4 = pdp_get_peer_ipv(pdp, false);
Harald Weltedda21ed2017-08-12 15:07:02 +0200536 unsigned int i;
537
538 OSMO_ASSERT(msg);
Harald Welte1ae98772017-08-09 20:28:52 +0200539 msgb_put_u8(msg, 0x80); /* ext-bit + configuration protocol byte */
540
Pau Espin Pedrol4ae8d822018-01-26 17:51:55 +0100541 if (peer_v4)
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100542 build_ipcp_pco(apn, pdp, msg);
Harald Welte1ae98772017-08-09 20:28:52 +0200543
544 if (pco_contains_proto(&pdp->pco_req, PCO_P_DNS_IPv6_ADDR)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200545 for (i = 0; i < ARRAY_SIZE(apn->v6.cfg.dns); i++) {
546 struct in46_addr *i46a = &apn->v6.cfg.dns[i];
547 if (i46a->len != 16)
548 continue;
549 msgb_t16lv_put(msg, PCO_P_DNS_IPv6_ADDR, i46a->len, i46a->v6.s6_addr);
550 }
Harald Welte1ae98772017-08-09 20:28:52 +0200551 }
552
553 if (pco_contains_proto(&pdp->pco_req, PCO_P_DNS_IPv4_ADDR)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200554 for (i = 0; i < ARRAY_SIZE(apn->v4.cfg.dns); i++) {
555 struct in46_addr *i46a = &apn->v4.cfg.dns[i];
556 if (i46a->len != 4)
557 continue;
558 msgb_t16lv_put(msg, PCO_P_DNS_IPv4_ADDR, i46a->len, (uint8_t *)&i46a->v4);
559 }
Harald Welte1ae98772017-08-09 20:28:52 +0200560 }
561
562 if (msgb_length(msg) > 1) {
563 memcpy(pdp->pco_neg.v, msgb_data(msg), msgb_length(msg));
564 pdp->pco_neg.l = msgb_length(msg);
565 } else
566 pdp->pco_neg.l = 0;
567
568 msgb_free(msg);
569}
570
Harald Welte9d9d91b2017-10-14 16:22:16 +0200571static bool apn_supports_ipv4(const struct apn_ctx *apn)
572{
573 if (apn->v4.cfg.static_prefix.addr.len || apn->v4.cfg.dynamic_prefix.addr.len)
574 return true;
575 return false;
576}
577
578static bool apn_supports_ipv6(const struct apn_ctx *apn)
579{
580 if (apn->v6.cfg.static_prefix.addr.len || apn->v6.cfg.dynamic_prefix.addr.len)
581 return true;
582 return false;
583}
584
Harald Weltebed35df2011-11-02 13:06:18 +0100585int create_context_ind(struct pdp_t *pdp)
586{
Harald Weltedda21ed2017-08-12 15:07:02 +0200587 static char name_buf[256];
588 struct gsn_t *gsn = pdp->gsn;
589 struct ggsn_ctx *ggsn = gsn->priv;
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100590 struct in46_addr addr[2];
Pau Espin Pedrol4e43ef52018-01-26 18:12:19 +0100591 struct ippoolm_t *member = NULL, *addrv4 = NULL, *addrv6 = NULL;
592 char straddrv4[INET_ADDRSTRLEN], straddrv6[INET6_ADDRSTRLEN];
Harald Weltedda21ed2017-08-12 15:07:02 +0200593 struct apn_ctx *apn;
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100594 int rc, num_addr, i;
jjako52c24142002-12-16 13:33:51 +0000595
Harald Weltedda21ed2017-08-12 15:07:02 +0200596 osmo_apn_to_str(name_buf, pdp->apn_req.v, pdp->apn_req.l);
597
598 LOGPPDP(LOGL_DEBUG, pdp, "Processing create PDP context request for APN '%s'\n", name_buf);
599
600 /* First find an exact APN name match */
601 apn = ggsn_find_apn(ggsn, name_buf);
Harald Welte2e84d2c2017-10-01 13:36:52 +0800602 /* ignore if the APN has not been started */
Pau Espin Pedrol958256f2017-10-11 20:32:55 +0200603 if (apn && !apn->started)
Harald Welte2e84d2c2017-10-01 13:36:52 +0800604 apn = NULL;
Harald Welteb16c46b2017-10-01 18:28:18 +0800605
Harald Weltedda21ed2017-08-12 15:07:02 +0200606 /* then try default (if any) */
607 if (!apn)
608 apn = ggsn->cfg.default_apn;
Harald Welteb16c46b2017-10-01 18:28:18 +0800609 /* ignore if the APN has not been started */
Pau Espin Pedrol958256f2017-10-11 20:32:55 +0200610 if (apn && !apn->started)
Harald Welteb16c46b2017-10-01 18:28:18 +0800611 apn = NULL;
612
Harald Weltedda21ed2017-08-12 15:07:02 +0200613 if (!apn) {
614 /* no APN found for what user requested */
615 LOGPPDP(LOGL_NOTICE, pdp, "Unknown APN '%s', rejecting\n", name_buf);
616 gtp_create_context_resp(gsn, pdp, GTPCAUSE_MISSING_APN);
617 return 0;
618 }
jjako52c24142002-12-16 13:33:51 +0000619
Harald Welted9d88622017-08-04 00:22:35 +0200620 /* FIXME: we manually force all context requests to dynamic here! */
621 if (pdp->eua.l > 2)
622 pdp->eua.l = 2;
jjako52c24142002-12-16 13:33:51 +0000623
Harald Weltebed35df2011-11-02 13:06:18 +0100624 memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_req0));
jjako52c24142002-12-16 13:33:51 +0000625
Harald Weltebed35df2011-11-02 13:06:18 +0100626 memcpy(pdp->qos_neg.v, pdp->qos_req.v, pdp->qos_req.l); /* TODO */
627 pdp->qos_neg.l = pdp->qos_req.l;
jjako52c24142002-12-16 13:33:51 +0000628
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100629 memset(addr, 0, sizeof(addr));
630 if ((num_addr = in46a_from_eua(&pdp->eua, addr)) < 0) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200631 LOGPPDP(LOGL_ERROR, pdp, "Cannot decode EUA from MS/SGSN: %s\n",
Harald Welted1bf1e12017-08-03 00:00:23 +0200632 osmo_hexdump(pdp->eua.v, pdp->eua.l));
633 gtp_create_context_resp(gsn, pdp, GTPCAUSE_UNKNOWN_PDP);
634 return 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100635 }
jjakoa7cd2492003-04-11 09:40:12 +0000636
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100637 /* Allocate dynamic addresses from the pool */
638 for (i = 0; i < num_addr; i++) {
639 if (addr[i].len == sizeof(struct in_addr)) {
640 /* does this APN actually have an IPv4 pool? */
641 if (!apn_supports_ipv4(apn))
642 goto err_wrong_af;
Harald Welte9d9d91b2017-10-14 16:22:16 +0200643
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100644 rc = ippool_newip(apn->v4.pool, &member, &addr[i], 0);
645 if (rc < 0)
646 goto err_pool_full;
647 /* copy back */
648 memcpy(&addr[i].v4.s_addr, &member->addr.v4, 4);
jjakoa7cd2492003-04-11 09:40:12 +0000649
Pau Espin Pedrol4e43ef52018-01-26 18:12:19 +0100650 addrv4 = member;
651
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100652 } else if (addr[i].len == sizeof(struct in6_addr)) {
653
654 /* does this APN actually have an IPv6 pool? */
655 if (!apn_supports_ipv6(apn))
656 goto err_wrong_af;
657
658 rc = ippool_newip(apn->v6.pool, &member, &addr[i], 0);
659 if (rc < 0)
660 goto err_pool_full;
661
662 /* IPv6 doesn't really send the real/allocated address at this point, but just
663 * the link-identifier which the MS shall use for router solicitation */
664 /* initialize upper 64 bits to prefix, they are discarded by MS anyway */
665 memcpy(addr[i].v6.s6_addr, &member->addr.v6, 8);
666 /* use allocated 64bit prefix as lower 64bit, used as link id by MS */
667 memcpy(addr[i].v6.s6_addr+8, &member->addr.v6, 8);
Pau Espin Pedrol4e43ef52018-01-26 18:12:19 +0100668
669 addrv6 = member;
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100670 } else
671 OSMO_ASSERT(0);
672
673 pdp->peer[i] = member;
674 member->peer = pdp;
675 }
676
677 in46a_to_eua(addr, num_addr, &pdp->eua);
678
679 if (apn_supports_ipv4(apn)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200680 /* TODO: In IPv6, EUA doesn't contain the actual IP addr/prefix! */
Harald Welte698a2332017-11-08 15:09:58 +0900681 if (gtp_kernel_tunnel_add(pdp, apn->tun.cfg.dev_name) < 0) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200682 LOGPPDP(LOGL_ERROR, pdp, "Cannot add tunnel to kernel: %s\n", strerror(errno));
683 gtp_create_context_resp(gsn, pdp, GTPCAUSE_SYS_FAIL);
684 return 0;
685 }
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100686 }
Harald Welte9d9d91b2017-10-14 16:22:16 +0200687
Harald Weltedda21ed2017-08-12 15:07:02 +0200688 pdp->ipif = apn->tun.tun; /* TODO */
Harald Welte698a2332017-11-08 15:09:58 +0900689 pdp->priv = apn;
Max3142d8d2017-05-04 17:45:10 +0200690
Pau Espin Pedrol4e43ef52018-01-26 18:12:19 +0100691 /* TODO: change trap to send 2 IPs */
Max3142d8d2017-05-04 17:45:10 +0200692 if (!send_trap(gsn, pdp, member, "imsi-ass-ip")) { /* TRAP with IP assignment */
Max727417d2016-08-02 17:10:38 +0200693 gtp_create_context_resp(gsn, pdp, GTPCAUSE_NO_RESOURCES);
694 return 0;
695 }
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100696
Harald Weltedda21ed2017-08-12 15:07:02 +0200697 process_pco(apn, pdp);
Harald Welte1ae98772017-08-09 20:28:52 +0200698
Harald Welte93fed3b2017-09-24 11:43:17 +0800699 /* Transmit G-PDU sequence numbers (only) if configured in APN */
700 pdp->tx_gpdu_seq = apn->cfg.tx_gpdu_seq;
701
Pau Espin Pedrol4e43ef52018-01-26 18:12:19 +0100702 LOGPPDP(LOGL_INFO, pdp, "Successful PDP Context Creation: APN=%s(%s), TEIC=%u, IPv4=%s, IPv6=%s\n",
703 name_buf, apn->cfg.name, pdp->teic_own,
704 addrv4 ? inet_ntop(AF_INET, &addrv4->addr.v4, straddrv4, sizeof(straddrv4)) : "none",
705 addrv6 ? inet_ntop(AF_INET6, &addrv6->addr.v6, straddrv6, sizeof(straddrv6)) : "none");
Harald Weltebed35df2011-11-02 13:06:18 +0100706 gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ);
707 return 0; /* Success */
Harald Weltedda21ed2017-08-12 15:07:02 +0200708
709err_pool_full:
710 LOGPPDP(LOGL_ERROR, pdp, "Cannot allocate IP address from pool (full!)\n");
711 gtp_create_context_resp(gsn, pdp, -rc);
712 return 0; /* Already in use, or no more available */
Harald Welte9d9d91b2017-10-14 16:22:16 +0200713
714err_wrong_af:
715 LOGPPDP(LOGL_ERROR, pdp, "APN doesn't support requested EUA / AF type\n");
716 gtp_create_context_resp(gsn, pdp, GTPCAUSE_UNKNOWN_PDP);
717 return 0;
jjako52c24142002-12-16 13:33:51 +0000718}
719
Harald Weltedda21ed2017-08-12 15:07:02 +0200720/* Internet-originated IP packet, needs to be sent via GTP towards MS */
721static int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len)
Harald Weltebed35df2011-11-02 13:06:18 +0100722{
Harald Weltedda21ed2017-08-12 15:07:02 +0200723 struct apn_ctx *apn = tun->priv;
Harald Weltebed35df2011-11-02 13:06:18 +0100724 struct ippoolm_t *ipm;
Harald Welted12eab92017-08-02 19:49:47 +0200725 struct in46_addr dst;
Harald Welte63ebccd2017-08-02 21:10:09 +0200726 struct iphdr *iph = (struct iphdr *)pack;
Harald Weltea0d281d2017-08-02 21:48:16 +0200727 struct ip6_hdr *ip6h = (struct ip6_hdr *)pack;
Harald Weltedda21ed2017-08-12 15:07:02 +0200728 struct ippool_t *pool;
Pau Espin Pedrol134855c2018-01-30 16:04:53 +0100729 char straddr[INET6_ADDRSTRLEN];
Pau Espin Pedroldddbbaa2018-01-30 16:16:33 +0100730 uint8_t pref_offset;
jjakoc6762cf2004-04-28 14:52:58 +0000731
Pau Espin Pedrola4942e62018-01-30 16:01:27 +0100732 switch (iph->version) {
733 case 4:
Harald Welted12eab92017-08-02 19:49:47 +0200734 if (len < sizeof(*iph) || len < 4*iph->ihl)
735 return -1;
736 dst.len = 4;
Harald Welte63ebccd2017-08-02 21:10:09 +0200737 dst.v4.s_addr = iph->daddr;
Harald Weltedda21ed2017-08-12 15:07:02 +0200738 pool = apn->v4.pool;
Pau Espin Pedrola4942e62018-01-30 16:01:27 +0100739 break;
740 case 6:
Harald Welted4d6e092017-08-08 18:10:43 +0200741 /* Due to the fact that 3GPP requires an allocation of a
742 * /64 prefix to each MS, we must instruct
743 * ippool_getip() below to match only the leading /64
Pau Espin Pedroldddbbaa2018-01-30 16:16:33 +0100744 * prefix, i.e. the first 8 bytes of the address. If the ll addr
745 * is used, then the match should be done on the trailing 64
746 * bits. */
Harald Welted4d6e092017-08-08 18:10:43 +0200747 dst.len = 8;
Pau Espin Pedroldddbbaa2018-01-30 16:16:33 +0100748 pref_offset = IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_dst) ? 8 : 0;
749 memcpy(&dst.v6, ((uint8_t*)&ip6h->ip6_dst) + pref_offset, 8);
Harald Weltedda21ed2017-08-12 15:07:02 +0200750 pool = apn->v6.pool;
Pau Espin Pedrola4942e62018-01-30 16:01:27 +0100751 break;
752 default:
Pau Espin Pedrol55d639f2017-12-04 13:17:07 +0100753 LOGP(DTUN, LOGL_NOTICE, "non-IPv%u packet received from tun\n", iph->version);
Harald Welted12eab92017-08-02 19:49:47 +0200754 return -1;
755 }
jjakoc6762cf2004-04-28 14:52:58 +0000756
Harald Weltedda21ed2017-08-12 15:07:02 +0200757 /* IPv6 packet but no IPv6 pool, or IPv4 packet with no IPv4 pool */
758 if (!pool)
759 return 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100760
Max427699e2017-12-05 16:30:37 +0100761 DEBUGP(DTUN, "Received packet for APN(%s) from tun %s", apn->cfg.name, tun->devname);
Harald Weltedda21ed2017-08-12 15:07:02 +0200762
763 if (ippool_getip(pool, &ipm, &dst)) {
Pau Espin Pedrol134855c2018-01-30 16:04:53 +0100764 DEBUGPC(DTUN, " with no PDP contex! (%s)\n", iph->version == 4 ?
765 inet_ntop(AF_INET, &iph->saddr, straddr, sizeof(straddr)) :
766 inet_ntop(AF_INET6, &ip6h->ip6_src, straddr, sizeof(straddr)));
Harald Weltebed35df2011-11-02 13:06:18 +0100767 return 0;
768 }
Max427699e2017-12-05 16:30:37 +0100769 DEBUGPC(DTUN, "\n");
Harald Weltebed35df2011-11-02 13:06:18 +0100770
771 if (ipm->peer) /* Check if a peer protocol is defined */
Harald Weltedda21ed2017-08-12 15:07:02 +0200772 gtp_data_req(apn->ggsn->gsn, (struct pdp_t *)ipm->peer, pack, len);
Harald Weltebed35df2011-11-02 13:06:18 +0100773 return 0;
jjako52c24142002-12-16 13:33:51 +0000774}
775
Harald Welted46bcd22017-08-08 23:27:22 +0200776/* RFC3307 link-local scope multicast address */
777static const struct in6_addr all_router_mcast_addr = {
778 .s6_addr = { 0xff,0x02,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,2 }
779};
780
Harald Weltedda21ed2017-08-12 15:07:02 +0200781/* MS-originated GTP1-U packet, needs to be sent via TUN device */
782static int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len)
Harald Weltebed35df2011-11-02 13:06:18 +0100783{
Harald Welted46bcd22017-08-08 23:27:22 +0200784 struct iphdr *iph = (struct iphdr *)pack;
785 struct ip6_hdr *ip6h = (struct ip6_hdr *)pack;
Harald Weltef85fe972017-09-24 20:00:34 +0800786 struct tun_t *tun = (struct tun_t *)pdp->ipif;
787 struct apn_ctx *apn = tun->priv;
Pau Espin Pedrol5b1ef952018-01-25 20:50:59 +0100788 char straddr[INET6_ADDRSTRLEN];
Pau Espin Pedrol7d54ed42018-01-25 20:09:16 +0100789 struct ippoolm_t *peer;
Pau Espin Pedrol5b1ef952018-01-25 20:50:59 +0100790 uint8_t pref_offset;
Harald Weltef85fe972017-09-24 20:00:34 +0800791
792 OSMO_ASSERT(tun);
793 OSMO_ASSERT(apn);
Harald Welted46bcd22017-08-08 23:27:22 +0200794
Max427699e2017-12-05 16:30:37 +0100795 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 +0200796
797 switch (iph->version) {
798 case 6:
Pau Espin Pedrol7d54ed42018-01-25 20:09:16 +0100799 peer = pdp_get_peer_ipv(pdp, true);
800 if (!peer) {
801 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS IPv6 with unassigned EUA: %s\n",
802 osmo_hexdump(pack, len));
803 return -1;
804 }
805
Pau Espin Pedrol5b1ef952018-01-25 20:50:59 +0100806 /* Validate packet comes from IPaddr assigned to the pdp ctx.
807 If packet is a LL addr, then EUA is in the lower 64 bits,
808 otherwise it's used as the 64 prefix */
809 pref_offset = IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src) ? 8 : 0;
810 if (memcmp(((uint8_t*)&ip6h->ip6_src) + pref_offset, &peer->addr.v6, 8)) {
811 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS using unassigned src IPv6: %s\n",
812 inet_ntop(AF_INET6, &ip6h->ip6_src, straddr, sizeof(straddr)));
813 return -1;
814 }
815
Harald Welted46bcd22017-08-08 23:27:22 +0200816 /* daddr: all-routers multicast addr */
817 if (IN6_ARE_ADDR_EQUAL(&ip6h->ip6_dst, &all_router_mcast_addr))
Pau Espin Pedrol7d54ed42018-01-25 20:09:16 +0100818 return handle_router_mcast(pdp->gsn, pdp, &peer->addr.v6,
819 &apn->v6_lladdr, pack, len);
Harald Welted46bcd22017-08-08 23:27:22 +0200820 break;
821 case 4:
Pau Espin Pedrol7d54ed42018-01-25 20:09:16 +0100822 peer = pdp_get_peer_ipv(pdp, false);
823 if (!peer) {
824 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS IPv4 with unassigned EUA: %s\n",
825 osmo_hexdump(pack, len));
826 return -1;
827 }
Pau Espin Pedrol5b1ef952018-01-25 20:50:59 +0100828
829 /* Validate packet comes from IPaddr assigned to the pdp ctx */
830 if (memcmp(&iph->saddr, &peer->addr.v4, sizeof(peer->addr.v4))) {
831 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS using unassigned src IPv4: %s\n",
832 inet_ntop(AF_INET, &iph->saddr, straddr, sizeof(straddr)));
833 return -1;
834 }
Harald Welted46bcd22017-08-08 23:27:22 +0200835 break;
836 default:
Harald Weltedda21ed2017-08-12 15:07:02 +0200837 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS is neither IPv4 nor IPv6: %s\n",
838 osmo_hexdump(pack, len));
Harald Welted46bcd22017-08-08 23:27:22 +0200839 return -1;
840 }
Harald Weltebed35df2011-11-02 13:06:18 +0100841 return tun_encaps((struct tun_t *)pdp->ipif, pack, len);
jjako52c24142002-12-16 13:33:51 +0000842}
843
Harald Welte632e8432017-09-05 18:12:14 +0200844static char *config_file = "osmo-ggsn.cfg";
Harald Weltedda21ed2017-08-12 15:07:02 +0200845
846/* callback for tun device osmocom select loop integration */
847static int ggsn_tun_fd_cb(struct osmo_fd *fd, unsigned int what)
848{
849 struct apn_ctx *apn = fd->data;
850
851 OSMO_ASSERT(what & BSC_FD_READ);
852
853 return tun_decaps(apn->tun.tun);
854}
855
856/* callback for libgtp osmocom select loop integration */
857static int ggsn_gtp_fd_cb(struct osmo_fd *fd, unsigned int what)
858{
859 struct ggsn_ctx *ggsn = fd->data;
860 int rc;
861
862 OSMO_ASSERT(what & BSC_FD_READ);
863
864 switch (fd->priv_nr) {
865 case 0:
866 rc = gtp_decaps0(ggsn->gsn);
867 break;
868 case 1:
869 rc = gtp_decaps1c(ggsn->gsn);
870 break;
871 case 2:
872 rc = gtp_decaps1u(ggsn->gsn);
873 break;
874 default:
875 OSMO_ASSERT(0);
876 break;
877 }
878 return rc;
879}
880
881static void ggsn_gtp_tmr_start(struct ggsn_ctx *ggsn)
882{
883 struct timeval next;
884
885 /* Retrieve next retransmission as timeval */
886 gtp_retranstimeout(ggsn->gsn, &next);
887
888 /* re-schedule the timer */
889 osmo_timer_schedule(&ggsn->gtp_timer, next.tv_sec, next.tv_usec/1000);
890}
891
892/* timer callback for libgtp retransmission and ping */
893static void ggsn_gtp_tmr_cb(void *data)
894{
895 struct ggsn_ctx *ggsn = data;
896
897 /* do all the retransmissions as needed */
898 gtp_retrans(ggsn->gsn);
899
900 ggsn_gtp_tmr_start(ggsn);
901}
902
903/* To exit gracefully. Used with GCC compilation flag -pg and gprof */
904static void signal_handler(int s)
905{
906 LOGP(DGGSN, LOGL_NOTICE, "signal %d received\n", s);
907 switch (s) {
908 case SIGINT:
Harald Weltee8049472017-08-20 12:44:21 +0200909 case SIGTERM:
Harald Weltedda21ed2017-08-12 15:07:02 +0200910 LOGP(DGGSN, LOGL_NOTICE, "SIGINT received, shutting down\n");
911 end = 1;
912 break;
913 case SIGABRT:
914 case SIGUSR1:
915 talloc_report(tall_vty_ctx, stderr);
916 talloc_report_full(tall_ggsn_ctx, stderr);
917 break;
918 case SIGUSR2:
919 talloc_report_full(tall_vty_ctx, stderr);
920 break;
921 default:
922 break;
923 }
924}
925
926
927/* Start a given GGSN */
928int ggsn_start(struct ggsn_ctx *ggsn)
929{
930 struct apn_ctx *apn;
931 int rc;
932
933 if (ggsn->started)
934 return 0;
935
936 LOGPGGSN(LOGL_INFO, ggsn, "Starting GGSN\n");
937
938 /* Start libgtp listener */
939 if (gtp_new(&ggsn->gsn, ggsn->cfg.state_dir, &ggsn->cfg.listen_addr.v4, GTP_MODE_GGSN)) {
940 LOGPGGSN(LOGL_ERROR, ggsn, "Failed to create GTP: %s\n", strerror(errno));
941 return -1;
942 }
943 ggsn->gsn->priv = ggsn;
944
Harald Welte98146772017-09-05 17:41:20 +0200945 /* patch in different addresses to use (in case we're behind NAT, the listen
946 * address is different from what we advertise externally) */
947 if (ggsn->cfg.gtpc_addr.v4.s_addr)
948 ggsn->gsn->gsnc = ggsn->cfg.gtpc_addr.v4;
949
950 if (ggsn->cfg.gtpu_addr.v4.s_addr)
951 ggsn->gsn->gsnu = ggsn->cfg.gtpu_addr.v4;
952
Harald Weltedda21ed2017-08-12 15:07:02 +0200953 /* Register File Descriptors */
954 osmo_fd_setup(&ggsn->gtp_fd0, ggsn->gsn->fd0, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 0);
955 rc = osmo_fd_register(&ggsn->gtp_fd0);
956 OSMO_ASSERT(rc == 0);
957
958 osmo_fd_setup(&ggsn->gtp_fd1c, ggsn->gsn->fd1c, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 1);
959 rc = osmo_fd_register(&ggsn->gtp_fd1c);
960 OSMO_ASSERT(rc == 0);
961
962 osmo_fd_setup(&ggsn->gtp_fd1u, ggsn->gsn->fd1u, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 2);
963 rc = osmo_fd_register(&ggsn->gtp_fd1u);
964 OSMO_ASSERT(rc == 0);
965
966 /* Start GTP re-transmission timer */
967 osmo_timer_setup(&ggsn->gtp_timer, ggsn_gtp_tmr_cb, ggsn);
968
969 gtp_set_cb_data_ind(ggsn->gsn, encaps_tun);
970 gtp_set_cb_delete_context(ggsn->gsn, delete_context);
971 gtp_set_cb_create_context_ind(ggsn->gsn, create_context_ind);
972
973 LOGPGGSN(LOGL_NOTICE, ggsn, "Successfully started\n");
974 ggsn->started = true;
975
976 llist_for_each_entry(apn, &ggsn->apn_list, list)
977 apn_start(apn);
978
979 return 0;
980}
981
982/* Stop a given GGSN */
983int ggsn_stop(struct ggsn_ctx *ggsn)
984{
985 struct apn_ctx *apn;
986
987 if (!ggsn->started)
988 return 0;
989
990 /* iterate over all APNs and stop them */
991 llist_for_each_entry(apn, &ggsn->apn_list, list)
992 apn_stop(apn, true);
993
994 osmo_timer_del(&ggsn->gtp_timer);
995
996 osmo_fd_unregister(&ggsn->gtp_fd1u);
997 osmo_fd_unregister(&ggsn->gtp_fd1c);
998 osmo_fd_unregister(&ggsn->gtp_fd0);
999
1000 if (ggsn->gsn) {
1001 gtp_free(ggsn->gsn);
1002 ggsn->gsn = NULL;
1003 }
1004
1005 ggsn->started = false;
1006 return 0;
1007}
1008
1009static void print_usage()
1010{
1011 printf("Usage: osmo-ggsn [-h] [-D] [-c configfile] [-V]\n");
1012}
1013
1014static void print_help()
1015{
1016 printf( " Some useful help...\n"
1017 " -h --help This help text\n"
1018 " -D --daemonize Fork the process into a background daemon\n"
1019 " -c --config-file filename The config file to use\n"
1020 " -V --version Print the version of OsmoGGSN\n"
1021 );
1022}
1023
1024static void handle_options(int argc, char **argv)
1025{
1026 while (1) {
1027 int option_index = 0, c;
1028 static struct option long_options[] = {
1029 { "help", 0, 0, 'h' },
1030 { "daemonize", 0, 0, 'D' },
1031 { "config-file", 1, 0, 'c' },
1032 { "version", 0, 0, 'V' },
1033 { 0, 0, 0, 0 }
1034 };
1035
1036 c = getopt_long(argc, argv, "hdc:V", long_options, &option_index);
1037 if (c == -1)
1038 break;
1039
1040 switch (c) {
1041 case 'h':
1042 print_usage();
1043 print_help();
1044 exit(0);
1045 case 'D':
1046 daemonize = 1;
1047 break;
1048 case 'c':
1049 config_file = optarg;
1050 break;
1051 case 'V':
1052 print_version(1);
1053 exit(0);
1054 break;
1055 }
1056 }
1057}
1058
jjako52c24142002-12-16 13:33:51 +00001059int main(int argc, char **argv)
1060{
Harald Weltedda21ed2017-08-12 15:07:02 +02001061 struct ggsn_ctx *ggsn;
1062 int rc;
jjako52c24142002-12-16 13:33:51 +00001063
Harald Welte632e8432017-09-05 18:12:14 +02001064 tall_ggsn_ctx = talloc_named_const(NULL, 0, "OsmoGGSN");
Harald Weltedda21ed2017-08-12 15:07:02 +02001065 msgb_talloc_ctx_init(tall_ggsn_ctx, 0);
Harald Welte3e443ca2018-02-14 01:04:04 +01001066 g_vty_info.tall_ctx = tall_ggsn_ctx;
jjako52c24142002-12-16 13:33:51 +00001067
Harald Weltee8049472017-08-20 12:44:21 +02001068 /* Handle keyboard interrupt SIGINT */
Harald Weltedda21ed2017-08-12 15:07:02 +02001069 signal(SIGINT, &signal_handler);
Harald Weltee8049472017-08-20 12:44:21 +02001070 signal(SIGTERM, &signal_handler);
Harald Weltedda21ed2017-08-12 15:07:02 +02001071 signal(SIGABRT, &signal_handler);
1072 signal(SIGUSR1, &signal_handler);
1073 signal(SIGUSR2, &signal_handler);
jjako52c24142002-12-16 13:33:51 +00001074
Harald Weltedda21ed2017-08-12 15:07:02 +02001075 osmo_init_ignore_signals();
Pau Espin Pedrol042a4452018-04-17 14:31:42 +02001076 osmo_init_logging2(tall_ggsn_ctx, &log_info);
Harald Weltedda21ed2017-08-12 15:07:02 +02001077 osmo_stats_init(tall_ggsn_ctx);
jjako0141d202004-01-09 15:19:20 +00001078
Harald Weltedda21ed2017-08-12 15:07:02 +02001079 vty_init(&g_vty_info);
1080 logging_vty_add_cmds(NULL);
Harald Welte3e443ca2018-02-14 01:04:04 +01001081 osmo_talloc_vty_add_cmds();
Harald Weltedda21ed2017-08-12 15:07:02 +02001082 osmo_stats_vty_add_cmds(&log_info);
1083 ggsn_vty_init();
1084 ctrl_vty_init(tall_ggsn_ctx);
1085
1086 handle_options(argc, argv);
1087
1088 rate_ctr_init(tall_ggsn_ctx);
1089
1090 rc = vty_read_config_file(config_file, NULL);
1091 if (rc < 0) {
1092 fprintf(stderr, "Failed to open config file: '%s'\n", config_file);
1093 exit(2);
Harald Weltebed35df2011-11-02 13:06:18 +01001094 }
jjako52c24142002-12-16 13:33:51 +00001095
Harald Weltedda21ed2017-08-12 15:07:02 +02001096 rc = telnet_init_dynif(tall_ggsn_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_GGSN);
1097 if (rc < 0)
Harald Weltebed35df2011-11-02 13:06:18 +01001098 exit(1);
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +01001099
Harald Weltedda21ed2017-08-12 15:07:02 +02001100 g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_GGSN, NULL);
1101 if (!g_ctrlh) {
1102 LOGP(DGGSN, LOGL_ERROR, "Failed to create CTRL interface.\n");
Harald Weltebed35df2011-11-02 13:06:18 +01001103 exit(1);
1104 }
jjako88c22162003-07-06 19:33:18 +00001105
Harald Weltedda21ed2017-08-12 15:07:02 +02001106 if (daemonize) {
1107 rc = osmo_daemonize();
1108 if (rc < 0) {
1109 perror("Error during daemonize");
Harald Weltebed35df2011-11-02 13:06:18 +01001110 exit(1);
1111 }
1112 }
jjako1d3db972004-01-16 09:56:56 +00001113
Harald Weltedda21ed2017-08-12 15:07:02 +02001114#if 0
Harald Weltebed35df2011-11-02 13:06:18 +01001115 /* qos */
1116 qos.l = 3;
1117 qos.v[2] = (args_info.qos_arg) & 0xff;
1118 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
1119 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
Harald Weltedda21ed2017-08-12 15:07:02 +02001120#endif
jjakoa7cd2492003-04-11 09:40:12 +00001121
Harald Weltedda21ed2017-08-12 15:07:02 +02001122 /* Main select loop */
1123 while (!end) {
1124 osmo_select_main(0);
Harald Weltebed35df2011-11-02 13:06:18 +01001125 }
jjakoe0149782003-07-06 17:07:04 +00001126
Harald Weltedda21ed2017-08-12 15:07:02 +02001127 llist_for_each_entry(ggsn, &g_ggsn_list, list)
1128 ggsn_stop(ggsn);
Harald Weltebed35df2011-11-02 13:06:18 +01001129
1130 return 1;
jjako52c24142002-12-16 13:33:51 +00001131}