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