Debian Codegraph @

lib/gtp/gtp_xact.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 #define TRACE_MODULE _gtp_xact
2 #include "core_debug.h"
3 #include "core_pool.h"
4 #include "core_event.h"
5
6 #include "3gpp_types.h"
7 #include "gtp_message.h"
8 #include "gtp_node.h"
9 #include "gtp_path.h"
10
11 #include "gtp_xact.h"
12
13 #define SIZE_OF_GTP_XACT_POOL 64
14 #define GTP_MIN_XACT_ID 1
15 #define GTP_MAX_XACT_ID 0x800000
16
17 #define GTP_T3_RESPONSE_DURATION 3000 /* 3 seconds */
18 #define GTP_T3_RESPONSE_RETRY_COUNT 3
19 #define GTP_T3_DUPLICATED_DURATION \
20 (GTP_T3_RESPONSE_DURATION * GTP_T3_RESPONSE_RETRY_COUNT) /* 9 seconds */
21 #define GTP_T3_DUPLICATED_RETRY_COUNT 1
22
23 typedef enum {
24 GTP_XACT_UNKNOWN_STAGE,
25 GTP_XACT_INITIAL_STAGE,
26 GTP_XACT_INTERMEDIATE_STAGE,
27 GTP_XACT_FINAL_STAGE,
28 } gtp_xact_stage_t;
29
30 static int gtp_xact_initialized = 0;
31 static tm_service_t *g_tm_service = NULL;
32 static c_uintptr_t g_response_event = 0;
33 static c_uintptr_t g_holding_event = 0;
34 static c_uint32_t g_xact_id = 0;
35
36 index_declare(gtp_xact_pool, gtp_xact_t, SIZE_OF_GTP_XACT_POOL);
37
38 static gtp_xact_stage_t gtp_xact_get_stage(c_uint8_t type, c_uint32_t sqn);
39 static status_t gtp_xact_delete(gtp_xact_t *xact);
40
41 status_t gtp_xact_init(tm_service_t *tm_service,
42 c_uintptr_t response_event, c_uintptr_t holding_event)
43 {
44 d_assert(gtp_xact_initialized == 0, return CORE_ERROR,
45 "GTP Transaction already has been initialized");
46
47 index_init(&gtp_xact_pool, SIZE_OF_GTP_XACT_POOL);
48
49 g_xact_id = 0;
50 g_tm_service = tm_service;
51 g_response_event = response_event;
52 g_holding_event = holding_event;
53
54 gtp_xact_initialized = 1;
55
56 return CORE_OK;
57 }
58
59 status_t gtp_xact_final(void)
60 {
61 d_assert(gtp_xact_initialized == 1, return CORE_ERROR,
62 "GTP Transaction context already has been finalized");
63
64 if (pool_used(&gtp_xact_pool))
65 d_error("%d not freed in gtp_xact_pool[%d] of GTP Transaction",
66 pool_used(&gtp_xact_pool), pool_size(&gtp_xact_pool));
67 d_trace(15, "%d not freed in gtp_xact_pool[%d] of GTP Transaction\n",
68 pool_used(&gtp_xact_pool), pool_size(&gtp_xact_pool));
69 index_final(&gtp_xact_pool);
70
71 gtp_xact_initialized = 0;
72
73 return CORE_OK;
74 }
75
76 gtp_xact_t *gtp_xact_local_create(
77 gtp_node_t *gnode, gtp_header_t *hdesc, pkbuf_t *pkbuf)
78 {
79 status_t rv;
80 char buf[CORE_ADDRSTRLEN];
81 gtp_xact_t *xact = NULL;
82
83 d_assert(gnode, return NULL, "Null param");
84
85 index_alloc(&gtp_xact_pool, &xact);
86 d_assert(xact, return NULL, "Transaction allocation failed");
87
88 xact->org = GTP_LOCAL_ORIGINATOR;
89 xact->xid = NEXT_ID(g_xact_id, GTP_MIN_XACT_ID, GTP_MAX_XACT_ID);
90 xact->gnode = gnode;
91
92 if (g_response_event)
93 {
94 xact->tm_response = timer_create(g_tm_service,
95 g_response_event, GTP_T3_RESPONSE_DURATION);
96 d_assert(xact->tm_response, return NULL, "Timer allocation failed");
97 timer_set_param1(xact->tm_response, xact->index);
98 xact->response_rcount = GTP_T3_RESPONSE_RETRY_COUNT;
99 }
100
101 if (g_holding_event)
102 {
103 xact->tm_holding = timer_create(g_tm_service,
104 g_holding_event, GTP_T3_DUPLICATED_DURATION);
105 d_assert(xact->tm_holding, return NULL, "Timer allocation failed");
106 timer_set_param1(xact->tm_holding, xact->index);
107 xact->holding_rcount = GTP_T3_DUPLICATED_RETRY_COUNT;
108 }
109
110 list_append(xact->org == GTP_LOCAL_ORIGINATOR ?
111 &xact->gnode->local_list : &xact->gnode->remote_list, xact);
112
113 rv = gtp_xact_update_tx(xact, hdesc, pkbuf);
114 d_assert(rv == CORE_OK, return NULL, "Update Tx failed");
115
116 d_trace(15, "[%d] %s Create peer [%s]:%d\n",
117 xact->xid,
118 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
119 CORE_ADDR(sock_remote_addr(gnode->sock), buf),
120 CORE_PORT(sock_remote_addr(gnode->sock)));
121
122 return xact;
123 }
124
125 gtp_xact_t *gtp_xact_remote_create(gtp_node_t *gnode, c_uint32_t sqn)
126 {
127 char buf[CORE_ADDRSTRLEN];
128 gtp_xact_t *xact = NULL;
129
130 d_assert(gnode, return NULL, "Null param");
131
132 index_alloc(&gtp_xact_pool, &xact);
133 d_assert(xact, return NULL, "Transaction allocation failed");
134
135 xact->org = GTP_REMOTE_ORIGINATOR;
136 xact->xid = GTP_SQN_TO_XID(sqn);
137 xact->gnode = gnode;
138
139 if (g_response_event)
140 {
141 xact->tm_response = timer_create(g_tm_service,
142 g_response_event, GTP_T3_RESPONSE_DURATION);
143 d_assert(xact->tm_response, return NULL, "Timer allocation failed");
144 timer_set_param1(xact->tm_response, xact->index);
145 xact->response_rcount = GTP_T3_RESPONSE_RETRY_COUNT;
146 }
147
148 if (g_holding_event)
149 {
150 xact->tm_holding = timer_create(g_tm_service,
151 g_holding_event, GTP_T3_DUPLICATED_DURATION);
152 d_assert(xact->tm_holding, return NULL, "Timer allocation failed");
153 timer_set_param1(xact->tm_holding, xact->index);
154 xact->holding_rcount = GTP_T3_DUPLICATED_RETRY_COUNT;
155 }
156
157 list_append(xact->org == GTP_LOCAL_ORIGINATOR ?
158 &xact->gnode->local_list : &xact->gnode->remote_list, xact);
159
160 d_trace(15, "[%d] %s Create peer [%s]:%d\n",
161 xact->xid,
162 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
163 CORE_ADDR(sock_remote_addr(gnode->sock), buf),
164 CORE_PORT(sock_remote_addr(gnode->sock)));
165
166 return xact;
167 }
168
169 void gtp_xact_delete_all(gtp_node_t *gnode)
170 {
171 gtp_xact_t *xact = NULL;
172
173 xact = list_first(&gnode->local_list);
174 while(xact)
175 {
176 gtp_xact_delete(xact);
177 xact = list_next(xact);
178 }
179 xact = list_first(&gnode->remote_list);
180 while(xact)
181 {
182 gtp_xact_delete(xact);
183 xact = list_next(xact);
184 }
185 }
186
187 status_t gtp_xact_update_tx(gtp_xact_t *xact,
188 gtp_header_t *hdesc, pkbuf_t *pkbuf)
189 {
190 char buf[CORE_ADDRSTRLEN];
191 gtp_xact_stage_t stage;
192 gtp_header_t *h = NULL;
193
194 d_assert(xact, return CORE_ERROR, "Null param");
195 d_assert(xact->gnode, return CORE_ERROR, "Null param");
196 d_assert(hdesc, return CORE_ERROR, "Null param");
197 d_assert(pkbuf, return CORE_ERROR, "Null param");
198
199 d_trace(15, "[%d] %s UPD TX-%d peer [%s]:%d\n",
200 xact->xid,
201 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
202 hdesc->type,
203 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
204 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
205
206 stage = gtp_xact_get_stage(hdesc->type, xact->xid);
207 if (xact->org == GTP_LOCAL_ORIGINATOR)
208 {
209 switch(stage)
210 {
211 case GTP_XACT_INITIAL_STAGE:
212 d_assert(xact->step == 0, return CORE_ERROR,
213 "[%d] %s invalid step %d for type %d peer [%s]:%d",
214 xact->xid,
215 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
216 xact->step, hdesc->type,
217 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
218 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
219 break;
220
221 case GTP_XACT_INTERMEDIATE_STAGE:
222 d_assert(0, return CORE_ERROR, "invalid step(%d)", xact->step);
223
224 case GTP_XACT_FINAL_STAGE:
225 d_assert(xact->step == 2, return CORE_ERROR,
226 "[%d] %s invalid step %d for type %d peer [%s]:%d",
227 xact->xid,
228 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
229 xact->step, hdesc->type,
230 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
231 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
232 break;
233
234 default:
235 d_assert(0, return CORE_ERROR, "invalid step(%d)", xact->step);
236 }
237 }
238 else if (xact->org == GTP_REMOTE_ORIGINATOR)
239 {
240 switch(stage)
241 {
242 case GTP_XACT_INITIAL_STAGE:
243 d_assert(0, return CORE_ERROR, "invalid step(%d)", xact->step);
244
245 case GTP_XACT_INTERMEDIATE_STAGE:
246 case GTP_XACT_FINAL_STAGE:
247 d_assert(xact->step == 1, return CORE_ERROR,
248 "[%d] %s invalid step %d for type %d peer [%s]:%d",
249 xact->xid,
250 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
251 xact->step, hdesc->type,
252 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
253 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
254 break;
255
256 default:
257 d_assert(0, return CORE_ERROR, "invalid step(%d)", xact->step);
258 }
259 }
260 else
261 d_assert(0, return CORE_ERROR, "invalid orginator(%d)", xact->org);
262
263
264 pkbuf_header(pkbuf, GTPV2C_HEADER_LEN);
265 h = pkbuf->payload;
266 d_assert(h, return CORE_ERROR, "Null param");
267
268 memset(h, 0, sizeof(gtp_header_t));
269 h->version = 2;
270 h->teid_presence = 1;
271 h->type = hdesc->type;
272 h->length = htons(pkbuf->len - 4);
273 h->teid = htonl(hdesc->teid);
274 h->sqn = GTP_XID_TO_SQN(xact->xid);
275
276 /* Save Message type and packet of this step */
277 xact->seq[xact->step].type = h->type;
278 xact->seq[xact->step].pkbuf = pkbuf;
279
280 /* Step */
281 xact->step++;
282
283 return CORE_OK;
284 }
285
286 status_t gtp_xact_update_rx(gtp_xact_t *xact, c_uint8_t type)
287 {
288 status_t rv = CORE_OK;
289 char buf[CORE_ADDRSTRLEN];
290 gtp_xact_stage_t stage;
291
292 d_trace(15, "[%d] %s UPD RX-%d peer [%s]:%d\n",
293 xact->xid,
294 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
295 type,
296 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
297 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
298
299 stage = gtp_xact_get_stage(type, xact->xid);
300 if (xact->org == GTP_LOCAL_ORIGINATOR)
301 {
302 switch(stage)
303 {
304 case GTP_XACT_INITIAL_STAGE:
305 d_assert(0, return CORE_ERROR, "invalid step(%d)", xact->step);
306
307 case GTP_XACT_INTERMEDIATE_STAGE:
308 if (xact->seq[1].type == type)
309 {
310 pkbuf_t *pkbuf = NULL;
311
312 d_assert(xact->step == 2 || xact->step == 3,
313 return CORE_ERROR,
314 "[%d] %s invalid step %d for type %d peer [%s]:%d",
315 xact->xid,
316 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
317 xact->step, type,
318 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
319 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
320
321 pkbuf = xact->seq[2].pkbuf;
322 if (pkbuf)
323 {
324 if (xact->tm_holding)
325 tm_start(xact->tm_holding);
326
327 d_warn("[%d] %s Request Duplicated. Retransmit!"
328 " for type %d peer [%s]:%d",
329 xact->xid,
330 xact->org == GTP_LOCAL_ORIGINATOR ?
331 "LOCAL " : "REMOTE",
332 xact->step, type,
333 CORE_ADDR(sock_remote_addr(xact->gnode->sock),
334 buf),
335 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
336 rv = gtp_send(xact->gnode, pkbuf);
337 d_assert(rv == CORE_OK, return CORE_ERROR,
338 "gtp_send error");
339 }
340 else
341 {
342 d_warn("[%d] %s Request Duplicated. Discard!"
343 " for type %d peer [%s]:%d",
344 xact->xid,
345 xact->org == GTP_LOCAL_ORIGINATOR ?
346 "LOCAL " : "REMOTE",
347 xact->step, type,
348 CORE_ADDR(sock_remote_addr(xact->gnode->sock),
349 buf),
350 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
351 }
352
353 return CORE_EAGAIN;
354 }
355
356 d_assert(xact->step == 1, return CORE_ERROR,
357 "[%d] %s invalid step %d for type %d peer [%s]:%d",
358 xact->xid,
359 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
360 xact->step, type,
361 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
362 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
363
364 if (xact->tm_holding)
365 tm_start(xact->tm_holding);
366
367 break;
368
369 case GTP_XACT_FINAL_STAGE:
370 d_assert(xact->step == 1, return CORE_ERROR,
371 "[%d] %s invalid step %d for type %d peer [%s]:%d",
372 xact->xid,
373 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
374 xact->step, type,
375 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
376 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
377
378 break;
379
380 default:
381 d_assert(0, return CORE_ERROR, "invalid step(%d)", xact->step);
382 }
383 }
384 else if (xact->org == GTP_REMOTE_ORIGINATOR)
385 {
386 switch(stage)
387 {
388 case GTP_XACT_INITIAL_STAGE:
389 if (xact->seq[0].type == type)
390 {
391 pkbuf_t *pkbuf = NULL;
392
393 d_assert(xact->step == 1 || xact->step == 2,
394 return CORE_ERROR,
395 "[%d] %s invalid step %d for type %d peer [%s]:%d",
396 xact->xid,
397 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
398 xact->step, type,
399 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
400 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
401
402 pkbuf = xact->seq[1].pkbuf;
403 if (pkbuf)
404 {
405 if (xact->tm_holding)
406 tm_start(xact->tm_holding);
407
408 d_warn("[%d] %s Request Duplicated. Retransmit!"
409 " for step %d type %d peer [%s]:%d",
410 xact->xid,
411 xact->org == GTP_LOCAL_ORIGINATOR ?
412 "LOCAL " : "REMOTE",
413 xact->step, type,
414 CORE_ADDR(sock_remote_addr(xact->gnode->sock),
415 buf),
416 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
417 rv = gtp_send(xact->gnode, pkbuf);
418 d_assert(rv == CORE_OK, return CORE_ERROR,
419 "gtp_send error");
420 }
421 else
422 {
423 d_warn("[%d] %s Request Duplicated. Discard!"
424 " for step %d type %d peer [%s]:%d",
425 xact->xid,
426 xact->org == GTP_LOCAL_ORIGINATOR ?
427 "LOCAL " : "REMOTE",
428 xact->step, type,
429 CORE_ADDR(sock_remote_addr(xact->gnode->sock),
430 buf),
431 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
432 }
433
434 return CORE_EAGAIN;
435 }
436
437 d_assert(xact->step == 0, return CORE_ERROR,
438 "[%d] %s invalid step %d for type %d peer [%s]:%d",
439 xact->xid,
440 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
441 xact->step, type,
442 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
443 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
444
445 if (xact->tm_holding)
446 tm_start(xact->tm_holding);
447
448 break;
449
450 case GTP_XACT_INTERMEDIATE_STAGE:
451 d_assert(0, return CORE_ERROR, "invalid step(%d)", xact->step);
452
453 case GTP_XACT_FINAL_STAGE:
454 d_assert(xact->step == 2, return CORE_ERROR,
455 "[%d] %s invalid step %d for type %d peer [%s]:%d",
456 xact->xid,
457 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
458 xact->step, type,
459 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
460 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
461
462 /* continue */
463 break;
464
465 default:
466 d_assert(0, return CORE_ERROR, "invalid step(%d)", xact->step);
467 }
468 }
469 else
470 d_assert(0, return CORE_ERROR, "invalid orginator(%d)", xact->org);
471
472 if (xact->tm_response)
473 tm_stop(xact->tm_response);
474
475 /* Save Message type of this step */
476 xact->seq[xact->step].type = type;
477
478 /* Step */
479 xact->step++;
480
481 return CORE_OK;
482 }
483
484
485 status_t gtp_xact_commit(gtp_xact_t *xact)
486 {
487 status_t rv;
488 char buf[CORE_ADDRSTRLEN];
489
490 c_uint8_t type;
491 pkbuf_t *pkbuf = NULL;
492 gtp_xact_stage_t stage;
493
494 d_assert(xact, return CORE_ERROR, "Null param");
495 d_assert(xact->gnode, return CORE_ERROR, "Null param");
496
497 d_trace(15, "[%d] %s Commit peer [%s]:%d\n",
498 xact->xid,
499 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
500 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
501 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
502
503 type = xact->seq[xact->step-1].type;
504 stage = gtp_xact_get_stage(type, xact->xid);
505
506 if (xact->org == GTP_LOCAL_ORIGINATOR)
507 {
508 switch(stage)
509 {
510 case GTP_XACT_INITIAL_STAGE:
511 {
512 d_assert(xact->step == 1, return CORE_ERROR,
513 "[%d] %s invalid step %d for type %d peer [%s]:%d",
514 xact->xid,
515 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
516 xact->step, type,
517 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
518 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
519
520 if (xact->tm_response)
521 tm_start(xact->tm_response);
522
523 break;
524 }
525
526 case GTP_XACT_INTERMEDIATE_STAGE:
527 d_assert(0, return CORE_ERROR, "invalid step(%d)", xact->step);
528
529 case GTP_XACT_FINAL_STAGE:
530 d_assert(xact->step == 2 || xact->step == 3, return CORE_ERROR,
531 "[%d] %s invalid step %d for type %d peer [%s]:%d",
532 xact->xid,
533 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
534 xact->step, type,
535 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
536 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
537
538 if (xact->step == 2)
539 {
540 gtp_xact_delete(xact);
541 return CORE_OK;
542 }
543
544 break;
545
546 default:
547 d_assert(0, return CORE_ERROR, "invalid step(%d)", xact->step);
548 }
549 }
550 else if (xact->org == GTP_REMOTE_ORIGINATOR)
551 {
552 switch(stage)
553 {
554 case GTP_XACT_INITIAL_STAGE:
555 d_assert(0, return CORE_ERROR, "invalid step(%d)", xact->step);
556
557 case GTP_XACT_INTERMEDIATE_STAGE:
558 d_assert(xact->step == 2, return CORE_ERROR,
559 "[%d] %s invalid step %d for type %d peer [%s]:%d",
560 xact->xid,
561 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
562 xact->step, type,
563 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
564 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
565
566 if (xact->tm_response)
567 tm_start(xact->tm_response);
568
569 break;
570
571 case GTP_XACT_FINAL_STAGE:
572 d_assert(xact->step == 2 || xact->step == 3,
573 return CORE_ERROR,
574 "[%d] %s invalid step %d for type %d peer [%s]:%d",
575 xact->xid,
576 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
577 xact->step, type,
578 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
579 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
580
581 if (xact->step == 3)
582 {
583 gtp_xact_delete(xact);
584 return CORE_OK;
585 }
586
587 break;
588
589 default:
590 d_assert(0, return CORE_ERROR, "invalid step(%d)", xact->step);
591 }
592 }
593 else
594 d_assert(0, return CORE_ERROR, "invalid orginator(%d)", xact->org);
595
596 pkbuf = xact->seq[xact->step-1].pkbuf;
597 d_assert(pkbuf, return CORE_ERROR, "Null param");
598
599 rv = gtp_send(xact->gnode, pkbuf);
600 d_assert(rv == CORE_OK, return CORE_ERROR, "gtp_send error");
601
602 return CORE_OK;
603 }
604
605 status_t gtp_xact_timeout(index_t index, c_uintptr_t event)
606 {
607 char buf[CORE_ADDRSTRLEN];
608 gtp_xact_t *xact = NULL;
609
610 d_assert(index, goto out, "Invalid Index");
611 xact = index_find(&gtp_xact_pool, index);
612 d_assert(xact, goto out, "Null param");
613 d_assert(xact->gnode, goto out, "Null param");
614
615 if (event == g_response_event)
616 {
617 d_trace(15, "[%d] %s Response Timeout "
618 "for step %d type %d peer [%s]:%d\n",
619 xact->xid,
620 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
621 xact->step, xact->seq[xact->step-1].type,
622 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
623 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
624
625 if (--xact->response_rcount > 0)
626 {
627 pkbuf_t *pkbuf = NULL;
628
629 if (xact->tm_response)
630 tm_start(xact->tm_response);
631
632 pkbuf = xact->seq[xact->step-1].pkbuf;
633 d_assert(pkbuf, return CORE_ERROR, "Null param");
634
635 if (gtp_send(xact->gnode, pkbuf) != CORE_OK)
636 {
637 d_error("gtp_send() failed");
638 goto out;
639 }
640 }
641 else
642 {
643 d_warn("[%d] %s No Reponse. Give up! "
644 "for step %d type %d peer [%s]:%d",
645 xact->xid,
646 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
647 xact->step, xact->seq[xact->step-1].type,
648 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
649 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
650 gtp_xact_delete(xact);
651 }
652 }
653 else if (event == g_holding_event)
654 {
655 d_trace(15, "[%d] %s Holding Timeout "
656 "for step %d type %d peer [%s]:%d\n",
657 xact->xid,
658 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
659 xact->step, xact->seq[xact->step-1].type,
660 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
661 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
662
663 if (--xact->holding_rcount > 0)
664 {
665 if (xact->tm_holding)
666 tm_start(xact->tm_holding);
667 }
668 else
669 {
670 d_trace(15, "[%d] %s Delete Transaction "
671 "for step %d type %d peer [%s]:%d\n",
672 xact->xid,
673 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
674 xact->step, xact->seq[xact->step-1].type,
675 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
676 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
677 gtp_xact_delete(xact);
678 }
679 }
680
681 return CORE_OK;
682
683 out:
684 gtp_xact_delete(xact);
685 return CORE_ERROR;
686 }
687
688 status_t gtp_xact_receive(
689 gtp_node_t *gnode, gtp_header_t *h, gtp_xact_t **xact)
690 {
691 char buf[CORE_ADDRSTRLEN];
692 status_t rv;
693 gtp_xact_t *new = NULL;
694
695 d_assert(gnode, return CORE_ERROR, "Null param");
696 d_assert(h, return CORE_ERROR, "Null param");
697
698 new = gtp_xact_find_by_xid(gnode, h->type, GTP_SQN_TO_XID(h->sqn));
699 if (!new)
700 new = gtp_xact_remote_create(gnode, h->sqn);
701 d_assert(new, return CORE_ERROR, "Null param");
702
703 d_trace(15, "[%d] %s Receive peer [%s]:%d\n",
704 new->xid,
705 new->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
706 CORE_ADDR(sock_remote_addr(gnode->sock), buf),
707 CORE_PORT(sock_remote_addr(gnode->sock)));
708
709 rv = gtp_xact_update_rx(new, h->type);
710 if (rv != CORE_OK)
711 {
712 return rv;
713 }
714
715 *xact = new;
716 return CORE_OK;
717 }
718
719 gtp_xact_t *gtp_xact_find(index_t index)
720 {
721 d_assert(index, return NULL, "Invalid Index");
722 return index_find(&gtp_xact_pool, index);
723 }
724
725 static gtp_xact_stage_t gtp_xact_get_stage(c_uint8_t type, c_uint32_t xid)
726 {
727 gtp_xact_stage_t stage = GTP_XACT_UNKNOWN_STAGE;
728
729 switch(type)
730 {
731 case GTP_CREATE_SESSION_REQUEST_TYPE:
732 case GTP_MODIFY_BEARER_REQUEST_TYPE:
733 case GTP_DELETE_SESSION_REQUEST_TYPE:
734 case GTP_MODIFY_BEARER_COMMAND_TYPE:
735 case GTP_DELETE_BEARER_COMMAND_TYPE:
736 case GTP_BEARER_RESOURCE_COMMAND_TYPE:
737 case GTP_RELEASE_ACCESS_BEARERS_REQUEST_TYPE:
738 case GTP_CREATE_INDIRECT_DATA_FORWARDING_TUNNEL_REQUEST_TYPE:
739 case GTP_DELETE_INDIRECT_DATA_FORWARDING_TUNNEL_REQUEST_TYPE:
740 case GTP_DOWNLINK_DATA_NOTIFICATION_TYPE:
741 stage = GTP_XACT_INITIAL_STAGE;
742 break;
743 case GTP_CREATE_BEARER_REQUEST_TYPE:
744 case GTP_UPDATE_BEARER_REQUEST_TYPE:
745 case GTP_DELETE_BEARER_REQUEST_TYPE:
746 if (xid & GTP_MAX_XACT_ID)
747 stage = GTP_XACT_INTERMEDIATE_STAGE;
748 else
749 stage = GTP_XACT_INITIAL_STAGE;
750 break;
751 case GTP_CREATE_SESSION_RESPONSE_TYPE:
752 case GTP_MODIFY_BEARER_RESPONSE_TYPE:
753 case GTP_DELETE_SESSION_RESPONSE_TYPE:
754 case GTP_MODIFY_BEARER_FAILURE_INDICATION_TYPE:
755 case GTP_DELETE_BEARER_FAILURE_INDICATION_TYPE:
756 case GTP_BEARER_RESOURCE_FAILURE_INDICATION_TYPE:
757 case GTP_CREATE_BEARER_RESPONSE_TYPE:
758 case GTP_UPDATE_BEARER_RESPONSE_TYPE:
759 case GTP_DELETE_BEARER_RESPONSE_TYPE:
760 case GTP_RELEASE_ACCESS_BEARERS_RESPONSE_TYPE:
761 case GTP_CREATE_INDIRECT_DATA_FORWARDING_TUNNEL_RESPONSE_TYPE:
762 case GTP_DELETE_INDIRECT_DATA_FORWARDING_TUNNEL_RESPONSE_TYPE:
763 case GTP_DOWNLINK_DATA_NOTIFICATION_ACKNOWLEDGE_TYPE:
764 stage = GTP_XACT_FINAL_STAGE;
765 break;
766
767 default:
768 d_error("Not implemented GTPv2 Message Type(%d)", type);
769 break;
770 }
771
772 return stage;
773 }
774
775 gtp_xact_t *gtp_xact_find_by_xid(
776 gtp_node_t *gnode, c_uint8_t type, c_uint32_t xid)
777 {
778 char buf[CORE_ADDRSTRLEN];
779 gtp_xact_t *xact = NULL;
780
781 d_assert(gnode, return NULL, "Null param");
782
783 switch(gtp_xact_get_stage(type, xid))
784 {
785 case GTP_XACT_INITIAL_STAGE:
786 xact = list_first(&gnode->remote_list);
787 break;
788 case GTP_XACT_INTERMEDIATE_STAGE:
789 xact = list_first(&gnode->local_list);
790 break;
791 case GTP_XACT_FINAL_STAGE:
792 if (xid & GTP_MAX_XACT_ID)
793 xact = list_first(&gnode->remote_list);
794 else
795 xact = list_first(&gnode->local_list);
796 break;
797
798 default:
799 d_assert(0, return NULL, "Unknown stage");
800 }
801
802 while(xact)
803 {
804 if (xact->xid == xid)
805 break;
806 xact = list_next(xact);
807 }
808
809 if (xact)
810 {
811 d_trace(15, "[%d] %s Find peer [%s]:%d\n",
812 xact->xid,
813 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
814 CORE_ADDR(sock_remote_addr(gnode->sock), buf),
815 CORE_PORT(sock_remote_addr(gnode->sock)));
816 }
817
818 return xact;
819 }
820
821 void gtp_xact_associate(gtp_xact_t *xact1, gtp_xact_t *xact2)
822 {
823 d_assert(xact1, return, "Null param");
824 d_assert(xact2, return, "Null param");
825
826 d_assert(xact1->assoc_xact == NULL, return, "Already assocaited");
827 d_assert(xact2->assoc_xact == NULL, return, "Already assocaited");
828
829 xact1->assoc_xact = xact2;
830 xact2->assoc_xact = xact1;
831 }
832
833 void gtp_xact_deassociate(gtp_xact_t *xact1, gtp_xact_t *xact2)
834 {
835 d_assert(xact1, return, "Null param");
836 d_assert(xact2, return, "Null param");
837
838 d_assert(xact1->assoc_xact != NULL, return, "Already deassocaited");
839 d_assert(xact2->assoc_xact != NULL, return, "Already deassocaited");
840
841 xact1->assoc_xact = NULL;
842 xact2->assoc_xact = NULL;
843 }
844
845 static status_t gtp_xact_delete(gtp_xact_t *xact)
846 {
847 char buf[CORE_ADDRSTRLEN];
848
849 d_assert(xact, , "Null param");
850 d_assert(xact->gnode, , "Null param");
851
852 d_trace(15, "[%d] %s Delete peer [%s]:%d\n",
853 xact->xid,
854 xact->org == GTP_LOCAL_ORIGINATOR ? "LOCAL " : "REMOTE",
855 CORE_ADDR(sock_remote_addr(xact->gnode->sock), buf),
856 CORE_PORT(sock_remote_addr(xact->gnode->sock)));
857
858 if (xact->seq[0].pkbuf)
859 pkbuf_free(xact->seq[0].pkbuf);
860 if (xact->seq[1].pkbuf)
861 pkbuf_free(xact->seq[1].pkbuf);
862 if (xact->seq[2].pkbuf)
863 pkbuf_free(xact->seq[2].pkbuf);
864
865 if (xact->tm_response)
866 tm_delete(xact->tm_response);
867 if (xact->tm_holding)
868 tm_delete(xact->tm_holding);
869
870 if (xact->assoc_xact)
871 gtp_xact_deassociate(xact, xact->assoc_xact);
872
873 list_remove(xact->org == GTP_LOCAL_ORIGINATOR ? &xact->gnode->local_list :
874 &xact->gnode->remote_list, xact);
875 index_free(&gtp_xact_pool, xact);
876
877 return CORE_OK;
878 }
879