blob: 157e279727ab91033e7b3eef64252316c4460b51 [file] [log] [blame]
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +02001/* Section "9.5 GPRS Session Management Messages"
2 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
4/* (C) 2009-2015 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2010 by On-Waves
6 * (C) 2019 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
7 *
8 * All Rights Reserved
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *
23 */
24
25#include <netinet/in.h>
26#include <arpa/inet.h>
27#include <netdb.h>
28
29#include "bscconfig.h"
30
31#include <osmocom/core/rate_ctr.h>
32#include <osmocom/core/tdef.h>
33#include <osmocom/gsm/apn.h>
34#include <osmocom/gprs/gprs_bssgp.h>
35
36#include <osmocom/sgsn/gprs_sm.h>
37#include <osmocom/sgsn/gprs_gmm.h>
38#include <osmocom/sgsn/gprs_utils.h>
Pau Espin Pedrol5f4736a2023-01-04 21:30:28 +010039#include <osmocom/sgsn/gtp_ggsn.h>
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +020040#include <osmocom/sgsn/sgsn.h>
41#include <osmocom/sgsn/debug.h>
42#include <osmocom/sgsn/gprs_llc.h>
43#include <osmocom/sgsn/gprs_sndcp.h>
44#include <osmocom/sgsn/gprs_ranap.h>
Pau Espin Pedrol8ec269a2023-01-05 19:19:32 +010045#include <osmocom/sgsn/gtp.h>
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +020046
47extern void *tall_sgsn_ctx;
48
49/* 3GPP TS 04.08 sec 6.1.3.4.3(.a) "Abnormal cases" */
50#define T339X_MAX_RETRANS 4
51
52static const struct tlv_definition gsm48_sm_att_tlvdef = {
53 .def = {
54 [GSM48_IE_GSM_APN] = { TLV_TYPE_TLV, 0 },
55 [GSM48_IE_GSM_PROTO_CONF_OPT] = { TLV_TYPE_TLV, 0 },
56 [GSM48_IE_GSM_PDP_ADDR] = { TLV_TYPE_TLV, 0 },
57 [GSM48_IE_GSM_AA_TMR] = { TLV_TYPE_TV, 1 },
58 [GSM48_IE_GSM_NAME_FULL] = { TLV_TYPE_TLV, 0 },
59 [GSM48_IE_GSM_NAME_SHORT] = { TLV_TYPE_TLV, 0 },
60 [GSM48_IE_GSM_TIMEZONE] = { TLV_TYPE_FIXED, 1 },
61 [GSM48_IE_GSM_UTC_AND_TZ] = { TLV_TYPE_FIXED, 7 },
62 [GSM48_IE_GSM_LSA_ID] = { TLV_TYPE_TLV, 0 },
63 },
64};
65
66static struct gsm48_qos default_qos = {
67 .delay_class = 4, /* best effort */
68 .reliab_class = GSM48_QOS_RC_LLC_UN_RLC_ACK_DATA_PROT,
69 .peak_tput = GSM48_QOS_PEAK_TPUT_32000bps,
70 .preced_class = GSM48_QOS_PC_NORMAL,
71 .mean_tput = GSM48_QOS_MEAN_TPUT_BEST_EFFORT,
72 .traf_class = GSM48_QOS_TC_INTERACTIVE,
73 .deliv_order = GSM48_QOS_DO_UNORDERED,
74 .deliv_err_sdu = GSM48_QOS_ERRSDU_YES,
75 .max_sdu_size = GSM48_QOS_MAXSDU_1520,
76 .max_bitrate_up = GSM48_QOS_MBRATE_63k,
77 .max_bitrate_down = GSM48_QOS_MBRATE_63k,
78 .resid_ber = GSM48_QOS_RBER_5e_2,
79 .sdu_err_ratio = GSM48_QOS_SERR_1e_2,
80 .handling_prio = 3,
81 .xfer_delay = 0x10, /* 200ms */
82 .guar_bitrate_up = GSM48_QOS_MBRATE_0k,
83 .guar_bitrate_down = GSM48_QOS_MBRATE_0k,
84 .sig_ind = 0, /* not optimised for signalling */
85 .max_bitrate_down_ext = 0, /* use octet 9 */
86 .guar_bitrate_down_ext = 0, /* use octet 13 */
87};
88
89/* GPRS SESSION MANAGEMENT */
90
91static void pdpctx_timer_cb(void *_mm);
92
93static void pdpctx_timer_rearm(struct sgsn_pdp_ctx *pdp, unsigned int T)
94{
95 unsigned long seconds;
96 if (osmo_timer_pending(&pdp->timer))
97 LOGPDPCTXP(LOGL_ERROR, pdp, "Scheduling PDP timer %u while old "
98 "timer %u pending\n", T, pdp->T);
99 seconds = osmo_tdef_get(sgsn->cfg.T_defs, T, OSMO_TDEF_S, -1);
100 osmo_timer_schedule(&pdp->timer, seconds, 0);
101}
102
103static void pdpctx_timer_start(struct sgsn_pdp_ctx *pdp, unsigned int T)
104{
105 if (osmo_timer_pending(&pdp->timer))
106 LOGPDPCTXP(LOGL_ERROR, pdp, "Starting PDP timer %u while old "
107 "timer %u pending\n", T, pdp->T);
108 pdp->T = T;
109 pdp->num_T_exp = 0;
110
111 osmo_timer_setup(&pdp->timer, pdpctx_timer_cb, pdp);
112 pdpctx_timer_rearm(pdp, pdp->T);
113}
114
115static void pdpctx_timer_stop(struct sgsn_pdp_ctx *pdp, unsigned int T)
116{
117 if (pdp->T != T)
118 LOGPDPCTXP(LOGL_ERROR, pdp, "Stopping PDP timer %u but "
119 "%u is running\n", T, pdp->T);
120 osmo_timer_del(&pdp->timer);
121}
122
123void pdp_ctx_detach_mm_ctx(struct sgsn_pdp_ctx *pdp)
124{
125 /* Detach from MM context */
126 llist_del(&pdp->list);
127 pdp->mm = NULL;
128
129 /* stop timer 3395 */
130 pdpctx_timer_stop(pdp, 3395);
131}
132
133#if 0
134static void msgb_put_pdp_addr_ipv4(struct msgb *msg, uint32_t ipaddr)
135{
136 uint8_t v[6];
137
138 v[0] = PDP_TYPE_ORG_IETF;
139 v[1] = PDP_TYPE_N_IETF_IPv4;
140 *(uint32_t *)(v+2) = htonl(ipaddr);
141
142 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
143}
144
145static void msgb_put_pdp_addr_ppp(struct msgb *msg)
146{
147 uint8_t v[2];
148
149 v[0] = PDP_TYPE_ORG_ETSI;
150 v[1] = PDP_TYPE_N_ETSI_PPP;
151
152 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR, sizeof(v), v);
153}
154#endif
155
156/* Chapter 9.4.18 */
157static int _tx_status(struct msgb *msg, uint8_t cause,
158 struct sgsn_mm_ctx *mmctx)
159{
160 struct gsm48_hdr *gh;
161
162 /* MMCTX might be NULL! */
163
164 DEBUGP(DMM, "<- GPRS MM STATUS (cause: %s)\n",
165 get_value_string(gsm48_gmm_cause_names, cause));
166
167 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
168 gh->proto_discr = GSM48_PDISC_SM_GPRS;
169 gh->msg_type = GSM48_MT_GSM_STATUS;
170 gh->data[0] = cause;
171
172 return gsm48_gmm_sendmsg(msg, 0, mmctx, true);
173}
174
175static int gsm48_tx_sm_status(struct sgsn_mm_ctx *mmctx, uint8_t cause)
176{
177 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 SM STATUS");
178
179 mmctx2msgid(msg, mmctx);
180 return _tx_status(msg, cause, mmctx);
181}
182
183/* 3GPP TS 24.008 § 9.5.2: Activate PDP Context Accept */
184int gsm48_tx_gsm_act_pdp_acc(struct sgsn_pdp_ctx *pdp)
185{
186 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 PDP ACC");
187 struct gsm48_hdr *gh;
188 uint8_t transaction_id = pdp->ti ^ 0x8; /* flip */
189
190 LOGPDPCTXP(LOGL_INFO, pdp, "<- ACTIVATE PDP CONTEXT ACK\n");
Pau Espin Pedrola33f0062021-06-04 17:27:03 +0200191 rate_ctr_inc(rate_ctr_group_get_ctr(sgsn->rate_ctrs, CTR_PDP_ACTIVATE_ACCEPT));
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +0200192
193 mmctx2msgid(msg, pdp->mm);
194
195 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
196 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
197 gh->msg_type = GSM48_MT_GSM_ACT_PDP_ACK;
198
199 /* Negotiated LLC SAPI */
200 msgb_v_put(msg, pdp->sapi);
201
202 /* FIXME: copy QoS parameters from original request */
203 //msgb_lv_put(msg, pdp->lib->qos_neg.l, pdp->lib->qos_neg.v);
204 msgb_lv_put(msg, sizeof(default_qos), (uint8_t *)&default_qos);
205
206 /* Radio priority 10.5.7.2 */
207 msgb_v_put(msg, pdp->lib->radio_pri);
208
209 /* PDP address */
210 /* Highest 4 bits of first byte need to be set to 1, otherwise
211 * the IE is identical with the 04.08 PDP Address IE */
212 pdp->lib->eua.v[0] &= ~0xf0;
213 msgb_tlv_put(msg, GSM48_IE_GSM_PDP_ADDR,
214 pdp->lib->eua.l, pdp->lib->eua.v);
215 pdp->lib->eua.v[0] |= 0xf0;
216
217 /* Optional: Protocol configuration options (FIXME: why 'req') */
218 if (pdp->lib->pco_req.l)
219 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT,
220 pdp->lib->pco_req.l, pdp->lib->pco_req.v);
221
222 /* Optional: Packet Flow Identifier */
223
224 return gsm48_gmm_sendmsg(msg, 0, pdp->mm, true);
225}
226
227/* 3GPP TS 24.008 § 9.5.3: Activate PDP Context reject */
228int gsm48_tx_gsm_act_pdp_rej(struct sgsn_mm_ctx *mm, uint8_t tid,
229 uint8_t cause, uint8_t pco_len, uint8_t *pco_v)
230{
231 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 PDP REJ");
232 struct gsm48_hdr *gh;
233 uint8_t transaction_id = tid ^ 0x8; /* flip */
234
235 LOGMMCTXP(LOGL_NOTICE, mm, "<- ACTIVATE PDP CONTEXT REJ: %s\n",
236 get_value_string(gsm48_gsm_cause_names, cause));
Pau Espin Pedrola33f0062021-06-04 17:27:03 +0200237 rate_ctr_inc(rate_ctr_group_get_ctr(sgsn->rate_ctrs, CTR_PDP_ACTIVATE_REJECT));
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +0200238
239 mmctx2msgid(msg, mm);
240
241 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
242 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
243 gh->msg_type = GSM48_MT_GSM_ACT_PDP_REJ;
244
245 msgb_v_put(msg, cause);
246 if (pco_len && pco_v)
247 msgb_tlv_put(msg, GSM48_IE_GSM_PROTO_CONF_OPT, pco_len, pco_v);
248
249 return gsm48_gmm_sendmsg(msg, 0, mm, true);
250}
251
252/* 3GPP TS 24.008 § 9.5.8: Deactivate PDP Context Request */
253static int _gsm48_tx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, uint8_t tid,
254 uint8_t sm_cause, bool teardown)
255{
256 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 PDP DET REQ");
257 struct gsm48_hdr *gh;
258 uint8_t transaction_id = tid ^ 0x8; /* flip */
259 uint8_t tear_down_ind = (0x9 << 4) | (!!teardown);
260
261 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT REQ\n");
Pau Espin Pedrola33f0062021-06-04 17:27:03 +0200262 rate_ctr_inc(rate_ctr_group_get_ctr(sgsn->rate_ctrs, CTR_PDP_DL_DEACTIVATE_REQUEST));
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +0200263
264 mmctx2msgid(msg, mm);
265
266 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
267 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
268 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_REQ;
269
270 msgb_v_put(msg, sm_cause);
271 msgb_v_put(msg, tear_down_ind);
272
273 return gsm48_gmm_sendmsg(msg, 0, mm, true);
274}
275int gsm48_tx_gsm_deact_pdp_req(struct sgsn_pdp_ctx *pdp, uint8_t sm_cause, bool teardown)
276{
277 pdpctx_timer_start(pdp, 3395);
278
279 return _gsm48_tx_gsm_deact_pdp_req(pdp->mm, pdp->ti, sm_cause, teardown);
280}
281
282/* 3GPP TS 24.008 § 9.5.9: Deactivate PDP Context Accept */
283static int _gsm48_tx_gsm_deact_pdp_acc(struct sgsn_mm_ctx *mm, uint8_t tid)
284{
285 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 PDP DET ACC");
286 struct gsm48_hdr *gh;
287 uint8_t transaction_id = tid ^ 0x8; /* flip */
288
289 LOGMMCTXP(LOGL_INFO, mm, "<- DEACTIVATE PDP CONTEXT ACK\n");
Pau Espin Pedrola33f0062021-06-04 17:27:03 +0200290 rate_ctr_inc(rate_ctr_group_get_ctr(sgsn->rate_ctrs, CTR_PDP_DL_DEACTIVATE_ACCEPT));
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +0200291
292 mmctx2msgid(msg, mm);
293
294 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
295 gh->proto_discr = GSM48_PDISC_SM_GPRS | (transaction_id << 4);
296 gh->msg_type = GSM48_MT_GSM_DEACT_PDP_ACK;
297
298 return gsm48_gmm_sendmsg(msg, 0, mm, true);
299}
300int gsm48_tx_gsm_deact_pdp_acc(struct sgsn_pdp_ctx *pdp)
301{
302 return _gsm48_tx_gsm_deact_pdp_acc(pdp->mm, pdp->ti);
303}
304
305static int activate_ggsn(struct sgsn_mm_ctx *mmctx,
306 struct sgsn_ggsn_ctx *ggsn, const uint8_t transaction_id,
307 const uint8_t req_nsapi, const uint8_t req_llc_sapi,
308 struct tlv_parsed *tp, int destroy_ggsn)
309{
310 struct sgsn_pdp_ctx *pdp;
311
312 LOGMMCTXP(LOGL_DEBUG, mmctx, "Using GGSN %u\n", ggsn->id);
313 ggsn->gsn = sgsn->gsn;
314 pdp = sgsn_create_pdp_ctx(ggsn, mmctx, req_nsapi, tp);
315 if (!pdp)
316 return -1;
317
318 /* Store SAPI and Transaction Identifier */
319 pdp->sapi = req_llc_sapi;
320 pdp->ti = transaction_id;
321 pdp->destroy_ggsn = destroy_ggsn;
322
323 return 0;
324}
325
326static void ggsn_lookup_cb(void *arg, int status, int timeouts, struct hostent *hostent)
327{
328 struct sgsn_ggsn_ctx *ggsn;
329 struct sgsn_ggsn_lookup *lookup = arg;
330 struct in_addr *addr = NULL;
331 char buf[INET_ADDRSTRLEN];
332
333 /* The context is gone while we made a request */
334 if (!lookup->mmctx) {
335 talloc_free(lookup->orig_msg);
336 talloc_free(lookup);
337 return;
338 }
339
340 if (status != ARES_SUCCESS) {
341 struct sgsn_mm_ctx *mmctx = lookup->mmctx;
342
343 LOGMMCTXP(LOGL_ERROR, mmctx, "DNS query failed.\n");
344
345 /* Need to try with three digits now */
346 if (lookup->state == SGSN_GGSN_2DIGIT) {
347 char *hostname;
348 int rc;
349
350 lookup->state = SGSN_GGSN_3DIGIT;
351 hostname = osmo_apn_qualify_from_imsi(mmctx->imsi,
352 lookup->apn_str, 1);
353 LOGMMCTXP(LOGL_DEBUG, mmctx,
354 "Going to query %s\n", hostname);
355 rc = sgsn_ares_query(sgsn, hostname,
356 ggsn_lookup_cb, lookup);
357 if (rc != 0) {
358 LOGMMCTXP(LOGL_ERROR, mmctx, "Couldn't start GGSN\n");
359 goto reject_due_failure;
360 }
361 return;
362 }
363
364 LOGMMCTXP(LOGL_ERROR, mmctx, "Couldn't resolve GGSN\n");
365 goto reject_due_failure;
366 }
367
368 if (hostent->h_length != sizeof(struct in_addr)) {
369 LOGMMCTXP(LOGL_ERROR, lookup->mmctx,
370 "Wrong addr size(%zu)\n", sizeof(struct in_addr));
371 goto reject_due_failure;
372 }
373
374 /* Get the first addr from the list */
375 addr = (struct in_addr *) hostent->h_addr_list[0];
376 if (!addr) {
377 LOGMMCTXP(LOGL_ERROR, lookup->mmctx, "No host address.\n");
378 goto reject_due_failure;
379 }
380
381 ggsn = sgsn_ggsn_ctx_alloc(UINT32_MAX);
382 if (!ggsn) {
383 LOGMMCTXP(LOGL_ERROR, lookup->mmctx, "Failed to create ggsn.\n");
384 goto reject_due_failure;
385 }
386 ggsn->remote_addr = *addr;
387 LOGMMCTXP(LOGL_NOTICE, lookup->mmctx,
388 "Selected %s as GGSN.\n",
389 inet_ntop(AF_INET, addr, buf, sizeof(buf)));
390
391 /* forget about the ggsn look-up */
392 lookup->mmctx->ggsn_lookup = NULL;
393
394 activate_ggsn(lookup->mmctx, ggsn, lookup->ti, lookup->nsapi,
395 lookup->sapi, &lookup->tp, 1);
396
397 /* Now free it */
398 talloc_free(lookup->orig_msg);
399 talloc_free(lookup);
400 return;
401
402reject_due_failure:
403 gsm48_tx_gsm_act_pdp_rej(lookup->mmctx, lookup->ti,
404 GMM_CAUSE_NET_FAIL, 0, NULL);
405 lookup->mmctx->ggsn_lookup = NULL;
406 talloc_free(lookup->orig_msg);
407 talloc_free(lookup);
408}
409
410static int do_act_pdp_req(struct sgsn_mm_ctx *mmctx, struct msgb *msg, bool *delete)
411{
412 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
413 struct gsm48_act_pdp_ctx_req *act_req = (struct gsm48_act_pdp_ctx_req *) gh->data;
414 uint8_t req_qos_len, req_pdpa_len;
415 uint8_t *req_qos, *req_pdpa;
416 struct tlv_parsed tp;
417 uint8_t transaction_id = gsm48_hdr_trans_id(gh);
418 struct sgsn_ggsn_ctx *ggsn;
419 struct sgsn_pdp_ctx *pdp;
420 enum gsm48_gsm_cause gsm_cause;
421 char apn_str[GSM_APN_LENGTH] = { 0, };
422 char *hostname;
423 int rc;
424 struct gprs_llc_lle *lle;
425 char buf[INET_ADDRSTRLEN];
426
427 LOGMMCTXP(LOGL_INFO, mmctx, "-> ACTIVATE PDP CONTEXT REQ: SAPI=%u NSAPI=%u ",
428 act_req->req_llc_sapi, act_req->req_nsapi);
429
430 /* FIXME: length checks! */
431 req_qos_len = act_req->data[0];
432 req_qos = act_req->data + 1; /* 10.5.6.5 */
433 req_pdpa_len = act_req->data[1 + req_qos_len];
434 req_pdpa = act_req->data + 1 + req_qos_len + 1; /* 10.5.6.4 */
435
436 switch (req_pdpa[0] & 0xf) {
437 case 0x0:
438 DEBUGPC(DMM, "ETSI ");
439 break;
440 case 0x1:
441 DEBUGPC(DMM, "IETF ");
442 break;
443 case 0xf:
444 DEBUGPC(DMM, "Empty ");
445 break;
446 }
447
448 switch (req_pdpa[1]) {
449 case 0x21:
450 DEBUGPC(DMM, "IPv4 ");
451 if (req_pdpa_len >= 6) {
452 struct in_addr ia;
453 ia.s_addr = ntohl(*((uint32_t *) (req_pdpa+2)));
454 DEBUGPC(DMM, "%s ", inet_ntop(AF_INET, &ia, buf, sizeof(buf)));
455 }
456 break;
457 case 0x57:
458 DEBUGPC(DMM, "IPv6 ");
459 if (req_pdpa_len >= 18) {
460 /* FIXME: print IPv6 address */
461 }
462 break;
463 default:
464 DEBUGPC(DMM, "0x%02x ", req_pdpa[1]);
465 break;
466 }
467
468 LOGPC(DMM, LOGL_INFO, "\n");
469
470 /* Check if NSAPI is out of range (TS 04.65 / 7.2) */
471 if (act_req->req_nsapi < 5 || act_req->req_nsapi > 15) {
472 /* Send reject with GSM_CAUSE_INV_MAND_INFO */
473 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
474 GSM_CAUSE_INV_MAND_INFO,
475 0, NULL);
476 }
477
478 /* Optional: Access Point Name, Protocol Config Options */
479 if (req_pdpa + req_pdpa_len < msg->data + msg->len)
480 tlv_parse(&tp, &gsm48_sm_att_tlvdef, req_pdpa + req_pdpa_len,
481 (msg->data + msg->len) - (req_pdpa + req_pdpa_len), 0, 0);
482 else
483 memset(&tp, 0, sizeof(tp));
484
485
486 /* put the non-TLV elements in the TLV parser structure to
487 * pass them on to the SGSN / GTP code */
488 tp.lv[OSMO_IE_GSM_REQ_QOS].len = req_qos_len;
489 tp.lv[OSMO_IE_GSM_REQ_QOS].val = req_qos;
490 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].len = req_pdpa_len;
491 tp.lv[OSMO_IE_GSM_REQ_PDP_ADDR].val = req_pdpa;
492
493 /* Check if NSAPI is already in use */
494 pdp = sgsn_pdp_ctx_by_nsapi(mmctx, act_req->req_nsapi);
495 if (pdp) {
Pau Espin Pedrold7276bd2019-09-06 21:34:31 +0200496 /* Make sure pdp ctx was not already torn down on GTP side */
497 if (!pdp->lib) {
498 gsm_cause = GSM_CAUSE_REACT_RQD;
499 goto no_context;
500 }
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +0200501 /* We already have a PDP context for this TLLI + NSAPI tuple */
502 if (pdp->sapi == act_req->req_llc_sapi &&
503 pdp->ti == transaction_id) {
504 /* This apparently is a re-transmission of a PDP CTX
505 * ACT REQ (our ACT ACK must have got dropped) */
506 rc = gsm48_tx_gsm_act_pdp_acc(pdp);
507 if (rc < 0)
508 return rc;
509
510 if (pdp->mm->ran_type == MM_CTX_T_GERAN_Gb) {
511 /* Also re-transmit the SNDCP XID message */
512 lle = &pdp->mm->gb.llme->lle[pdp->sapi];
513 rc = sndcp_sn_xid_req(lle,pdp->nsapi);
514 if (rc < 0)
515 return rc;
516 }
517
518 return 0;
519 }
520
521 /* Send reject with GSM_CAUSE_NSAPI_IN_USE */
522 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
523 GSM_CAUSE_NSAPI_IN_USE,
524 0, NULL);
525 }
526
527 if (mmctx->ggsn_lookup) {
528 if (mmctx->ggsn_lookup->sapi == act_req->req_llc_sapi &&
529 mmctx->ggsn_lookup->ti == transaction_id) {
530 LOGMMCTXP(LOGL_NOTICE, mmctx,
531 "Re-transmission while doing look-up. Ignoring.\n");
532 return 0;
533 }
534 }
535
536 /* Only increment counter for a real activation, after we checked
537 * for re-transmissions */
Pau Espin Pedrola33f0062021-06-04 17:27:03 +0200538 rate_ctr_inc(rate_ctr_group_get_ctr(mmctx->ctrg, GMM_CTR_PDP_CTX_ACT));
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +0200539
540 /* Determine GGSN based on APN and subscription options */
541 ggsn = sgsn_mm_ctx_find_ggsn_ctx(mmctx, &tp, &gsm_cause, apn_str);
542 if (ggsn)
543 return activate_ggsn(mmctx, ggsn, transaction_id,
544 act_req->req_nsapi, act_req->req_llc_sapi,
545 &tp, 0);
546
547 if (strlen(apn_str) == 0)
548 goto no_context;
549 if (!sgsn->cfg.dynamic_lookup)
550 goto no_context;
551
552 /* schedule a dynamic look-up */
553 mmctx->ggsn_lookup = talloc_zero(tall_sgsn_ctx, struct sgsn_ggsn_lookup);
554 if (!mmctx->ggsn_lookup)
555 goto no_context;
556
557 mmctx->ggsn_lookup->state = SGSN_GGSN_2DIGIT;
558 mmctx->ggsn_lookup->mmctx = mmctx;
559 strcpy(mmctx->ggsn_lookup->apn_str, apn_str);
560
561 mmctx->ggsn_lookup->orig_msg = msg;
562 mmctx->ggsn_lookup->tp = tp;
563
564 mmctx->ggsn_lookup->ti = transaction_id;
565 mmctx->ggsn_lookup->nsapi = act_req->req_nsapi;
566 mmctx->ggsn_lookup->sapi = act_req->req_llc_sapi;
567
568 hostname = osmo_apn_qualify_from_imsi(mmctx->imsi,
569 mmctx->ggsn_lookup->apn_str, 0);
570
571 LOGMMCTXP(LOGL_DEBUG, mmctx, "Going to query %s\n", hostname);
572 rc = sgsn_ares_query(sgsn, hostname,
573 ggsn_lookup_cb, mmctx->ggsn_lookup);
574 if (rc != 0) {
575 LOGMMCTXP(LOGL_ERROR, mmctx, "Failed to start ares query.\n");
576 goto no_context;
577 }
578 *delete = 0;
579
580 return 0;
581
582no_context:
583 LOGMMCTXP(LOGL_ERROR, mmctx, "No GGSN context found!\n");
584 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
585 gsm_cause, 0, NULL);
586}
587
588/* 3GPP TS 24.008 § 9.5.1: Activate PDP Context Request */
589static int gsm48_rx_gsm_act_pdp_req(struct sgsn_mm_ctx *mmctx,
590 struct msgb *_msg)
591{
592 bool delete = 1;
593 struct msgb *msg;
594 int rc;
595
Pau Espin Pedrola33f0062021-06-04 17:27:03 +0200596 rate_ctr_inc(rate_ctr_group_get_ctr(sgsn->rate_ctrs, CTR_PDP_ACTIVATE_REQUEST));
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +0200597
598 /*
599 * This is painful. We might not have a static GGSN
600 * configuration and then would need to copy the msg
601 * and re-do most of this routine (or call it again
602 * and make sure it only goes through the dynamic
603 * resolving. The question is what to optimize for
604 * and the dynamic resolution will be the right thing
605 * in the long run.
606 */
607 msg = bssgp_msgb_copy(_msg, __func__);
608 if (!msg) {
609 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(_msg);
610 uint8_t transaction_id = gsm48_hdr_trans_id(gh);
611
612 LOGMMCTXP(LOGL_ERROR, mmctx, "-> ACTIVATE PDP CONTEXT REQ failed copy.\n");
613 /* Send reject with GSM_CAUSE_INV_MAND_INFO */
614 return gsm48_tx_gsm_act_pdp_rej(mmctx, transaction_id,
615 GSM_CAUSE_NET_FAIL,
616 0, NULL);
617 }
618
619 rc = do_act_pdp_req(mmctx, msg, &delete);
620 if (delete)
621 msgb_free(msg);
622 return rc;
623}
624
625/* 3GPP TS 24.008 § 9.5.8: Deactivate PDP Context Request */
626static int gsm48_rx_gsm_deact_pdp_req(struct sgsn_mm_ctx *mm, struct msgb *msg)
627{
628 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
629 uint8_t transaction_id = gsm48_hdr_trans_id(gh);
630 struct sgsn_pdp_ctx *pdp;
631
632 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT REQ (cause: %s)\n",
633 get_value_string(gsm48_gsm_cause_names, gh->data[0]));
Pau Espin Pedrola33f0062021-06-04 17:27:03 +0200634 rate_ctr_inc(rate_ctr_group_get_ctr(sgsn->rate_ctrs, CTR_PDP_UL_DEACTIVATE_REQUEST));
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +0200635
636 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
637 if (!pdp) {
638 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Request for "
639 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
640 mm->imsi, transaction_id);
641 return _gsm48_tx_gsm_deact_pdp_acc(mm, transaction_id);
642 }
643
Pau Espin Pedrol5ce54ba2020-10-19 16:38:25 +0200644 if (pdp->ggsn)
645 return sgsn_delete_pdp_ctx(pdp);
646 /* GTP side already detached, freeing */
647 sgsn_pdp_ctx_free(pdp);
648 return 0;
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +0200649}
650
651/* 3GPP TS 24.008 § 9.5.9: Deactivate PDP Context Accept */
652static int gsm48_rx_gsm_deact_pdp_ack(struct sgsn_mm_ctx *mm, struct msgb *msg)
653{
654 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
655 uint8_t transaction_id = gsm48_hdr_trans_id(gh);
656 struct sgsn_pdp_ctx *pdp;
657
658 LOGMMCTXP(LOGL_INFO, mm, "-> DEACTIVATE PDP CONTEXT ACK\n");
Pau Espin Pedrola33f0062021-06-04 17:27:03 +0200659 rate_ctr_inc(rate_ctr_group_get_ctr(sgsn->rate_ctrs, CTR_PDP_UL_DEACTIVATE_ACCEPT));
Pau Espin Pedrol35f0e662019-09-02 18:27:27 +0200660
661 pdp = sgsn_pdp_ctx_by_tid(mm, transaction_id);
662 if (!pdp) {
663 LOGMMCTXP(LOGL_NOTICE, mm, "Deactivate PDP Context Accept for "
664 "non-existing PDP Context (IMSI=%s, TI=%u)\n",
665 mm->imsi, transaction_id);
666 return 0;
667 }
668 /* stop timer 3395 */
669 pdpctx_timer_stop(pdp, 3395);
670 if (pdp->ggsn)
671 return sgsn_delete_pdp_ctx(pdp);
672 /* GTP side already detached, freeing */
673 sgsn_pdp_ctx_free(pdp);
674 return 0;
675}
676
677static int gsm48_rx_gsm_status(struct sgsn_mm_ctx *ctx, struct msgb *msg)
678{
679 struct gsm48_hdr *gh = msgb_l3(msg);
680
681 LOGMMCTXP(LOGL_INFO, ctx, "-> GPRS SM STATUS (cause: %s)\n",
682 get_value_string(gsm48_gsm_cause_names, gh->data[0]));
683
684 return 0;
685}
686
687static void pdpctx_timer_cb(void *_pdp)
688{
689 struct sgsn_pdp_ctx *pdp = _pdp;
690
691 pdp->num_T_exp++;
692
693 switch (pdp->T) {
694 case 3395: /* waiting for PDP CTX DEACT ACK */
695 if (pdp->num_T_exp > T339X_MAX_RETRANS) {
696 LOGPDPCTXP(LOGL_NOTICE, pdp, "T3395 expired > %d times\n", T339X_MAX_RETRANS);
697 pdp->state = PDP_STATE_INACTIVE;
698 if (pdp->ggsn)
699 sgsn_delete_pdp_ctx(pdp);
700 else
701 sgsn_pdp_ctx_free(pdp);
702 break;
703 }
704 _gsm48_tx_gsm_deact_pdp_req(pdp->mm, pdp->ti, GSM_CAUSE_NET_FAIL, true);
705 pdpctx_timer_rearm(pdp, 3395);
706 break;
707 default:
708 LOGPDPCTXP(LOGL_ERROR, pdp, "timer expired in unknown mode %u\n",
709 pdp->T);
710 }
711}
712
713/* GPRS Session Management */
714int gsm0408_rcv_gsm(struct sgsn_mm_ctx *mmctx, struct msgb *msg,
715 struct gprs_llc_llme *llme)
716{
717 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_gmmh(msg);
718 int rc;
719
720 /* MMCTX can be NULL when called */
721
722 if (!mmctx) {
723 LOGGBIUP(llme, msg, LOGL_NOTICE, "Cannot handle SM for unknown MM CTX\n");
724 /* 6.1.3.6 */
725 if (gh->msg_type == GSM48_MT_GSM_STATUS)
726 return 0;
727
728 return gsm0408_gprs_force_reattach_oldmsg(msg, llme);
729 }
730
731 switch (gh->msg_type) {
732 case GSM48_MT_GSM_ACT_PDP_REQ:
733 rc = gsm48_rx_gsm_act_pdp_req(mmctx, msg);
734 break;
735 case GSM48_MT_GSM_DEACT_PDP_REQ:
736 rc = gsm48_rx_gsm_deact_pdp_req(mmctx, msg);
737 break;
738 case GSM48_MT_GSM_DEACT_PDP_ACK:
739 rc = gsm48_rx_gsm_deact_pdp_ack(mmctx, msg);
740 break;
741 case GSM48_MT_GSM_STATUS:
742 rc = gsm48_rx_gsm_status(mmctx, msg);
743 break;
744 case GSM48_MT_GSM_REQ_PDP_ACT_REJ:
745 case GSM48_MT_GSM_ACT_AA_PDP_REQ:
746 case GSM48_MT_GSM_DEACT_AA_PDP_REQ:
747 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unimplemented GSM 04.08 GSM msg type 0x%02x: %s\n",
748 gh->msg_type, osmo_hexdump((uint8_t *)gh, msgb_l3len(msg)));
749 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
750 break;
751 default:
752 LOGMMCTXP(LOGL_NOTICE, mmctx, "Unknown GSM 04.08 GSM msg type 0x%02x: %s\n",
753 gh->msg_type, osmo_hexdump((uint8_t *)gh, msgb_l3len(msg)));
754 rc = gsm48_tx_sm_status(mmctx, GSM_CAUSE_MSGT_NOTEXIST_NOTIMPL);
755 break;
756
757 }
758
759 return rc;
760}