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