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