Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 1 | /* |
Harald Welte | 632e843 | 2017-09-05 18:12:14 +0200 | [diff] [blame] | 2 | * OsmoGGSN - Gateway GPRS Support Node |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 3 | * Copyright (C) 2002, 2003, 2004 Mondru AB. |
Harald Welte | 632e843 | 2017-09-05 18:12:14 +0200 | [diff] [blame] | 4 | * Copyright (C) 2010-2011, 2016-2017 Harald Welte <laforge@gnumonks.org> |
| 5 | * Copyright (C) 2015-2017 sysmocom - s.f.m.c. GmbH |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 6 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 7 | * The contents of this file may be used under the terms of the GNU |
| 8 | * General Public License Version 2, provided that the above copyright |
| 9 | * notice and this permission notice is included in all copies or |
| 10 | * substantial portions of the software. |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 11 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * gtp.c: Contains all GTP functionality. Should be able to handle multiple |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 16 | * tunnels in the same program. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 17 | * |
| 18 | * TODO: |
| 19 | * - Do we need to handle fragmentation? |
| 20 | */ |
| 21 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 22 | #ifdef __linux__ |
| 23 | #define _GNU_SOURCE 1 |
| 24 | #endif |
| 25 | |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 26 | #include <osmocom/core/logging.h> |
| 27 | #include <osmocom/core/utils.h> |
| 28 | |
Holger Hans Peter Freyther | 5816bcf | 2014-04-04 11:43:09 +0200 | [diff] [blame] | 29 | #if defined(__FreeBSD__) |
| 30 | #include <sys/endian.h> |
| 31 | #endif |
| 32 | |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 33 | #include "../config.h" |
| 34 | #ifdef HAVE_STDINT_H |
| 35 | #include <stdint.h> |
| 36 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 37 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 38 | #include <stdio.h> |
| 39 | #include <stdarg.h> |
| 40 | #include <stdlib.h> |
| 41 | #include <sys/time.h> |
| 42 | #include <sys/types.h> |
| 43 | #include <sys/socket.h> |
| 44 | #include <netinet/in.h> |
| 45 | #include <arpa/inet.h> |
| 46 | #include <sys/stat.h> |
| 47 | #include <time.h> |
| 48 | #include <unistd.h> |
| 49 | #include <string.h> |
| 50 | #include <errno.h> |
| 51 | #include <fcntl.h> |
Alexander Huemer | db852a1 | 2015-11-06 20:59:01 +0100 | [diff] [blame] | 52 | #include <inttypes.h> |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 53 | |
| 54 | #include <arpa/inet.h> |
| 55 | |
jjako | bae2cd4 | 2004-01-09 12:04:39 +0000 | [diff] [blame] | 56 | /* #include <stdint.h> ISO C99 types */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 57 | |
| 58 | #include "pdp.h" |
| 59 | #include "gtp.h" |
| 60 | #include "gtpie.h" |
| 61 | #include "queue.h" |
| 62 | |
Harald Welte | 95848ba | 2011-11-02 18:17:50 +0100 | [diff] [blame] | 63 | /* According to section 14.2 of 3GPP TS 29.006 version 6.9.0 */ |
| 64 | #define N3_REQUESTS 5 |
| 65 | |
| 66 | #define T3_REQUEST 3 |
| 67 | |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 68 | /* Error reporting functions */ |
| 69 | |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 70 | #define GTP_LOGPKG(pri, peer, pack, len, fmt, args...) \ |
| 71 | logp2(DLGTP, pri, __FILE__, __LINE__, 0, \ |
| 72 | "Packet from %s:%u, length: %d content: %s: " fmt, \ |
| 73 | inet_ntoa((peer)->sin_addr), htons((peer)->sin_port), \ |
| 74 | len, osmo_hexdump((const uint8_t *) pack, len), \ |
| 75 | ##args); |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 76 | |
Neels Hofmeyr | 9b09738 | 2015-10-12 14:00:19 +0200 | [diff] [blame] | 77 | #define LOGP_WITH_ADDR(ss, level, addr, fmt, args...) \ |
| 78 | LOGP(ss, level, "addr(%s:%d) " fmt, \ |
| 79 | inet_ntoa((addr).sin_addr), htons((addr).sin_port), \ |
| 80 | ##args); |
| 81 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 82 | /* API Functions */ |
| 83 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 84 | const char *gtp_version() |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 85 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 86 | return VERSION; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Max | e661277 | 2018-01-11 18:25:37 +0100 | [diff] [blame] | 89 | const struct value_string gtp_type_names[] = { |
| 90 | { GTP_ECHO_REQ, "Echo Request" }, |
| 91 | { GTP_ECHO_RSP, "Echo Response" }, |
| 92 | { GTP_NOT_SUPPORTED, "Version Not Supported" }, |
| 93 | { GTP_ALIVE_REQ, "Node Alive Request" }, |
| 94 | { GTP_ALIVE_RSP, "Node Alive Response" }, |
| 95 | { GTP_REDIR_REQ, "Redirection Request" }, |
| 96 | { GTP_REDIR_RSP, "Redirection Response" }, |
| 97 | { GTP_CREATE_PDP_REQ, "Create PDP Context Request" }, |
| 98 | { GTP_CREATE_PDP_RSP, "Create PDP Context Response" }, |
| 99 | { GTP_UPDATE_PDP_REQ, "Update PDP Context Request" }, |
| 100 | { GTP_UPDATE_PDP_RSP, "Update PDP Context Response" }, |
| 101 | { GTP_DELETE_PDP_REQ, "Delete PDP Context Request" }, |
| 102 | { GTP_DELETE_PDP_RSP, "Delete PDP Context Response" }, |
| 103 | { GTP_ERROR, "Error Indication" }, |
| 104 | { GTP_PDU_NOT_REQ, "PDU Notification Request" }, |
| 105 | { GTP_PDU_NOT_RSP, "PDU Notification Response" }, |
| 106 | { GTP_PDU_NOT_REJ_REQ, "PDU Notification Reject Request" }, |
| 107 | { GTP_PDU_NOT_REJ_RSP, "PDU Notification Reject Response" }, |
| 108 | { GTP_SUPP_EXT_HEADER, "Supported Extension Headers Notification" }, |
| 109 | { GTP_SND_ROUTE_REQ, "Send Routeing Information for GPRS Request" }, |
| 110 | { GTP_SND_ROUTE_RSP, "Send Routeing Information for GPRS Response" }, |
| 111 | { GTP_FAILURE_REQ, "Failure Report Request" }, |
| 112 | { GTP_FAILURE_RSP, "Failure Report Response" }, |
| 113 | { GTP_MS_PRESENT_REQ, "Note MS GPRS Present Request" }, |
| 114 | { GTP_MS_PRESENT_RSP, "Note MS GPRS Present Response" }, |
| 115 | { GTP_IDEN_REQ, "Identification Request" }, |
| 116 | { GTP_IDEN_RSP, "Identification Response" }, |
| 117 | { GTP_SGSN_CONTEXT_REQ,"SGSN Context Request" }, |
| 118 | { GTP_SGSN_CONTEXT_RSP,"SGSN Context Response" }, |
| 119 | { GTP_SGSN_CONTEXT_ACK,"SGSN Context Acknowledge" }, |
| 120 | { GTP_FWD_RELOC_REQ, "Forward Relocation Request" }, |
| 121 | { GTP_FWD_RELOC_RSP, "Forward Relocation Response" }, |
| 122 | { GTP_FWD_RELOC_COMPL, "Forward Relocation Complete" }, |
| 123 | { GTP_RELOC_CANCEL_REQ,"Relocation Cancel Request" }, |
| 124 | { GTP_RELOC_CANCEL_RSP,"Relocation Cancel Response" }, |
| 125 | { GTP_FWD_SRNS, "Forward SRNS Context" }, |
| 126 | { GTP_FWD_RELOC_ACK, "Forward Relocation Complete Acknowledge" }, |
| 127 | { GTP_FWD_SRNS_ACK, "Forward SRNS Context Acknowledge" }, |
| 128 | { GTP_DATA_TRAN_REQ, "Data Record Transfer Request" }, |
| 129 | { GTP_DATA_TRAN_RSP, "Data Record Transfer Response" }, |
| 130 | { GTP_GPDU, "G-PDU" }, |
| 131 | { 0, NULL } |
| 132 | }; |
| 133 | |
Pau Espin Pedrol | eefa30d | 2019-05-31 15:42:49 +0200 | [diff] [blame] | 134 | /* Deprecated, use gtp_pdp_newpdp() instead */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 135 | int gtp_newpdp(struct gsn_t *gsn, struct pdp_t **pdp, |
| 136 | uint64_t imsi, uint8_t nsapi) |
| 137 | { |
Harald Welte | e257be1 | 2017-08-12 14:55:09 +0200 | [diff] [blame] | 138 | int rc; |
Pau Espin Pedrol | eefa30d | 2019-05-31 15:42:49 +0200 | [diff] [blame] | 139 | rc = gtp_pdp_newpdp(gsn, pdp, imsi, nsapi, NULL); |
Harald Welte | e257be1 | 2017-08-12 14:55:09 +0200 | [diff] [blame] | 140 | return rc; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 143 | int gtp_freepdp(struct gsn_t *gsn, struct pdp_t *pdp) |
| 144 | { |
Pau Espin Pedrol | 0d0b059 | 2019-05-29 20:42:09 +0200 | [diff] [blame] | 145 | if (gsn->cb_delete_context) |
| 146 | gsn->cb_delete_context(pdp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 147 | return pdp_freepdp(pdp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Pau Espin Pedrol | 0d0b059 | 2019-05-29 20:42:09 +0200 | [diff] [blame] | 150 | /* Free pdp and all its secondary PDP contexts. Must be called on the primary PDP context. */ |
| 151 | int gtp_freepdp_teardown(struct gsn_t *gsn, struct pdp_t *pdp) |
| 152 | { |
| 153 | int n; |
| 154 | struct pdp_t *secondary_pdp; |
| 155 | OSMO_ASSERT(!pdp->secondary); |
| 156 | |
| 157 | for (n = 0; n < PDP_MAXNSAPI; n++) { |
| 158 | if (pdp->secondary_tei[n]) { |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 159 | if (gtp_pdp_getgtp1(gsn, &secondary_pdp, |
| 160 | pdp->secondary_tei[n])) { |
Pau Espin Pedrol | 0d0b059 | 2019-05-29 20:42:09 +0200 | [diff] [blame] | 161 | LOGP(DLGTP, LOGL_ERROR, |
| 162 | "Unknown secondary PDP context\n"); |
| 163 | continue; |
| 164 | } |
| 165 | if (pdp != secondary_pdp) { |
| 166 | gtp_freepdp(gsn, secondary_pdp); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return gtp_freepdp(gsn, pdp); |
| 172 | } |
| 173 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 174 | /* gtp_gpdu */ |
| 175 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 176 | extern int gtp_fd(struct gsn_t *gsn) |
| 177 | { |
| 178 | return gsn->fd0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* gtp_decaps */ |
| 182 | /* gtp_retrans */ |
| 183 | /* gtp_retranstimeout */ |
| 184 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 185 | int gtp_set_cb_unsup_ind(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 186 | int (*cb) (struct sockaddr_in * peer)) |
| 187 | { |
| 188 | gsn->cb_unsup_ind = cb; |
| 189 | return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 190 | } |
| 191 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 192 | int gtp_set_cb_extheader_ind(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 193 | int (*cb) (struct sockaddr_in * peer)) |
| 194 | { |
| 195 | gsn->cb_extheader_ind = cb; |
| 196 | return 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 197 | } |
| 198 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 199 | /* API: Initialise delete context callback */ |
| 200 | /* Called whenever a pdp context is deleted for any reason */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 201 | int gtp_set_cb_delete_context(struct gsn_t *gsn, int (*cb) (struct pdp_t * pdp)) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 202 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 203 | gsn->cb_delete_context = cb; |
| 204 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 205 | } |
| 206 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 207 | int gtp_set_cb_conf(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 208 | int (*cb) (int type, int cause, |
| 209 | struct pdp_t * pdp, void *cbp)) |
| 210 | { |
| 211 | gsn->cb_conf = cb; |
| 212 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 215 | static void emit_cb_recovery(struct gsn_t *gsn, struct sockaddr_in * peer, |
| 216 | struct pdp_t * pdp, uint8_t recovery) |
| 217 | { |
| 218 | if (gsn->cb_recovery) |
| 219 | gsn->cb_recovery(peer, recovery); |
| 220 | if (gsn->cb_recovery2) |
| 221 | gsn->cb_recovery2(peer, pdp, recovery); |
| 222 | } |
| 223 | |
Harald Welte | 629e986 | 2010-12-24 20:58:09 +0100 | [diff] [blame] | 224 | int gtp_set_cb_recovery(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 225 | int (*cb) (struct sockaddr_in * peer, uint8_t recovery)) |
| 226 | { |
| 227 | gsn->cb_recovery = cb; |
| 228 | return 0; |
Harald Welte | 629e986 | 2010-12-24 20:58:09 +0100 | [diff] [blame] | 229 | } |
| 230 | |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 231 | /* cb_recovery() |
| 232 | * pdp may be NULL if Recovery IE was received from a message independent |
| 233 | * of any PDP ctx (such as Echo Response), or because pdp ctx is unknown to the |
| 234 | * local setup. In case pdp is known, caller may want to keep that pdp alive to |
| 235 | * handle subsequent msg cb as this specific pdp ctx is still valid according to |
| 236 | * specs. |
| 237 | */ |
| 238 | int gtp_set_cb_recovery2(struct gsn_t *gsn, |
| 239 | int (*cb_recovery2) (struct sockaddr_in * peer, struct pdp_t * pdp, uint8_t recovery)) |
| 240 | { |
| 241 | gsn->cb_recovery2 = cb_recovery2; |
| 242 | return 0; |
| 243 | } |
| 244 | |
Pau Espin Pedrol | 7b38af5 | 2018-01-25 18:35:33 +0100 | [diff] [blame] | 245 | int gtp_set_cb_data_ind(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 246 | int (*cb_data_ind) (struct pdp_t * pdp, |
| 247 | void *pack, unsigned len)) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 248 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 249 | gsn->cb_data_ind = cb_data_ind; |
| 250 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 251 | } |
| 252 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 253 | /** |
| 254 | * get_default_gtp() |
| 255 | * Generate a GPRS Tunneling Protocol signalling packet header, depending |
| 256 | * on GTP version and message type. pdp is used for teid/flow label. |
| 257 | * *packet must be allocated by the calling function, and be large enough |
| 258 | * to hold the packet header. |
| 259 | * returns the length of the header. 0 on error. |
| 260 | **/ |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 261 | static unsigned int get_default_gtp(uint8_t version, uint8_t type, void *packet) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 262 | { |
| 263 | struct gtp0_header *gtp0_default = (struct gtp0_header *)packet; |
| 264 | struct gtp1_header_long *gtp1_default = |
| 265 | (struct gtp1_header_long *)packet; |
| 266 | switch (version) { |
| 267 | case 0: |
| 268 | /* Initialise "standard" GTP0 header */ |
| 269 | memset(gtp0_default, 0, sizeof(struct gtp0_header)); |
| 270 | gtp0_default->flags = 0x1e; |
| 271 | gtp0_default->type = hton8(type); |
| 272 | gtp0_default->spare1 = 0xff; |
| 273 | gtp0_default->spare2 = 0xff; |
| 274 | gtp0_default->spare3 = 0xff; |
| 275 | gtp0_default->number = 0xff; |
| 276 | return GTP0_HEADER_SIZE; |
| 277 | case 1: |
| 278 | /* Initialise "standard" GTP1 header */ |
| 279 | /* 29.060: 8.2: S=1 and PN=0 */ |
| 280 | /* 29.060 9.3.1: For GTP-U messages Echo Request, Echo Response */ |
| 281 | /* and Supported Extension Headers Notification, the S field shall be */ |
| 282 | /* set to 1 */ |
| 283 | /* Currently extension headers are not supported */ |
| 284 | memset(gtp1_default, 0, sizeof(struct gtp1_header_long)); |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 285 | /* No extension, enable sequence, no N-PDU */ |
| 286 | gtp1_default->flags = GTPHDR_F_VER(1) | GTP1HDR_F_GTP1 | GTP1HDR_F_SEQ; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 287 | gtp1_default->type = hton8(type); |
| 288 | return GTP1_HEADER_SIZE_LONG; |
| 289 | default: |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 290 | LOGP(DLGTP, LOGL_ERROR, |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 291 | "Unknown GTP packet version: %d\n", version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 292 | return 0; |
| 293 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 294 | } |
| 295 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 296 | /** |
| 297 | * get_seq() |
| 298 | * Get sequence number of a packet. |
| 299 | * Returns 0 on error |
| 300 | **/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 301 | static uint16_t get_seq(void *pack) |
| 302 | { |
| 303 | union gtp_packet *packet = (union gtp_packet *)pack; |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 304 | uint8_t ver = GTPHDR_F_GET_VER(packet->flags); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 305 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 306 | if (ver == 0) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 307 | return ntoh16(packet->gtp0.h.seq); |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 308 | } else if (ver == 1 && (packet->flags & GTP1HDR_F_SEQ)) { /* Version 1 with seq */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 309 | return ntoh16(packet->gtp1l.h.seq); |
| 310 | } else { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 311 | return 0; |
| 312 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** |
| 316 | * get_tid() |
| 317 | * Get tunnel identifier of a packet. |
| 318 | * Returns 0 on error |
| 319 | **/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 320 | static uint64_t get_tid(void *pack) |
| 321 | { |
| 322 | union gtp_packet *packet = (union gtp_packet *)pack; |
| 323 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 324 | if (GTPHDR_F_GET_VER(packet->flags) == 0) { /* Version 0 */ |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 325 | return be64toh(packet->gtp0.h.tid); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 326 | } |
| 327 | return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
| 331 | * get_hlen() |
| 332 | * Get the header length of a packet. |
| 333 | * Returns 0 on error |
| 334 | **/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 335 | static uint16_t get_hlen(void *pack) |
| 336 | { |
| 337 | union gtp_packet *packet = (union gtp_packet *)pack; |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 338 | uint8_t ver = GTPHDR_F_GET_VER(packet->flags); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 339 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 340 | if (ver == 0) { /* Version 0 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 341 | return GTP0_HEADER_SIZE; |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 342 | } else if (ver == 1 && (packet->flags & 0x07) == 0) { /* Short version 1 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 343 | return GTP1_HEADER_SIZE_SHORT; |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 344 | } else if (ver == 1) { /* Version 1 with seq/n-pdu/ext */ |
| 345 | return GTP1_HEADER_SIZE_LONG; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 346 | } else { |
Harald Welte | d37b80a | 2016-12-15 18:33:15 +0100 | [diff] [blame] | 347 | LOGP(DLGTP, LOGL_ERROR, "Unknown packet flags: 0x%02x\n", packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 348 | return 0; |
| 349 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /** |
| 353 | * get_tei() |
| 354 | * Get the tunnel endpoint identifier (flow label) of a packet. |
| 355 | * Returns 0xffffffff on error. |
| 356 | **/ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 357 | static uint32_t get_tei(void *pack) |
| 358 | { |
| 359 | union gtp_packet *packet = (union gtp_packet *)pack; |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 360 | uint8_t ver = GTPHDR_F_GET_VER(packet->flags); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 361 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 362 | if (ver == 0) { /* Version 0 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 363 | return ntoh16(packet->gtp0.h.flow); |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 364 | } else if (ver == 1) { /* Version 1 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 365 | return ntoh32(packet->gtp1l.h.tei); |
| 366 | } else { |
Harald Welte | d37b80a | 2016-12-15 18:33:15 +0100 | [diff] [blame] | 367 | LOGP(DLGTP, LOGL_ERROR, "Unknown packet flags: 0x%02x\n", packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 368 | return 0xffffffff; |
| 369 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 370 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 371 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 372 | /* *********************************************************** |
| 373 | * Reliable delivery of signalling messages |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 374 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 375 | * Sequence numbers are used for both signalling messages and |
| 376 | * data messages. |
| 377 | * |
| 378 | * For data messages each tunnel maintains a sequence counter, |
| 379 | * which is incremented by one each time a new data message |
| 380 | * is sent. The sequence number starts at (0) zero at tunnel |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 381 | * establishment, and wraps around at 65535 (29.060 9.3.1.1 |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 382 | * and 09.60 8.1.1.1). The sequence numbers are either ignored, |
| 383 | * or can be used to check the validity of the message in the |
| 384 | * receiver, or for reordering af packets. |
| 385 | * |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 386 | * For signalling messages the sequence number is used by |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 387 | * signalling messages for which a response is defined. A response |
| 388 | * message should copy the sequence from the corresponding request |
| 389 | * message. The sequence number "unambiguously" identifies a request |
| 390 | * message within a given path, with a path being defined as a set of |
| 391 | * two endpoints (29.060 8.2, 29.060 7.6, 09.60 7.8). "All request |
| 392 | * messages shall be responded to, and all response messages associated |
| 393 | * with a certain request shall always include the same information" |
| 394 | * |
| 395 | * We take this to mean that the GSN transmitting a request is free to |
| 396 | * choose the sequence number, as long as it is unique within a given path. |
| 397 | * It means that we are allowed to count backwards, or roll over at 17 |
| 398 | * if we prefer that. It also means that we can use the same counter for |
| 399 | * all paths. This has the advantage that the transmitted request sequence |
| 400 | * numbers are unique within each GSN, and also we dont have to mess around |
| 401 | * with path setup and teardown. |
| 402 | * |
| 403 | * If a response message is lost, the request will be retransmitted, and |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 404 | * the receiving GSN will receive a "duplicated" request. The standard |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 405 | * requires the receiving GSN to send a response, with the same information |
| 406 | * as in the original response. For most messages this happens automatically: |
| 407 | * |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 408 | * Echo: Automatically duplicates the original response |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 409 | * Create pdp context: The SGSN may send create context request even if |
| 410 | * a context allready exist (imsi+nsapi?). This means that the reply will |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 411 | automatically duplicate the original response. It might however have |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 412 | * side effects in the application which is asked twice to validate |
| 413 | * the login. |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 414 | * Update pdp context: Automatically duplicates the original response??? |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 415 | * Delete pdp context. Automatically in gtp0, but in gtp1 will generate |
| 416 | * a nonexist reply message. |
| 417 | * |
| 418 | * The correct solution will be to make a queue containing response messages. |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 419 | * This queue should be checked whenever a request is received. If the |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 420 | * response is allready in the queue that response should be transmitted. |
| 421 | * It should be possible to find messages in this queue on the basis of |
| 422 | * the sequence number and peer GSN IP address (The sequense number is unique |
| 423 | * within each path). This need to be implemented by a hash table. Furthermore |
| 424 | * it should be possibly to delete messages based on a timeout. This can be |
| 425 | * achieved by means of a linked list. The timeout value need to be larger |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 426 | * than T3-RESPONSE * N3-REQUESTS (recommended value 5). These timers are |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 427 | * set in the peer GSN, so there is no way to know these parameters. On the |
| 428 | * other hand the timeout value need to be so small that we do not receive |
| 429 | * wraparound sequence numbere before the message is deleted. 60 seconds is |
| 430 | * probably not a bad choise. |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 431 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 432 | * This queue however is first really needed from gtp1. |
| 433 | * |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 434 | * gtp_req: |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 435 | * Send off a signalling message with appropiate sequence |
| 436 | * number. Store packet in queue. |
| 437 | * gtp_conf: |
| 438 | * Remove an incoming confirmation from the queue |
| 439 | * gtp_resp: |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 440 | * Send off a response to a request. Use the same sequence |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 441 | * number in the response as in the request. |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 442 | * gtp_notification: |
| 443 | * Send off a notification message. This is neither a request nor |
| 444 | * a response. Both TEI and SEQ are zero. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 445 | * gtp_retrans: |
| 446 | * Retransmit any outstanding packets which have exceeded |
| 447 | * a predefined timeout. |
| 448 | *************************************************************/ |
| 449 | |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 450 | static int gtp_req(struct gsn_t *gsn, uint8_t version, struct pdp_t *pdp, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 451 | union gtp_packet *packet, int len, |
| 452 | struct in_addr *inetaddr, void *cbp) |
| 453 | { |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 454 | uint8_t ver = GTPHDR_F_GET_VER(packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 455 | struct sockaddr_in addr; |
| 456 | struct qmsg_t *qmsg; |
| 457 | int fd; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 458 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 459 | memset(&addr, 0, sizeof(addr)); |
| 460 | addr.sin_family = AF_INET; |
| 461 | addr.sin_addr = *inetaddr; |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 462 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 463 | addr.sin_len = sizeof(addr); |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 464 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 465 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 466 | if (ver == 0) { /* Version 0 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 467 | addr.sin_port = htons(GTP0_PORT); |
| 468 | packet->gtp0.h.length = hton16(len - GTP0_HEADER_SIZE); |
| 469 | packet->gtp0.h.seq = hton16(gsn->seq_next); |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 470 | if (pdp) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 471 | packet->gtp0.h.tid = |
Pablo Neira Ayuso | 746b944 | 2014-03-24 17:58:27 +0100 | [diff] [blame] | 472 | htobe64(pdp_gettid(pdp->imsi, pdp->nsapi)); |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 473 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 474 | if (pdp && ((packet->gtp0.h.type == GTP_GPDU) |
| 475 | || (packet->gtp0.h.type == GTP_ERROR))) |
| 476 | packet->gtp0.h.flow = hton16(pdp->flru); |
| 477 | else if (pdp) |
| 478 | packet->gtp0.h.flow = hton16(pdp->flrc); |
| 479 | fd = gsn->fd0; |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 480 | } else if (ver == 1 && (packet->flags & GTP1HDR_F_SEQ)) { /* Version 1 with seq */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 481 | addr.sin_port = htons(GTP1C_PORT); |
| 482 | packet->gtp1l.h.length = hton16(len - GTP1_HEADER_SIZE_SHORT); |
| 483 | packet->gtp1l.h.seq = hton16(gsn->seq_next); |
| 484 | if (pdp && ((packet->gtp1l.h.type == GTP_GPDU) || |
| 485 | (packet->gtp1l.h.type == GTP_ERROR))) |
| 486 | packet->gtp1l.h.tei = hton32(pdp->teid_gn); |
| 487 | else if (pdp) |
| 488 | packet->gtp1l.h.tei = hton32(pdp->teic_gn); |
| 489 | fd = gsn->fd1c; |
| 490 | } else { |
Harald Welte | d37b80a | 2016-12-15 18:33:15 +0100 | [diff] [blame] | 491 | LOGP(DLGTP, LOGL_ERROR, "Unknown packet flags: 0x%02x\n", packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 492 | return -1; |
| 493 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 494 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 495 | if (sendto(fd, packet, len, 0, |
| 496 | (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 497 | gsn->err_sendto++; |
Max | 14b1b63 | 2017-08-21 20:14:59 +0200 | [diff] [blame] | 498 | LOGP(DLGTP, LOGL_ERROR, "Sendto(fd=%d, msg=%lx, len=%d, dst=%s) failed: Error = %s\n", fd, |
| 499 | (unsigned long)&packet, len, inet_ntoa(addr.sin_addr), strerror(errno)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 500 | return -1; |
| 501 | } |
| 502 | |
| 503 | /* Use new queue structure */ |
| 504 | if (queue_newmsg(gsn->queue_req, &qmsg, &addr, gsn->seq_next)) { |
| 505 | gsn->err_queuefull++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 506 | LOGP(DLGTP, LOGL_ERROR, |
| 507 | "Retransmit queue is full\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 508 | } else { |
| 509 | memcpy(&qmsg->p, packet, sizeof(union gtp_packet)); |
| 510 | qmsg->l = len; |
Harald Welte | 95848ba | 2011-11-02 18:17:50 +0100 | [diff] [blame] | 511 | qmsg->timeout = time(NULL) + T3_REQUEST; /* When to timeout */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 512 | qmsg->retrans = 0; /* No retransmissions so far */ |
| 513 | qmsg->cbp = cbp; |
| 514 | qmsg->type = ntoh8(packet->gtp0.h.type); |
| 515 | qmsg->fd = fd; |
| 516 | } |
| 517 | gsn->seq_next++; /* Count up this time */ |
| 518 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Alexander Couzens | e1412d9 | 2018-09-16 05:10:03 +0200 | [diff] [blame] | 521 | |
| 522 | /** |
| 523 | * @brief clear the request and response queue. Useful for debugging to reset "some" state. |
| 524 | * @param gsn The GGSN instance |
| 525 | */ |
| 526 | void gtp_clear_queues(struct gsn_t *gsn) |
| 527 | { |
| 528 | struct qmsg_t *qmsg; |
| 529 | |
| 530 | while (!queue_getfirst(gsn->queue_req, &qmsg)) { |
| 531 | queue_freemsg(gsn->queue_req, qmsg); |
| 532 | } |
| 533 | |
| 534 | while (!queue_getfirst(gsn->queue_resp, &qmsg)) { |
| 535 | queue_freemsg(gsn->queue_resp, qmsg); |
| 536 | } |
| 537 | } |
| 538 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 539 | /* gtp_conf |
| 540 | * Remove signalling packet from retransmission queue. |
| 541 | * return 0 on success, EOF if packet was not found */ |
| 542 | |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 543 | static int gtp_conf(struct gsn_t *gsn, uint8_t version, struct sockaddr_in *peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 544 | union gtp_packet *packet, int len, uint8_t * type, void **cbp) |
| 545 | { |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 546 | uint8_t ver = GTPHDR_F_GET_VER(packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 547 | uint16_t seq; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 548 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 549 | if (ver == 0) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 550 | seq = ntoh16(packet->gtp0.h.seq); |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 551 | else if (ver == 1 && (packet->gtp1l.h.flags & GTP1HDR_F_SEQ)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 552 | seq = ntoh16(packet->gtp1l.h.seq); |
| 553 | else { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 554 | GTP_LOGPKG(LOGL_ERROR, peer, packet, len, |
| 555 | "Unknown GTP packet version\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 556 | return EOF; |
| 557 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 558 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 559 | if (queue_freemsg_seq(gsn->queue_req, peer, seq, type, cbp)) { |
| 560 | gsn->err_seq++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 561 | GTP_LOGPKG(LOGL_ERROR, peer, packet, len, |
| 562 | "Confirmation packet not found in queue\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 563 | return EOF; |
| 564 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 565 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 566 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 569 | int gtp_retrans(struct gsn_t *gsn) |
| 570 | { |
| 571 | /* Retransmit any outstanding packets */ |
| 572 | /* Remove from queue if maxretrans exceeded */ |
| 573 | time_t now; |
| 574 | struct qmsg_t *qmsg; |
| 575 | now = time(NULL); |
| 576 | /*printf("Retrans: New beginning %d\n", (int) now); */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 577 | |
Harald Welte | 95848ba | 2011-11-02 18:17:50 +0100 | [diff] [blame] | 578 | /* get first element in queue, as long as the timeout of that |
| 579 | * element has expired */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 580 | while ((!queue_getfirst(gsn->queue_req, &qmsg)) && |
| 581 | (qmsg->timeout <= now)) { |
| 582 | /*printf("Retrans timeout found: %d\n", (int) time(NULL)); */ |
Harald Welte | 95848ba | 2011-11-02 18:17:50 +0100 | [diff] [blame] | 583 | if (qmsg->retrans > N3_REQUESTS) { /* To many retrans */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 584 | if (gsn->cb_conf) |
| 585 | gsn->cb_conf(qmsg->type, EOF, NULL, qmsg->cbp); |
| 586 | queue_freemsg(gsn->queue_req, qmsg); |
| 587 | } else { |
| 588 | if (sendto(qmsg->fd, &qmsg->p, qmsg->l, 0, |
| 589 | (struct sockaddr *)&qmsg->peer, |
| 590 | sizeof(struct sockaddr_in)) < 0) { |
| 591 | gsn->err_sendto++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 592 | LOGP(DLGTP, LOGL_ERROR, |
| 593 | "Sendto(fd0=%d, msg=%lx, len=%d) failed: Error = %s\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 594 | gsn->fd0, (unsigned long)&qmsg->p, |
| 595 | qmsg->l, strerror(errno)); |
| 596 | } |
| 597 | queue_back(gsn->queue_req, qmsg); |
Harald Welte | 95848ba | 2011-11-02 18:17:50 +0100 | [diff] [blame] | 598 | qmsg->timeout = now + T3_REQUEST; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 599 | qmsg->retrans++; |
| 600 | } |
| 601 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 602 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 603 | /* Also clean up reply timeouts */ |
| 604 | while ((!queue_getfirst(gsn->queue_resp, &qmsg)) && |
| 605 | (qmsg->timeout < now)) { |
| 606 | /*printf("Retrans (reply) timeout found: %d\n", (int) time(NULL)); */ |
| 607 | queue_freemsg(gsn->queue_resp, qmsg); |
| 608 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 609 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 610 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 613 | int gtp_retranstimeout(struct gsn_t *gsn, struct timeval *timeout) |
| 614 | { |
Pau Espin Pedrol | ceac078 | 2019-05-27 17:39:45 +0200 | [diff] [blame] | 615 | time_t now, later, diff; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 616 | struct qmsg_t *qmsg; |
Pau Espin Pedrol | ceac078 | 2019-05-27 17:39:45 +0200 | [diff] [blame] | 617 | timeout->tv_usec = 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 618 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 619 | if (queue_getfirst(gsn->queue_req, &qmsg)) { |
| 620 | timeout->tv_sec = 10; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 621 | } else { |
| 622 | now = time(NULL); |
| 623 | later = qmsg->timeout; |
| 624 | timeout->tv_sec = later - now; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 625 | if (timeout->tv_sec < 0) |
| 626 | timeout->tv_sec = 0; /* No negative allowed */ |
| 627 | if (timeout->tv_sec > 10) |
| 628 | timeout->tv_sec = 10; /* Max sleep for 10 sec */ |
| 629 | } |
Pau Espin Pedrol | ceac078 | 2019-05-27 17:39:45 +0200 | [diff] [blame] | 630 | |
| 631 | if (queue_getfirst(gsn->queue_resp, &qmsg)) { |
| 632 | /* already set by queue_req, do nothing */ |
| 633 | } else { /* trigger faster if earlier timeout exists in queue_resp */ |
| 634 | now = time(NULL); |
| 635 | later = qmsg->timeout; |
| 636 | diff = later - now; |
| 637 | if (diff < 0) |
| 638 | diff = 0; |
| 639 | if (diff < timeout->tv_sec) |
| 640 | timeout->tv_sec = diff; |
| 641 | } |
| 642 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 643 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 646 | static int gtp_resp(uint8_t version, struct gsn_t *gsn, struct pdp_t *pdp, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 647 | union gtp_packet *packet, int len, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 648 | struct sockaddr_in *peer, int fd, uint16_t seq, uint64_t tid) |
| 649 | { |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 650 | uint8_t ver = GTPHDR_F_GET_VER(packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 651 | struct qmsg_t *qmsg; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 652 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 653 | if (ver == 0) { /* Version 0 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 654 | packet->gtp0.h.length = hton16(len - GTP0_HEADER_SIZE); |
| 655 | packet->gtp0.h.seq = hton16(seq); |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 656 | packet->gtp0.h.tid = htobe64(tid); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 657 | if (pdp && ((packet->gtp0.h.type == GTP_GPDU) || |
| 658 | (packet->gtp0.h.type == GTP_ERROR))) |
| 659 | packet->gtp0.h.flow = hton16(pdp->flru); |
| 660 | else if (pdp) |
| 661 | packet->gtp0.h.flow = hton16(pdp->flrc); |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 662 | } else if (ver == 1 && (packet->flags & GTP1HDR_F_SEQ)) { /* Version 1 with seq */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 663 | packet->gtp1l.h.length = hton16(len - GTP1_HEADER_SIZE_SHORT); |
| 664 | packet->gtp1l.h.seq = hton16(seq); |
| 665 | if (pdp && (fd == gsn->fd1u)) |
| 666 | packet->gtp1l.h.tei = hton32(pdp->teid_gn); |
| 667 | else if (pdp) |
| 668 | packet->gtp1l.h.tei = hton32(pdp->teic_gn); |
| 669 | } else { |
Harald Welte | d37b80a | 2016-12-15 18:33:15 +0100 | [diff] [blame] | 670 | LOGP(DLGTP, LOGL_ERROR, "Unknown packet flags: 0x%02x\n", packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 671 | return -1; |
| 672 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 673 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 674 | if (fcntl(fd, F_SETFL, 0)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 675 | LOGP(DLGTP, LOGL_ERROR, "fnctl()\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 676 | return -1; |
| 677 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 678 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 679 | if (sendto(fd, packet, len, 0, |
| 680 | (struct sockaddr *)peer, sizeof(struct sockaddr_in)) < 0) { |
| 681 | gsn->err_sendto++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 682 | LOGP(DLGTP, LOGL_ERROR, |
| 683 | "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s\n", fd, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 684 | (unsigned long)&packet, len, strerror(errno)); |
| 685 | return -1; |
| 686 | } |
| 687 | |
| 688 | /* Use new queue structure */ |
| 689 | if (queue_newmsg(gsn->queue_resp, &qmsg, peer, seq)) { |
| 690 | gsn->err_queuefull++; |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 691 | LOGP(DLGTP, LOGL_ERROR, "Retransmit queue is full\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 692 | } else { |
| 693 | memcpy(&qmsg->p, packet, sizeof(union gtp_packet)); |
| 694 | qmsg->l = len; |
| 695 | qmsg->timeout = time(NULL) + 60; /* When to timeout */ |
| 696 | qmsg->retrans = 0; /* No retransmissions so far */ |
| 697 | qmsg->cbp = NULL; |
| 698 | qmsg->type = 0; |
| 699 | qmsg->fd = fd; |
| 700 | } |
| 701 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 704 | static int gtp_notification(struct gsn_t *gsn, uint8_t version, |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 705 | union gtp_packet *packet, int len, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 706 | struct sockaddr_in *peer, int fd, uint16_t seq) |
| 707 | { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 708 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 709 | uint8_t ver = GTPHDR_F_GET_VER(packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 710 | struct sockaddr_in addr; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 711 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 712 | memcpy(&addr, peer, sizeof(addr)); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 713 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 714 | /* In GTP0 notifications are treated as replies. In GTP1 they |
| 715 | are requests for which there is no reply */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 716 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 717 | if (fd == gsn->fd1c) |
| 718 | addr.sin_port = htons(GTP1C_PORT); |
| 719 | else if (fd == gsn->fd1u) |
| 720 | addr.sin_port = htons(GTP1C_PORT); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 721 | |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 722 | if (ver == 0) { /* Version 0 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 723 | packet->gtp0.h.length = hton16(len - GTP0_HEADER_SIZE); |
| 724 | packet->gtp0.h.seq = hton16(seq); |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 725 | } else if (ver == 1 && (packet->flags & GTP1HDR_F_SEQ)) { /* Version 1 with seq */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 726 | packet->gtp1l.h.length = hton16(len - GTP1_HEADER_SIZE_SHORT); |
| 727 | packet->gtp1l.h.seq = hton16(seq); |
| 728 | } else { |
Harald Welte | d37b80a | 2016-12-15 18:33:15 +0100 | [diff] [blame] | 729 | LOGP(DLGTP, LOGL_ERROR, "Unknown packet flags: 0x%02x\n", packet->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 730 | return -1; |
| 731 | } |
| 732 | |
| 733 | if (fcntl(fd, F_SETFL, 0)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 734 | LOGP(DLGTP, LOGL_ERROR, "fnctl()\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 735 | return -1; |
| 736 | } |
| 737 | |
| 738 | if (sendto(fd, packet, len, 0, |
| 739 | (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { |
| 740 | gsn->err_sendto++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 741 | LOGP(DLGTP, LOGL_ERROR, |
| 742 | "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s\n", fd, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 743 | (unsigned long)&packet, len, strerror(errno)); |
| 744 | return -1; |
| 745 | } |
| 746 | return 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 749 | static int gtp_duplicate(struct gsn_t *gsn, uint8_t version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 750 | struct sockaddr_in *peer, uint16_t seq) |
| 751 | { |
| 752 | struct qmsg_t *qmsg; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 753 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 754 | if (queue_seqget(gsn->queue_resp, &qmsg, peer, seq)) { |
| 755 | return EOF; /* Notfound */ |
| 756 | } |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 757 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 758 | if (fcntl(qmsg->fd, F_SETFL, 0)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 759 | LOGP(DLGTP, LOGL_ERROR, "fnctl()\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 760 | return -1; |
| 761 | } |
| 762 | |
| 763 | if (sendto(qmsg->fd, &qmsg->p, qmsg->l, 0, |
| 764 | (struct sockaddr *)peer, sizeof(struct sockaddr_in)) < 0) { |
| 765 | gsn->err_sendto++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 766 | LOGP(DLGTP, LOGL_ERROR, |
| 767 | "Sendto(fd=%d, msg=%lx, len=%d) failed: Error = %s\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 768 | qmsg->fd, (unsigned long)&qmsg->p, qmsg->l, |
| 769 | strerror(errno)); |
| 770 | } |
| 771 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 772 | } |
| 773 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 774 | /* Perform restoration and recovery error handling as described in 29.060 */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 775 | static void log_restart(struct gsn_t *gsn) |
| 776 | { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 777 | FILE *f; |
Emmanuel Bretelle | 111e054 | 2010-09-06 19:49:16 +0200 | [diff] [blame] | 778 | int i, rc; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 779 | int counter = 0; |
Neels Hofmeyr | f7611c3 | 2016-08-18 03:53:09 +0200 | [diff] [blame] | 780 | char *filename; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 781 | |
Neels Hofmeyr | f7611c3 | 2016-08-18 03:53:09 +0200 | [diff] [blame] | 782 | filename = talloc_asprintf(NULL, "%s/%s", gsn->statedir, RESTART_FILE); |
| 783 | OSMO_ASSERT(filename); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 784 | |
| 785 | /* We try to open file. On failure we will later try to create file */ |
| 786 | if (!(f = fopen(filename, "r"))) { |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 787 | LOGP(DLGTP, LOGL_NOTICE, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 788 | "State information file (%s) not found. Creating new file.\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 789 | filename); |
| 790 | } else { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 791 | rc = fscanf(f, "%d", &counter); |
| 792 | if (rc != 1) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 793 | LOGP(DLGTP, LOGL_ERROR, |
| 794 | "fscanf failed to read counter value\n"); |
Holger Hans Peter Freyther | 8ddb680 | 2016-01-23 10:40:52 +0100 | [diff] [blame] | 795 | goto close_file; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 796 | } |
| 797 | if (fclose(f)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 798 | LOGP(DLGTP, LOGL_ERROR, |
| 799 | "fclose failed: Error = %s\n", strerror(errno)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 800 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 801 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 802 | |
| 803 | gsn->restart_counter = (unsigned char)counter; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 804 | gsn->restart_counter++; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 805 | |
Neels Hofmeyr | f41f586 | 2016-09-19 03:35:53 +0200 | [diff] [blame] | 806 | /* Keep the umask closely wrapped around our fopen() call in case the |
| 807 | * log outputs cause file creation. */ |
| 808 | i = umask(022); |
| 809 | f = fopen(filename, "w"); |
| 810 | umask(i); |
| 811 | if (!f) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 812 | LOGP(DLGTP, LOGL_ERROR, |
| 813 | "fopen(path=%s, mode=%s) failed: Error = %s\n", filename, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 814 | "w", strerror(errno)); |
Neels Hofmeyr | f7611c3 | 2016-08-18 03:53:09 +0200 | [diff] [blame] | 815 | goto free_filename; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 816 | } |
| 817 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 818 | fprintf(f, "%d\n", gsn->restart_counter); |
Holger Hans Peter Freyther | 8ddb680 | 2016-01-23 10:40:52 +0100 | [diff] [blame] | 819 | close_file: |
Neels Hofmeyr | f7611c3 | 2016-08-18 03:53:09 +0200 | [diff] [blame] | 820 | if (fclose(f)) |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 821 | LOGP(DLGTP, LOGL_ERROR, |
| 822 | "fclose failed: Error = %s\n", strerror(errno)); |
Neels Hofmeyr | f7611c3 | 2016-08-18 03:53:09 +0200 | [diff] [blame] | 823 | free_filename: |
| 824 | talloc_free(filename); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 825 | } |
| 826 | |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 827 | int gtp_new(struct gsn_t **gsn, char *statedir, struct in_addr *listen, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 828 | int mode) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 829 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 830 | struct sockaddr_in addr; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 831 | |
Max | 14b1b63 | 2017-08-21 20:14:59 +0200 | [diff] [blame] | 832 | LOGP(DLGTP, LOGL_NOTICE, "GTP: gtp_newgsn() started at %s\n", inet_ntoa(*listen)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 833 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 834 | *gsn = calloc(sizeof(struct gsn_t), 1); /* TODO */ |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 835 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 836 | (*gsn)->statedir = statedir; |
| 837 | log_restart(*gsn); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 838 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 839 | /* Initialise sequence number */ |
| 840 | (*gsn)->seq_next = (*gsn)->restart_counter * 1024; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 841 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 842 | /* Initialise request retransmit queue */ |
| 843 | queue_new(&(*gsn)->queue_req); |
| 844 | queue_new(&(*gsn)->queue_resp); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 845 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 846 | /* Initialise pdp table */ |
Pau Espin Pedrol | eefa30d | 2019-05-31 15:42:49 +0200 | [diff] [blame] | 847 | pdp_init(*gsn); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 848 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 849 | /* Initialise call back functions */ |
| 850 | (*gsn)->cb_create_context_ind = 0; |
| 851 | (*gsn)->cb_delete_context = 0; |
| 852 | (*gsn)->cb_unsup_ind = 0; |
| 853 | (*gsn)->cb_conf = 0; |
| 854 | (*gsn)->cb_data_ind = 0; |
| 855 | |
| 856 | /* Store function parameters */ |
| 857 | (*gsn)->gsnc = *listen; |
| 858 | (*gsn)->gsnu = *listen; |
| 859 | (*gsn)->mode = mode; |
| 860 | |
| 861 | /* Create GTP version 0 socket */ |
| 862 | if (((*gsn)->fd0 = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 863 | (*gsn)->err_socket++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 864 | LOGP(DLGTP, LOGL_ERROR, |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 865 | "GTPv0 socket(domain=%d, type=%d, protocol=%d) failed: Error = %s\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 866 | AF_INET, SOCK_DGRAM, 0, strerror(errno)); |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 867 | return -errno; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | memset(&addr, 0, sizeof(addr)); |
| 871 | addr.sin_family = AF_INET; |
| 872 | addr.sin_addr = *listen; /* Same IP for user traffic and signalling */ |
| 873 | addr.sin_port = htons(GTP0_PORT); |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 874 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 875 | addr.sin_len = sizeof(addr); |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 876 | #endif |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 877 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 878 | if (bind((*gsn)->fd0, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 879 | (*gsn)->err_socket++; |
Neels Hofmeyr | 9b09738 | 2015-10-12 14:00:19 +0200 | [diff] [blame] | 880 | LOGP_WITH_ADDR(DLGTP, LOGL_ERROR, addr, |
| 881 | "bind(fd0=%d) failed: Error = %s\n", |
| 882 | (*gsn)->fd0, strerror(errno)); |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 883 | return -errno; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 884 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 885 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 886 | /* Create GTP version 1 control plane socket */ |
| 887 | if (((*gsn)->fd1c = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 888 | (*gsn)->err_socket++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 889 | LOGP(DLGTP, LOGL_ERROR, |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 890 | "GTPv1 control plane socket(domain=%d, type=%d, protocol=%d) failed: Error = %s\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 891 | AF_INET, SOCK_DGRAM, 0, strerror(errno)); |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 892 | return -errno; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | memset(&addr, 0, sizeof(addr)); |
| 896 | addr.sin_family = AF_INET; |
| 897 | addr.sin_addr = *listen; /* Same IP for user traffic and signalling */ |
| 898 | addr.sin_port = htons(GTP1C_PORT); |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 899 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 900 | addr.sin_len = sizeof(addr); |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 901 | #endif |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 902 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 903 | if (bind((*gsn)->fd1c, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 904 | (*gsn)->err_socket++; |
Neels Hofmeyr | 9b09738 | 2015-10-12 14:00:19 +0200 | [diff] [blame] | 905 | LOGP_WITH_ADDR(DLGTP, LOGL_ERROR, addr, |
| 906 | "bind(fd1c=%d) failed: Error = %s\n", |
| 907 | (*gsn)->fd1c, strerror(errno)); |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 908 | return -errno; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 909 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 910 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 911 | /* Create GTP version 1 user plane socket */ |
| 912 | if (((*gsn)->fd1u = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { |
| 913 | (*gsn)->err_socket++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 914 | LOGP(DLGTP, LOGL_ERROR, |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 915 | "GTPv1 user plane socket(domain=%d, type=%d, protocol=%d) failed: Error = %s\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 916 | AF_INET, SOCK_DGRAM, 0, strerror(errno)); |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 917 | return -errno; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | memset(&addr, 0, sizeof(addr)); |
| 921 | addr.sin_family = AF_INET; |
| 922 | addr.sin_addr = *listen; /* Same IP for user traffic and signalling */ |
| 923 | addr.sin_port = htons(GTP1U_PORT); |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 924 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 925 | addr.sin_len = sizeof(addr); |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 926 | #endif |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 927 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 928 | if (bind((*gsn)->fd1u, (struct sockaddr *)&addr, sizeof(addr)) < 0) { |
| 929 | (*gsn)->err_socket++; |
Neels Hofmeyr | 9b09738 | 2015-10-12 14:00:19 +0200 | [diff] [blame] | 930 | LOGP_WITH_ADDR(DLGTP, LOGL_ERROR, addr, |
Neels Hofmeyr | e845cb9 | 2015-10-12 14:00:22 +0200 | [diff] [blame] | 931 | "bind(fd1u=%d) failed: Error = %s\n", |
| 932 | (*gsn)->fd1u, strerror(errno)); |
Max | 2831887 | 2017-05-16 17:03:02 +0200 | [diff] [blame] | 933 | return -errno; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 937 | } |
| 938 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 939 | int gtp_free(struct gsn_t *gsn) |
| 940 | { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 941 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 942 | /* Clean up retransmit queues */ |
| 943 | queue_free(gsn->queue_req); |
| 944 | queue_free(gsn->queue_resp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 945 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 946 | close(gsn->fd0); |
| 947 | close(gsn->fd1c); |
| 948 | close(gsn->fd1u); |
| 949 | |
| 950 | free(gsn); |
| 951 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /* *********************************************************** |
| 955 | * Path management messages |
| 956 | * Messages: echo and version not supported. |
| 957 | * A path is connection between two UDP/IP endpoints |
| 958 | * |
| 959 | * A path is either using GTP0 or GTP1. A path can be |
| 960 | * established by any kind of GTP message?? |
| 961 | |
| 962 | * Which source port to use? |
| 963 | * GTP-C request destination port is 2123/3386 |
| 964 | * GTP-U request destination port is 2152/3386 |
| 965 | * T-PDU destination port is 2152/3386. |
| 966 | * For the above messages the source port is locally allocated. |
| 967 | * For response messages src=rx-dst and dst=rx-src. |
| 968 | * For simplicity we should probably use 2123+2152/3386 as |
| 969 | * src port even for the cases where src can be locally |
| 970 | * allocated. This also means that we have to listen only to |
| 971 | * the same ports. |
| 972 | * For response messages we need to be able to respond to |
| 973 | * the relevant src port even if it is locally allocated by |
| 974 | * the peer. |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 975 | * |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 976 | * The need for path management! |
| 977 | * We might need to keep a list of active paths. This might |
| 978 | * be in the form of remote IP address + UDP port numbers. |
| 979 | * (We will consider a path astablished if we have a context |
| 980 | * with the node in question) |
| 981 | *************************************************************/ |
| 982 | |
| 983 | /* Send off an echo request */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 984 | int gtp_echo_req(struct gsn_t *gsn, int version, void *cbp, |
| 985 | struct in_addr *inetaddr) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 986 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 987 | union gtp_packet packet; |
| 988 | unsigned int length = get_default_gtp(version, GTP_ECHO_REQ, &packet); |
| 989 | return gtp_req(gsn, version, NULL, &packet, length, inetaddr, cbp); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 990 | } |
| 991 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 992 | /* Send off an echo reply */ |
| 993 | int gtp_echo_resp(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 994 | struct sockaddr_in *peer, int fd, void *pack, unsigned len) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 995 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 996 | union gtp_packet packet; |
| 997 | unsigned int length = get_default_gtp(version, GTP_ECHO_RSP, &packet); |
| 998 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 999 | gsn->restart_counter); |
| 1000 | return gtp_resp(version, gsn, NULL, &packet, length, peer, fd, |
| 1001 | get_seq(pack), get_tid(pack)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1004 | /* Handle a received echo request */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1005 | int gtp_echo_ind(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
| 1006 | int fd, void *pack, unsigned len) |
| 1007 | { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1008 | |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 1009 | /* Check if it was a duplicate request */ |
| 1010 | if (!gtp_duplicate(gsn, 0, peer, get_seq(pack))) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1011 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1012 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1013 | /* Send off reply to request */ |
| 1014 | return gtp_echo_resp(gsn, version, peer, fd, pack, len); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
| 1017 | /* Handle a received echo reply */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1018 | int gtp_echo_conf(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1019 | void *pack, unsigned len) |
| 1020 | { |
| 1021 | union gtpie_member *ie[GTPIE_SIZE]; |
| 1022 | unsigned char recovery; |
| 1023 | void *cbp = NULL; |
| 1024 | uint8_t type = 0; |
| 1025 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1026 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1027 | /* Remove packet from queue */ |
| 1028 | if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) |
| 1029 | return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1030 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1031 | /* Extract information elements into a pointer array */ |
| 1032 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 1033 | gsn->invalid++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1034 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1035 | "Invalid message format\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1036 | if (gsn->cb_conf) |
| 1037 | gsn->cb_conf(type, EOF, NULL, cbp); |
| 1038 | return EOF; |
| 1039 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1040 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1041 | if (gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
| 1042 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1043 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1044 | "Missing mandatory field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1045 | if (gsn->cb_conf) |
| 1046 | gsn->cb_conf(type, EOF, NULL, cbp); |
| 1047 | return EOF; |
| 1048 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1049 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1050 | /* Echo reply packages does not have a cause information element */ |
| 1051 | /* Instead we return the recovery number in the callback function */ |
| 1052 | if (gsn->cb_conf) |
| 1053 | gsn->cb_conf(type, recovery, NULL, cbp); |
Harald Welte | 629e986 | 2010-12-24 20:58:09 +0100 | [diff] [blame] | 1054 | |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 1055 | emit_cb_recovery(gsn, peer, NULL, recovery); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1056 | |
| 1057 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | /* Send off a Version Not Supported message */ |
| 1061 | /* This message is somewhat special in that it actually is a |
| 1062 | * response to some other message with unsupported GTP version |
| 1063 | * For this reason it has parameters like a response, and does |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 1064 | * its own message transmission. No signalling queue is used |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1065 | * The reply is sent to the peer IP and peer UDP. This means that |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 1066 | * the peer will be receiving a GTP0 message on a GTP1 port! |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1067 | * In practice however this will never happen as a GTP0 GSN will |
| 1068 | * only listen to the GTP0 port, and therefore will never receive |
| 1069 | * anything else than GTP0 */ |
| 1070 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1071 | int gtp_unsup_req(struct gsn_t *gsn, int version, struct sockaddr_in *peer, |
| 1072 | int fd, void *pack, unsigned len) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1073 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1074 | union gtp_packet packet; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1075 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1076 | /* GTP 1 is the highest supported protocol */ |
| 1077 | unsigned int length = get_default_gtp(1, GTP_NOT_SUPPORTED, &packet); |
| 1078 | return gtp_notification(gsn, version, &packet, length, peer, fd, 0); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /* Handle a Version Not Supported message */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1082 | int gtp_unsup_ind(struct gsn_t *gsn, struct sockaddr_in *peer, |
| 1083 | void *pack, unsigned len) |
| 1084 | { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1085 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1086 | if (gsn->cb_unsup_ind) |
| 1087 | gsn->cb_unsup_ind(peer); |
| 1088 | |
| 1089 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1090 | } |
| 1091 | |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1092 | /* Send off an Supported Extension Headers Notification */ |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 1093 | static int gtp_extheader_req(struct gsn_t *gsn, uint8_t version, struct sockaddr_in *peer, |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1094 | int fd, void *pack, unsigned len) |
| 1095 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1096 | union gtp_packet packet; |
| 1097 | unsigned int length = |
| 1098 | get_default_gtp(version, GTP_SUPP_EXT_HEADER, &packet); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1099 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1100 | uint8_t pdcp_pdu = GTP_EXT_PDCP_PDU; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1101 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1102 | if (version < 1) |
| 1103 | return 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1104 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1105 | /* We report back that we support only PDCP PDU headers */ |
| 1106 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EXT_HEADER_T, |
| 1107 | sizeof(pdcp_pdu), &pdcp_pdu); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1108 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1109 | return gtp_notification(gsn, version, &packet, length, |
| 1110 | peer, fd, get_seq(pack)); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | /* Handle a Supported Extension Headers Notification */ |
Pau Espin Pedrol | 7b38af5 | 2018-01-25 18:35:33 +0100 | [diff] [blame] | 1114 | static int gtp_extheader_ind(struct gsn_t *gsn, struct sockaddr_in *peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1115 | void *pack, unsigned len) |
| 1116 | { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1117 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1118 | if (gsn->cb_extheader_ind) |
| 1119 | gsn->cb_extheader_ind(peer); |
| 1120 | |
| 1121 | return 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1124 | /* *********************************************************** |
| 1125 | * Session management messages |
| 1126 | * Messages: create, update and delete PDP context |
| 1127 | * |
| 1128 | * Information storage |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 1129 | * Information storage for each PDP context is defined in |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1130 | * 23.060 section 13.3. Includes IMSI, MSISDN, APN, PDP-type, |
| 1131 | * PDP-address (IP address), sequence numbers, charging ID. |
| 1132 | * For the SGSN it also includes radio related mobility |
| 1133 | * information. |
| 1134 | *************************************************************/ |
| 1135 | |
Harald Welte | 7b3347b | 2010-05-15 12:18:46 +0200 | [diff] [blame] | 1136 | /* API: Send Create PDP Context Request (7.3.1) */ |
Pau Espin Pedrol | 7b38af5 | 2018-01-25 18:35:33 +0100 | [diff] [blame] | 1137 | int gtp_create_context_req(struct gsn_t *gsn, struct pdp_t *pdp, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1138 | void *cbp) |
| 1139 | { |
| 1140 | union gtp_packet packet; |
| 1141 | unsigned int length = |
| 1142 | get_default_gtp(pdp->version, GTP_CREATE_PDP_REQ, &packet); |
| 1143 | struct pdp_t *linked_pdp = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1144 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1145 | /* TODO: Secondary PDP Context Activation Procedure */ |
| 1146 | /* In secondary activation procedure the PDP context is identified |
| 1147 | by tei in the header. The following fields are omitted: Selection |
| 1148 | mode, IMSI, MSISDN, End User Address, Access Point Name and |
| 1149 | Protocol Configuration Options */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1150 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1151 | if (pdp->secondary) { |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 1152 | if (gtp_pdp_getgtp1(gsn, &linked_pdp, pdp->teic_own)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1153 | LOGP(DLGTP, LOGL_ERROR, |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 1154 | "Unknown linked PDP context: %u\n", pdp->teic_own); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1155 | return EOF; |
| 1156 | } |
| 1157 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1158 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1159 | if (pdp->version == 0) { |
| 1160 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0, |
| 1161 | sizeof(pdp->qos_req0), pdp->qos_req0); |
| 1162 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1163 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1164 | /* Section 7.7.2 */ |
| 1165 | if (pdp->version == 1) { |
| 1166 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1167 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_IMSI, |
| 1168 | sizeof(pdp->imsi), (uint8_t *) & pdp->imsi); |
| 1169 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1170 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1171 | /* Section 7.7.3 Routing Area Information */ |
| 1172 | if (pdp->rai_given == 1) |
| 1173 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_RAI, |
| 1174 | pdp->rai.l, (uint8_t *) & pdp->rai.v); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 1175 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1176 | /* Section 7.7.11 */ |
| 1177 | if (pdp->norecovery_given == 0) |
| 1178 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 1179 | gsn->restart_counter); |
Harald Welte | 41af569 | 2011-10-07 18:42:34 +0200 | [diff] [blame] | 1180 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1181 | /* Section 7.7.12 */ |
| 1182 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1183 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_SELECTION_MODE, |
| 1184 | pdp->selmode); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1185 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1186 | if (pdp->version == 0) { |
| 1187 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI, pdp->fllu); |
| 1188 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C, pdp->fllc); |
| 1189 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1190 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1191 | /* Section 7.7.13 */ |
| 1192 | if (pdp->version == 1) { |
| 1193 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI, |
| 1194 | pdp->teid_own); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1195 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1196 | /* Section 7.7.14 */ |
| 1197 | if (!pdp->teic_confirmed) |
| 1198 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C, |
| 1199 | pdp->teic_own); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1200 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1201 | /* Section 7.7.17 */ |
| 1202 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI, pdp->nsapi); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1203 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1204 | /* Section 7.7.17 */ |
| 1205 | if (pdp->secondary) /* Secondary PDP Context Activation Procedure */ |
| 1206 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI, |
| 1207 | linked_pdp->nsapi); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1208 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1209 | /* Section 7.7.23 */ |
| 1210 | if (pdp->cch_pdp) /* Only include charging if flags are set */ |
| 1211 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_CHARGING_C, |
| 1212 | pdp->cch_pdp); |
| 1213 | } |
jjako | 9b4971d | 2004-05-27 20:30:19 +0000 | [diff] [blame] | 1214 | |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 1215 | /* TODO |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1216 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_REF, |
| 1217 | pdp->traceref); |
| 1218 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_TYPE, |
| 1219 | pdp->tracetype); */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1220 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1221 | /* Section 7.7.27 */ |
| 1222 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1223 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA, |
| 1224 | pdp->eua.l, pdp->eua.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1225 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1226 | /* Section 7.7.30 */ |
| 1227 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1228 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_APN, |
| 1229 | pdp->apn_use.l, pdp->apn_use.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1230 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1231 | /* Section 7.7.31 */ |
| 1232 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1233 | if (pdp->pco_req.l) |
| 1234 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_PCO, |
| 1235 | pdp->pco_req.l, pdp->pco_req.v); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1236 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1237 | /* Section 7.7.32 */ |
| 1238 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1239 | pdp->gsnlc.l, pdp->gsnlc.v); |
| 1240 | /* Section 7.7.32 */ |
| 1241 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1242 | pdp->gsnlu.l, pdp->gsnlu.v); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1243 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1244 | /* Section 7.7.33 */ |
| 1245 | if (!pdp->secondary) /* Not Secondary PDP Context Activation Procedure */ |
| 1246 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_MSISDN, |
| 1247 | pdp->msisdn.l, pdp->msisdn.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1248 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1249 | /* Section 7.7.34 */ |
| 1250 | if (pdp->version == 1) |
| 1251 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE, |
| 1252 | pdp->qos_req.l, pdp->qos_req.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1253 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1254 | /* Section 7.7.36 */ |
| 1255 | if ((pdp->version == 1) && pdp->tft.l) |
| 1256 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TFT, |
| 1257 | pdp->tft.l, pdp->tft.v); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1258 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1259 | /* Section 7.7.41 */ |
| 1260 | if ((pdp->version == 1) && pdp->triggerid.l) |
| 1261 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TRIGGER_ID, |
| 1262 | pdp->triggerid.l, pdp->triggerid.v); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1263 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1264 | /* Section 7.7.42 */ |
| 1265 | if ((pdp->version == 1) && pdp->omcid.l) |
| 1266 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_OMC_ID, |
| 1267 | pdp->omcid.l, pdp->omcid.v); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1268 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1269 | /* new R7 fields */ |
| 1270 | if (pdp->rattype_given == 1) |
| 1271 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_RAT_TYPE, |
| 1272 | pdp->rattype.l, pdp->rattype.v); |
Yann BONNAMY | 944dce3 | 2010-10-29 17:07:44 +0200 | [diff] [blame] | 1273 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1274 | if (pdp->userloc_given == 1) |
| 1275 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_USER_LOC, |
| 1276 | pdp->userloc.l, pdp->userloc.v); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1277 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1278 | if (pdp->mstz_given == 1) |
| 1279 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_MS_TZ, |
| 1280 | pdp->mstz.l, pdp->mstz.v); |
| 1281 | |
| 1282 | if (pdp->imeisv_given == 1) |
| 1283 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_IMEI_SV, |
| 1284 | pdp->imeisv.l, pdp->imeisv.v); |
| 1285 | |
| 1286 | /* TODO hisaddr0 */ |
| 1287 | gtp_req(gsn, pdp->version, pdp, &packet, length, &pdp->hisaddr0, cbp); |
| 1288 | |
| 1289 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1292 | /* API: Application response to context indication */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1293 | int gtp_create_context_resp(struct gsn_t *gsn, struct pdp_t *pdp, int cause) |
| 1294 | { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1295 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1296 | /* Now send off a reply to the peer */ |
| 1297 | gtp_create_pdp_resp(gsn, pdp->version, pdp, cause); |
| 1298 | |
| 1299 | if (cause != GTPCAUSE_ACC_REQ) { |
| 1300 | pdp_freepdp(pdp); |
| 1301 | } |
| 1302 | |
| 1303 | return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1304 | } |
| 1305 | |
| 1306 | /* API: Register create context indication callback */ |
| 1307 | int gtp_set_cb_create_context_ind(struct gsn_t *gsn, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1308 | int (*cb_create_context_ind) (struct pdp_t * |
| 1309 | pdp)) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1310 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1311 | gsn->cb_create_context_ind = cb_create_context_ind; |
| 1312 | return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1315 | /* Send Create PDP Context Response */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1316 | int gtp_create_pdp_resp(struct gsn_t *gsn, int version, struct pdp_t *pdp, |
| 1317 | uint8_t cause) |
| 1318 | { |
| 1319 | union gtp_packet packet; |
| 1320 | unsigned int length = |
| 1321 | get_default_gtp(version, GTP_CREATE_PDP_RSP, &packet); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1322 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1323 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1324 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1325 | if (cause == GTPCAUSE_ACC_REQ) { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1326 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1327 | if (version == 0) |
| 1328 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0, |
| 1329 | sizeof(pdp->qos_neg0), pdp->qos_neg0); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1330 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1331 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_REORDER, |
| 1332 | pdp->reorder); |
| 1333 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 1334 | gsn->restart_counter); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1335 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1336 | if (version == 0) { |
| 1337 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI, |
| 1338 | pdp->fllu); |
| 1339 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C, |
| 1340 | pdp->fllc); |
| 1341 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1342 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1343 | if (version == 1) { |
| 1344 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI, |
| 1345 | pdp->teid_own); |
| 1346 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C, |
| 1347 | pdp->teic_own); |
| 1348 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1349 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1350 | /* TODO: We use teic_own as charging ID */ |
| 1351 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_CHARGING_ID, |
| 1352 | pdp->teic_own); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1353 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1354 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA, |
| 1355 | pdp->eua.l, pdp->eua.v); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1356 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1357 | if (pdp->pco_neg.l) { /* Optional PCO */ |
| 1358 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_PCO, |
| 1359 | pdp->pco_neg.l, pdp->pco_neg.v); |
| 1360 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1361 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1362 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1363 | pdp->gsnlc.l, pdp->gsnlc.v); |
| 1364 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1365 | pdp->gsnlu.l, pdp->gsnlu.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1366 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1367 | if (version == 1) |
| 1368 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE, |
| 1369 | pdp->qos_neg.l, pdp->qos_neg.v); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1370 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1371 | /* TODO: Charging gateway address */ |
| 1372 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1373 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1374 | return gtp_resp(version, gsn, pdp, &packet, length, &pdp->sa_peer, |
| 1375 | pdp->fd, pdp->seq, pdp->tid); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | /* Handle Create PDP Context Request */ |
| 1379 | int gtp_create_pdp_ind(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1380 | struct sockaddr_in *peer, int fd, |
| 1381 | void *pack, unsigned len) |
| 1382 | { |
| 1383 | struct pdp_t *pdp, *pdp_old; |
| 1384 | struct pdp_t pdp_buf; |
| 1385 | union gtpie_member *ie[GTPIE_SIZE]; |
| 1386 | uint8_t recovery; |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 1387 | bool recovery_recvd = false; |
| 1388 | int rc; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1389 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1390 | uint16_t seq = get_seq(pack); |
| 1391 | int hlen = get_hlen(pack); |
| 1392 | uint8_t linked_nsapi = 0; |
| 1393 | struct pdp_t *linked_pdp = NULL; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1394 | |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 1395 | if (!gtp_duplicate(gsn, version, peer, seq)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1396 | return 0; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1397 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1398 | pdp = &pdp_buf; |
| 1399 | memset(pdp, 0, sizeof(struct pdp_t)); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1400 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1401 | if (version == 0) { |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 1402 | uint64_t tid = be64toh(((union gtp_packet *)pack)->gtp0.h.tid); |
| 1403 | |
Pablo Neira Ayuso | 746b944 | 2014-03-24 17:58:27 +0100 | [diff] [blame] | 1404 | pdp_set_imsi_nsapi(pdp, tid); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1405 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1406 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1407 | pdp->seq = seq; |
| 1408 | pdp->sa_peer = *peer; |
| 1409 | pdp->fd = fd; |
| 1410 | pdp->version = version; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1411 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1412 | /* Decode information elements */ |
| 1413 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 1414 | gsn->invalid++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1415 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1416 | "Invalid message format\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1417 | if (0 == version) |
| 1418 | return EOF; |
| 1419 | else |
| 1420 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1421 | GTPCAUSE_INVALID_MESSAGE); |
| 1422 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1423 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1424 | if (version == 1) { |
| 1425 | /* Linked NSAPI (conditional) */ |
| 1426 | /* If included this is the Secondary PDP Context Activation Procedure */ |
| 1427 | /* In secondary activation IMSI is not included, so the context must be */ |
| 1428 | /* identified by the tei */ |
| 1429 | if (!gtpie_gettv1(ie, GTPIE_NSAPI, 1, &linked_nsapi)) { |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1430 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1431 | /* Find the primary PDP context */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 1432 | if (gtp_pdp_getgtp1(gsn, &linked_pdp, get_tei(pack))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1433 | gsn->incorrect++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1434 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1435 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1436 | "Incorrect optional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1437 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1438 | GTPCAUSE_OPT_IE_INCORRECT); |
| 1439 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1440 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1441 | /* Check that the primary PDP context matches linked nsapi */ |
| 1442 | if (linked_pdp->nsapi != linked_nsapi) { |
| 1443 | gsn->incorrect++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1444 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1445 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1446 | "Incorrect optional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1447 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1448 | GTPCAUSE_OPT_IE_INCORRECT); |
| 1449 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1450 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1451 | /* Copy parameters from primary context */ |
| 1452 | pdp->selmode = linked_pdp->selmode; |
| 1453 | pdp->imsi = linked_pdp->imsi; |
| 1454 | pdp->msisdn = linked_pdp->msisdn; |
| 1455 | pdp->eua = linked_pdp->eua; |
| 1456 | pdp->pco_req = linked_pdp->pco_req; |
| 1457 | pdp->apn_req = linked_pdp->apn_req; |
| 1458 | pdp->teic_gn = linked_pdp->teic_gn; |
| 1459 | pdp->secondary = 1; |
Pau Espin Pedrol | aad77a0 | 2019-05-30 12:44:20 +0200 | [diff] [blame] | 1460 | } else { |
| 1461 | /* Not Secondary PDP Context Activation Procedure */ |
| 1462 | /* IMSI (conditional): If the MS is emergency attached |
| 1463 | and the MS is UICCless, the IMSI cannot be included |
| 1464 | in the message and therefore IMSI shall not be |
| 1465 | included in the message. */ |
| 1466 | if (gtpie_gettv0 |
| 1467 | (ie, GTPIE_IMSI, 0, &pdp->imsi, sizeof(pdp->imsi))) { |
| 1468 | gsn->missing++; |
| 1469 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1470 | len, "Missing IMSI not supported\n"); |
| 1471 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1472 | GTPCAUSE_MAN_IE_MISSING); |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | /* TEID (mandatory) */ |
| 1477 | if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) { |
| 1478 | gsn->missing++; |
| 1479 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1480 | len, "Missing mandatory information field\n"); |
| 1481 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1482 | GTPCAUSE_MAN_IE_MISSING); |
| 1483 | } |
| 1484 | /* TEIC (conditional) */ |
| 1485 | if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */ |
| 1486 | if (gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn)) { |
| 1487 | gsn->missing++; |
| 1488 | GTP_LOGPKG(LOGL_ERROR, peer, |
| 1489 | pack, len, |
| 1490 | "Missing mandatory information field\n"); |
| 1491 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1492 | GTPCAUSE_MAN_IE_MISSING); |
| 1493 | } |
| 1494 | } |
| 1495 | /* NSAPI (mandatory) */ |
| 1496 | if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &pdp->nsapi)) { |
| 1497 | gsn->missing++; |
| 1498 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1499 | len, "Missing mandatory information field\n"); |
| 1500 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1501 | GTPCAUSE_MAN_IE_MISSING); |
| 1502 | } |
| 1503 | /* QoS (mandatory) */ |
| 1504 | if (gtpie_gettlv(ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_req.l, |
| 1505 | &pdp->qos_req.v, sizeof(pdp->qos_req.v))) { |
| 1506 | gsn->missing++; |
| 1507 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1508 | len, "Missing mandatory information field\n"); |
| 1509 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1510 | GTPCAUSE_MAN_IE_MISSING); |
| 1511 | } |
| 1512 | /* TFT (conditional) */ |
| 1513 | if (gtpie_gettlv(ie, GTPIE_TFT, 0, &pdp->tft.l, |
| 1514 | &pdp->tft.v, sizeof(pdp->tft.v))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1515 | } |
| 1516 | } |
| 1517 | /* if (version == 1) */ |
| 1518 | if (version == 0) { |
| 1519 | if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0, |
| 1520 | pdp->qos_req0, sizeof(pdp->qos_req0))) { |
| 1521 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1522 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1523 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1524 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1525 | GTPCAUSE_MAN_IE_MISSING); |
| 1526 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1527 | if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) { |
| 1528 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1529 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1530 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1531 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1532 | GTPCAUSE_MAN_IE_MISSING); |
| 1533 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1534 | if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) { |
| 1535 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1536 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1537 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1538 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1539 | GTPCAUSE_MAN_IE_MISSING); |
| 1540 | } |
| 1541 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1542 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1543 | /* SGSN address for signalling (mandatory) */ |
| 1544 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l, |
| 1545 | &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) { |
| 1546 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1547 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1548 | "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1549 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1550 | GTPCAUSE_MAN_IE_MISSING); |
| 1551 | } |
jjako | 2e840a3 | 2003-01-28 16:05:18 +0000 | [diff] [blame] | 1552 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1553 | /* SGSN address for user traffic (mandatory) */ |
| 1554 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l, |
| 1555 | &pdp->gsnru.v, sizeof(pdp->gsnru.v))) { |
| 1556 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1557 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1558 | "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1559 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1560 | GTPCAUSE_MAN_IE_MISSING); |
| 1561 | } |
Pau Espin Pedrol | aad77a0 | 2019-05-30 12:44:20 +0200 | [diff] [blame] | 1562 | /* Recovery (optional) */ |
| 1563 | if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
| 1564 | /* we use recovery futher down after announcing new pdp ctx to user */ |
| 1565 | recovery_recvd = true; |
| 1566 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1567 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1568 | if (!linked_pdp) { /* Not Secondary PDP Context Activation Procedure */ |
Pau Espin Pedrol | aad77a0 | 2019-05-30 12:44:20 +0200 | [diff] [blame] | 1569 | /* Selection mode (conditional) */ |
| 1570 | if (gtpie_gettv0(ie, GTPIE_SELECTION_MODE, 0, |
| 1571 | &pdp->selmode, sizeof(pdp->selmode))) { |
| 1572 | gsn->missing++; |
| 1573 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1574 | len, "Missing mandatory information field\n"); |
| 1575 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1576 | GTPCAUSE_MAN_IE_MISSING); |
| 1577 | } |
| 1578 | /* End User Address (conditional) */ |
| 1579 | if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l, |
| 1580 | &pdp->eua.v, sizeof(pdp->eua.v))) { |
| 1581 | gsn->missing++; |
| 1582 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1583 | len, "Missing mandatory information field\n"); |
| 1584 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1585 | GTPCAUSE_MAN_IE_MISSING); |
| 1586 | } |
| 1587 | /* APN */ |
| 1588 | if (gtpie_gettlv(ie, GTPIE_APN, 0, &pdp->apn_req.l, |
| 1589 | &pdp->apn_req.v, sizeof(pdp->apn_req.v))) { |
| 1590 | gsn->missing++; |
| 1591 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1592 | len, "Missing mandatory information field\n"); |
| 1593 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1594 | GTPCAUSE_MAN_IE_MISSING); |
| 1595 | } |
| 1596 | /* Extract protocol configuration options (optional) */ |
| 1597 | if (!gtpie_gettlv(ie, GTPIE_PCO, 0, &pdp->pco_req.l, |
| 1598 | &pdp->pco_req.v, sizeof(pdp->pco_req.v))) { |
| 1599 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1600 | /* MSISDN (conditional) */ |
| 1601 | if (gtpie_gettlv(ie, GTPIE_MSISDN, 0, &pdp->msisdn.l, |
| 1602 | &pdp->msisdn.v, sizeof(pdp->msisdn.v))) { |
| 1603 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1604 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 1605 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1606 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1607 | GTPCAUSE_MAN_IE_MISSING); |
| 1608 | } |
| 1609 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1610 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1611 | /* Initialize our own IP addresses */ |
| 1612 | in_addr2gsna(&pdp->gsnlc, &gsn->gsnc); |
| 1613 | in_addr2gsna(&pdp->gsnlu, &gsn->gsnu); |
| 1614 | |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 1615 | DEBUGP(DLGTP, "gtp_create_pdp_ind: Before gtp_pdp_tidget\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1616 | |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 1617 | if (!gtp_pdp_getimsi(gsn, &pdp_old, pdp->imsi, pdp->nsapi)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1618 | /* Found old pdp with same tid. Now the voodoo begins! */ |
| 1619 | /* 09.60 / 29.060 allows create on existing context to "steal" */ |
| 1620 | /* the context which was allready established */ |
| 1621 | /* We check that the APN, selection mode and MSISDN is the same */ |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 1622 | DEBUGP(DLGTP, "gtp_create_pdp_ind: Old context found\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1623 | if ((pdp->apn_req.l == pdp_old->apn_req.l) |
| 1624 | && |
| 1625 | (!memcmp |
| 1626 | (pdp->apn_req.v, pdp_old->apn_req.v, pdp->apn_req.l)) |
| 1627 | && (pdp->selmode == pdp_old->selmode) |
| 1628 | && (pdp->msisdn.l == pdp_old->msisdn.l) |
| 1629 | && |
| 1630 | (!memcmp(pdp->msisdn.v, pdp_old->msisdn.v, pdp->msisdn.l))) |
| 1631 | { |
| 1632 | /* OK! We are dealing with the same APN. We will copy new |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 1633 | * parameters to the old pdp and send off confirmation |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1634 | * We ignore the following information elements: |
| 1635 | * QoS: MS will get originally negotiated QoS. |
| 1636 | * End user address (EUA). MS will get old EUA anyway. |
| 1637 | * Protocol configuration option (PCO): Only application can verify */ |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 1638 | DEBUGP(DLGTP, "gtp_create_pdp_ind: Old context found\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1639 | |
| 1640 | /* Copy remote flow label */ |
| 1641 | pdp_old->flru = pdp->flru; |
| 1642 | pdp_old->flrc = pdp->flrc; |
| 1643 | |
| 1644 | /* Copy remote tei */ |
| 1645 | pdp_old->teid_gn = pdp->teid_gn; |
| 1646 | pdp_old->teic_gn = pdp->teic_gn; |
| 1647 | |
| 1648 | /* Copy peer GSN address */ |
| 1649 | pdp_old->gsnrc.l = pdp->gsnrc.l; |
| 1650 | memcpy(&pdp_old->gsnrc.v, &pdp->gsnrc.v, pdp->gsnrc.l); |
| 1651 | pdp_old->gsnru.l = pdp->gsnru.l; |
| 1652 | memcpy(&pdp_old->gsnru.v, &pdp->gsnru.v, pdp->gsnru.l); |
| 1653 | |
| 1654 | /* Copy request parameters */ |
| 1655 | pdp_old->seq = pdp->seq; |
| 1656 | pdp_old->sa_peer = pdp->sa_peer; |
| 1657 | pdp_old->fd = pdp->fd = fd; |
| 1658 | pdp_old->version = pdp->version = version; |
| 1659 | |
| 1660 | /* Switch to using the old pdp context */ |
| 1661 | pdp = pdp_old; |
| 1662 | |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 1663 | if (recovery_recvd) |
| 1664 | emit_cb_recovery(gsn, peer, pdp, recovery); |
| 1665 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1666 | /* Confirm to peer that things were "successful" */ |
| 1667 | return gtp_create_pdp_resp(gsn, version, pdp, |
| 1668 | GTPCAUSE_ACC_REQ); |
| 1669 | } else { /* This is not the same PDP context. Delete the old one. */ |
| 1670 | |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 1671 | DEBUGP(DLGTP, "gtp_create_pdp_ind: Deleting old context\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1672 | |
Pau Espin Pedrol | 0d0b059 | 2019-05-29 20:42:09 +0200 | [diff] [blame] | 1673 | gtp_freepdp(gsn, pdp_old); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1674 | |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 1675 | DEBUGP(DLGTP, "gtp_create_pdp_ind: Deleted...\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1676 | } |
| 1677 | } |
| 1678 | |
Pau Espin Pedrol | eefa30d | 2019-05-31 15:42:49 +0200 | [diff] [blame] | 1679 | gtp_pdp_newpdp(gsn, &pdp, pdp->imsi, pdp->nsapi, pdp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1680 | |
Pau Espin Pedrol | aad77a0 | 2019-05-30 12:44:20 +0200 | [diff] [blame] | 1681 | /* Callback function to validate login */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1682 | if (gsn->cb_create_context_ind != 0) |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 1683 | rc = gsn->cb_create_context_ind(pdp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1684 | else { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1685 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1686 | "No create_context_ind callback defined\n"); |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 1687 | rc = gtp_create_pdp_resp(gsn, version, pdp, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1688 | GTPCAUSE_NOT_SUPPORTED); |
| 1689 | } |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 1690 | if (recovery_recvd) |
| 1691 | emit_cb_recovery(gsn, peer, pdp, recovery); |
| 1692 | return rc; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1695 | /* Handle Create PDP Context Response */ |
| 1696 | int gtp_create_pdp_conf(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1697 | struct sockaddr_in *peer, void *pack, unsigned len) |
| 1698 | { |
| 1699 | struct pdp_t *pdp; |
| 1700 | union gtpie_member *ie[GTPIE_SIZE]; |
| 1701 | uint8_t cause, recovery; |
| 1702 | void *cbp = NULL; |
| 1703 | uint8_t type = 0; |
| 1704 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1705 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1706 | /* Remove packet from queue */ |
| 1707 | if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) |
| 1708 | return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1709 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1710 | /* Find the context in question */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 1711 | if (gtp_pdp_getgtp1(gsn, &pdp, get_tei(pack))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1712 | gsn->err_unknownpdp++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1713 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 1714 | "Unknown PDP context: %u\n", get_tei(pack)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1715 | if (gsn->cb_conf) |
| 1716 | gsn->cb_conf(type, EOF, NULL, cbp); |
| 1717 | return EOF; |
| 1718 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 1719 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1720 | /* Register that we have received a valid teic from GGSN */ |
| 1721 | pdp->teic_confirmed = 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1722 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1723 | /* Decode information elements */ |
| 1724 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 1725 | gsn->invalid++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1726 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1727 | "Invalid message format\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1728 | if (gsn->cb_conf) |
| 1729 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1730 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1731 | pdp_freepdp(pdp); */ |
| 1732 | return EOF; |
| 1733 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1734 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1735 | /* Extract cause value (mandatory) */ |
| 1736 | if (gtpie_gettv1(ie, GTPIE_CAUSE, 0, &cause)) { |
| 1737 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1738 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 1739 | "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1740 | if (gsn->cb_conf) |
| 1741 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1742 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1743 | pdp_freepdp(pdp); */ |
| 1744 | return EOF; |
| 1745 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1746 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1747 | /* Extract recovery (optional) */ |
| 1748 | if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 1749 | emit_cb_recovery(gsn, peer, pdp, recovery); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1750 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1751 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1752 | /* Extract protocol configuration options (optional) */ |
| 1753 | if (!gtpie_gettlv(ie, GTPIE_PCO, 0, &pdp->pco_req.l, |
| 1754 | &pdp->pco_req.v, sizeof(pdp->pco_req.v))) { |
| 1755 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1756 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1757 | /* Check all conditional information elements */ |
| 1758 | if (GTPCAUSE_ACC_REQ == cause) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1759 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1760 | if (version == 0) { |
| 1761 | if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0, |
| 1762 | &pdp->qos_neg0, |
| 1763 | sizeof(pdp->qos_neg0))) { |
| 1764 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1765 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1766 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1767 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1768 | if (gsn->cb_conf) |
| 1769 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1770 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1771 | pdp_freepdp(pdp); */ |
| 1772 | return EOF; |
| 1773 | } |
| 1774 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1775 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1776 | if (gtpie_gettv1(ie, GTPIE_REORDER, 0, &pdp->reorder)) { |
| 1777 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1778 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1779 | len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1780 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1781 | if (gsn->cb_conf) |
| 1782 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1783 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1784 | pdp_freepdp(pdp); */ |
| 1785 | return EOF; |
| 1786 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1787 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1788 | if (version == 0) { |
| 1789 | if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) { |
| 1790 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1791 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1792 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1793 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1794 | if (gsn->cb_conf) |
| 1795 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1796 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1797 | pdp_freepdp(pdp); */ |
| 1798 | return EOF; |
| 1799 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1800 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1801 | if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) { |
| 1802 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1803 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1804 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1805 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1806 | if (gsn->cb_conf) |
| 1807 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1808 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1809 | pdp_freepdp(pdp); */ |
| 1810 | return EOF; |
| 1811 | } |
| 1812 | } |
| 1813 | |
| 1814 | if (version == 1) { |
| 1815 | if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) { |
| 1816 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1817 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1818 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1819 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1820 | if (gsn->cb_conf) |
| 1821 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1822 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1823 | pdp_freepdp(pdp); */ |
| 1824 | return EOF; |
| 1825 | } |
| 1826 | |
| 1827 | if (gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn)) { |
| 1828 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1829 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1830 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1831 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1832 | if (gsn->cb_conf) |
| 1833 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1834 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1835 | pdp_freepdp(pdp); */ |
| 1836 | return EOF; |
| 1837 | } |
| 1838 | } |
| 1839 | |
| 1840 | if (gtpie_gettv4(ie, GTPIE_CHARGING_ID, 0, &pdp->cid)) { |
| 1841 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1842 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1843 | len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1844 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1845 | if (gsn->cb_conf) |
| 1846 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1847 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1848 | pdp_freepdp(pdp); */ |
| 1849 | } |
| 1850 | |
| 1851 | if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l, |
| 1852 | &pdp->eua.v, sizeof(pdp->eua.v))) { |
| 1853 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1854 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1855 | len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1856 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1857 | if (gsn->cb_conf) |
| 1858 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1859 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1860 | pdp_freepdp(pdp); */ |
| 1861 | return EOF; |
| 1862 | } |
| 1863 | |
| 1864 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l, |
| 1865 | &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) { |
| 1866 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1867 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1868 | len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1869 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1870 | if (gsn->cb_conf) |
| 1871 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1872 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1873 | pdp_freepdp(pdp); */ |
| 1874 | return EOF; |
| 1875 | } |
| 1876 | |
| 1877 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l, |
| 1878 | &pdp->gsnru.v, sizeof(pdp->gsnru.v))) { |
| 1879 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1880 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1881 | len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1882 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1883 | if (gsn->cb_conf) |
| 1884 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1885 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1886 | pdp_freepdp(pdp); */ |
| 1887 | return EOF; |
| 1888 | } |
| 1889 | |
| 1890 | if (version == 1) { |
| 1891 | if (gtpie_gettlv |
| 1892 | (ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_neg.l, |
| 1893 | &pdp->qos_neg.v, sizeof(pdp->qos_neg.v))) { |
| 1894 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1895 | GTP_LOGPKG(LOGL_ERROR, peer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1896 | pack, len, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 1897 | "Missing conditional information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1898 | if (gsn->cb_conf) |
| 1899 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 1900 | /* if (gsn->cb_delete_context) gsn->cb_delete_context(pdp); |
| 1901 | pdp_freepdp(pdp); */ |
| 1902 | return EOF; |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | } |
| 1907 | |
| 1908 | if (gsn->cb_conf) |
| 1909 | gsn->cb_conf(type, cause, pdp, cbp); |
| 1910 | |
| 1911 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1914 | /* API: Send Update PDP Context Request */ |
| 1915 | int gtp_update_context(struct gsn_t *gsn, struct pdp_t *pdp, void *cbp, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1916 | struct in_addr *inetaddr) |
| 1917 | { |
| 1918 | union gtp_packet packet; |
| 1919 | unsigned int length = |
| 1920 | get_default_gtp(pdp->version, GTP_UPDATE_PDP_REQ, &packet); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1921 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1922 | if (pdp->version == 0) |
| 1923 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0, |
| 1924 | sizeof(pdp->qos_req0), pdp->qos_req0); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1925 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1926 | /* Include IMSI if updating with unknown teic_gn */ |
| 1927 | if ((pdp->version == 1) && (!pdp->teic_gn)) |
| 1928 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_IMSI, |
| 1929 | sizeof(pdp->imsi), (uint8_t *) & pdp->imsi); |
| 1930 | |
| 1931 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 1932 | gsn->restart_counter); |
| 1933 | |
| 1934 | if (pdp->version == 0) { |
| 1935 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI, pdp->fllu); |
| 1936 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C, pdp->fllc); |
| 1937 | } |
| 1938 | |
| 1939 | if (pdp->version == 1) { |
| 1940 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI, |
| 1941 | pdp->teid_own); |
| 1942 | |
| 1943 | if (!pdp->teic_confirmed) |
| 1944 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_C, |
| 1945 | pdp->teic_own); |
| 1946 | } |
| 1947 | |
| 1948 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI, pdp->nsapi); |
| 1949 | |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 1950 | /* TODO |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1951 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_REF, |
| 1952 | pdp->traceref); |
| 1953 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_TRACE_TYPE, |
| 1954 | pdp->tracetype); */ |
| 1955 | |
| 1956 | /* TODO if ggsn update message |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 1957 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1958 | pdp->eua.l, pdp->eua.v); |
| 1959 | */ |
| 1960 | |
| 1961 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1962 | pdp->gsnlc.l, pdp->gsnlc.v); |
| 1963 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 1964 | pdp->gsnlu.l, pdp->gsnlu.v); |
| 1965 | |
| 1966 | if (pdp->version == 1) |
| 1967 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE, |
| 1968 | pdp->qos_req.l, pdp->qos_req.v); |
| 1969 | |
| 1970 | if ((pdp->version == 1) && pdp->tft.l) |
| 1971 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TFT, |
| 1972 | pdp->tft.l, pdp->tft.v); |
| 1973 | |
| 1974 | if ((pdp->version == 1) && pdp->triggerid.l) |
| 1975 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_TRIGGER_ID, |
| 1976 | pdp->triggerid.l, pdp->triggerid.v); |
| 1977 | |
| 1978 | if ((pdp->version == 1) && pdp->omcid.l) |
| 1979 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_OMC_ID, |
| 1980 | pdp->omcid.l, pdp->omcid.v); |
| 1981 | |
Daniel Willmann | 134a775 | 2016-02-03 18:53:29 +0100 | [diff] [blame] | 1982 | gtp_req(gsn, pdp->version, pdp, &packet, length, inetaddr, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1983 | |
| 1984 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1987 | /* Send Update PDP Context Response */ |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 1988 | static int gtp_update_pdp_resp(struct gsn_t *gsn, uint8_t version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1989 | struct sockaddr_in *peer, int fd, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1990 | void *pack, unsigned len, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1991 | struct pdp_t *pdp, uint8_t cause) |
| 1992 | { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1993 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1994 | union gtp_packet packet; |
| 1995 | unsigned int length = |
| 1996 | get_default_gtp(version, GTP_UPDATE_PDP_RSP, &packet); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1997 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 1998 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 1999 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2000 | if (cause == GTPCAUSE_ACC_REQ) { |
| 2001 | |
| 2002 | if (version == 0) |
| 2003 | gtpie_tv0(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE0, |
| 2004 | sizeof(pdp->qos_neg0), pdp->qos_neg0); |
| 2005 | |
| 2006 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_RECOVERY, |
| 2007 | gsn->restart_counter); |
| 2008 | |
| 2009 | if (version == 0) { |
| 2010 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_DI, |
| 2011 | pdp->fllu); |
| 2012 | gtpie_tv2(&packet, &length, GTP_MAX, GTPIE_FL_C, |
| 2013 | pdp->fllc); |
| 2014 | } |
| 2015 | |
| 2016 | if (version == 1) { |
| 2017 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI, |
| 2018 | pdp->teid_own); |
| 2019 | |
| 2020 | if (!pdp->teic_confirmed) |
| 2021 | gtpie_tv4(&packet, &length, GTP_MAX, |
| 2022 | GTPIE_TEI_C, pdp->teic_own); |
| 2023 | } |
| 2024 | |
| 2025 | /* TODO we use teid_own as charging ID address */ |
| 2026 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_CHARGING_ID, |
| 2027 | pdp->teid_own); |
| 2028 | |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 2029 | /* If ggsn |
| 2030 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_EUA, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2031 | pdp->eua.l, pdp->eua.v); */ |
| 2032 | |
| 2033 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 2034 | pdp->gsnlc.l, pdp->gsnlc.v); |
| 2035 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 2036 | pdp->gsnlu.l, pdp->gsnlu.v); |
| 2037 | |
| 2038 | if (version == 1) |
| 2039 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_QOS_PROFILE, |
| 2040 | pdp->qos_neg.l, pdp->qos_neg.v); |
| 2041 | |
| 2042 | /* TODO: Charging gateway address */ |
| 2043 | } |
| 2044 | |
| 2045 | return gtp_resp(version, gsn, pdp, &packet, length, peer, |
| 2046 | fd, get_seq(pack), get_tid(pack)); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2047 | } |
| 2048 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2049 | /* Handle Update PDP Context Request */ |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 2050 | static int gtp_update_pdp_ind(struct gsn_t *gsn, uint8_t version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2051 | struct sockaddr_in *peer, int fd, |
| 2052 | void *pack, unsigned len) |
| 2053 | { |
| 2054 | struct pdp_t *pdp; |
| 2055 | struct pdp_t pdp_backup; |
| 2056 | union gtpie_member *ie[GTPIE_SIZE]; |
| 2057 | uint8_t recovery; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2058 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2059 | uint16_t seq = get_seq(pack); |
| 2060 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2061 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2062 | uint64_t imsi; |
| 2063 | uint8_t nsapi; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2064 | |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 2065 | /* Is this a duplicate ? */ |
| 2066 | if (!gtp_duplicate(gsn, version, peer, seq)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2067 | return 0; /* We allready send of response once */ |
| 2068 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2069 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2070 | /* Decode information elements */ |
| 2071 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 2072 | gsn->invalid++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2073 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2074 | "Invalid message format\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2075 | if (0 == version) |
| 2076 | return EOF; |
| 2077 | else |
| 2078 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2079 | len, NULL, |
| 2080 | GTPCAUSE_INVALID_MESSAGE); |
| 2081 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2082 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2083 | /* Finding PDP: */ |
| 2084 | /* For GTP0 we use the tunnel identifier to provide imsi and nsapi. */ |
| 2085 | /* For GTP1 we must use imsi and nsapi if imsi is present. Otherwise */ |
| 2086 | /* we have to use the tunnel endpoint identifier */ |
| 2087 | if (version == 0) { |
Pablo Neira Ayuso | 1a1ba02 | 2014-03-20 12:35:26 +0100 | [diff] [blame] | 2088 | uint64_t tid = be64toh(((union gtp_packet *)pack)->gtp0.h.tid); |
| 2089 | |
Pablo Neira Ayuso | 746b944 | 2014-03-24 17:58:27 +0100 | [diff] [blame] | 2090 | pdp_set_imsi_nsapi(pdp, tid); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2091 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2092 | /* Find the context in question */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2093 | if (gtp_pdp_getimsi(gsn, &pdp, imsi, nsapi)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2094 | gsn->err_unknownpdp++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2095 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2096 | len, "Unknown PDP context\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2097 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2098 | len, NULL, |
| 2099 | GTPCAUSE_NON_EXIST); |
| 2100 | } |
| 2101 | } else if (version == 1) { |
| 2102 | /* NSAPI (mandatory) */ |
| 2103 | if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &nsapi)) { |
| 2104 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2105 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2106 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2107 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2108 | len, NULL, |
| 2109 | GTPCAUSE_MAN_IE_MISSING); |
| 2110 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2111 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2112 | /* IMSI (conditional) */ |
| 2113 | if (gtpie_gettv0(ie, GTPIE_IMSI, 0, &imsi, sizeof(imsi))) { |
| 2114 | /* Find the context in question */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2115 | if (gtp_pdp_getgtp1(gsn, &pdp, get_tei(pack))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2116 | gsn->err_unknownpdp++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2117 | GTP_LOGPKG(LOGL_ERROR, peer, |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 2118 | pack, len, "Unknown PDP context: %u\n", |
| 2119 | get_tei(pack)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2120 | return gtp_update_pdp_resp(gsn, version, peer, |
| 2121 | fd, pack, len, NULL, |
| 2122 | GTPCAUSE_NON_EXIST); |
| 2123 | } |
| 2124 | } else { |
| 2125 | /* Find the context in question */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2126 | if (gtp_pdp_getimsi(gsn, &pdp, imsi, nsapi)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2127 | gsn->err_unknownpdp++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2128 | GTP_LOGPKG(LOGL_ERROR, peer, |
| 2129 | pack, len, "Unknown PDP context\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2130 | return gtp_update_pdp_resp(gsn, version, peer, |
| 2131 | fd, pack, len, NULL, |
| 2132 | GTPCAUSE_NON_EXIST); |
| 2133 | } |
| 2134 | } |
| 2135 | } else { |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 2136 | LOGP(DLGTP, LOGL_ERROR, "Unknown version: %d\n", version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2137 | return EOF; |
| 2138 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2139 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2140 | /* Make a backup copy in case anything is wrong */ |
| 2141 | memcpy(&pdp_backup, pdp, sizeof(pdp_backup)); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2142 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2143 | if (version == 0) { |
| 2144 | if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0, |
| 2145 | pdp->qos_req0, sizeof(pdp->qos_req0))) { |
| 2146 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2147 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2148 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2149 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2150 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2151 | len, pdp, |
| 2152 | GTPCAUSE_MAN_IE_MISSING); |
| 2153 | } |
| 2154 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2155 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2156 | /* Recovery (optional) */ |
| 2157 | if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 2158 | emit_cb_recovery(gsn, peer, pdp, recovery); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2159 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2160 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2161 | if (version == 0) { |
| 2162 | if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) { |
| 2163 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2164 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2165 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2166 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2167 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2168 | len, pdp, |
| 2169 | GTPCAUSE_MAN_IE_MISSING); |
| 2170 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2171 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2172 | if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) { |
| 2173 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2174 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2175 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2176 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2177 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2178 | len, pdp, |
| 2179 | GTPCAUSE_MAN_IE_MISSING); |
| 2180 | } |
| 2181 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2182 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2183 | if (version == 1) { |
| 2184 | /* TEID (mandatory) */ |
| 2185 | if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) { |
| 2186 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2187 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2188 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2189 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2190 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2191 | len, pdp, |
| 2192 | GTPCAUSE_MAN_IE_MISSING); |
| 2193 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2194 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2195 | /* TEIC (conditional) */ |
| 2196 | /* If TEIC is not included it means that we have allready received it */ |
| 2197 | /* TODO: From 29.060 it is not clear if TEI_C MUST be included for */ |
| 2198 | /* all updated contexts, or only for one of the linked contexts */ |
| 2199 | gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn); |
| 2200 | |
| 2201 | /* NSAPI (mandatory) */ |
| 2202 | if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &pdp->nsapi)) { |
| 2203 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2204 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2205 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2206 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2207 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2208 | len, pdp, |
| 2209 | GTPCAUSE_MAN_IE_MISSING); |
| 2210 | } |
| 2211 | } |
| 2212 | |
| 2213 | /* Trace reference (optional) */ |
| 2214 | /* Trace type (optional) */ |
| 2215 | |
| 2216 | /* End User Address (conditional) TODO: GGSN Initiated |
| 2217 | if (gtpie_gettlv(ie, GTPIE_EUA, 0, &pdp->eua.l, |
| 2218 | &pdp->eua.v, sizeof(pdp->eua.v))) { |
| 2219 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2220 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2221 | "Missing mandatory information field"); |
| 2222 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 2223 | return gtp_update_pdp_resp(gsn, version, pdp, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2224 | GTPCAUSE_MAN_IE_MISSING); |
| 2225 | } */ |
| 2226 | |
| 2227 | /* SGSN address for signalling (mandatory) */ |
| 2228 | /* It is weird that this is mandatory when TEIC is conditional */ |
| 2229 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l, |
| 2230 | &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) { |
| 2231 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2232 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2233 | "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2234 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2235 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, |
| 2236 | pdp, GTPCAUSE_MAN_IE_MISSING); |
| 2237 | } |
| 2238 | |
| 2239 | /* SGSN address for user traffic (mandatory) */ |
| 2240 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l, |
| 2241 | &pdp->gsnru.v, sizeof(pdp->gsnru.v))) { |
| 2242 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2243 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2244 | "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2245 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2246 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, |
| 2247 | pdp, GTPCAUSE_MAN_IE_MISSING); |
| 2248 | } |
| 2249 | |
| 2250 | if (version == 1) { |
| 2251 | /* QoS (mandatory) */ |
| 2252 | if (gtpie_gettlv(ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_req.l, |
| 2253 | &pdp->qos_req.v, sizeof(pdp->qos_req.v))) { |
| 2254 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2255 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2256 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2257 | memcpy(pdp, &pdp_backup, sizeof(pdp_backup)); |
| 2258 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, |
| 2259 | len, pdp, |
| 2260 | GTPCAUSE_MAN_IE_MISSING); |
| 2261 | } |
| 2262 | |
| 2263 | /* TFT (conditional) */ |
| 2264 | if (gtpie_gettlv(ie, GTPIE_TFT, 0, &pdp->tft.l, |
| 2265 | &pdp->tft.v, sizeof(pdp->tft.v))) { |
| 2266 | } |
| 2267 | |
| 2268 | /* OMC identity */ |
| 2269 | } |
| 2270 | |
| 2271 | /* Confirm to peer that things were "successful" */ |
| 2272 | return gtp_update_pdp_resp(gsn, version, peer, fd, pack, len, pdp, |
| 2273 | GTPCAUSE_ACC_REQ); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2276 | /* Handle Update PDP Context Response */ |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 2277 | static int gtp_update_pdp_conf(struct gsn_t *gsn, uint8_t version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2278 | struct sockaddr_in *peer, void *pack, unsigned len) |
| 2279 | { |
| 2280 | struct pdp_t *pdp; |
| 2281 | union gtpie_member *ie[GTPIE_SIZE]; |
| 2282 | uint8_t cause, recovery; |
| 2283 | void *cbp = NULL; |
| 2284 | uint8_t type = 0; |
Daniel Willmann | 05f3ef3 | 2016-02-03 18:53:30 +0100 | [diff] [blame] | 2285 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2286 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2287 | /* Remove packet from queue */ |
| 2288 | if (gtp_conf(gsn, 0, peer, pack, len, &type, &cbp)) |
| 2289 | return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2290 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2291 | /* Find the context in question */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2292 | if (gtp_pdp_getgtp1(gsn, &pdp, get_tei(pack))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2293 | gsn->err_unknownpdp++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2294 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
Daniel Willmann | 05f3ef3 | 2016-02-03 18:53:30 +0100 | [diff] [blame] | 2295 | "Unknown PDP context: %u\n", get_tei(pack)); |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2296 | pdp = NULL; |
| 2297 | goto err_out; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2298 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2299 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2300 | /* Register that we have received a valid teic from GGSN */ |
| 2301 | pdp->teic_confirmed = 1; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2302 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2303 | /* Decode information elements */ |
Daniel Willmann | 05f3ef3 | 2016-02-03 18:53:30 +0100 | [diff] [blame] | 2304 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2305 | gsn->invalid++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2306 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2307 | "Invalid message format\n"); |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2308 | goto err_out; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2309 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2310 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2311 | /* Extract cause value (mandatory) */ |
| 2312 | if (gtpie_gettv1(ie, GTPIE_CAUSE, 0, &cause)) { |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2313 | goto err_missing; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2314 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2315 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2316 | /* Extract recovery (optional) */ |
| 2317 | if (!gtpie_gettv1(ie, GTPIE_RECOVERY, 0, &recovery)) { |
Pau Espin Pedrol | b5f9334 | 2018-07-23 11:24:07 +0200 | [diff] [blame] | 2318 | emit_cb_recovery(gsn, peer, pdp, recovery); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | /* Check all conditional information elements */ |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2322 | /* TODO: This does not handle GGSN-initiated update responses */ |
| 2323 | if (GTPCAUSE_ACC_REQ == cause) { |
| 2324 | if (version == 0) { |
| 2325 | if (gtpie_gettv0(ie, GTPIE_QOS_PROFILE0, 0, |
| 2326 | &pdp->qos_neg0, |
| 2327 | sizeof(pdp->qos_neg0))) { |
| 2328 | goto err_missing; |
| 2329 | } |
| 2330 | |
| 2331 | if (gtpie_gettv2(ie, GTPIE_FL_DI, 0, &pdp->flru)) { |
| 2332 | goto err_missing; |
| 2333 | } |
| 2334 | |
| 2335 | if (gtpie_gettv2(ie, GTPIE_FL_C, 0, &pdp->flrc)) { |
| 2336 | goto err_missing; |
| 2337 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2338 | } |
| 2339 | |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2340 | if (version == 1) { |
| 2341 | if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &pdp->teid_gn)) { |
| 2342 | goto err_missing; |
| 2343 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2344 | |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2345 | if (gtpie_gettv4(ie, GTPIE_TEI_C, 0, &pdp->teic_gn)) { |
| 2346 | goto err_missing; |
| 2347 | } |
| 2348 | } |
| 2349 | |
| 2350 | if (gtpie_gettv4(ie, GTPIE_CHARGING_ID, 0, &pdp->cid)) { |
| 2351 | goto err_missing; |
| 2352 | } |
| 2353 | |
| 2354 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 0, &pdp->gsnrc.l, |
| 2355 | &pdp->gsnrc.v, sizeof(pdp->gsnrc.v))) { |
| 2356 | goto err_missing; |
| 2357 | } |
| 2358 | |
| 2359 | if (gtpie_gettlv(ie, GTPIE_GSN_ADDR, 1, &pdp->gsnru.l, |
| 2360 | &pdp->gsnru.v, sizeof(pdp->gsnru.v))) { |
| 2361 | goto err_missing; |
| 2362 | } |
| 2363 | |
| 2364 | if (version == 1) { |
| 2365 | if (gtpie_gettlv |
| 2366 | (ie, GTPIE_QOS_PROFILE, 0, &pdp->qos_neg.l, |
| 2367 | &pdp->qos_neg.v, sizeof(pdp->qos_neg.v))) { |
| 2368 | goto err_missing; |
| 2369 | } |
| 2370 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2371 | } |
Daniel Willmann | d997552 | 2016-02-04 15:38:12 +0100 | [diff] [blame] | 2372 | |
| 2373 | if (gsn->cb_conf) |
| 2374 | gsn->cb_conf(type, cause, pdp, cbp); |
| 2375 | return 0; /* Succes */ |
| 2376 | |
| 2377 | err_missing: |
| 2378 | gsn->missing++; |
| 2379 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2380 | "Missing information field\n"); |
| 2381 | err_out: |
| 2382 | if (gsn->cb_conf) |
| 2383 | gsn->cb_conf(type, EOF, pdp, cbp); |
| 2384 | return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2387 | /* API: Deprecated. Send Delete PDP Context Request And free pdp ctx. */ |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2388 | int gtp_delete_context_req(struct gsn_t *gsn, struct pdp_t *pdp, void *cbp, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2389 | int teardown) |
| 2390 | { |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2391 | struct pdp_t *linked_pdp; |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2392 | |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2393 | if (gtp_pdp_getgtp1(gsn, &linked_pdp, pdp->teic_own)) { |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2394 | LOGP(DLGTP, LOGL_ERROR, |
| 2395 | "Unknown linked PDP context: %u\n", pdp->teic_own); |
| 2396 | return EOF; |
| 2397 | } |
| 2398 | |
| 2399 | if (gtp_delete_context_req2(gsn, pdp, cbp, teardown) == EOF) |
| 2400 | return EOF; |
| 2401 | |
| 2402 | if (teardown) { /* Remove all contexts */ |
Pau Espin Pedrol | 0d0b059 | 2019-05-29 20:42:09 +0200 | [diff] [blame] | 2403 | gtp_freepdp_teardown(gsn, linked_pdp); |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2404 | } else { |
Pau Espin Pedrol | 8651573 | 2019-05-31 16:40:37 +0200 | [diff] [blame] | 2405 | /* If we end up here (no teardown) it means we still |
| 2406 | have at least another pdp context active for this |
| 2407 | PDN connection (since last DeleteReq should come |
| 2408 | with teardown enabled). If the ctx to delete is a |
| 2409 | secondary ctx, simply free it. If it's the primary |
| 2410 | ctx, mark it as nodata but don't free it since we |
| 2411 | need it to hold data linked together and we'll |
| 2412 | require it later to tear down the entire tree. Still, |
| 2413 | we announce its deletion through cb_delete_context |
| 2414 | because we don't want user to release its related |
| 2415 | data and not use it anymore. |
| 2416 | */ |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2417 | if (pdp == linked_pdp) { |
Pau Espin Pedrol | 93dd798 | 2019-05-31 16:42:05 +0200 | [diff] [blame] | 2418 | if (gsn->cb_delete_context) |
| 2419 | gsn->cb_delete_context(pdp); |
| 2420 | pdp->secondary_tei[pdp->nsapi & 0xf0] = 0; |
| 2421 | pdp->nodata = 1; |
| 2422 | } else { |
| 2423 | gtp_freepdp(gsn, pdp); |
| 2424 | } |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | return 0; |
| 2428 | } |
| 2429 | |
Oliver Smith | 1cde2c1 | 2019-05-13 11:35:03 +0200 | [diff] [blame] | 2430 | /* API: Send Delete PDP Context Request. PDP CTX shall be free'd by user at any |
| 2431 | point in time later than this function through a call to pdp_freepdp(pdp), but |
| 2432 | it must be freed no later than during cb_conf(GTP_DELETE_PDP_REQ, pdp) */ |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2433 | int gtp_delete_context_req2(struct gsn_t *gsn, struct pdp_t *pdp, void *cbp, |
| 2434 | int teardown) |
| 2435 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2436 | union gtp_packet packet; |
| 2437 | unsigned int length = |
| 2438 | get_default_gtp(pdp->version, GTP_DELETE_PDP_REQ, &packet); |
| 2439 | struct in_addr addr; |
| 2440 | struct pdp_t *linked_pdp; |
Pau Espin Pedrol | 9ee8d32 | 2019-05-30 14:11:22 +0200 | [diff] [blame] | 2441 | int count; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2442 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2443 | if (gsna2in_addr(&addr, &pdp->gsnrc)) { |
| 2444 | gsn->err_address++; |
Max | 14b1b63 | 2017-08-21 20:14:59 +0200 | [diff] [blame] | 2445 | LOGP(DLGTP, LOGL_ERROR, "GSN address (len=%u) conversion failed\n", pdp->gsnrc.l); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2446 | return EOF; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2447 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2448 | |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2449 | if (gtp_pdp_getgtp1(gsn, &linked_pdp, pdp->teic_own)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2450 | LOGP(DLGTP, LOGL_ERROR, |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 2451 | "Unknown linked PDP context: %u\n", pdp->teic_own); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2452 | return EOF; |
| 2453 | } |
| 2454 | |
| 2455 | if (!teardown) { |
Pau Espin Pedrol | 9ee8d32 | 2019-05-30 14:11:22 +0200 | [diff] [blame] | 2456 | count = pdp_count_secondary(linked_pdp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2457 | if (count <= 1) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2458 | LOGP(DLGTP, LOGL_ERROR, |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 2459 | "Must use teardown for last context: %d\n", count); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2460 | return EOF; |
| 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | if (pdp->version == 1) { |
| 2465 | if (teardown) |
| 2466 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_TEARDOWN, |
| 2467 | 0xff); |
| 2468 | |
| 2469 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_NSAPI, pdp->nsapi); |
| 2470 | } |
| 2471 | |
| 2472 | gtp_req(gsn, pdp->version, pdp, &packet, length, &addr, cbp); |
| 2473 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2474 | return 0; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2475 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2476 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2477 | /* Send Delete PDP Context Response */ |
| 2478 | int gtp_delete_pdp_resp(struct gsn_t *gsn, int version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2479 | struct sockaddr_in *peer, int fd, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2480 | void *pack, unsigned len, |
| 2481 | struct pdp_t *pdp, struct pdp_t *linked_pdp, |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2482 | uint8_t cause, int teardown) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2483 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2484 | union gtp_packet packet; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2485 | unsigned int length = |
| 2486 | get_default_gtp(version, GTP_DELETE_PDP_RSP, &packet); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2487 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2488 | gtpie_tv1(&packet, &length, GTP_MAX, GTPIE_CAUSE, cause); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2489 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2490 | gtp_resp(version, gsn, pdp, &packet, length, peer, fd, |
| 2491 | get_seq(pack), get_tid(pack)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2492 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2493 | if (cause == GTPCAUSE_ACC_REQ) { |
| 2494 | if ((teardown) || (version == 0)) { /* Remove all contexts */ |
Pau Espin Pedrol | 0d0b059 | 2019-05-29 20:42:09 +0200 | [diff] [blame] | 2495 | gtp_freepdp_teardown(gsn, linked_pdp); |
Pau Espin Pedrol | 8651573 | 2019-05-31 16:40:37 +0200 | [diff] [blame] | 2496 | } else { |
| 2497 | /* If we end up here (no teardown) it means we still |
| 2498 | have at least another pdp context active for this |
| 2499 | PDN connection (since last DeleteReq should come |
| 2500 | with teardown enabled). If the ctx to delete is a |
| 2501 | secondary ctx, simply free it. If it's the primary |
| 2502 | ctx, mark it as nodata but don't free it since we |
| 2503 | need it to hold data linked together and we'll |
| 2504 | require it later to tear down the entire tree. Still, |
| 2505 | we announce its deletion through cb_delete_context |
| 2506 | because we don't want user to release its related |
| 2507 | data and not use it anymore. |
| 2508 | */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2509 | if (pdp == linked_pdp) { |
Pau Espin Pedrol | 93dd798 | 2019-05-31 16:42:05 +0200 | [diff] [blame] | 2510 | if (gsn->cb_delete_context) |
| 2511 | gsn->cb_delete_context(pdp); |
| 2512 | pdp->secondary_tei[pdp->nsapi & 0xf0] = 0; |
| 2513 | pdp->nodata = 1; |
| 2514 | } else { |
| 2515 | gtp_freepdp(gsn, pdp); |
| 2516 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2517 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2518 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2519 | /* if (cause == GTPCAUSE_ACC_REQ) */ |
| 2520 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2521 | } |
| 2522 | |
| 2523 | /* Handle Delete PDP Context Request */ |
| 2524 | int gtp_delete_pdp_ind(struct gsn_t *gsn, int version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2525 | struct sockaddr_in *peer, int fd, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2526 | void *pack, unsigned len) |
| 2527 | { |
| 2528 | struct pdp_t *pdp = NULL; |
| 2529 | struct pdp_t *linked_pdp = NULL; |
| 2530 | union gtpie_member *ie[GTPIE_SIZE]; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2531 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2532 | uint16_t seq = get_seq(pack); |
| 2533 | int hlen = get_hlen(pack); |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2534 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2535 | uint8_t nsapi; |
| 2536 | uint8_t teardown = 0; |
Pau Espin Pedrol | 9ee8d32 | 2019-05-30 14:11:22 +0200 | [diff] [blame] | 2537 | int count; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2538 | |
Pau Espin Pedrol | de72d26 | 2019-05-29 18:17:05 +0200 | [diff] [blame] | 2539 | /* Is this a duplicate ? */ |
| 2540 | if (!gtp_duplicate(gsn, version, peer, seq)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2541 | return 0; /* We allready send off response once */ |
| 2542 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2543 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2544 | /* Find the linked context in question */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2545 | if (gtp_pdp_getgtp1(gsn, &linked_pdp, get_tei(pack))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2546 | gsn->err_unknownpdp++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2547 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 2548 | "Unknown PDP context: %u\n", get_tei(pack)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2549 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, |
| 2550 | NULL, NULL, GTPCAUSE_NON_EXIST, |
| 2551 | teardown); |
| 2552 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2553 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2554 | /* If version 0 this is also the secondary context */ |
| 2555 | if (version == 0) |
| 2556 | pdp = linked_pdp; |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 2557 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2558 | /* Decode information elements */ |
| 2559 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 2560 | gsn->invalid++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2561 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2562 | "Invalid message format\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2563 | if (0 == version) |
| 2564 | return EOF; |
| 2565 | else |
| 2566 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, |
| 2567 | len, NULL, NULL, |
| 2568 | GTPCAUSE_INVALID_MESSAGE, |
| 2569 | teardown); |
| 2570 | } |
| 2571 | |
| 2572 | if (version == 1) { |
| 2573 | /* NSAPI (mandatory) */ |
| 2574 | if (gtpie_gettv1(ie, GTPIE_NSAPI, 0, &nsapi)) { |
| 2575 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2576 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2577 | len, "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2578 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, |
| 2579 | len, NULL, NULL, |
| 2580 | GTPCAUSE_MAN_IE_MISSING, |
| 2581 | teardown); |
| 2582 | } |
| 2583 | |
| 2584 | /* Find the context in question */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2585 | if (gtp_pdp_getgtp1(gsn, &pdp, linked_pdp->secondary_tei[nsapi & 0x0f])) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2586 | gsn->err_unknownpdp++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2587 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
| 2588 | len, "Unknown PDP context\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2589 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, |
| 2590 | len, NULL, NULL, |
| 2591 | GTPCAUSE_NON_EXIST, |
| 2592 | teardown); |
| 2593 | } |
| 2594 | |
| 2595 | /* Teardown (conditional) */ |
| 2596 | gtpie_gettv1(ie, GTPIE_TEARDOWN, 0, &teardown); |
| 2597 | |
| 2598 | if (!teardown) { |
Pau Espin Pedrol | 742a6b5 | 2019-05-30 13:36:10 +0200 | [diff] [blame] | 2599 | /* TS 29.060 section 7.3.5: If a GSN receives a Delete PDP context |
| 2600 | * without a Teardown Indicator or with a Teardown Indicator with |
| 2601 | * value set to "0" and only that PDP context is active for a PDN |
| 2602 | * connection, then the GSN shall ignore the message. (Note: |
| 2603 | * This is symptom of a race condition. The reliable delivery of |
| 2604 | * signalling messages will eventually lead to a consistent |
| 2605 | * situation, allowing the teardown of the PDP context.) |
| 2606 | */ |
Pau Espin Pedrol | 9ee8d32 | 2019-05-30 14:11:22 +0200 | [diff] [blame] | 2607 | count = pdp_count_secondary(linked_pdp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2608 | if (count <= 1) { |
Pau Espin Pedrol | d1bd6fc | 2018-07-13 19:11:45 +0200 | [diff] [blame] | 2609 | GTP_LOGPKG(LOGL_NOTICE, peer, pack, len, |
| 2610 | "Ignoring CTX DEL without teardown and count=%d\n", |
| 2611 | count); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2612 | return 0; /* 29.060 7.3.5 Ignore message */ |
| 2613 | } |
| 2614 | } |
| 2615 | } |
| 2616 | |
| 2617 | return gtp_delete_pdp_resp(gsn, version, peer, fd, pack, len, |
| 2618 | pdp, linked_pdp, GTPCAUSE_ACC_REQ, teardown); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2619 | } |
| 2620 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2621 | /* Handle Delete PDP Context Response */ |
| 2622 | int gtp_delete_pdp_conf(struct gsn_t *gsn, int version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2623 | struct sockaddr_in *peer, void *pack, unsigned len) |
| 2624 | { |
| 2625 | union gtpie_member *ie[GTPIE_SIZE]; |
| 2626 | uint8_t cause; |
| 2627 | void *cbp = NULL; |
| 2628 | uint8_t type = 0; |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2629 | struct pdp_t *pdp = NULL; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2630 | int hlen = get_hlen(pack); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2631 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2632 | /* Remove packet from queue */ |
| 2633 | if (gtp_conf(gsn, version, peer, pack, len, &type, &cbp)) |
| 2634 | return EOF; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2635 | |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2636 | /* Find the context in question. It may not be available if gtp_delete_context_req |
| 2637 | * was used and as a result the PDP ctx was already freed */ |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2638 | if (gtp_pdp_getgtp1(gsn, &pdp, get_tei(pack))) { |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2639 | gsn->err_unknownpdp++; |
| 2640 | GTP_LOGPKG(LOGL_NOTICE, peer, pack, len, |
Oliver Smith | 1cde2c1 | 2019-05-13 11:35:03 +0200 | [diff] [blame] | 2641 | "Unknown PDP context: %u (expected if gtp_delete_context_req is used or pdp ctx was freed manually before response)\n", |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2642 | get_tei(pack)); |
| 2643 | if (gsn->cb_conf) |
| 2644 | gsn->cb_conf(type, EOF, NULL, cbp); |
| 2645 | return EOF; |
| 2646 | } |
| 2647 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2648 | /* Decode information elements */ |
| 2649 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 2650 | gsn->invalid++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2651 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2652 | "Invalid message format\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2653 | if (gsn->cb_conf) |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2654 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2655 | return EOF; |
| 2656 | } |
| 2657 | |
| 2658 | /* Extract cause value (mandatory) */ |
| 2659 | if (gtpie_gettv1(ie, GTPIE_CAUSE, 0, &cause)) { |
| 2660 | gsn->missing++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2661 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2662 | "Missing mandatory information field\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2663 | if (gsn->cb_conf) |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2664 | gsn->cb_conf(type, EOF, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2665 | return EOF; |
| 2666 | } |
| 2667 | |
| 2668 | /* Check the cause value (again) */ |
| 2669 | if ((GTPCAUSE_ACC_REQ != cause) && (GTPCAUSE_NON_EXIST != cause)) { |
| 2670 | gsn->err_cause++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2671 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2672 | "Unexpected cause value received: %d\n", cause); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2673 | if (gsn->cb_conf) |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2674 | gsn->cb_conf(type, cause, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2675 | return EOF; |
| 2676 | } |
| 2677 | |
| 2678 | /* Callback function to notify application */ |
| 2679 | if (gsn->cb_conf) |
Pau Espin Pedrol | 8e8c7ef | 2018-07-16 16:47:12 +0200 | [diff] [blame] | 2680 | gsn->cb_conf(type, cause, pdp, cbp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2681 | |
| 2682 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2683 | } |
| 2684 | |
Harald Welte | 54d082e | 2017-08-12 22:43:21 +0200 | [diff] [blame] | 2685 | /* Send Error Indication (response to a GPDU message) - 3GPP TS 29.060 7.3.7 */ |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 2686 | static int gtp_error_ind_resp(struct gsn_t *gsn, uint8_t version, |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2687 | struct sockaddr_in *peer, int fd, |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2688 | void *pack, unsigned len) |
| 2689 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2690 | union gtp_packet packet; |
| 2691 | unsigned int length = get_default_gtp(version, GTP_ERROR, &packet); |
| 2692 | |
Harald Welte | 54d082e | 2017-08-12 22:43:21 +0200 | [diff] [blame] | 2693 | if (version == 1) { |
| 2694 | /* Mandatory 7.7.13 TEI Data I */ |
| 2695 | gtpie_tv4(&packet, &length, GTP_MAX, GTPIE_TEI_DI, |
| 2696 | ntoh32(((union gtp_packet *)pack)->gtp1l.h.tei)); |
| 2697 | |
| 2698 | /* Mandatory 7.7.32 GSN Address */ |
| 2699 | gtpie_tlv(&packet, &length, GTP_MAX, GTPIE_GSN_ADDR, |
| 2700 | sizeof(gsn->gsnu), &gsn->gsnu); |
| 2701 | } |
| 2702 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2703 | return gtp_resp(version, gsn, NULL, &packet, length, peer, fd, |
| 2704 | get_seq(pack), get_tid(pack)); |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2705 | } |
| 2706 | |
| 2707 | /* Handle Error Indication */ |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 2708 | static int gtp_error_ind_conf(struct gsn_t *gsn, uint8_t version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2709 | struct sockaddr_in *peer, void *pack, unsigned len) |
| 2710 | { |
Harald Welte | 37d5b15 | 2017-08-12 23:58:29 +0200 | [diff] [blame] | 2711 | union gtpie_member *ie[GTPIE_SIZE]; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2712 | struct pdp_t *pdp; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2713 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2714 | /* Find the context in question */ |
Harald Welte | 37d5b15 | 2017-08-12 23:58:29 +0200 | [diff] [blame] | 2715 | if (version == 0) { |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2716 | if (gtp_pdp_tidget(gsn, &pdp, be64toh(((union gtp_packet *)pack)->gtp0.h.tid))) { |
Harald Welte | 37d5b15 | 2017-08-12 23:58:29 +0200 | [diff] [blame] | 2717 | gsn->err_unknownpdp++; |
| 2718 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2719 | "Unknown PDP context\n"); |
| 2720 | return EOF; |
| 2721 | } |
| 2722 | } else if (version == 1) { |
| 2723 | /* we have to look-up based on the *peer* TEID */ |
| 2724 | int hlen = get_hlen(pack); |
| 2725 | uint32_t teid_gn; |
| 2726 | |
| 2727 | /* Decode information elements */ |
| 2728 | if (gtpie_decaps(ie, version, pack + hlen, len - hlen)) { |
| 2729 | gsn->invalid++; |
| 2730 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2731 | "Invalid message format\n"); |
| 2732 | return EOF; |
| 2733 | } |
| 2734 | |
| 2735 | if (gtpie_gettv4(ie, GTPIE_TEI_DI, 0, &teid_gn)) { |
| 2736 | gsn->missing++; |
| 2737 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2738 | "Missing mandatory information field\n"); |
| 2739 | return EOF; |
| 2740 | } |
| 2741 | |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2742 | if (gtp_pdp_getgtp1_peer_d(gsn, &pdp, peer, teid_gn)) { |
Harald Welte | 37d5b15 | 2017-08-12 23:58:29 +0200 | [diff] [blame] | 2743 | gsn->err_unknownpdp++; |
| 2744 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, "Unknown PDP context\n"); |
| 2745 | return EOF; |
| 2746 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2747 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2748 | |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2749 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
| 2750 | "Received Error Indication\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2751 | |
Harald Welte | bd22824 | 2017-11-06 03:16:49 +0900 | [diff] [blame] | 2752 | /* This is obvious from above code, given the semantics of the |
| 2753 | * functions above, but Coverity doesn't figure this out, so |
| 2754 | * let's make it clear. It's good style anyway in case above |
| 2755 | * code should ever change. */ |
| 2756 | OSMO_ASSERT(pdp); |
| 2757 | |
Pau Espin Pedrol | 0d0b059 | 2019-05-29 20:42:09 +0200 | [diff] [blame] | 2758 | gtp_freepdp(gsn, pdp); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2759 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
Pau Espin Pedrol | 07730bb | 2018-01-25 18:43:38 +0100 | [diff] [blame] | 2762 | static int gtp_gpdu_ind(struct gsn_t *gsn, uint8_t version, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2763 | struct sockaddr_in *peer, int fd, void *pack, unsigned len) |
| 2764 | { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2765 | |
Pau Espin Pedrol | 282d4e3 | 2018-01-25 18:20:51 +0100 | [diff] [blame] | 2766 | int hlen; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2767 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2768 | struct pdp_t *pdp; |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 2769 | |
Pau Espin Pedrol | 42d3250 | 2018-01-25 18:17:17 +0100 | [diff] [blame] | 2770 | switch (version) { |
| 2771 | case 0: |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2772 | if (gtp_pdp_getgtp0(gsn, &pdp, |
| 2773 | ntoh16(((union gtp_packet *)pack)->gtp0.h.flow))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2774 | gsn->err_unknownpdp++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2775 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
Max | 14b1b63 | 2017-08-21 20:14:59 +0200 | [diff] [blame] | 2776 | len, "Unknown PDP context, GTPv0\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2777 | return gtp_error_ind_resp(gsn, version, peer, fd, pack, |
| 2778 | len); |
| 2779 | } |
| 2780 | hlen = GTP0_HEADER_SIZE; |
Pau Espin Pedrol | 42d3250 | 2018-01-25 18:17:17 +0100 | [diff] [blame] | 2781 | break; |
| 2782 | case 1: |
Pau Espin Pedrol | 9fbcb10 | 2019-05-31 16:29:32 +0200 | [diff] [blame] | 2783 | if (gtp_pdp_getgtp1(gsn, &pdp, |
| 2784 | ntoh32(((union gtp_packet *)pack)->gtp1l.h.tei))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2785 | gsn->err_unknownpdp++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2786 | GTP_LOGPKG(LOGL_ERROR, peer, pack, |
Max | 14b1b63 | 2017-08-21 20:14:59 +0200 | [diff] [blame] | 2787 | len, "Unknown PDP context, GTPv1\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2788 | return gtp_error_ind_resp(gsn, version, peer, fd, pack, |
| 2789 | len); |
| 2790 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2791 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2792 | /* Is this a long or a short header ? */ |
| 2793 | if (((union gtp_packet *)pack)->gtp1l.h.flags & 0x07) |
| 2794 | hlen = GTP1_HEADER_SIZE_LONG; |
| 2795 | else |
| 2796 | hlen = GTP1_HEADER_SIZE_SHORT; |
Pau Espin Pedrol | 42d3250 | 2018-01-25 18:17:17 +0100 | [diff] [blame] | 2797 | break; |
| 2798 | default: |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2799 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 2800 | "Unknown version: %d\n", version); |
Pau Espin Pedrol | 282d4e3 | 2018-01-25 18:20:51 +0100 | [diff] [blame] | 2801 | return EOF; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2802 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2803 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2804 | /* If the GPDU was not from the peer GSN tell him to delete context */ |
| 2805 | if (memcmp(&peer->sin_addr, pdp->gsnru.v, pdp->gsnru.l)) { /* TODO Range? */ |
| 2806 | gsn->err_unknownpdp++; |
Max | 14b1b63 | 2017-08-21 20:14:59 +0200 | [diff] [blame] | 2807 | GTP_LOGPKG(LOGL_ERROR, peer, pack, len, "Unknown GSN peer %s\n", inet_ntoa(peer->sin_addr)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2808 | return gtp_error_ind_resp(gsn, version, peer, fd, pack, len); |
| 2809 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2810 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2811 | /* Callback function */ |
| 2812 | if (gsn->cb_data_ind != 0) |
| 2813 | return gsn->cb_data_ind(pdp, pack + hlen, len - hlen); |
| 2814 | |
| 2815 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 2818 | /* Receives GTP packet and sends off for further processing |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2819 | * Function will check the validity of the header. If the header |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 2820 | * is not valid the packet is either dropped or a version not |
| 2821 | * supported is returned to the peer. |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2822 | * TODO: Need to decide on return values! */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2823 | int gtp_decaps0(struct gsn_t *gsn) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2824 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2825 | unsigned char buffer[PACKET_MAX]; |
| 2826 | struct sockaddr_in peer; |
Harald Welte | d88e11d | 2011-11-02 13:09:43 +0100 | [diff] [blame] | 2827 | socklen_t peerlen; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2828 | int status; |
| 2829 | struct gtp0_header *pheader; |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 2830 | uint8_t version; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2831 | int fd = gsn->fd0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2832 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2833 | /* TODO: Need strategy of userspace buffering and blocking */ |
| 2834 | /* Currently read is non-blocking and send is blocking. */ |
| 2835 | /* This means that the program have to wait for busy send calls... */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 2836 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2837 | while (1) { /* Loop until no more to read */ |
| 2838 | if (fcntl(gsn->fd0, F_SETFL, O_NONBLOCK)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2839 | LOGP(DLGTP, LOGL_ERROR, "fnctl()\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2840 | return -1; |
| 2841 | } |
| 2842 | peerlen = sizeof(peer); |
| 2843 | if ((status = |
| 2844 | recvfrom(gsn->fd0, buffer, sizeof(buffer), 0, |
| 2845 | (struct sockaddr *)&peer, &peerlen)) < 0) { |
| 2846 | if (errno == EAGAIN) |
| 2847 | return 0; |
| 2848 | gsn->err_readfrom++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2849 | LOGP(DLGTP, LOGL_ERROR, |
Alexander Huemer | db852a1 | 2015-11-06 20:59:01 +0100 | [diff] [blame] | 2850 | "recvfrom(fd0=%d, buffer=%lx, len=%zu) failed: status = %d error = %s\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2851 | gsn->fd0, (unsigned long)buffer, sizeof(buffer), |
| 2852 | status, status ? strerror(errno) : "No error"); |
| 2853 | return -1; |
| 2854 | } |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 2855 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2856 | /* Need at least 1 byte in order to check version */ |
| 2857 | if (status < (1)) { |
| 2858 | gsn->empty++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2859 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 2860 | status, "Discarding packet - too small\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2861 | continue; |
| 2862 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2863 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2864 | pheader = (struct gtp0_header *)(buffer); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2865 | |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 2866 | version = GTPHDR_F_GET_VER(pheader->flags); |
| 2867 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2868 | /* Version should be gtp0 (or earlier) */ |
| 2869 | /* 09.60 is somewhat unclear on this issue. On gsn->fd0 we expect only */ |
| 2870 | /* GTP 0 messages. If other version message is received we reply that we */ |
| 2871 | /* only support version 0, implying that this is the only version */ |
| 2872 | /* supported on this port */ |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 2873 | if (version > 0) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2874 | gsn->unsup++; |
Pau Espin Pedrol | a884a95 | 2018-01-25 17:28:11 +0100 | [diff] [blame] | 2875 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 2876 | "Unsupported GTP version %"PRIu8"\n", version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2877 | gtp_unsup_req(gsn, 0, &peer, gsn->fd0, buffer, status); /* 29.60: 11.1.1 */ |
| 2878 | continue; |
| 2879 | } |
| 2880 | |
| 2881 | /* Check length of gtp0 packet */ |
| 2882 | if (status < GTP0_HEADER_SIZE) { |
| 2883 | gsn->tooshort++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2884 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 2885 | status, "GTP0 packet too short\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2886 | continue; /* Silently discard 29.60: 11.1.2 */ |
| 2887 | } |
| 2888 | |
| 2889 | /* Check packet length field versus length of packet */ |
| 2890 | if (status != (ntoh16(pheader->length) + GTP0_HEADER_SIZE)) { |
| 2891 | gsn->tooshort++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2892 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2893 | status, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2894 | "GTP packet length field does not match actual length\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2895 | continue; /* Silently discard */ |
| 2896 | } |
| 2897 | |
| 2898 | if ((gsn->mode == GTP_MODE_GGSN) && |
| 2899 | ((pheader->type == GTP_CREATE_PDP_RSP) || |
Pau Espin Pedrol | a32e4c4 | 2018-07-13 19:05:00 +0200 | [diff] [blame] | 2900 | (pheader->type == GTP_UPDATE_PDP_RSP))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2901 | gsn->unexpect++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2902 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2903 | status, |
Pau Espin Pedrol | 3b84e92 | 2018-07-13 18:32:35 +0200 | [diff] [blame] | 2904 | "Unexpected GTPv0 Signalling Message '%s'\n", |
| 2905 | get_value_string(gtp_type_names, pheader->type)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2906 | continue; /* Silently discard 29.60: 11.1.4 */ |
| 2907 | } |
| 2908 | |
| 2909 | if ((gsn->mode == GTP_MODE_SGSN) && |
| 2910 | ((pheader->type == GTP_CREATE_PDP_REQ) || |
Pau Espin Pedrol | a32e4c4 | 2018-07-13 19:05:00 +0200 | [diff] [blame] | 2911 | (pheader->type == GTP_UPDATE_PDP_REQ))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2912 | gsn->unexpect++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2913 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2914 | status, |
Pau Espin Pedrol | 3b84e92 | 2018-07-13 18:32:35 +0200 | [diff] [blame] | 2915 | "Unexpected GTPv0 Signalling Message '%s'\n", |
| 2916 | get_value_string(gtp_type_names, pheader->type)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2917 | continue; /* Silently discard 29.60: 11.1.4 */ |
| 2918 | } |
| 2919 | |
| 2920 | switch (pheader->type) { |
| 2921 | case GTP_ECHO_REQ: |
| 2922 | gtp_echo_ind(gsn, version, &peer, fd, buffer, status); |
| 2923 | break; |
| 2924 | case GTP_ECHO_RSP: |
| 2925 | gtp_echo_conf(gsn, version, &peer, buffer, status); |
| 2926 | break; |
| 2927 | case GTP_NOT_SUPPORTED: |
| 2928 | gtp_unsup_ind(gsn, &peer, buffer, status); |
| 2929 | break; |
| 2930 | case GTP_CREATE_PDP_REQ: |
| 2931 | gtp_create_pdp_ind(gsn, version, &peer, fd, buffer, |
| 2932 | status); |
| 2933 | break; |
| 2934 | case GTP_CREATE_PDP_RSP: |
| 2935 | gtp_create_pdp_conf(gsn, version, &peer, buffer, |
| 2936 | status); |
| 2937 | break; |
| 2938 | case GTP_UPDATE_PDP_REQ: |
| 2939 | gtp_update_pdp_ind(gsn, version, &peer, fd, buffer, |
| 2940 | status); |
| 2941 | break; |
| 2942 | case GTP_UPDATE_PDP_RSP: |
| 2943 | gtp_update_pdp_conf(gsn, version, &peer, buffer, |
| 2944 | status); |
| 2945 | break; |
| 2946 | case GTP_DELETE_PDP_REQ: |
| 2947 | gtp_delete_pdp_ind(gsn, version, &peer, fd, buffer, |
| 2948 | status); |
| 2949 | break; |
| 2950 | case GTP_DELETE_PDP_RSP: |
| 2951 | gtp_delete_pdp_conf(gsn, version, &peer, buffer, |
| 2952 | status); |
| 2953 | break; |
| 2954 | case GTP_ERROR: |
| 2955 | gtp_error_ind_conf(gsn, version, &peer, buffer, status); |
| 2956 | break; |
| 2957 | case GTP_GPDU: |
| 2958 | gtp_gpdu_ind(gsn, version, &peer, fd, buffer, status); |
| 2959 | break; |
| 2960 | default: |
| 2961 | gsn->unknown++; |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 2962 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 2963 | "Unknown GTP message type received: %d\n", |
| 2964 | pheader->type); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2965 | break; |
| 2966 | } |
| 2967 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2970 | int gtp_decaps1c(struct gsn_t *gsn) |
| 2971 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2972 | unsigned char buffer[PACKET_MAX]; |
| 2973 | struct sockaddr_in peer; |
Harald Welte | d88e11d | 2011-11-02 13:09:43 +0100 | [diff] [blame] | 2974 | socklen_t peerlen; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2975 | int status; |
| 2976 | struct gtp1_header_short *pheader; |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 2977 | uint8_t version; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2978 | int fd = gsn->fd1c; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2979 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2980 | /* TODO: Need strategy of userspace buffering and blocking */ |
| 2981 | /* Currently read is non-blocking and send is blocking. */ |
| 2982 | /* This means that the program have to wait for busy send calls... */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 2983 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2984 | while (1) { /* Loop until no more to read */ |
| 2985 | if (fcntl(fd, F_SETFL, O_NONBLOCK)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2986 | LOGP(DLGTP, LOGL_ERROR, "fnctl()\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2987 | return -1; |
| 2988 | } |
| 2989 | peerlen = sizeof(peer); |
| 2990 | if ((status = |
| 2991 | recvfrom(fd, buffer, sizeof(buffer), 0, |
| 2992 | (struct sockaddr *)&peer, &peerlen)) < 0) { |
| 2993 | if (errno == EAGAIN) |
| 2994 | return 0; |
| 2995 | gsn->err_readfrom++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 2996 | LOGP(DLGTP, LOGL_ERROR, |
Alexander Huemer | db852a1 | 2015-11-06 20:59:01 +0100 | [diff] [blame] | 2997 | "recvfrom(fd=%d, buffer=%lx, len=%zu) failed: status = %d error = %s\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 2998 | fd, (unsigned long)buffer, sizeof(buffer), |
| 2999 | status, status ? strerror(errno) : "No error"); |
| 3000 | return -1; |
| 3001 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3002 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3003 | /* Need at least 1 byte in order to check version */ |
| 3004 | if (status < (1)) { |
| 3005 | gsn->empty++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3006 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 3007 | status, "Discarding packet - too small\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3008 | continue; |
| 3009 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3010 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3011 | pheader = (struct gtp1_header_short *)(buffer); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3012 | |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 3013 | version = GTPHDR_F_GET_VER(pheader->flags); |
| 3014 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3015 | /* Version must be no larger than GTP 1 */ |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 3016 | if (version > 1) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3017 | gsn->unsup++; |
Pau Espin Pedrol | a884a95 | 2018-01-25 17:28:11 +0100 | [diff] [blame] | 3018 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 3019 | "Unsupported GTP version %"PRIu8"\n", version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3020 | gtp_unsup_req(gsn, version, &peer, fd, buffer, status); |
| 3021 | /*29.60: 11.1.1 */ |
| 3022 | continue; |
| 3023 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3024 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3025 | /* Version must be at least GTP 1 */ |
| 3026 | /* 29.060 is somewhat unclear on this issue. On gsn->fd1c we expect only */ |
| 3027 | /* GTP 1 messages. If GTP 0 message is received we silently discard */ |
| 3028 | /* the message */ |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 3029 | if (version < 1) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3030 | gsn->unsup++; |
Pau Espin Pedrol | a884a95 | 2018-01-25 17:28:11 +0100 | [diff] [blame] | 3031 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 3032 | "Unsupported GTP version %"PRIu8"\n", version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3033 | continue; |
| 3034 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3035 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3036 | /* Check packet flag field */ |
| 3037 | if (((pheader->flags & 0xf7) != 0x32)) { |
| 3038 | gsn->unsup++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3039 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | d37b80a | 2016-12-15 18:33:15 +0100 | [diff] [blame] | 3040 | status, "Unsupported packet flags: 0x%02x\n", pheader->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3041 | continue; |
| 3042 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 3043 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3044 | /* Check length of packet */ |
| 3045 | if (status < GTP1_HEADER_SIZE_LONG) { |
| 3046 | gsn->tooshort++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3047 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 3048 | status, "GTP packet too short\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3049 | continue; /* Silently discard 29.60: 11.1.2 */ |
| 3050 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 3051 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3052 | /* Check packet length field versus length of packet */ |
| 3053 | if (status != |
| 3054 | (ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT)) { |
| 3055 | gsn->tooshort++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3056 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3057 | status, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3058 | "GTP packet length field does not match actual length\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3059 | continue; /* Silently discard */ |
| 3060 | } |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 3061 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3062 | /* Check for extension headers */ |
| 3063 | /* TODO: We really should cycle through the headers and determine */ |
| 3064 | /* if any have the comprehension required flag set */ |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 3065 | if (((pheader->flags & GTP1HDR_F_EXT) != 0x00)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3066 | gsn->unsup++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3067 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 3068 | status, "Unsupported extension header\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3069 | gtp_extheader_req(gsn, version, &peer, fd, buffer, |
| 3070 | status); |
jjako | 1db1c81 | 2003-07-06 20:53:57 +0000 | [diff] [blame] | 3071 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3072 | continue; |
| 3073 | } |
| 3074 | |
| 3075 | if ((gsn->mode == GTP_MODE_GGSN) && |
| 3076 | ((pheader->type == GTP_CREATE_PDP_RSP) || |
Pau Espin Pedrol | a32e4c4 | 2018-07-13 19:05:00 +0200 | [diff] [blame] | 3077 | (pheader->type == GTP_UPDATE_PDP_RSP))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3078 | gsn->unexpect++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3079 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3080 | status, |
Pau Espin Pedrol | 3b84e92 | 2018-07-13 18:32:35 +0200 | [diff] [blame] | 3081 | "Unexpected GTPv1 Signalling Message '%s'\n", |
| 3082 | get_value_string(gtp_type_names, pheader->type)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3083 | continue; /* Silently discard 29.60: 11.1.4 */ |
| 3084 | } |
| 3085 | |
| 3086 | if ((gsn->mode == GTP_MODE_SGSN) && |
| 3087 | ((pheader->type == GTP_CREATE_PDP_REQ) || |
Pau Espin Pedrol | a32e4c4 | 2018-07-13 19:05:00 +0200 | [diff] [blame] | 3088 | (pheader->type == GTP_UPDATE_PDP_REQ))) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3089 | gsn->unexpect++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3090 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3091 | status, |
Pau Espin Pedrol | 3b84e92 | 2018-07-13 18:32:35 +0200 | [diff] [blame] | 3092 | "Unexpected GTPv1 Signalling Message '%s'\n", |
| 3093 | get_value_string(gtp_type_names, pheader->type)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3094 | continue; /* Silently discard 29.60: 11.1.4 */ |
| 3095 | } |
| 3096 | |
| 3097 | switch (pheader->type) { |
| 3098 | case GTP_ECHO_REQ: |
| 3099 | gtp_echo_ind(gsn, version, &peer, fd, buffer, status); |
| 3100 | break; |
| 3101 | case GTP_ECHO_RSP: |
| 3102 | gtp_echo_conf(gsn, version, &peer, buffer, status); |
| 3103 | break; |
| 3104 | case GTP_NOT_SUPPORTED: |
| 3105 | gtp_unsup_ind(gsn, &peer, buffer, status); |
| 3106 | break; |
| 3107 | case GTP_SUPP_EXT_HEADER: |
| 3108 | gtp_extheader_ind(gsn, &peer, buffer, status); |
| 3109 | break; |
| 3110 | case GTP_CREATE_PDP_REQ: |
| 3111 | gtp_create_pdp_ind(gsn, version, &peer, fd, buffer, |
| 3112 | status); |
| 3113 | break; |
| 3114 | case GTP_CREATE_PDP_RSP: |
| 3115 | gtp_create_pdp_conf(gsn, version, &peer, buffer, |
| 3116 | status); |
| 3117 | break; |
| 3118 | case GTP_UPDATE_PDP_REQ: |
| 3119 | gtp_update_pdp_ind(gsn, version, &peer, fd, buffer, |
| 3120 | status); |
| 3121 | break; |
| 3122 | case GTP_UPDATE_PDP_RSP: |
| 3123 | gtp_update_pdp_conf(gsn, version, &peer, buffer, |
| 3124 | status); |
| 3125 | break; |
| 3126 | case GTP_DELETE_PDP_REQ: |
| 3127 | gtp_delete_pdp_ind(gsn, version, &peer, fd, buffer, |
| 3128 | status); |
| 3129 | break; |
| 3130 | case GTP_DELETE_PDP_RSP: |
| 3131 | gtp_delete_pdp_conf(gsn, version, &peer, buffer, |
| 3132 | status); |
| 3133 | break; |
| 3134 | case GTP_ERROR: |
| 3135 | gtp_error_ind_conf(gsn, version, &peer, buffer, status); |
| 3136 | break; |
| 3137 | default: |
| 3138 | gsn->unknown++; |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 3139 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 3140 | "Unknown GTP message type received: %u\n", |
| 3141 | pheader->type); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3142 | break; |
| 3143 | } |
| 3144 | } |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3147 | int gtp_decaps1u(struct gsn_t *gsn) |
| 3148 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3149 | unsigned char buffer[PACKET_MAX]; |
| 3150 | struct sockaddr_in peer; |
Harald Welte | d88e11d | 2011-11-02 13:09:43 +0100 | [diff] [blame] | 3151 | socklen_t peerlen; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3152 | int status; |
| 3153 | struct gtp1_header_short *pheader; |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 3154 | uint8_t version; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3155 | int fd = gsn->fd1u; |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3156 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3157 | /* TODO: Need strategy of userspace buffering and blocking */ |
| 3158 | /* Currently read is non-blocking and send is blocking. */ |
| 3159 | /* This means that the program have to wait for busy send calls... */ |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3160 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3161 | while (1) { /* Loop until no more to read */ |
| 3162 | if (fcntl(gsn->fd1u, F_SETFL, O_NONBLOCK)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3163 | LOGP(DLGTP, LOGL_ERROR, "fnctl()\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3164 | return -1; |
| 3165 | } |
| 3166 | peerlen = sizeof(peer); |
| 3167 | if ((status = |
| 3168 | recvfrom(gsn->fd1u, buffer, sizeof(buffer), 0, |
| 3169 | (struct sockaddr *)&peer, &peerlen)) < 0) { |
| 3170 | if (errno == EAGAIN) |
| 3171 | return 0; |
| 3172 | gsn->err_readfrom++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3173 | LOGP(DLGTP, LOGL_ERROR, |
Alexander Huemer | db852a1 | 2015-11-06 20:59:01 +0100 | [diff] [blame] | 3174 | "recvfrom(fd1u=%d, buffer=%lx, len=%zu) failed: status = %d error = %s\n", |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3175 | gsn->fd1u, (unsigned long)buffer, |
| 3176 | sizeof(buffer), status, |
| 3177 | status ? strerror(errno) : "No error"); |
| 3178 | return -1; |
| 3179 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3180 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3181 | /* Need at least 1 byte in order to check version */ |
| 3182 | if (status < (1)) { |
| 3183 | gsn->empty++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3184 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 3185 | status, "Discarding packet - too small\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3186 | continue; |
| 3187 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3188 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3189 | pheader = (struct gtp1_header_short *)(buffer); |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3190 | |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 3191 | version = GTPHDR_F_GET_VER(pheader->flags); |
| 3192 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3193 | /* Version must be no larger than GTP 1 */ |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 3194 | if (version > 1) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3195 | gsn->unsup++; |
Pau Espin Pedrol | a884a95 | 2018-01-25 17:28:11 +0100 | [diff] [blame] | 3196 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 3197 | "Unsupported GTP version %"PRIu8"\n", version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3198 | gtp_unsup_req(gsn, 1, &peer, gsn->fd1c, buffer, status); /*29.60: 11.1.1 */ |
| 3199 | continue; |
| 3200 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3201 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3202 | /* Version must be at least GTP 1 */ |
| 3203 | /* 29.060 is somewhat unclear on this issue. On gsn->fd1c we expect only */ |
| 3204 | /* GTP 1 messages. If GTP 0 message is received we silently discard */ |
| 3205 | /* the message */ |
Pau Espin Pedrol | a4aada0 | 2018-01-25 17:24:38 +0100 | [diff] [blame] | 3206 | if (version < 1) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3207 | gsn->unsup++; |
Pau Espin Pedrol | a884a95 | 2018-01-25 17:28:11 +0100 | [diff] [blame] | 3208 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 3209 | "Unsupported GTP version %"PRIu8"\n", version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3210 | continue; |
| 3211 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 3212 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3213 | /* Check packet flag field (allow both with and without sequence number) */ |
| 3214 | if (((pheader->flags & 0xf5) != 0x30)) { |
| 3215 | gsn->unsup++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3216 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | d37b80a | 2016-12-15 18:33:15 +0100 | [diff] [blame] | 3217 | status, "Unsupported packet flags 0x%02x\n", pheader->flags); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3218 | continue; |
| 3219 | } |
jjako | 2c38133 | 2003-10-21 19:09:53 +0000 | [diff] [blame] | 3220 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3221 | /* Check length of packet */ |
| 3222 | if (status < GTP1_HEADER_SIZE_SHORT) { |
| 3223 | gsn->tooshort++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3224 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 3225 | status, "GTP packet too short\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3226 | continue; /* Silently discard 29.60: 11.1.2 */ |
| 3227 | } |
| 3228 | |
| 3229 | /* Check packet length field versus length of packet */ |
| 3230 | if (status != |
| 3231 | (ntoh16(pheader->length) + GTP1_HEADER_SIZE_SHORT)) { |
| 3232 | gsn->tooshort++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3233 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3234 | status, |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3235 | "GTP packet length field does not match actual length\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3236 | continue; /* Silently discard */ |
| 3237 | } |
| 3238 | |
| 3239 | /* Check for extension headers */ |
| 3240 | /* TODO: We really should cycle through the headers and determine */ |
| 3241 | /* if any have the comprehension required flag set */ |
Harald Welte | fed598f | 2017-09-24 16:39:22 +0800 | [diff] [blame] | 3242 | if (((pheader->flags & GTP1HDR_F_EXT) != 0x00)) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3243 | gsn->unsup++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3244 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, |
| 3245 | status, "Unsupported extension header\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3246 | gtp_extheader_req(gsn, version, &peer, fd, buffer, |
| 3247 | status); |
| 3248 | |
| 3249 | continue; |
| 3250 | } |
| 3251 | |
| 3252 | switch (pheader->type) { |
| 3253 | case GTP_ECHO_REQ: |
| 3254 | gtp_echo_ind(gsn, version, &peer, fd, buffer, status); |
| 3255 | break; |
| 3256 | case GTP_ECHO_RSP: |
| 3257 | gtp_echo_conf(gsn, version, &peer, buffer, status); |
| 3258 | break; |
| 3259 | case GTP_SUPP_EXT_HEADER: |
| 3260 | gtp_extheader_ind(gsn, &peer, buffer, status); |
| 3261 | break; |
| 3262 | case GTP_ERROR: |
| 3263 | gtp_error_ind_conf(gsn, version, &peer, buffer, status); |
| 3264 | break; |
| 3265 | /* Supported header extensions */ |
| 3266 | case GTP_GPDU: |
| 3267 | gtp_gpdu_ind(gsn, version, &peer, fd, buffer, status); |
| 3268 | break; |
| 3269 | default: |
| 3270 | gsn->unknown++; |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 3271 | GTP_LOGPKG(LOGL_ERROR, &peer, buffer, status, |
| 3272 | "Unknown GTP message type received: %u\n", |
| 3273 | pheader->type); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3274 | break; |
| 3275 | } |
| 3276 | } |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3279 | int gtp_data_req(struct gsn_t *gsn, struct pdp_t *pdp, void *pack, unsigned len) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3280 | { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3281 | union gtp_packet packet; |
| 3282 | struct sockaddr_in addr; |
Harald Welte | 471e349 | 2017-09-24 16:12:39 +0800 | [diff] [blame] | 3283 | struct msghdr msgh; |
| 3284 | struct iovec iov[2]; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3285 | int fd; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3286 | |
Harald Welte | 471e349 | 2017-09-24 16:12:39 +0800 | [diff] [blame] | 3287 | /* prepare destination address */ |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3288 | memset(&addr, 0, sizeof(addr)); |
| 3289 | addr.sin_family = AF_INET; |
jjako | 0fe0df0 | 2004-09-17 11:30:40 +0000 | [diff] [blame] | 3290 | #if defined(__FreeBSD__) || defined(__APPLE__) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3291 | addr.sin_len = sizeof(addr); |
jjako | 06e9f12 | 2004-01-19 18:37:58 +0000 | [diff] [blame] | 3292 | #endif |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3293 | memcpy(&addr.sin_addr, pdp->gsnru.v, pdp->gsnru.l); /* TODO range check */ |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3294 | |
Harald Welte | 471e349 | 2017-09-24 16:12:39 +0800 | [diff] [blame] | 3295 | /* prepare msghdr */ |
| 3296 | memset(&msgh, 0, sizeof(msgh)); |
| 3297 | msgh.msg_name = &addr; |
| 3298 | msgh.msg_namelen = sizeof(addr); |
| 3299 | msgh.msg_iov = iov; |
| 3300 | msgh.msg_iovlen = ARRAY_SIZE(iov); |
| 3301 | |
| 3302 | /* prepare iovectors */ |
| 3303 | iov[0].iov_base = &packet; |
| 3304 | /* iov[0].iov_len is not known here yet */ |
| 3305 | iov[1].iov_base = pack; |
| 3306 | iov[1].iov_len = len; |
| 3307 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3308 | if (pdp->version == 0) { |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3309 | |
Harald Welte | 471e349 | 2017-09-24 16:12:39 +0800 | [diff] [blame] | 3310 | iov[0].iov_len = GTP0_HEADER_SIZE; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3311 | addr.sin_port = htons(GTP0_PORT); |
| 3312 | fd = gsn->fd0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3313 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3314 | get_default_gtp(0, GTP_GPDU, &packet); |
| 3315 | packet.gtp0.h.length = hton16(len); |
Harald Welte | 3c1cce2 | 2017-09-24 16:40:12 +0800 | [diff] [blame] | 3316 | if (pdp->tx_gpdu_seq) |
| 3317 | packet.gtp0.h.seq = hton16(pdp->gtpsntx++); |
| 3318 | else |
| 3319 | packet.gtp0.h.seq = 0; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3320 | packet.gtp0.h.flow = hton16(pdp->flru); |
Pablo Neira Ayuso | 746b944 | 2014-03-24 17:58:27 +0100 | [diff] [blame] | 3321 | packet.gtp0.h.tid = htobe64(pdp_gettid(pdp->imsi, pdp->nsapi)); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3322 | } else if (pdp->version == 1) { |
jjako | 08d331d | 2003-10-13 20:33:30 +0000 | [diff] [blame] | 3323 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3324 | addr.sin_port = htons(GTP1U_PORT); |
| 3325 | fd = gsn->fd1u; |
jjako | a7cd249 | 2003-04-11 09:40:12 +0000 | [diff] [blame] | 3326 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3327 | get_default_gtp(1, GTP_GPDU, &packet); |
Harald Welte | 3c1cce2 | 2017-09-24 16:40:12 +0800 | [diff] [blame] | 3328 | if (pdp->tx_gpdu_seq) { |
| 3329 | packet.gtp1l.h.seq = hton16(pdp->gtpsntx++); |
| 3330 | packet.gtp1l.h.length = hton16(len - GTP1_HEADER_SIZE_SHORT + |
| 3331 | GTP1_HEADER_SIZE_LONG); |
| 3332 | packet.gtp1l.h.tei = hton32(pdp->teid_gn); |
| 3333 | iov[0].iov_len = GTP1_HEADER_SIZE_LONG; |
| 3334 | } else { |
| 3335 | packet.gtp1s.h.flags &= ~GTP1HDR_F_SEQ; |
| 3336 | packet.gtp1s.h.length = hton16(len); |
| 3337 | packet.gtp1s.h.tei = hton32(pdp->teid_gn); |
| 3338 | iov[0].iov_len = GTP1_HEADER_SIZE_SHORT; |
| 3339 | } |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3340 | } else { |
Holger Hans Peter Freyther | 01b40d0 | 2014-12-04 15:01:53 +0100 | [diff] [blame] | 3341 | LOGP(DLGTP, LOGL_ERROR, "Unknown version: %d\n", pdp->version); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3342 | return EOF; |
| 3343 | } |
| 3344 | |
| 3345 | if (fcntl(fd, F_SETFL, 0)) { |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3346 | LOGP(DLGTP, LOGL_ERROR, "fnctl()\n"); |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3347 | return -1; |
| 3348 | } |
| 3349 | |
Harald Welte | 471e349 | 2017-09-24 16:12:39 +0800 | [diff] [blame] | 3350 | if (sendmsg(fd, &msgh, 0) < 0) { |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3351 | gsn->err_sendto++; |
Holger Hans Peter Freyther | 42ca1d1 | 2014-12-03 20:18:43 +0100 | [diff] [blame] | 3352 | LOGP(DLGTP, LOGL_ERROR, |
Harald Welte | 471e349 | 2017-09-24 16:12:39 +0800 | [diff] [blame] | 3353 | "sendmsg(fd=%d, msg=%lx, len=%d) failed: Error = %s\n", fd, |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3354 | (unsigned long)&packet, GTP0_HEADER_SIZE + len, |
| 3355 | strerror(errno)); |
| 3356 | return EOF; |
| 3357 | } |
| 3358 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3361 | /* *********************************************************** |
| 3362 | * Conversion functions |
| 3363 | *************************************************************/ |
| 3364 | |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3365 | /* *********************************************************** |
| 3366 | * IP address conversion functions |
| 3367 | * There exist several types of address representations: |
Pau Espin Pedrol | 732131d | 2018-01-25 17:23:09 +0100 | [diff] [blame] | 3368 | * - eua: End User Address. (29.060, 7.7.27, message type 128) |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3369 | * Used for signalling address to mobile station. Supports IPv4 |
| 3370 | * IPv6 x.25 etc. etc. |
| 3371 | * - gsna: GSN Address. (29.060, 7.7.32, message type 133): IP address |
| 3372 | * of GSN. If length is 4 it is IPv4. If length is 16 it is IPv6. |
| 3373 | * - in_addr: IPv4 address struct. |
| 3374 | * - sockaddr_in: Socket API representation of IP address and |
| 3375 | * port number. |
| 3376 | *************************************************************/ |
| 3377 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3378 | int ipv42eua(struct ul66_t *eua, struct in_addr *src) |
| 3379 | { |
Harald Welte | cee7546 | 2017-09-24 17:45:05 +0800 | [diff] [blame] | 3380 | eua->v[0] = PDP_EUA_ORG_IETF; |
| 3381 | eua->v[1] = PDP_EUA_TYPE_v4; |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3382 | if (src) { |
| 3383 | eua->l = 6; |
| 3384 | memcpy(&eua->v[2], src, 4); |
| 3385 | } else { |
| 3386 | eua->l = 2; |
| 3387 | } |
| 3388 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3391 | int eua2ipv4(struct in_addr *dst, struct ul66_t *eua) |
| 3392 | { |
Harald Welte | cee7546 | 2017-09-24 17:45:05 +0800 | [diff] [blame] | 3393 | if ((eua->l != 6) || (eua->v[0] != PDP_EUA_ORG_IETF) || (eua->v[1] != PDP_EUA_TYPE_v4)) |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3394 | return -1; /* Not IPv4 address */ |
| 3395 | memcpy(dst, &eua->v[2], 4); |
| 3396 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3397 | } |
| 3398 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3399 | int gsna2in_addr(struct in_addr *dst, struct ul16_t *gsna) |
| 3400 | { |
| 3401 | memset(dst, 0, sizeof(struct in_addr)); |
| 3402 | if (gsna->l != 4) |
| 3403 | return EOF; /* Return if not IPv4 */ |
| 3404 | memcpy(dst, gsna->v, gsna->l); |
| 3405 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3406 | } |
| 3407 | |
Harald Welte | bed35df | 2011-11-02 13:06:18 +0100 | [diff] [blame] | 3408 | int in_addr2gsna(struct ul16_t *gsna, struct in_addr *src) |
| 3409 | { |
| 3410 | memset(gsna, 0, sizeof(struct ul16_t)); |
| 3411 | gsna->l = 4; |
| 3412 | memcpy(gsna->v, src, gsna->l); |
| 3413 | return 0; |
jjako | 52c2414 | 2002-12-16 13:33:51 +0000 | [diff] [blame] | 3414 | } |
Harald Welte | b10ee08 | 2017-08-12 19:29:16 +0200 | [diff] [blame] | 3415 | |
| 3416 | /* TS 29.060 has yet again a different encoding for IMSIs than |
| 3417 | * what we have in other places, so we cannot use the gsm48 |
| 3418 | * decoding functions. Also, libgtp uses an uint64_t in |
| 3419 | * _network byte order_ to contain BCD digits ?!? */ |
| 3420 | const char *imsi_gtp2str(const uint64_t *imsi) |
| 3421 | { |
Harald Welte | a06120d | 2017-11-06 03:12:54 +0900 | [diff] [blame] | 3422 | static char buf[sizeof(*imsi)*2+1]; |
Harald Welte | b10ee08 | 2017-08-12 19:29:16 +0200 | [diff] [blame] | 3423 | const uint8_t *imsi8 = (const uint8_t *) imsi; |
| 3424 | unsigned int i, j = 0; |
| 3425 | |
| 3426 | for (i = 0; i < sizeof(*imsi); i++) { |
| 3427 | uint8_t nibble; |
| 3428 | |
| 3429 | nibble = imsi8[i] & 0xf; |
| 3430 | if (nibble == 0xf) |
| 3431 | break; |
| 3432 | buf[j++] = osmo_bcd2char(nibble); |
| 3433 | |
| 3434 | nibble = imsi8[i] >> 4; |
| 3435 | if (nibble == 0xf) |
| 3436 | break; |
| 3437 | buf[j++] = osmo_bcd2char(nibble); |
| 3438 | } |
| 3439 | |
| 3440 | buf[j++] = '\0'; |
| 3441 | return buf; |
| 3442 | } |