blob: c4e28f88332b51e317632e4cc72249c2730613c5 [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 }
Harald Welte0d0e2422017-11-08 15:49:15 +0900133 gtp_kernel_stop(apn->tun.cfg.dev_name);
Harald Weltedda21ed2017-08-12 15:07:02 +0200134
135 if (apn->v4.pool) {
136 LOGPAPN(LOGL_INFO, apn, "Releasing IPv4 pool\n");
137 ippool_free(apn->v4.pool);
138 apn->v4.pool = NULL;
139 }
140 if (apn->v6.pool) {
141 LOGPAPN(LOGL_INFO, apn, "Releasing IPv6 pool\n");
142 ippool_free(apn->v6.pool);
143 apn->v6.pool = NULL;
144 }
145
146 apn->started = false;
147 return 0;
148}
149
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200150
Harald Weltef55a0392017-11-08 14:33:55 +0900151static int alloc_ippool_blacklist(struct apn_ctx *apn, struct in46_prefix **blacklist, bool ipv6)
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200152{
153
154 int flags, len, len2, i;
155
Harald Weltee2a1de52017-11-08 15:24:07 +0900156 *blacklist = NULL;
157
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200158 if (ipv6)
159 flags = IP_TYPE_IPv6_NONLINK;
160 else
161 flags = IP_TYPE_IPv4;
162
163 while (1) {
Harald Weltee2a1de52017-11-08 15:24:07 +0900164 len = netdev_ip_local_get(apn->tun.cfg.dev_name, NULL, 0, flags);
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200165 if (len < 1)
166 return len;
167
168 *blacklist = talloc_zero_size(apn, len * sizeof(struct in46_prefix));
Harald Weltee2a1de52017-11-08 15:24:07 +0900169 len2 = netdev_ip_local_get(apn->tun.cfg.dev_name, *blacklist, len, flags);
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200170 if (len2 < 1) {
171 talloc_free(*blacklist);
Harald Weltee2a1de52017-11-08 15:24:07 +0900172 *blacklist = NULL;
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200173 return len2;
174 }
175
Harald Weltee2a1de52017-11-08 15:24:07 +0900176 if (len2 > len) { /* iface was added between 2 calls, repeat operation */
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200177 talloc_free(*blacklist);
Harald Weltee2a1de52017-11-08 15:24:07 +0900178 *blacklist = NULL;
179 } else
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200180 break;
181 }
182
183 for (i = 0; i < len2; i++)
184 LOGPAPN(LOGL_INFO, apn, "Blacklist tun IP %s\n",
185 in46p_ntoa(&(*blacklist)[i]));
186
187 return len2;
188}
189
Harald Weltedda21ed2017-08-12 15:07:02 +0200190/* actually start the APN with its current config */
191int apn_start(struct apn_ctx *apn)
192{
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200193 int ippool_flags = IPPOOL_NONETWORK | IPPOOL_NOBROADCAST;
Pau Espin Pedrola037e592017-10-16 14:41:37 +0200194 struct in46_prefix ipv6_tun_linklocal_ip;
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200195 struct in46_prefix *blacklist;
196 int blacklist_size;
Pau Espin Pedrolbffc3f92017-12-14 11:19:10 +0100197 int rc;
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200198
Harald Weltedda21ed2017-08-12 15:07:02 +0200199 if (apn->started)
200 return 0;
201
202 LOGPAPN(LOGL_INFO, apn, "Starting\n");
203 switch (apn->cfg.gtpu_mode) {
204 case APN_GTPU_MODE_TUN:
205 LOGPAPN(LOGL_INFO, apn, "Opening TUN device %s\n", apn->tun.cfg.dev_name);
206 if (tun_new(&apn->tun.tun, apn->tun.cfg.dev_name)) {
207 LOGPAPN(LOGL_ERROR, apn, "Failed to configure tun device\n");
208 return -1;
209 }
210 LOGPAPN(LOGL_INFO, apn, "Opened TUN device %s\n", apn->tun.tun->devname);
211
212 /* Register with libosmcoore */
213 osmo_fd_setup(&apn->tun.fd, apn->tun.tun->fd, BSC_FD_READ, ggsn_tun_fd_cb, apn, 0);
214 osmo_fd_register(&apn->tun.fd);
215
216 /* Set TUN library callback */
217 tun_set_cb_ind(apn->tun.tun, cb_tun_ind);
218
219 if (apn->v4.cfg.ifconfig_prefix.addr.len) {
220 LOGPAPN(LOGL_INFO, apn, "Setting tun IP address %s\n",
221 in46p_ntoa(&apn->v4.cfg.ifconfig_prefix));
222 if (tun_setaddr(apn->tun.tun, &apn->v4.cfg.ifconfig_prefix.addr, NULL,
223 apn->v4.cfg.ifconfig_prefix.prefixlen)) {
224 LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv4 address %s: %s\n",
225 in46p_ntoa(&apn->v4.cfg.ifconfig_prefix), strerror(errno));
226 apn_stop(apn, false);
227 return -1;
228 }
229 }
230
231 if (apn->v6.cfg.ifconfig_prefix.addr.len) {
232 LOGPAPN(LOGL_INFO, apn, "Setting tun IPv6 address %s\n",
233 in46p_ntoa(&apn->v6.cfg.ifconfig_prefix));
234 if (tun_setaddr(apn->tun.tun, &apn->v6.cfg.ifconfig_prefix.addr, NULL,
235 apn->v6.cfg.ifconfig_prefix.prefixlen)) {
Harald Welteed1ba2c2017-10-01 18:16:17 +0800236 LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv6 address %s: %s. "
237 "Ensure you have ipv6 support and not used the disable_ipv6 sysctl?\n",
Harald Weltedda21ed2017-08-12 15:07:02 +0200238 in46p_ntoa(&apn->v6.cfg.ifconfig_prefix), strerror(errno));
239 apn_stop(apn, false);
240 return -1;
241 }
242 }
243
Pau Espin Pedrol37c45e32017-12-14 14:09:13 +0100244 if (apn->v6.cfg.ll_prefix.addr.len) {
245 LOGPAPN(LOGL_INFO, apn, "Setting tun IPv6 link-local address %s\n",
246 in46p_ntoa(&apn->v6.cfg.ll_prefix));
247 if (tun_addaddr(apn->tun.tun, &apn->v6.cfg.ll_prefix.addr, NULL,
248 apn->v6.cfg.ll_prefix.prefixlen)) {
249 LOGPAPN(LOGL_ERROR, apn, "Failed to set tun IPv6 link-local address %s: %s. "
250 "Ensure you have ipv6 support and not used the disable_ipv6 sysctl?\n",
251 in46p_ntoa(&apn->v6.cfg.ll_prefix), strerror(errno));
252 apn_stop(apn, false);
253 return -1;
254 }
255 apn->v6_lladdr = apn->v6.cfg.ll_prefix.addr.v6;
256 }
257
Harald Weltedda21ed2017-08-12 15:07:02 +0200258 if (apn->tun.cfg.ipup_script) {
259 LOGPAPN(LOGL_INFO, apn, "Running ip-up script %s\n",
260 apn->tun.cfg.ipup_script);
261 tun_runscript(apn->tun.tun, apn->tun.cfg.ipup_script);
262 }
Harald Weltef85fe972017-09-24 20:00:34 +0800263
Pau Espin Pedrol37c45e32017-12-14 14:09:13 +0100264 if (apn->cfg.apn_type_mask & (APN_TYPE_IPv6|APN_TYPE_IPv4v6) &&
265 apn->v6.cfg.ll_prefix.addr.len == 0) {
Pau Espin Pedrolbffc3f92017-12-14 11:19:10 +0100266 rc = tun_ip_local_get(apn->tun.tun, &ipv6_tun_linklocal_ip, 1, IP_TYPE_IPv6_LINK);
267 if (rc < 1) {
268 LOGPAPN(LOGL_ERROR, apn, "Cannot obtain IPv6 link-local address of interface: %s\n",
269 rc ? strerror(errno) : "tun interface has no link-local IP assigned");
Harald Weltef85fe972017-09-24 20:00:34 +0800270 apn_stop(apn, false);
271 return -1;
272 }
Pau Espin Pedrola037e592017-10-16 14:41:37 +0200273 apn->v6_lladdr = ipv6_tun_linklocal_ip.addr.v6;
Harald Weltef85fe972017-09-24 20:00:34 +0800274 }
275
Harald Weltedda21ed2017-08-12 15:07:02 +0200276 /* set back-pointer from TUN device to APN */
277 apn->tun.tun->priv = apn;
278 break;
279 case APN_GTPU_MODE_KERNEL_GTP:
Harald Welte2fc2bc62017-11-08 15:50:53 +0900280 LOGPAPN(LOGL_INFO, apn, "Opening Kernel GTP device %s\n", apn->tun.cfg.dev_name);
Harald Welte490782d2017-11-08 14:09:51 +0900281 if (apn->cfg.apn_type_mask & (APN_TYPE_IPv6|APN_TYPE_IPv4v6)) {
282 LOGPAPN(LOGL_ERROR, apn, "Kernel GTP currently supports only IPv4\n");
283 apn_stop(apn, false);
284 return -1;
285 }
Harald Weltedda21ed2017-08-12 15:07:02 +0200286 /* use GTP kernel module for data packet encapsulation */
Harald Welte698a2332017-11-08 15:09:58 +0900287 if (gtp_kernel_init(apn->ggsn->gsn, apn->tun.cfg.dev_name,
288 &apn->v4.cfg.ifconfig_prefix, apn->tun.cfg.ipup_script) < 0) {
Harald Welte490782d2017-11-08 14:09:51 +0900289 return -1;
290 }
Harald Weltebed35df2011-11-02 13:06:18 +0100291 break;
292 default:
Harald Weltedda21ed2017-08-12 15:07:02 +0200293 LOGPAPN(LOGL_ERROR, apn, "Unknown GTPU Mode %d\n", apn->cfg.gtpu_mode);
294 return -1;
Harald Weltebed35df2011-11-02 13:06:18 +0100295 }
jjako0141d202004-01-09 15:19:20 +0000296
Harald Weltedda21ed2017-08-12 15:07:02 +0200297 /* Create IPv4 pool */
298 if (apn->v4.cfg.dynamic_prefix.addr.len) {
299 LOGPAPN(LOGL_INFO, apn, "Creating IPv4 pool %s\n",
300 in46p_ntoa(&apn->v4.cfg.dynamic_prefix));
Harald Weltef55a0392017-11-08 14:33:55 +0900301 if ((blacklist_size = alloc_ippool_blacklist(apn, &blacklist, false)) < 0)
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200302 LOGPAPN(LOGL_ERROR, apn, "Failed obtaining IPv4 tun IPs\n");
Harald Weltedda21ed2017-08-12 15:07:02 +0200303 if (ippool_new(&apn->v4.pool, &apn->v4.cfg.dynamic_prefix,
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200304 &apn->v4.cfg.static_prefix, ippool_flags,
305 blacklist, blacklist_size)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200306 LOGPAPN(LOGL_ERROR, apn, "Failed to create IPv4 pool\n");
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200307 talloc_free(blacklist);
Harald Weltedda21ed2017-08-12 15:07:02 +0200308 apn_stop(apn, false);
309 return -1;
310 }
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200311 talloc_free(blacklist);
Harald Weltebed35df2011-11-02 13:06:18 +0100312 }
Harald Weltedda21ed2017-08-12 15:07:02 +0200313
314 /* Create IPv6 pool */
315 if (apn->v6.cfg.dynamic_prefix.addr.len) {
316 LOGPAPN(LOGL_INFO, apn, "Creating IPv6 pool %s\n",
317 in46p_ntoa(&apn->v6.cfg.dynamic_prefix));
Harald Weltef55a0392017-11-08 14:33:55 +0900318 if ((blacklist_size = alloc_ippool_blacklist(apn, &blacklist, true)) < 0)
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200319 LOGPAPN(LOGL_ERROR, apn, "Failed obtaining IPv6 tun IPs\n");
Harald Weltedda21ed2017-08-12 15:07:02 +0200320 if (ippool_new(&apn->v6.pool, &apn->v6.cfg.dynamic_prefix,
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200321 &apn->v6.cfg.static_prefix, ippool_flags,
322 blacklist, blacklist_size)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200323 LOGPAPN(LOGL_ERROR, apn, "Failed to create IPv6 pool\n");
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200324 talloc_free(blacklist);
Harald Weltedda21ed2017-08-12 15:07:02 +0200325 apn_stop(apn, false);
326 return -1;
327 }
Pau Espin Pedrol859f9b02017-10-16 14:52:25 +0200328 talloc_free(blacklist);
Harald Weltedda21ed2017-08-12 15:07:02 +0200329 }
330
331 LOGPAPN(LOGL_NOTICE, apn, "Successfully started\n");
332 apn->started = true;
333 return 0;
jjako0141d202004-01-09 15:19:20 +0000334}
jjako0141d202004-01-09 15:19:20 +0000335
Max3142d8d2017-05-04 17:45:10 +0200336static bool send_trap(const struct gsn_t *gsn, const struct pdp_t *pdp, const struct ippoolm_t *member, const char *var)
337{
Harald Welted12eab92017-08-02 19:49:47 +0200338 char addrbuf[256];
Max3142d8d2017-05-04 17:45:10 +0200339 char val[NAMESIZE];
340
Harald Welted12eab92017-08-02 19:49:47 +0200341 const char *addrstr = in46a_ntop(&member->addr, addrbuf, sizeof(addrbuf));
342
Harald Welteb10ee082017-08-12 19:29:16 +0200343 snprintf(val, sizeof(val), "%s,%s", imsi_gtp2str(&pdp->imsi), addrstr);
Max3142d8d2017-05-04 17:45:10 +0200344
Harald Weltedda21ed2017-08-12 15:07:02 +0200345 if (ctrl_cmd_send_trap(g_ctrlh, var, val) < 0) {
346 LOGPPDP(LOGL_ERROR, pdp, "Failed to create and send TRAP %s\n", var);
Max3142d8d2017-05-04 17:45:10 +0200347 return false;
348 }
349 return true;
350}
351
Harald Weltedda21ed2017-08-12 15:07:02 +0200352static int delete_context(struct pdp_t *pdp)
Harald Weltebed35df2011-11-02 13:06:18 +0100353{
Harald Weltedda21ed2017-08-12 15:07:02 +0200354 struct gsn_t *gsn = pdp->gsn;
Harald Welte698a2332017-11-08 15:09:58 +0900355 struct apn_ctx *apn = pdp->priv;
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100356 struct ippoolm_t *member;
357 int i;
Harald Weltedda21ed2017-08-12 15:07:02 +0200358
359 LOGPPDP(LOGL_INFO, pdp, "Deleting PDP context\n");
Maxdbd70242016-10-14 13:38:05 +0200360
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100361 for (i = 0; i < 2; i++) {
362 if (pdp->peer[i]) {
363 member = pdp->peer[i];
364 send_trap(gsn, pdp, member, "imsi-rem-ip"); /* TRAP with IP removal */
365 ippool_freeip(member->pool, member);
366 } else if(i == 0)
367 LOGPPDP(LOGL_ERROR, pdp, "Cannot find/free IP Pool member\n");
368 }
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100369
Harald Welte698a2332017-11-08 15:09:58 +0900370 if (gtp_kernel_tunnel_del(pdp, apn->tun.cfg.dev_name)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200371 LOGPPDP(LOGL_ERROR, pdp, "Cannot delete tunnel from kernel:%s\n",
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100372 strerror(errno));
373 }
374
Harald Weltebed35df2011-11-02 13:06:18 +0100375 return 0;
jjako52c24142002-12-16 13:33:51 +0000376}
377
Harald Welte1ae98772017-08-09 20:28:52 +0200378#include <osmocom/gsm/tlv.h>
379
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100380/* RFC 1332 */
381enum ipcp_options {
382 IPCP_OPT_IPADDR = 3,
383 IPCP_OPT_PRIMARY_DNS = 129,
384 IPCP_OPT_SECONDARY_DNS = 131,
385};
386
387struct ipcp_option_hdr {
388 uint8_t type;
389 uint8_t len;
390 uint8_t data[0];
391};
392
393struct ipcp_hdr {
394 uint8_t code;
395 uint8_t id;
396 uint16_t len;
397 uint8_t options[0];
398};
399
400/* determine if IPCP contains given option */
401static struct ipcp_option_hdr *ipcp_contains_option(struct ipcp_hdr *ipcp, enum ipcp_options opt)
402{
403 uint8_t *cur = ipcp->options;
404
405 /* iterate over Options and check if protocol contained */
406 while (cur + 2 <= ((uint8_t *)ipcp) + ipcp->len) {
407 struct ipcp_option_hdr *cur_opt = (struct ipcp_option_hdr *) cur;
408 if (cur_opt->type == opt)
409 return cur_opt;
410 cur += cur_opt->len;
411 }
412 return NULL;
413}
414
Harald Welte1ae98772017-08-09 20:28:52 +0200415/* 3GPP TS 24.008 10.6.5.3 */
416enum pco_protocols {
417 PCO_P_LCP = 0xC021,
418 PCO_P_PAP = 0xC023,
419 PCO_P_CHAP = 0xC223,
420 PCO_P_IPCP = 0x8021,
421 PCO_P_PCSCF_ADDR = 0x0001,
422 PCO_P_IM_CN_SS_F = 0x0002,
423 PCO_P_DNS_IPv6_ADDR = 0x0003,
424 PCO_P_POLICY_CTRL_REJ = 0x0004, /* only in Network->MS */
425 PCO_P_MS_SUP_NETREQ_BCI = 0x0005,
426 /* reserved */
427 PCO_P_DSMIPv6_HA_ADDR = 0x0007,
428 PCO_P_DSMIPv6_HN_PREF = 0x0008,
429 PCO_P_DSMIPv6_v4_HA_ADDR= 0x0009,
430 PCO_P_IP_ADDR_VIA_NAS = 0x000a, /* only MS->Network */
431 PCO_P_IPv4_ADDR_VIA_DHCP= 0x000b, /* only MS->Netowrk */
432 PCO_P_PCSCF_IPv4_ADDR = 0x000c,
433 PCO_P_DNS_IPv4_ADDR = 0x000d,
434 PCO_P_MSISDN = 0x000e,
435 PCO_P_IFOM_SUPPORT = 0x000f,
436 PCO_P_IPv4_LINK_MTU = 0x0010,
437 PCO_P_MS_SUPP_LOC_A_TFT = 0x0011,
438 PCO_P_PCSCF_RESEL_SUP = 0x0012, /* only MS->Network */
439 PCO_P_NBIFOM_REQ = 0x0013,
440 PCO_P_NBIFOM_MODE = 0x0014,
441 PCO_P_NONIP_LINK_MTU = 0x0015,
442 PCO_P_APN_RATE_CTRL_SUP = 0x0016,
443 PCO_P_PS_DATA_OFF_UE = 0x0017,
444 PCO_P_REL_DATA_SVC = 0x0018,
445};
446
447/* determine if PCO contains given protocol */
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100448static uint8_t *pco_contains_proto(struct ul255_t *pco, uint16_t prot)
Harald Welte1ae98772017-08-09 20:28:52 +0200449{
450 uint8_t *cur = pco->v + 1;
451
452 /* iterate over PCO and check if protocol contained */
Pau Espin Pedrol0ab62fe2017-08-30 15:51:24 +0200453 while (cur + 3 <= pco->v + pco->l) {
Harald Welte1ae98772017-08-09 20:28:52 +0200454 uint16_t cur_prot = osmo_load16be(cur);
455 uint8_t cur_len = cur[2];
456 if (cur_prot == prot)
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100457 return cur;
Pau Espin Pedrol0ab62fe2017-08-30 15:51:24 +0200458 cur += cur_len + 3;
Harald Welte1ae98772017-08-09 20:28:52 +0200459 }
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100460 return NULL;
Harald Welte1ae98772017-08-09 20:28:52 +0200461}
462
Pau Espin Pedrol7d54ed42018-01-25 20:09:16 +0100463/*! Get the peer of pdp based on IP version used.
464 * \param[in] pdp PDP context to select the peer from.
465 * \param[in] v4v6 IP version to select. Valid values are 4 and 6.
466 * \returns The selected peer matching the given IP version. NULL if not present.
467 */
468static struct ippoolm_t *pdp_get_peer_ipv(struct pdp_t *pdp, bool is_ipv6) {
469 uint8_t len1, len2, i;
470
471 if (is_ipv6) {
472 len1 = 8;
473 len2 = 16;
474 } else {
475 len1 = sizeof(struct in_addr);
476 len2 = len1;
477 }
478
479 for (i = 0; i < 2; i++) {
480 struct ippoolm_t * ippool = pdp->peer[i];
481 if (ippool && (ippool->addr.len == len1 || ippool->addr.len == len2))
482 return ippool;
483 }
484 return NULL;
485}
486
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100487/* construct an IPCP PCO response from request*/
488static int build_ipcp_pco(struct apn_ctx *apn, struct pdp_t *pdp, struct msgb *msg)
Harald Weltedda21ed2017-08-12 15:07:02 +0200489{
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100490 const struct in46_addr *dns1 = &apn->v4.cfg.dns[0];
491 const struct in46_addr *dns2 = &apn->v4.cfg.dns[1];
492 struct ipcp_hdr *ipcp;
493 uint8_t *len1, *len2, *pco_ipcp;
Harald Weltedda21ed2017-08-12 15:07:02 +0200494 uint8_t *start = msg->tail;
495 unsigned int len_appended;
496
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100497 if (!(pco_ipcp = pco_contains_proto(&pdp->pco_req, PCO_P_IPCP)))
498 return 0;
499 ipcp = (struct ipcp_hdr*) (pco_ipcp + 3); /* 2=type + 1=len */
500
Harald Weltedda21ed2017-08-12 15:07:02 +0200501 /* Three byte T16L header */
502 msgb_put_u16(msg, 0x8021); /* IPCP */
503 len1 = msgb_put(msg, 1); /* Length of contents: delay */
504
505 msgb_put_u8(msg, 0x02); /* ACK */
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100506 msgb_put_u8(msg, ipcp->id); /* ID: Needs to match request */
Harald Weltedda21ed2017-08-12 15:07:02 +0200507 msgb_put_u8(msg, 0x00); /* Length MSB */
508 len2 = msgb_put(msg, 1); /* Length LSB: delay */
509
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100510 if (dns1->len == 4 && ipcp_contains_option(ipcp, IPCP_OPT_PRIMARY_DNS)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200511 msgb_put_u8(msg, 0x81); /* DNS1 Tag */
512 msgb_put_u8(msg, 2 + dns1->len);/* DNS1 Length, incl. TL */
Harald Weltebcab7fb2017-12-03 21:43:50 +0100513 msgb_put_u32(msg, ntohl(dns1->v4.s_addr));
Harald Weltedda21ed2017-08-12 15:07:02 +0200514 }
515
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100516 if (dns2->len == 4 && ipcp_contains_option(ipcp, IPCP_OPT_SECONDARY_DNS)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200517 msgb_put_u8(msg, 0x83); /* DNS2 Tag */
518 msgb_put_u8(msg, 2 + dns2->len);/* DNS2 Length, incl. TL */
Harald Weltebcab7fb2017-12-03 21:43:50 +0100519 msgb_put_u32(msg, ntohl(dns2->v4.s_addr));
Harald Weltedda21ed2017-08-12 15:07:02 +0200520 }
521
522 /* patch in length values */
523 len_appended = msg->tail - start;
524 *len1 = len_appended - 3;
525 *len2 = len_appended - 3;
526
527 return 0;
528}
529
Harald Welte1ae98772017-08-09 20:28:52 +0200530/* process one PCO request from a MS/UE, putting together the proper responses */
Harald Weltedda21ed2017-08-12 15:07:02 +0200531static void process_pco(struct apn_ctx *apn, struct pdp_t *pdp)
Harald Welte1ae98772017-08-09 20:28:52 +0200532{
533 struct msgb *msg = msgb_alloc(256, "PCO");
Pau Espin Pedrol4ae8d822018-01-26 17:51:55 +0100534 struct ippoolm_t *peer_v4 = pdp_get_peer_ipv(pdp, false);
Harald Weltedda21ed2017-08-12 15:07:02 +0200535 unsigned int i;
536
537 OSMO_ASSERT(msg);
Harald Welte1ae98772017-08-09 20:28:52 +0200538 msgb_put_u8(msg, 0x80); /* ext-bit + configuration protocol byte */
539
Pau Espin Pedrol4ae8d822018-01-26 17:51:55 +0100540 if (peer_v4)
Pau Espin Pedrol0bdd8bf2018-01-26 17:46:37 +0100541 build_ipcp_pco(apn, pdp, msg);
Harald Welte1ae98772017-08-09 20:28:52 +0200542
543 if (pco_contains_proto(&pdp->pco_req, PCO_P_DNS_IPv6_ADDR)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200544 for (i = 0; i < ARRAY_SIZE(apn->v6.cfg.dns); i++) {
545 struct in46_addr *i46a = &apn->v6.cfg.dns[i];
546 if (i46a->len != 16)
547 continue;
548 msgb_t16lv_put(msg, PCO_P_DNS_IPv6_ADDR, i46a->len, i46a->v6.s6_addr);
549 }
Harald Welte1ae98772017-08-09 20:28:52 +0200550 }
551
552 if (pco_contains_proto(&pdp->pco_req, PCO_P_DNS_IPv4_ADDR)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200553 for (i = 0; i < ARRAY_SIZE(apn->v4.cfg.dns); i++) {
554 struct in46_addr *i46a = &apn->v4.cfg.dns[i];
555 if (i46a->len != 4)
556 continue;
557 msgb_t16lv_put(msg, PCO_P_DNS_IPv4_ADDR, i46a->len, (uint8_t *)&i46a->v4);
558 }
Harald Welte1ae98772017-08-09 20:28:52 +0200559 }
560
561 if (msgb_length(msg) > 1) {
562 memcpy(pdp->pco_neg.v, msgb_data(msg), msgb_length(msg));
563 pdp->pco_neg.l = msgb_length(msg);
564 } else
565 pdp->pco_neg.l = 0;
566
567 msgb_free(msg);
568}
569
Harald Welte9d9d91b2017-10-14 16:22:16 +0200570static bool apn_supports_ipv4(const struct apn_ctx *apn)
571{
572 if (apn->v4.cfg.static_prefix.addr.len || apn->v4.cfg.dynamic_prefix.addr.len)
573 return true;
574 return false;
575}
576
577static bool apn_supports_ipv6(const struct apn_ctx *apn)
578{
579 if (apn->v6.cfg.static_prefix.addr.len || apn->v6.cfg.dynamic_prefix.addr.len)
580 return true;
581 return false;
582}
583
Harald Weltebed35df2011-11-02 13:06:18 +0100584int create_context_ind(struct pdp_t *pdp)
585{
Harald Weltedda21ed2017-08-12 15:07:02 +0200586 static char name_buf[256];
587 struct gsn_t *gsn = pdp->gsn;
588 struct ggsn_ctx *ggsn = gsn->priv;
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100589 struct in46_addr addr[2];
590 struct ippoolm_t *member = NULL;
Harald Weltedda21ed2017-08-12 15:07:02 +0200591 struct apn_ctx *apn;
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100592 int rc, num_addr, i;
jjako52c24142002-12-16 13:33:51 +0000593
Harald Weltedda21ed2017-08-12 15:07:02 +0200594 osmo_apn_to_str(name_buf, pdp->apn_req.v, pdp->apn_req.l);
595
596 LOGPPDP(LOGL_DEBUG, pdp, "Processing create PDP context request for APN '%s'\n", name_buf);
597
598 /* First find an exact APN name match */
599 apn = ggsn_find_apn(ggsn, name_buf);
Harald Welte2e84d2c2017-10-01 13:36:52 +0800600 /* ignore if the APN has not been started */
Pau Espin Pedrol958256f2017-10-11 20:32:55 +0200601 if (apn && !apn->started)
Harald Welte2e84d2c2017-10-01 13:36:52 +0800602 apn = NULL;
Harald Welteb16c46b2017-10-01 18:28:18 +0800603
Harald Weltedda21ed2017-08-12 15:07:02 +0200604 /* then try default (if any) */
605 if (!apn)
606 apn = ggsn->cfg.default_apn;
Harald Welteb16c46b2017-10-01 18:28:18 +0800607 /* ignore if the APN has not been started */
Pau Espin Pedrol958256f2017-10-11 20:32:55 +0200608 if (apn && !apn->started)
Harald Welteb16c46b2017-10-01 18:28:18 +0800609 apn = NULL;
610
Harald Weltedda21ed2017-08-12 15:07:02 +0200611 if (!apn) {
612 /* no APN found for what user requested */
613 LOGPPDP(LOGL_NOTICE, pdp, "Unknown APN '%s', rejecting\n", name_buf);
614 gtp_create_context_resp(gsn, pdp, GTPCAUSE_MISSING_APN);
615 return 0;
616 }
jjako52c24142002-12-16 13:33:51 +0000617
Harald Welted9d88622017-08-04 00:22:35 +0200618 /* FIXME: we manually force all context requests to dynamic here! */
619 if (pdp->eua.l > 2)
620 pdp->eua.l = 2;
jjako52c24142002-12-16 13:33:51 +0000621
Harald Weltebed35df2011-11-02 13:06:18 +0100622 memcpy(pdp->qos_neg0, pdp->qos_req0, sizeof(pdp->qos_req0));
jjako52c24142002-12-16 13:33:51 +0000623
Harald Weltebed35df2011-11-02 13:06:18 +0100624 memcpy(pdp->qos_neg.v, pdp->qos_req.v, pdp->qos_req.l); /* TODO */
625 pdp->qos_neg.l = pdp->qos_req.l;
jjako52c24142002-12-16 13:33:51 +0000626
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100627 memset(addr, 0, sizeof(addr));
628 if ((num_addr = in46a_from_eua(&pdp->eua, addr)) < 0) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200629 LOGPPDP(LOGL_ERROR, pdp, "Cannot decode EUA from MS/SGSN: %s\n",
Harald Welted1bf1e12017-08-03 00:00:23 +0200630 osmo_hexdump(pdp->eua.v, pdp->eua.l));
631 gtp_create_context_resp(gsn, pdp, GTPCAUSE_UNKNOWN_PDP);
632 return 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100633 }
jjakoa7cd2492003-04-11 09:40:12 +0000634
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100635 /* Allocate dynamic addresses from the pool */
636 for (i = 0; i < num_addr; i++) {
637 if (addr[i].len == sizeof(struct in_addr)) {
638 /* does this APN actually have an IPv4 pool? */
639 if (!apn_supports_ipv4(apn))
640 goto err_wrong_af;
Harald Welte9d9d91b2017-10-14 16:22:16 +0200641
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100642 rc = ippool_newip(apn->v4.pool, &member, &addr[i], 0);
643 if (rc < 0)
644 goto err_pool_full;
645 /* copy back */
646 memcpy(&addr[i].v4.s_addr, &member->addr.v4, 4);
jjakoa7cd2492003-04-11 09:40:12 +0000647
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100648 } else if (addr[i].len == sizeof(struct in6_addr)) {
649
650 /* does this APN actually have an IPv6 pool? */
651 if (!apn_supports_ipv6(apn))
652 goto err_wrong_af;
653
654 rc = ippool_newip(apn->v6.pool, &member, &addr[i], 0);
655 if (rc < 0)
656 goto err_pool_full;
657
658 /* IPv6 doesn't really send the real/allocated address at this point, but just
659 * the link-identifier which the MS shall use for router solicitation */
660 /* initialize upper 64 bits to prefix, they are discarded by MS anyway */
661 memcpy(addr[i].v6.s6_addr, &member->addr.v6, 8);
662 /* use allocated 64bit prefix as lower 64bit, used as link id by MS */
663 memcpy(addr[i].v6.s6_addr+8, &member->addr.v6, 8);
664 } else
665 OSMO_ASSERT(0);
666
667 pdp->peer[i] = member;
668 member->peer = pdp;
669 }
670
671 in46a_to_eua(addr, num_addr, &pdp->eua);
672
673 if (apn_supports_ipv4(apn)) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200674 /* TODO: In IPv6, EUA doesn't contain the actual IP addr/prefix! */
Harald Welte698a2332017-11-08 15:09:58 +0900675 if (gtp_kernel_tunnel_add(pdp, apn->tun.cfg.dev_name) < 0) {
Harald Weltedda21ed2017-08-12 15:07:02 +0200676 LOGPPDP(LOGL_ERROR, pdp, "Cannot add tunnel to kernel: %s\n", strerror(errno));
677 gtp_create_context_resp(gsn, pdp, GTPCAUSE_SYS_FAIL);
678 return 0;
679 }
Pau Espin Pedrol2d6a69e2017-12-06 19:26:25 +0100680 }
Harald Welte9d9d91b2017-10-14 16:22:16 +0200681
Harald Weltedda21ed2017-08-12 15:07:02 +0200682 pdp->ipif = apn->tun.tun; /* TODO */
Harald Welte698a2332017-11-08 15:09:58 +0900683 pdp->priv = apn;
Max3142d8d2017-05-04 17:45:10 +0200684
685 if (!send_trap(gsn, pdp, member, "imsi-ass-ip")) { /* TRAP with IP assignment */
Max727417d2016-08-02 17:10:38 +0200686 gtp_create_context_resp(gsn, pdp, GTPCAUSE_NO_RESOURCES);
687 return 0;
688 }
Pablo Neira Ayuso4b075b62015-11-17 12:22:42 +0100689
Harald Weltedda21ed2017-08-12 15:07:02 +0200690 process_pco(apn, pdp);
Harald Welte1ae98772017-08-09 20:28:52 +0200691
Harald Welte93fed3b2017-09-24 11:43:17 +0800692 /* Transmit G-PDU sequence numbers (only) if configured in APN */
693 pdp->tx_gpdu_seq = apn->cfg.tx_gpdu_seq;
694
Harald Weltedda21ed2017-08-12 15:07:02 +0200695 LOGPPDP(LOGL_INFO, pdp, "Successful PDP Context Creation: APN=%s(%s), TEIC=%u, IP=%s\n",
696 name_buf, apn->cfg.name, pdp->teic_own, in46a_ntoa(&member->addr));
Harald Weltebed35df2011-11-02 13:06:18 +0100697 gtp_create_context_resp(gsn, pdp, GTPCAUSE_ACC_REQ);
698 return 0; /* Success */
Harald Weltedda21ed2017-08-12 15:07:02 +0200699
700err_pool_full:
701 LOGPPDP(LOGL_ERROR, pdp, "Cannot allocate IP address from pool (full!)\n");
702 gtp_create_context_resp(gsn, pdp, -rc);
703 return 0; /* Already in use, or no more available */
Harald Welte9d9d91b2017-10-14 16:22:16 +0200704
705err_wrong_af:
706 LOGPPDP(LOGL_ERROR, pdp, "APN doesn't support requested EUA / AF type\n");
707 gtp_create_context_resp(gsn, pdp, GTPCAUSE_UNKNOWN_PDP);
708 return 0;
jjako52c24142002-12-16 13:33:51 +0000709}
710
Harald Weltedda21ed2017-08-12 15:07:02 +0200711/* Internet-originated IP packet, needs to be sent via GTP towards MS */
712static int cb_tun_ind(struct tun_t *tun, void *pack, unsigned len)
Harald Weltebed35df2011-11-02 13:06:18 +0100713{
Harald Weltedda21ed2017-08-12 15:07:02 +0200714 struct apn_ctx *apn = tun->priv;
Harald Weltebed35df2011-11-02 13:06:18 +0100715 struct ippoolm_t *ipm;
Harald Welted12eab92017-08-02 19:49:47 +0200716 struct in46_addr dst;
Harald Welte63ebccd2017-08-02 21:10:09 +0200717 struct iphdr *iph = (struct iphdr *)pack;
Harald Weltea0d281d2017-08-02 21:48:16 +0200718 struct ip6_hdr *ip6h = (struct ip6_hdr *)pack;
Harald Weltedda21ed2017-08-12 15:07:02 +0200719 struct ippool_t *pool;
jjakoc6762cf2004-04-28 14:52:58 +0000720
Harald Welte63ebccd2017-08-02 21:10:09 +0200721 if (iph->version == 4) {
Harald Welted12eab92017-08-02 19:49:47 +0200722 if (len < sizeof(*iph) || len < 4*iph->ihl)
723 return -1;
724 dst.len = 4;
Harald Welte63ebccd2017-08-02 21:10:09 +0200725 dst.v4.s_addr = iph->daddr;
Harald Weltedda21ed2017-08-12 15:07:02 +0200726 pool = apn->v4.pool;
Harald Weltea0d281d2017-08-02 21:48:16 +0200727 } else if (iph->version == 6) {
Harald Welted4d6e092017-08-08 18:10:43 +0200728 /* Due to the fact that 3GPP requires an allocation of a
729 * /64 prefix to each MS, we must instruct
730 * ippool_getip() below to match only the leading /64
731 * prefix, i.e. the first 8 bytes of the address */
732 dst.len = 8;
Harald Weltea0d281d2017-08-02 21:48:16 +0200733 dst.v6 = ip6h->ip6_dst;
Harald Weltedda21ed2017-08-12 15:07:02 +0200734 pool = apn->v6.pool;
Harald Welted12eab92017-08-02 19:49:47 +0200735 } else {
Pau Espin Pedrol55d639f2017-12-04 13:17:07 +0100736 LOGP(DTUN, LOGL_NOTICE, "non-IPv%u packet received from tun\n", iph->version);
Harald Welted12eab92017-08-02 19:49:47 +0200737 return -1;
738 }
jjakoc6762cf2004-04-28 14:52:58 +0000739
Harald Weltedda21ed2017-08-12 15:07:02 +0200740 /* IPv6 packet but no IPv6 pool, or IPv4 packet with no IPv4 pool */
741 if (!pool)
742 return 0;
Harald Weltebed35df2011-11-02 13:06:18 +0100743
Max427699e2017-12-05 16:30:37 +0100744 DEBUGP(DTUN, "Received packet for APN(%s) from tun %s", apn->cfg.name, tun->devname);
Harald Weltedda21ed2017-08-12 15:07:02 +0200745
746 if (ippool_getip(pool, &ipm, &dst)) {
Max427699e2017-12-05 16:30:37 +0100747 DEBUGPC(DTUN, " with no PDP contex!!\n");
Harald Weltebed35df2011-11-02 13:06:18 +0100748 return 0;
749 }
Max427699e2017-12-05 16:30:37 +0100750 DEBUGPC(DTUN, "\n");
Harald Weltebed35df2011-11-02 13:06:18 +0100751
752 if (ipm->peer) /* Check if a peer protocol is defined */
Harald Weltedda21ed2017-08-12 15:07:02 +0200753 gtp_data_req(apn->ggsn->gsn, (struct pdp_t *)ipm->peer, pack, len);
Harald Weltebed35df2011-11-02 13:06:18 +0100754 return 0;
jjako52c24142002-12-16 13:33:51 +0000755}
756
Harald Welted46bcd22017-08-08 23:27:22 +0200757/* RFC3307 link-local scope multicast address */
758static const struct in6_addr all_router_mcast_addr = {
759 .s6_addr = { 0xff,0x02,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,2 }
760};
761
Harald Weltedda21ed2017-08-12 15:07:02 +0200762/* MS-originated GTP1-U packet, needs to be sent via TUN device */
763static int encaps_tun(struct pdp_t *pdp, void *pack, unsigned len)
Harald Weltebed35df2011-11-02 13:06:18 +0100764{
Harald Welted46bcd22017-08-08 23:27:22 +0200765 struct iphdr *iph = (struct iphdr *)pack;
766 struct ip6_hdr *ip6h = (struct ip6_hdr *)pack;
Harald Weltef85fe972017-09-24 20:00:34 +0800767 struct tun_t *tun = (struct tun_t *)pdp->ipif;
768 struct apn_ctx *apn = tun->priv;
Pau Espin Pedrol5b1ef952018-01-25 20:50:59 +0100769 char straddr[INET6_ADDRSTRLEN];
Pau Espin Pedrol7d54ed42018-01-25 20:09:16 +0100770 struct ippoolm_t *peer;
Pau Espin Pedrol5b1ef952018-01-25 20:50:59 +0100771 uint8_t pref_offset;
Harald Weltef85fe972017-09-24 20:00:34 +0800772
773 OSMO_ASSERT(tun);
774 OSMO_ASSERT(apn);
Harald Welted46bcd22017-08-08 23:27:22 +0200775
Max427699e2017-12-05 16:30:37 +0100776 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 +0200777
778 switch (iph->version) {
779 case 6:
Pau Espin Pedrol7d54ed42018-01-25 20:09:16 +0100780 peer = pdp_get_peer_ipv(pdp, true);
781 if (!peer) {
782 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS IPv6 with unassigned EUA: %s\n",
783 osmo_hexdump(pack, len));
784 return -1;
785 }
786
Pau Espin Pedrol5b1ef952018-01-25 20:50:59 +0100787 /* Validate packet comes from IPaddr assigned to the pdp ctx.
788 If packet is a LL addr, then EUA is in the lower 64 bits,
789 otherwise it's used as the 64 prefix */
790 pref_offset = IN6_IS_ADDR_LINKLOCAL(&ip6h->ip6_src) ? 8 : 0;
791 if (memcmp(((uint8_t*)&ip6h->ip6_src) + pref_offset, &peer->addr.v6, 8)) {
792 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS using unassigned src IPv6: %s\n",
793 inet_ntop(AF_INET6, &ip6h->ip6_src, straddr, sizeof(straddr)));
794 return -1;
795 }
796
Harald Welted46bcd22017-08-08 23:27:22 +0200797 /* daddr: all-routers multicast addr */
798 if (IN6_ARE_ADDR_EQUAL(&ip6h->ip6_dst, &all_router_mcast_addr))
Pau Espin Pedrol7d54ed42018-01-25 20:09:16 +0100799 return handle_router_mcast(pdp->gsn, pdp, &peer->addr.v6,
800 &apn->v6_lladdr, pack, len);
Harald Welted46bcd22017-08-08 23:27:22 +0200801 break;
802 case 4:
Pau Espin Pedrol7d54ed42018-01-25 20:09:16 +0100803 peer = pdp_get_peer_ipv(pdp, false);
804 if (!peer) {
805 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS IPv4 with unassigned EUA: %s\n",
806 osmo_hexdump(pack, len));
807 return -1;
808 }
Pau Espin Pedrol5b1ef952018-01-25 20:50:59 +0100809
810 /* Validate packet comes from IPaddr assigned to the pdp ctx */
811 if (memcmp(&iph->saddr, &peer->addr.v4, sizeof(peer->addr.v4))) {
812 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS using unassigned src IPv4: %s\n",
813 inet_ntop(AF_INET, &iph->saddr, straddr, sizeof(straddr)));
814 return -1;
815 }
Harald Welted46bcd22017-08-08 23:27:22 +0200816 break;
817 default:
Harald Weltedda21ed2017-08-12 15:07:02 +0200818 LOGPPDP(LOGL_ERROR, pdp, "Packet from MS is neither IPv4 nor IPv6: %s\n",
819 osmo_hexdump(pack, len));
Harald Welted46bcd22017-08-08 23:27:22 +0200820 return -1;
821 }
Harald Weltebed35df2011-11-02 13:06:18 +0100822 return tun_encaps((struct tun_t *)pdp->ipif, pack, len);
jjako52c24142002-12-16 13:33:51 +0000823}
824
Harald Welte632e8432017-09-05 18:12:14 +0200825static char *config_file = "osmo-ggsn.cfg";
Harald Weltedda21ed2017-08-12 15:07:02 +0200826
827/* callback for tun device osmocom select loop integration */
828static int ggsn_tun_fd_cb(struct osmo_fd *fd, unsigned int what)
829{
830 struct apn_ctx *apn = fd->data;
831
832 OSMO_ASSERT(what & BSC_FD_READ);
833
834 return tun_decaps(apn->tun.tun);
835}
836
837/* callback for libgtp osmocom select loop integration */
838static int ggsn_gtp_fd_cb(struct osmo_fd *fd, unsigned int what)
839{
840 struct ggsn_ctx *ggsn = fd->data;
841 int rc;
842
843 OSMO_ASSERT(what & BSC_FD_READ);
844
845 switch (fd->priv_nr) {
846 case 0:
847 rc = gtp_decaps0(ggsn->gsn);
848 break;
849 case 1:
850 rc = gtp_decaps1c(ggsn->gsn);
851 break;
852 case 2:
853 rc = gtp_decaps1u(ggsn->gsn);
854 break;
855 default:
856 OSMO_ASSERT(0);
857 break;
858 }
859 return rc;
860}
861
862static void ggsn_gtp_tmr_start(struct ggsn_ctx *ggsn)
863{
864 struct timeval next;
865
866 /* Retrieve next retransmission as timeval */
867 gtp_retranstimeout(ggsn->gsn, &next);
868
869 /* re-schedule the timer */
870 osmo_timer_schedule(&ggsn->gtp_timer, next.tv_sec, next.tv_usec/1000);
871}
872
873/* timer callback for libgtp retransmission and ping */
874static void ggsn_gtp_tmr_cb(void *data)
875{
876 struct ggsn_ctx *ggsn = data;
877
878 /* do all the retransmissions as needed */
879 gtp_retrans(ggsn->gsn);
880
881 ggsn_gtp_tmr_start(ggsn);
882}
883
884/* To exit gracefully. Used with GCC compilation flag -pg and gprof */
885static void signal_handler(int s)
886{
887 LOGP(DGGSN, LOGL_NOTICE, "signal %d received\n", s);
888 switch (s) {
889 case SIGINT:
Harald Weltee8049472017-08-20 12:44:21 +0200890 case SIGTERM:
Harald Weltedda21ed2017-08-12 15:07:02 +0200891 LOGP(DGGSN, LOGL_NOTICE, "SIGINT received, shutting down\n");
892 end = 1;
893 break;
894 case SIGABRT:
895 case SIGUSR1:
896 talloc_report(tall_vty_ctx, stderr);
897 talloc_report_full(tall_ggsn_ctx, stderr);
898 break;
899 case SIGUSR2:
900 talloc_report_full(tall_vty_ctx, stderr);
901 break;
902 default:
903 break;
904 }
905}
906
907
908/* Start a given GGSN */
909int ggsn_start(struct ggsn_ctx *ggsn)
910{
911 struct apn_ctx *apn;
912 int rc;
913
914 if (ggsn->started)
915 return 0;
916
917 LOGPGGSN(LOGL_INFO, ggsn, "Starting GGSN\n");
918
919 /* Start libgtp listener */
920 if (gtp_new(&ggsn->gsn, ggsn->cfg.state_dir, &ggsn->cfg.listen_addr.v4, GTP_MODE_GGSN)) {
921 LOGPGGSN(LOGL_ERROR, ggsn, "Failed to create GTP: %s\n", strerror(errno));
922 return -1;
923 }
924 ggsn->gsn->priv = ggsn;
925
Harald Welte98146772017-09-05 17:41:20 +0200926 /* patch in different addresses to use (in case we're behind NAT, the listen
927 * address is different from what we advertise externally) */
928 if (ggsn->cfg.gtpc_addr.v4.s_addr)
929 ggsn->gsn->gsnc = ggsn->cfg.gtpc_addr.v4;
930
931 if (ggsn->cfg.gtpu_addr.v4.s_addr)
932 ggsn->gsn->gsnu = ggsn->cfg.gtpu_addr.v4;
933
Harald Weltedda21ed2017-08-12 15:07:02 +0200934 /* Register File Descriptors */
935 osmo_fd_setup(&ggsn->gtp_fd0, ggsn->gsn->fd0, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 0);
936 rc = osmo_fd_register(&ggsn->gtp_fd0);
937 OSMO_ASSERT(rc == 0);
938
939 osmo_fd_setup(&ggsn->gtp_fd1c, ggsn->gsn->fd1c, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 1);
940 rc = osmo_fd_register(&ggsn->gtp_fd1c);
941 OSMO_ASSERT(rc == 0);
942
943 osmo_fd_setup(&ggsn->gtp_fd1u, ggsn->gsn->fd1u, BSC_FD_READ, ggsn_gtp_fd_cb, ggsn, 2);
944 rc = osmo_fd_register(&ggsn->gtp_fd1u);
945 OSMO_ASSERT(rc == 0);
946
947 /* Start GTP re-transmission timer */
948 osmo_timer_setup(&ggsn->gtp_timer, ggsn_gtp_tmr_cb, ggsn);
949
950 gtp_set_cb_data_ind(ggsn->gsn, encaps_tun);
951 gtp_set_cb_delete_context(ggsn->gsn, delete_context);
952 gtp_set_cb_create_context_ind(ggsn->gsn, create_context_ind);
953
954 LOGPGGSN(LOGL_NOTICE, ggsn, "Successfully started\n");
955 ggsn->started = true;
956
957 llist_for_each_entry(apn, &ggsn->apn_list, list)
958 apn_start(apn);
959
960 return 0;
961}
962
963/* Stop a given GGSN */
964int ggsn_stop(struct ggsn_ctx *ggsn)
965{
966 struct apn_ctx *apn;
967
968 if (!ggsn->started)
969 return 0;
970
971 /* iterate over all APNs and stop them */
972 llist_for_each_entry(apn, &ggsn->apn_list, list)
973 apn_stop(apn, true);
974
975 osmo_timer_del(&ggsn->gtp_timer);
976
977 osmo_fd_unregister(&ggsn->gtp_fd1u);
978 osmo_fd_unregister(&ggsn->gtp_fd1c);
979 osmo_fd_unregister(&ggsn->gtp_fd0);
980
981 if (ggsn->gsn) {
982 gtp_free(ggsn->gsn);
983 ggsn->gsn = NULL;
984 }
985
986 ggsn->started = false;
987 return 0;
988}
989
990static void print_usage()
991{
992 printf("Usage: osmo-ggsn [-h] [-D] [-c configfile] [-V]\n");
993}
994
995static void print_help()
996{
997 printf( " Some useful help...\n"
998 " -h --help This help text\n"
999 " -D --daemonize Fork the process into a background daemon\n"
1000 " -c --config-file filename The config file to use\n"
1001 " -V --version Print the version of OsmoGGSN\n"
1002 );
1003}
1004
1005static void handle_options(int argc, char **argv)
1006{
1007 while (1) {
1008 int option_index = 0, c;
1009 static struct option long_options[] = {
1010 { "help", 0, 0, 'h' },
1011 { "daemonize", 0, 0, 'D' },
1012 { "config-file", 1, 0, 'c' },
1013 { "version", 0, 0, 'V' },
1014 { 0, 0, 0, 0 }
1015 };
1016
1017 c = getopt_long(argc, argv, "hdc:V", long_options, &option_index);
1018 if (c == -1)
1019 break;
1020
1021 switch (c) {
1022 case 'h':
1023 print_usage();
1024 print_help();
1025 exit(0);
1026 case 'D':
1027 daemonize = 1;
1028 break;
1029 case 'c':
1030 config_file = optarg;
1031 break;
1032 case 'V':
1033 print_version(1);
1034 exit(0);
1035 break;
1036 }
1037 }
1038}
1039
jjako52c24142002-12-16 13:33:51 +00001040int main(int argc, char **argv)
1041{
Harald Weltedda21ed2017-08-12 15:07:02 +02001042 struct ggsn_ctx *ggsn;
1043 int rc;
jjako52c24142002-12-16 13:33:51 +00001044
Harald Welte632e8432017-09-05 18:12:14 +02001045 tall_ggsn_ctx = talloc_named_const(NULL, 0, "OsmoGGSN");
Harald Weltedda21ed2017-08-12 15:07:02 +02001046 msgb_talloc_ctx_init(tall_ggsn_ctx, 0);
jjako52c24142002-12-16 13:33:51 +00001047
Harald Weltee8049472017-08-20 12:44:21 +02001048 /* Handle keyboard interrupt SIGINT */
Harald Weltedda21ed2017-08-12 15:07:02 +02001049 signal(SIGINT, &signal_handler);
Harald Weltee8049472017-08-20 12:44:21 +02001050 signal(SIGTERM, &signal_handler);
Harald Weltedda21ed2017-08-12 15:07:02 +02001051 signal(SIGABRT, &signal_handler);
1052 signal(SIGUSR1, &signal_handler);
1053 signal(SIGUSR2, &signal_handler);
jjako52c24142002-12-16 13:33:51 +00001054
Harald Weltedda21ed2017-08-12 15:07:02 +02001055 osmo_init_ignore_signals();
Holger Hans Peter Freyther9c7fd8e2014-12-04 16:32:37 +01001056 osmo_init_logging(&log_info);
Harald Weltedda21ed2017-08-12 15:07:02 +02001057 osmo_stats_init(tall_ggsn_ctx);
jjako0141d202004-01-09 15:19:20 +00001058
Harald Weltedda21ed2017-08-12 15:07:02 +02001059 vty_init(&g_vty_info);
1060 logging_vty_add_cmds(NULL);
1061 osmo_stats_vty_add_cmds(&log_info);
1062 ggsn_vty_init();
1063 ctrl_vty_init(tall_ggsn_ctx);
1064
1065 handle_options(argc, argv);
1066
1067 rate_ctr_init(tall_ggsn_ctx);
1068
1069 rc = vty_read_config_file(config_file, NULL);
1070 if (rc < 0) {
1071 fprintf(stderr, "Failed to open config file: '%s'\n", config_file);
1072 exit(2);
Harald Weltebed35df2011-11-02 13:06:18 +01001073 }
jjako52c24142002-12-16 13:33:51 +00001074
Harald Weltedda21ed2017-08-12 15:07:02 +02001075 rc = telnet_init_dynif(tall_ggsn_ctx, NULL, vty_get_bind_addr(), OSMO_VTY_PORT_GGSN);
1076 if (rc < 0)
Harald Weltebed35df2011-11-02 13:06:18 +01001077 exit(1);
Holger Hans Peter Freyther9c0ff4f2014-03-23 10:07:26 +01001078
Harald Weltedda21ed2017-08-12 15:07:02 +02001079 g_ctrlh = ctrl_interface_setup(NULL, OSMO_CTRL_PORT_GGSN, NULL);
1080 if (!g_ctrlh) {
1081 LOGP(DGGSN, LOGL_ERROR, "Failed to create CTRL interface.\n");
Harald Weltebed35df2011-11-02 13:06:18 +01001082 exit(1);
1083 }
jjako88c22162003-07-06 19:33:18 +00001084
Harald Weltedda21ed2017-08-12 15:07:02 +02001085 if (daemonize) {
1086 rc = osmo_daemonize();
1087 if (rc < 0) {
1088 perror("Error during daemonize");
Harald Weltebed35df2011-11-02 13:06:18 +01001089 exit(1);
1090 }
1091 }
jjako1d3db972004-01-16 09:56:56 +00001092
Harald Weltedda21ed2017-08-12 15:07:02 +02001093#if 0
Harald Weltebed35df2011-11-02 13:06:18 +01001094 /* qos */
1095 qos.l = 3;
1096 qos.v[2] = (args_info.qos_arg) & 0xff;
1097 qos.v[1] = ((args_info.qos_arg) >> 8) & 0xff;
1098 qos.v[0] = ((args_info.qos_arg) >> 16) & 0xff;
Harald Weltedda21ed2017-08-12 15:07:02 +02001099#endif
jjakoa7cd2492003-04-11 09:40:12 +00001100
Harald Weltedda21ed2017-08-12 15:07:02 +02001101 /* Main select loop */
1102 while (!end) {
1103 osmo_select_main(0);
Harald Weltebed35df2011-11-02 13:06:18 +01001104 }
jjakoe0149782003-07-06 17:07:04 +00001105
Harald Weltedda21ed2017-08-12 15:07:02 +02001106 llist_for_each_entry(ggsn, &g_ggsn_list, list)
1107 ggsn_stop(ggsn);
Harald Weltebed35df2011-11-02 13:06:18 +01001108
1109 return 1;
jjako52c24142002-12-16 13:33:51 +00001110}