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